function SwitchUIforwebtil(type)
{
	if(type == '1')
	{
		document.getElementById('webtillogin').style.display = 'none';
		document.getElementById('webtilpwd').style.display = '';
	}
	else
	{
		document.getElementById('webtilpwd').style.display = 'none';
		document.getElementById('webtillogin').style.display = '';
	}
}
function Init_AJAX()
{
   try
   {
     return new ActiveXObject("Msxml2.XMLHTTP");
   } 
   catch(e)
   {} //IE
   	try 
   	{
    	return new ActiveXObject("Microsoft.XMLHTTP"); 
   	}
   	catch(e)
   	{} //IE
   	try
   	{
   	return new XMLHttpRequest();
   	}
   	catch(e)
   	{} //Native Javascript
   	alert("XMLHttpRequest not supported");
   	return null;
}	
function ajaxcall_webtil(user_name,passwords)
{
	var obj_name = document.getElementById(user_name);
	var obj_password = document.getElementById(passwords);
	
	if(obj_name.value == "")
		return alertmsg(obj_name,'Please enter Email');
	else if(obj_password.value == "")
		return alertmsg(obj_password,'Please enter Password');
		
	var username = document.getElementById(user_name).value
	var pwd = document.getElementById(passwords).value;
	var xmlhttp;
 	xmlhttp= Init_AJAX();
	xmlhttp.open("GET", "webtil/webtil_login.ajax.php?name="+username+"&password="+pwd, true);
	xmlhttp.onreadystatechange=function() 
	{
		if(xmlhttp.readyState == 4) 
		{
			var i = xmlhttp.responseText;
			if(i == "Success")
				window.location.href = "webtil/customer-login.php";
			else
				alert(i);
 		}
	}
	xmlhttp.send(null);
}
function ajaxcall_webtil_forgot_pwd(email,type)
{
	var obj_email = document.getElementById(email);
	
	if(obj_email.value == "")
		return alertmsg(obj_email,'Please enter Email');

	if(obj_email.value != "")		
	{
		var val = checkEmail(obj_email);
		if(val == 'false')
			return val;
	}
		
	var emailid = document.getElementById(email).value;
	var xmlhttp;
 	xmlhttp= Init_AJAX();
	xmlhttp.open("GET", "webtil/forgot_pwd.ajax.php?email="+emailid+"&typeid="+type, true);
	xmlhttp.onreadystatechange=function() 
	{
		if(xmlhttp.readyState == 4) 
		{
 			var i = xmlhttp.responseText;
			if(i == "Success")
			{
				alert("Your Password has been sent to your Email Address");
				SwitchUIforwebtil(type);
			}
			else
			{
				alert(i);
			}
 		}
	}
	xmlhttp.send(null);
}
function ajaxcall_addtocart(val,qty,cat,subchuild)
{
	var xmlhttp;
 	xmlhttp= Init_AJAX();
	xmlhttp.open("GET", "orderbox.ajax.php?ids="+val+"&quantity="+qty+"&category="+cat+"&childname="+subchuild, true);
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState == 4)
		{
 			var i = xmlhttp.responseText;
			var getstring = i.split("####@@@@");
			if(getstring[0] == "Success")
			{
				//document.getElementById('ajaxloader').style.display = 'none';
				document.getElementById('yourorder').innerHTML = getstring[1];
                document.getElementById("hd_submit").value = 0;
			}
			else
			{
				alert(i);
			}
 		}
	}
	xmlhttp.send(null);
}
function ajaxcall_quantity(qty,index)
{
	var xmlhttp;
 	xmlhttp= Init_AJAX();
	xmlhttp.open("GET", "orderbox.ajax.php?e="+index+"&quantity="+qty, true);
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState == 4)
		{
 			var i = xmlhttp.responseText;
			var getstring = i.split("####@@@@");
			if(getstring[0] == "Success")
			{
				//document.getElementById('ajaxloader').style.display = 'none';
				document.getElementById('yourorder').innerHTML = getstring[1];
                document.getElementById("hd_submit").value = 0;
			}
			else
			{
				alert(i);
			}
 		}
	}
	xmlhttp.send(null);
}
function ajaxcall_addtocart_search(fields)
{
	var xmlhttp;
 	xmlhttp = Init_AJAX();
    var val = document.getElementById(fields).value;
	xmlhttp.open("GET", "search.ajax.php?id="+val, true);
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState == 4)
		{
 			var i = xmlhttp.responseText;
            var getstring = i.split("####@@@@");
            if(getstring[0] == "Success")
            {
			    ajaxcall_addtocart(getstring[1],'1',getstring[2],'');
            }
            else
            {
                alert("Please enter valid Product Code");
                document.getElementById(fields).focus();
            }
 		}
	}
	xmlhttp.send(null);
}
function Checkforenter(e)
{
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer")
        var val = e.keyCode;
    else
        var val = e.which;
    if(val == 13)
    {
        document.getElementById("hd_submit").value = 1;
        ajaxcall_addtocart_search('order_item');
    }
}
function validateSubmitform()
{
    if(document.getElementById("hd_submit").value == 0)
        return true;
    else
        return false;
}
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function checkcheckbox(field)
{
	var val = "";
	var qty = "";
	var cat = "";
	var subchuild = "";
	for(i = 0; i < field.length; i++)
	{
		if(field[i].checked == true)
		{
			var idarray = (field[i].value).split("##");
            if(idarray.length > 2)
			    val += idarray[idarray.length - 1] + ",";
            else
                val += idarray[0] + ",";
			qty += document.getElementById('quantity' + idarray[0]).value + ",";
			cat += idarray[1] + ",";
			if(document.getElementById('subchild' + idarray[0]))
			{
				var subchildarray = (document.getElementById('subchild' + idarray[0]).value).split("##");
				subchuild += subchildarray[1] + ",";
			}
			else
			{
				subchuild += ",";
			}

		}
	}
	if(val == "")
	{
		alert("Please select any Item from list");
		field[0].focus();
		return false;
	}
	else
	{
		val = rtrim(val,",");
		qty = rtrim(qty,",");
		cat = rtrim(cat,",");
		subchuild = rtrim(subchuild,",");
		ajaxcall_addtocart(val,qty,cat,subchuild);
		for(i = 0; i < field.length; i++)
		{
			if(field[i].checked == true)
				field[i].checked = false;
		}
	}
}
function Addproducttroughlink(field)
{
	var val = "";
	var qty = "";
	var cat = "";
	var subchuild = "";
    var idarray = (field).split("##");

    if(idarray.length > 2)
    {
        if(document.getElementById('subchild' + idarray[0]) && document.getElementById('hd_child_id').value != "0")
            val += document.getElementById('hd_child_id').value + ",";
	    else
	        val += idarray[idarray.length - 1] + ",";
    }
    else
    {
        if(document.getElementById('subchild' + idarray[0]) && document.getElementById('hd_child_id').value != "0")
            val += document.getElementById('hd_child_id').value + ",";
	    else
	        val += idarray[0] + ",";
    }
    document.getElementById('hd_child_id').value = "0";
	qty += document.getElementById('quantity' + idarray[0]).value + ",";
	cat += idarray[1] + ",";
	if(document.getElementById('subchild' + idarray[0]))
	{
	    var subchildarray = (document.getElementById('subchild' + idarray[0]).value).split("##");
	    subchuild += subchildarray[1] + ",";
	}
	else
	{
	    subchuild += ",";
	}

	val = rtrim(val,",");
	qty = rtrim(qty,",");
	cat = rtrim(cat,",");
	subchuild = rtrim(subchuild,",");
	ajaxcall_addtocart(val,qty,cat,subchuild);
}
function ChangeProductname(ddid,id,indexs)
{
    var checkboxval = document.getElementById('add' + indexs).value;
    //checkboxval = checkboxval.split("##");
    var e = document.getElementById(ddid);
    var ddval = e.value;
    ddval = ddval.split("##");
	var strtext = e.options[e.selectedIndex].text;
	var productname = strtext.split(" ");
	product = productname[0];
    document.getElementById('add' + indexs).value = "";
    document.getElementById('add' + indexs).value = checkboxval + "##" + ddval[0];
    document.getElementById('hd_child_id').value = ddval[0];
	document.getElementById(id).innerHTML = "- "+product;
}
function RedirectUrlAjax(qty,index)
{
	window.location.href = "takeaway-menu.php?e=" + index + "&qty=" + qty;
}
function validateloginforuser(name,pwd,type)
{
    var obj_name = document.getElementById(name);
	var obj_password = document.getElementById(pwd);

	if((obj_name.value == "") && (obj_password.value == ""))
		return alertmsg(obj_name,'Please enter either Telephone or Email');
	ajaxcall_signup_users(name,pwd,type);
    for(i = 0; i < 200000; i++)
    {

    }
	if(document.getElementById('hd_set_val').value == '0')
		return false;
	else
		return true;
}
function ajaxcall_signup_users(email,pwd,type)
{
	var emailid = document.getElementById(email).value;
	var password = document.getElementById(pwd).value;
	var xmlhttp;
 	xmlhttp= Init_AJAX();
	xmlhttp.open("GET", "customer-login.ajax.php?email=" + emailid + "&pwd=" + password + "&type=" + type, false);
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState == 4)
		{
 			var i = xmlhttp.responseText;
			if(i == "Success")
			{
				document.getElementById('hd_set_val').value = "1";
				return true;
			}
			else
			{
				document.getElementById('hd_set_val').value = "0";
				alert(i);
				return false;
			}
 		}
	}
	xmlhttp.send(null);
}
function CalculateDistanceforPostCodeforuser(Postcode){
	if(document.getElementById(Postcode).value == "")
		return alertmsg(document.getElementById(Postcode),'Please enter Postcode');
	var scriptTag = document.getElementById('twsScript');
	var headTag = document.getElementsByTagName('head').item(0);
	var strUrl = '';
	var strUrl = '';
	strUrl = 'http://services.postcodeanywhere.co.uk/DISTANCESANDDIRECTIONS/INTERACTIVE/DISTANCE/1/json.ws?';
	strUrl += 'key=' + escape('CG41-TD94-TA43-MH31');
	strUrl += '&Start=sw96aq';
	strUrl += '&Finish=' + escape(document.getElementById(Postcode).value);
	strUrl += '&WayPoints=';
	strUrl += '&DistanceType=';
	strUrl += '&CallbackFunction=DistanceCallBackforPostCodeforuser';
	//Add a waiting message
	//if(document.getElementById('pnlOutput'))
	//{
///		document.getElementById('pnlOutput').style.display='none';
   //		document.getElementById('pnlError').style.display='none';
	 //	document.getElementById('pnlResults').style.display='none';
	   //	document.getElementById('btnTest').value = 'Running...';
		//document.getElementById('btnTest').disabled = true;
	//}
	//Make the request
	if (scriptTag)
	{
		try
		{
			headTag.removeChild(scriptTag);
		}
		catch (e)
		{
			//Ignore
		}
	}
	scriptTag = document.createElement('script');
	scriptTag.src = strUrl;
	scriptTag.type = 'text/javascript';
	scriptTag.id = 'twsScript';
	headTag.appendChild(scriptTag);
}
function DistanceCallBackforPostCodeforuser(response){
  	var strHtml='';
	var intColumn=0;
	var intRow=0;
	var strColumns=new Array('SegmentNumber','TotalTime','TotalDistance');
	var strColumn='';
	if (response.length==1 && typeof(response[0].Error) != 'undefined'){
		alert(response[0].Description);
	}
	else{
		if (response.length==0){
			strHtml = '<h2>Sorry, no matching items found</h2>';
		}
		else{
			for (intRow=0; intRow<response.length; intRow++){
				document.getElementById('hd_totaldistance').value = response[intRow].TotalDistance;
			 	if(parseInt(document.getElementById('hd_totaldistance').value) > parseInt(document.getElementById('hd_radious').value))
					document.getElementById('badpostcode_notification').style.display = '';
			 	else
    		 		window.location.href = "destroysession.php?t=1&postcode=" + document.getElementById('txt_delivery_postcode').value;
				//window.location.href = "takeaway/destroysession.php?t=1";
			}
		}
	}
}
function Checkoutconfirmation()
{
    if(confirm('Do you want to Checkout?'))
        window.location.href = 'confirm-order.php';
}
function ajaxcall_webtil_payment_option(paymentoption)
{
	var xmlhttp;
 	xmlhttp= Init_AJAX();
	xmlhttp.open("GET", "payment_option.ajax.php?name="+paymentoption, true);
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState == 4)
		{
			var i = xmlhttp.responseText;
			if(i != "Success")
				alert(i);
 		}
	}
	xmlhttp.send(null);
}
function ajaxcall_webtil_checkout_option(checkoutoption,option)
{
	var xmlhttp;
 	xmlhttp= Init_AJAX();
	xmlhttp.open("GET", "checkout_option.ajax.php?time="+checkoutoption+"&option="+option, true);
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState == 4)
		{
			var i = xmlhttp.responseText;
			if(i != "Success")
				alert(i);
 		}
	}
	xmlhttp.send(null);
}

