/**  http://www.codecreate.co.uk **/
function createXMLHttp(){
	if(typeof XMLHttpRequest != "undefined"){
		return new XMLHttpRequest();
	} else {
	var aVersions = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
	for(var i=0;i<aVersions.length;i++){
		try {
			var oXmlHttp = new ActiveXObject(aVersions[i]);
			return oXmlHttp;
		} catch(oError){
		
		}
	}
	}
	throw new Error("XMLHttp could not be created");
}
function addToRecipeBinder(){
	var oXmlHttp = createXMLHttp();
    oXmlHttp.open("post",'../../../xmlhttp-recipebinder.php',true);
    oXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	var rl = document.getElementById('recipebinderl');
    	rl.innerHTML = 'Adding this recipe to the Recipe Binder...';
    oXmlHttp.onreadystatechange = function(){
    	
  		if(oXmlHttp.readyState==4) {
    		if(oXmlHttp.responseText.indexOf('Success')!=-1){			
				rl.innerHTML = 'Successfully saved to your Recipe Binder';
			} else {
				rl.innerHTML = 'We are unable to save this recipe to your Recipe Binder';
				//alert(oXmlHttp.responseText);
			} 
    	}
    }
    oXmlHttp.send('loc='+location.pathname)
}
function removeFromRecipeBinder(obj){
	var oXmlHttp = createXMLHttp();
    oXmlHttp.open("post",'xmlhttp-recipebinder.php',true);
    oXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    oXmlHttp.onreadystatechange = function(){
  		if(oXmlHttp.readyState==4) {
    		if(oXmlHttp.responseText.indexOf('Success')==-1){			
				alert("Unable to remove the recipe");
				return false;
			} else {
				location.href = location.href;
			} 
    	}
    }
    oXmlHttp.send('removeid='+obj.id.substr(7))
}
function recipeBinderLink(){
	if(document.getElementById('xmlhttp_rb')){
	document.getElementById('xmlhttp_rb').onclick = function(){
	return addToRecipeBinder();
	}
	}
}
function recipeRemoveLink(){
	if(document.getElementById('rbdiv')){
	var rl = document.getElementById('rbdiv').getElementsByTagName('a');
	//alert(rl.length);
	for(var i=0;i<rl.length;i++){
		if(rl[i].id.substr(0,6)=='remove'){
		rl[i].onclick = function(){
			if(confirm('Are you sure you want to remove this recipe from your Recipe Binder?')){
			removeFromRecipeBinder(this);
			}
		}
		}
	}
	}
}


function setMenuDivs(){
if(!document.getElementById('restaurant_div')) return;
var d = document.getElementById('restaurant_div');
var rd = d.getElementsByTagName('div')
if(rd.length==0) return;
var fullheight = d.offsetHeight;
var currentheight = 0;
var startnewheight = 0;
if(rd.length>1){
for(i=0;i<rd.length;i++){
	currentheight += rd[i].offsetHeight
	if(currentheight>=(fullheight+100)){
	if(!cheight) var cheight=0;
	cheight = rd[i].offsetHeight+cheight+20;
	}
}
} 
var zheight=0;
var xheight=0;
for(i=0;i<rd.length;i++){
	if(rd[i].style.position =='absolute'){
	zheight = rd[i].offsetHeight+zheight+20;
	} else {
	xheight = rd[i].offsetHeight+xheight+20;
	}
}
d.style.height = (zheight>xheight?zheight:xheight)+'px';
document.getElementById('columnBody').style.height = (zheight>xheight?zheight:xheight)+40+'px';
}
window.onload = function(){
	recipeBinderLink();
	recipeRemoveLink();
	//setMenuDivs();
}
window.onerror = function(){
	return false;
}