
var connection;
var connection2;
var connectionform;
var active_obj = null;
var active_content = null;
var active_content2 = null;
var active_contentform = null;
var popupwidth = 500;
var popupheight = 380;

function checkEnabled()
{
	if (active_obj!=null) return false;
	else return true;
}



function relocatePopup()
{
	setPopupLocation(active_obj);
	var d = document.getElementById('ibody');
	//var d2 = document.getElementById('blackoutdiv');
	var divHeight;

	var d2 = document.layers ? document.layers['blackoutdiv'] :
	document.getElementById ?  document.getElementById('blackoutdiv').style :
	document.all['blackoutdiv'].style;




	if (d2.setProperty)
	{ //Mozilla
		var docHeight = Math.max(d.offsetHeight, document.documentElement.clientHeight, d.scrollHeight, document.documentElement.scrollHeight, window.innerHeight);
		var docWidth = Math.max(d.offsetWidth, document.documentElement.clientWidth, d.scrollWidth, document.documentElement.scrollWidth, window.innerWidth);
		d2.setProperty('height',docHeight,null);
		d2.setProperty('width',docWidth,null);
	}
	else
	{
		var docHeight = Math.max(d.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, d.scrollHeight, d.style.pixelHeight, d.style.height);
		var docWidth = Math.max(d.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, d.scrollWidth, d.style.pixelWidth, d.style.width);
		//d3 = document.getElementById('blackoutdiv');//d2.height = docHeight;
		//d3.style.height = docHeight;
		d2.height = docHeight;
		d2.width = docWidth;
	}
	//if(d.offsetHeight)
	//{ //Mozilla

	//	divHeight=d.offsetHeight;
	//	alert('height:'+docHeight);
	//	d2.setProperty('height',docHeight,null);
	//}
	//else if (d.style.pixelHeight)
	//{ //IE


	    // divHeight=d.style.pixelHeight;
	  		//alert('height:'+divHeight);
	     //d2.height = divHeight;
	//}
}

function setPopupLocation(obj)
{
	if (!obj)
	{
		 obj = active_obj;
	}
	if (obj)
	{
		var offset_y = 0;
		var objstyle = document.layers ? document.layers[obj.id] :
		document.getElementById ?  obj.style :
		document.all[obj.id].style;

		var windowwidth = getWindowSize().x;
		var windowheight = getWindowSize().y;
		var scrollypos = getScrollPosition().y;
		var newy = scrollypos + (windowheight / 2) - (popupheight / 2);

		if (newy < 0) objstyle.top = 0;
		else objstyle.top = newy + 'px';
		objstyle.left = ((windowwidth / 2) - (popupwidth /2))  + 'px';

		//COMPENSATE IF POPUP IS LARGER THAN BODY
		var bodyobj = document.getElementById('ibody');
		var blackoutstyle = document.getElementById('blackoutdiv').style;
		if (blackoutstyle.setProperty)
		{ //Mozilla
			var docHeight = Math.max(bodyobj.offsetHeight, document.documentElement.clientHeight, bodyobj.scrollHeight, document.documentElement.scrollHeight, window.innerHeight);
			blackoutstyle.setProperty('height',docHeight + offset_y + "px",null);
		}
		else
		{
			var docHeight = Math.max(bodyobj.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, bodyobj.scrollHeight, bodyobj.style.pixelHeight, bodyobj.style.height);
			blackoutstyle.height = docHeight + offset_y + "px";
		}
		blackoutstyle.visibility = "visible";
	}
}

function callBack(content)
{
	active_content.innerHTML = content;
}

function callBack2(content)
{
	active_content2.innerHTML = content;
}

function callBackForm(content)
{
	active_contentform.innerHTML = content;
}

function xmlRequest(page, postvars, destination_id)
{
	// except IE

	if (connection)
	{
		delete connection;
	}

	active_content = document.getElementById(destination_id);


	if(window.XMLHttpRequest)
	{
	  connection = new XMLHttpRequest();
	} //IE
	else if(window.ActiveXObject)
	{
	  try
	  {
	    connection = new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch(e)
	  {
	    connection = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}

	if (connection)
	{
		connection.open("POST", page, true);
		connection.onreadystatechange = function()
		{
		        if (connection.readyState != 4)
		        {
		         	var d = document.getElementById(destination_id);
		         	d.innerHTML = '<img src="/images/loading.gif" border="0">';
		          return;
		        }
		        eval('callBack(connection.responseText);');
		        delete connection;
		}
		connection.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		connection.send(postvars);
	}

}

function xmlRequest2(page, postvars, destination_id)
{
	// except IE

	if (connection2)
	{
		delete connection2;
	}

	active_content2 = document.getElementById(destination_id);


	if(window.XMLHttpRequest)
	{
	  connection2 = new XMLHttpRequest();
	} //IE
	else if(window.ActiveXObject)
	{
	  try
	  {
	    connection2 = new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch(e)
	  {
	    connection2 = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}

	if (connection2)
	{
		connection2.open("POST", page, true);
		connection2.onreadystatechange = function()
		{
		        if (connection2.readyState != 4)
		        {
		         	var d = document.getElementById(destination_id);
		         	d.innerHTML = '<img src="/images/loading.gif" border="0">';
		          return;
		        }
		        eval('callBack2(connection2.responseText);');
		        delete connection2;
		}
		connection2.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		connection2.send(postvars);
	}

}

function xmlRequestForm(page, postvars, destination_id)
{
	// except IE

	if (connectionform)
	{
		delete connectionform;
	}

	active_contentform = document.getElementById(destination_id);


	if(window.XMLHttpRequest)
	{
	  connectionform = new XMLHttpRequest();
	} //IE
	else if(window.ActiveXObject)
	{
	  try
	  {
	    connectionform = new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch(e)
	  {
	    connectionform = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}

	if (connectionform)
	{
		connectionform.open("POST", page, true);
		connectionform.onreadystatechange = function()
		{
		        if (connectionform.readyState != 4)
		        {
		         	var d = document.getElementById(destination_id);
		         	d.innerHTML = '<img src="/images/loading.gif" border="0">';
		          return;
		        }
		        eval('callBackForm(connectionform.responseText);');
		        delete connectionform;
		}
		connectionform.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		connectionform.send(postvars);
	}

}

function xmlFormSubmit(form_id, destination_id)
{
	var postvars="";
	var i;
	theform = document.getElementById(form_id);
	for (i=0;i<theform.elements.length;i++)
	{
		//check radio, checkbuttons and selects
		if (theform.elements[i].type=='text' || theform.elements[i].type=='textarea' || theform.elements[i].type=='hidden' || (theform.elements[i].type=='radio' && theform.elements[i].checked) || (theform.elements[i].type=='checkbox' && theform.elements[i].checked) || theform.elements[i].type=='password')
		{
			if (postvars.length==0)	postvars = theform.elements[i].name + "=" + escape(theform.elements[i].value);
			else postvars = postvars + "&" + theform.elements[i].name + "=" + escape(theform.elements[i].value);
		}
		else if (theform.elements[i].type=='select-one')
		{
			if (postvars.length==0)	postvars = theform.elements[i].name + "=" + escape(theform.elements[i].options[theform.elements[i].selectedIndex].value);
			else postvars = postvars + "&" + theform.elements[i].name + "=" + escape(theform.elements[i].options[theform.elements[i].selectedIndex].value);
		}
	}
	xmlRequestForm(theform.action, postvars, destination_id);
	return false;
}

function getStyle(el, style) {
   if(!document.getElementById) return;

     var value = el.style[style];

    if(!value)
        if(document.defaultView)
            value = document.defaultView.
                 getComputedStyle(el, "").getPropertyValue(style);
        else if(el.currentStyle)
            value = el.currentStyle[style];

     return value;
}

function setStyle(objId, style, value) {
    document.getElementById(objId).style[style] = value;
}

function popUp(objid, pwidth, pheight)
{
	if (active_obj!=null) return false;

	var obj = document.getElementById(objid);

	if (pwidth && pheight)
	{
		popupwidth = pwidth;
		popupheight = pheight;
		obj.style.width = popupwidth+"px";
		obj.style.height = popupheight+"px";

	}
	else
	{
		popupwidth = parseInt(getStyle(obj,"width"));
		popupheight = parseInt(getStyle(obj,"height"));
	}




	if (isNaN(popupwidth)) popupwidth = obj.offsetWidth;
	if (isNaN(popupheight)) popupheight = obj.offsetHeight;



	setPopupLocation(obj);
	active_obj = obj;

	obj.style.visibility = "visible";

}

function getWindowSize(){
	if(typeof window.innerWidth != 'undefined'){
		return {'x': window.innerWidth, 'y':window.innerHeight};
	}
	return {'x': document.documentElement.clientWidth, 'y':document.documentElement.clientHeight};
}

function getElementSize(elm){
	return {'x' :elm.offsetWidth, 'y': elm.offsetHeight}
}

function getScrollPosition()
{
	var scrollPosY = document.body.scrollTop;
	var scrollPosX = document.body.scrollLeft;

	if (scrollPosY == 0)
	{
		if (window.pageYOffset) scrollPosY = window.pageYOffset;
		else scrollPosY = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}

	if (scrollPosX == 0)
	{
		if (window.pageXOffset) scrollPosX = window.pageXOffset;
		else scrollPosX = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
	}

	return {'x' : scrollPosX, 'y' : scrollPosY};
}

function closeActivePopUp()
{
	active_obj.style.visibility = "hidden";
	active_obj = null;
	var d2 = document.getElementById('blackoutdiv');
	d2.style.visibility = "hidden";
}

function changeExtraPrice()
{
	if (document.extrasform)
	{
		if (document.extrasform.orderbaseprice)
		{
			var i = 0;
			var selectbox;
			var plus = 0.0;
			selectbox = document.getElementById('iextraselect'+i);
			var str;
			var arr;
			var pl;
			var newpr;
			while (selectbox)
			{
				str = new String(selectbox.options[selectbox.selectedIndex].value);
				arr = str.split('|');
				pl = parseFloat(arr[2]);
				if (arr.length==3 && !isNaN(pl))
				{
					plus += pl;
				}
				i++;
				selectbox = document.getElementById('iextraselect'+i);
				if (i > 10) break;
			}
			var baseprice = parseFloat(document.extrasform.orderbaseprice.value);
			if (!isNaN(baseprice))
			{
				newpr = new String((baseprice + plus));
				arr = newpr.split('.');
				var elt1 = document.getElementById('extracurr');
				var elt2 = document.getElementById('extracent');
				if (elt1 && elt2)
				{
					if (arr.length==2)
					{
						elt1.innerHTML = arr[0];
						if (arr[1].length==1) elt2.innerHTML = arr[1] + '0';
						else elt2.innerHTML = arr[1];
					}
					else if (arr.length==1)
					{
						elt1.innerHTML = arr[0];
						elt2.innerHTML = '00';
					}

				}
			}


		}
	}
}

var tooltipvisible = false;
var tooltiporientation = "bottomleft";
var mX=0;
var mY=0;
var fadetimer;
var fadealpha = 0;
document.onmousemove = positionTooltip;


function fadeTooltip2(d)
{


		obj = document.getElementById('tooltipdiv2');
		if (d==1)
		{
			if (fadealpha>0) obj.style.visibility = "visible";
			fadealpha += 20;
			if (fadealpha >= 100)
			{
				fadealpha = 100;
				clearInterval(fadetimer);
			}

		}
		else
		{
			fadealpha -= 20;
			if (fadealpha<=0)
			{
				fadealhpa = 0;
				obj.style.visibility = "hidden";
				clearInterval(fadetimer);
			}
		}

		obj.style.opacity = fadealpha * .01;
		obj.style.filter = 'alpha(opacity=' + fadealpha + ')';
}

function showTooltip2(tooltext)
{
	elt = document.getElementById('tooltipdiv2');
	elt.innerHTML = tooltext;

	var obj2 = document.layers ? document.layers['tooltipdiv2'] :
	document.getElementById ?  document.getElementById('tooltipdiv2').style :
	document.all['tooltipdiv2'].style;
	if (obj2.setProperty)
	{ //Mozilla
			obj2.setProperty('top',mY - 180 ,null);
			obj2.setProperty('left',mX + 30 ,null);
	}
	else
	{ //IE
			obj2.top = mY - 180;
			obj2.left = mX + 30;
	}
	if (fadetimer) clearInterval(fadetimer);
	fadealpha = 0;
	fadetimer = setInterval("fadeTooltip2(1)",20);


}

function hideTooltip2()
{
	//elt = document.getElementById('tooltipdiv2');
	if (fadetimer) clearInterval(fadetimer);
	fadetimer = setInterval("fadeTooltip2(-1)",20);


}

function showTooltip(tooltext, toolorientation)
{
	elt = document.getElementById('tooltipdiv');
	elt.innerHTML = tooltext;
	tooltipvisible = true;
	tooltiporientation = toolorientation;
	elt.style.visibility = "visible";
}

function hideTooltip()
{
	elt = document.getElementById('tooltipdiv');
	tooltipvisible = false;
	elt.style.visibility = "hidden";
}

function positionTooltip(evt)
{
	if (tooltipvisible)
	{
		var obj = document.layers ? document.layers['tooltipdiv'] :
		document.getElementById ?  document.getElementById('tooltipdiv').style :
		document.all['tooltipdiv'].style;
		//alert(obj.visibility);

		var gainY;
		var gainX;
		if (tooltiporientation=="bottomright")
		{
			gainY = 20;
			gainX = 20;
		}
		else //bottomleft
		{
			gainY = 20;
			gainX = -170;
		}

		if (obj.setProperty)
		{ //Mozilla
			mouseX = evt.pageX;
			mouseY = evt.pageY;
			mX = mouseX;
			mY = mouseY;
			obj.setProperty('top',mouseY + gainY,null);
			obj.setProperty('left',mouseX + gainX,null);
		}
		else
		{ //IE
			mouseX = window.event.clientX + document.body.scrollLeft;
			mouseY = window.event.clientY + document.body.scrollTop;
			mX = mouseX;
			mY = mouseY;
			obj.top = mouseY+gainY;
			obj.left = mouseX+gainX;
		}
	}
	else
	{
		var obj2 = document.layers ? document.layers['tooltipdiv2'] :
		document.getElementById ?  document.getElementById('tooltipdiv2').style :
		document.all['tooltipdiv2'].style;
		if (obj2.setProperty)
		{ //Mozilla
			mX = evt.pageX;
			mY = evt.pageY;
		}
		else
		{ //IE
			mX = window.event.clientX + document.body.scrollLeft;
			mY = window.event.clientY + document.body.scrollTop;
		}
	}

}

function doSrOffercode(formid,divid)
{
    var acode = document.getElementById('offerCDE').value;
    xmlRequest2('getdiscounts.php','offerCDE='+escape(acode)+'&discountaction=check', divid);
    xmlRequest('basket.php','orderaction=doSrOffer&order='+escape(acode),'basketdiv');
}


function selSelection(paymentid)
{
	var selectpayid = "selectPayment";
	var i = 0;
	var elt;
	var elt2;
	while (elt = document.getElementById(selectpayid+i))
	{
		elt2 = document.getElementById('labelPayment'+i);
		if (elt.value==paymentid)
		{
			elt2.className = "payment" + elt.value;
		}
		else
		{
			elt2.className = "payment" + elt.value + "Dimmed";
		}
		i++;
	}
	xmlRequest('getpaymentdetails.php','method='+paymentid, 'paymentoptions');
	if (paymentid==3) xmlRequest2('basket.php','showwithideal=true&orderaction=noremove','basketdiv');
	else xmlRequest2('basket.php','orderaction=noremove','basketdiv');
}

function updateBranchInfo(branch, postcode)
{
	var br = document.getElementById('ibranch');
	var pc = document.getElementById('ipc');
	br.innerHTML = branch;
	pc.innerHTML = postcode;
}


var intervaltime = 10000; //poll every 10 seconds
var maxtries = 10 * 6; //poll for 10 minutes
var tries = 0;

function doIdealRequest(url, vars)
{
	tries++;
	var idealconnection = false;


	// except IE
	if(window.XMLHttpRequest) {
	  idealconnection = new XMLHttpRequest();
	// IE
	} else if(window.ActiveXObject) {
	  try {
	    idealconnection = new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  	catch(e) {
	    idealconnection = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}

	if (!idealconnection)
	{
		return false;
	}

	idealconnection.open("POST", url, true);

	idealconnection.onreadystatechange=function()
	{

		if (idealconnection.readyState == 1 || idealconnection.readyState == 2 || idealconnection.readyState == 3)
		{
			//do nothing
		}
		if (idealconnection.readyState != 4) return;
		if (idealconnection.readyState == 4)
		{
			if (idealconnection.responseText=="failure" || idealconnection.responseText=="expired" || idealconnection.responseText=="cancelled" || idealconnection.responseText=="unknown" || idealconnection.responseText=="success")
			{
				idealDone(idealconnection.responseText);
				delete idealconnection;
			}
			else if (idealconnection.responseText=="new" || idealconnection.responseText=="open")
			{
				//do nothing wait for another request..
				if (tries > maxtries)
				{
					//ideal request done
					idealDone(idealconnection.responseText);
				}
				else
				{
					window.setTimeout("doIdealRequest('"+url+"','"+vars+"')", intervaltime);
				}
				delete idealconnection;
			}
			else
			{
				idealDone('generalerror');
				delete idealconnection;
			}
		}
	}

  idealconnection.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	idealconnection.send(vars);
}

function expand()
{
	if (document.layers)
	{
		document.layers["intro2"].display = 'none';
		document.layers["intro1"].display = 'block';
	}
	else if (document.all)
	{
		document.all["intro2"].style.display = 'none';
		document.all["intro1"].style.display = 'block';
	}
	else if (document.getElementById)
	{
		document.getElementById("intro2").style.display = 'none';
		document.getElementById("intro1").style.display = 'block';
	}
}

function collapse()
{
	if (document.layers)
	{
		document.layers["intro1"].display = 'none';
		document.layers["intro2"].display = 'block';
	}
	else if (document.all)
	{
		document.all["intro1"].style.display = 'none';
		document.all["intro2"].style.display = 'block';
	}
	else if (document.getElementById)
	{
		document.getElementById("intro1").style.display = 'none';
		document.getElementById("intro2").style.display = 'block';
	}
}

function replaceFooterLinks(oldbranch, newbranch)
{
	var elt = document.getElementById('dishes');
	var myString = new String(elt.innerHTML);
	var re = new RegExp("/([a-z]+)/", "g");
	var res = myString.replace(re, "/"+newbranch+"/");
	elt.innerHTML = res;
}