var connection;
targets = new Array(); /* array that will hold ids of targeted divs */

function createRequestObject() {
    var ro = false;
	if (typeof (XMLHttpRequest) != "undefined") {
		ro = new XMLHttpRequest();
	};
	if (typeof (ActiveXObject) != "undefined"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	};
	connection = ro;
    return ro;
}

var http = createRequestObject();
var scrollUp;
var linkObj_0;

if (connection) {
	httpSend("target=set_ajax_flag",handleResponse2);
}

function httpSend(vars,respFn) {
	
	if (!respFn) respFn = handleResponse;
	http.open('post', 'http://hungryhouse.co.uk/ajax.php');	/* use post, because we may be sending many variables */
	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	http.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ); /* important line for ie */
	http.onreadystatechange = respFn;
	http.send(vars);
}


function sndReq(linkObj,target_id,scr,menusect_name) {
	if (connection) {
		var a;
		if (typeof linkObj == "string") {
			if (linkObj.indexOf("?") == -1 ) {
				a = linkObj + '&target=' + target_id;
			} else {
				a = linkObj.substring(linkObj.indexOf("?") + 1) + '&target=' + target_id;
			}

		} else {
			if (linkObj.href.indexOf("?") == -1) {
				a = 'target='+target_id;
			} else {
				a = linkObj.href.substring(linkObj.href.indexOf("?") + 1) + '&target=' + target_id;
			}
			linkObj_0 = linkObj;
		}
		scrollUp = scr;
		targets[0] = target_id;

		document.documentElement.style.cursor = 'wait';
		for (var j=0; j < document.links.length; j++) {
				document.links[j].style.cursor='wait';	/* all links should also 'wait' */
		}
		if (typeof linkObj != "string") linkObj.style.cursor = 'wait';


		httpSend(a);

		if (menusect_name) {
			document.getElementById('menu_panel_title').innerHTML = "<b>Loading " + menusect_name.substr(0,25) + " ...</b>";
		};

		return false;	/* false means don't follow the href link, since the connection is working*/

	} else {

		return true;	/* true means follow the <a> href link*/

	}
}

function sndReq_2(inputObj,fobj){	/* used for submitting the form in edit panel */
	if (connection) {
	
		/* first get the values of the form elements */
	
		var str = "menu=order&submenu=build_order&target=edit_panel_submit&";
		for (var i=0; i < fobj.elements.length; i++){
			var el = fobj.elements[i];
			if ( el.type == "checkbox" || el.type == "radio"){
	
				if (el.checked) str += el.name + "=" + el.value + "&";
	
			} else if (el.type != "submit" && el.type != "image") {
	
				str += el.name + "=" + el.value + "&";
	
			}
										
		}
		str = str.substring(0,str.length-1);
		/* these divs will be updated in handleResponse */
		targets[0] = "menu_div";
		targets[1] = "order_summary_div";
/*		targets[2] = "my_order_div";	the panels were merged, so this is not needed */

		inputObj.style.cursor = 'wait';
		document.documentElement.style.cursor = 'wait';
		for (var j=0; j < document.links.length; j++) {
				document.links[j].style.cursor='wait';	/* all links should also 'wait' */
		}


		httpSend (str);
		

		document.getElementById('menu_panel_title').innerHTML = "<b>Adding to my order...</b>";

		return false;	/* false means don't follow the submit button, since the request was sent by xmlhttprequest */

	} else {

		return true;	/* true means follow the submit button */

	}
}

function handleResponse() {
	if(http.readyState == 4){
		resp = new String();
		response = new Array();
		tempx = new String();
		response = http.responseText.split("[separator]");	/* if more divs are targeted, the text is separated by the string "[separator]" */
		for (var i=0; i < targets.length ; i++) {
			resp = response[i];
			/* don't assign script sections to innerHTML */
/*			var tempx = resp.replace(/<script[^>]*>[^<]*<\/script>/gi,"");*/
			tempx = resp.replace(/<script>[^<]*<\/script>/gmi,"");
	        document.getElementById(targets[i]).innerHTML = tempx;
			document.documentElement.style.cursor = '';
			for (var j=0; j < document.links.length; j++) {
					document.links[j].style.cursor='pointer';	/* reset the cursor for links */
			}
			if (linkObj_0) linkObj_0.style.cursor = 'pointer';
			if (scrollUp){	/* in case we need to scroll up the window */
					if (self.pageYOffset) { scrY = self.pageYOffset; }
					else { scrY = document.body.scrollTop + document.documentElement.scrollTop; }
					if (scrY>440){
						window.scrollTo(0,260);
						scrollUp = false;
					}
			}
			/* evaluate scripts if present */
			var script_pos = 0;
			while (script_pos != -1) {
				script_pos = resp.indexOf("<script",script_pos);
				if ( script_pos != -1){
					script_pos = resp.indexOf(">",script_pos) + 1;
					var script_end = resp.indexOf("</script>",script_pos);
					var script_content = resp.substring(script_pos, script_end);
					eval (script_content);
					script_pos = script_end + 9;
				}
			}
		}
		targets = new Array();
	}
}
function handleResponse2(){
}

function send_dodirectpaymentrequest()
{
for (var i=0; i < document.form1_dopay.x.length; i++)
   {
   if (document.form1_dopay.x[i].checked)
      {
      var x = document.form1_dopay.x[i].value;
      }
   }

var order_num=document.form1_dopay.order_num.value;
var amount=document.form1_dopay.amount.value;
http.open("GET", "profile/payflow_data_ajax.php?x=" + x + "&order_num=" + order_num + "&amount=" + amount, true);
http.onreadystatechange = handleHttpResponsepayflow;

		document.documentElement.style.cursor = 'wait';
		for (var j=0; j < document.links.length; j++) {
				document.links[j].style.cursor='wait';	/* all links should also 'wait' */
		}

		document.getElementById('payment_form_here').innerHTML = "<b>Please wait...</b>";

http.send(null);
}

function handleHttpResponsepayflow() {

  if (http.readyState == 4) {

    // Split the comma delimited response into an array

    results = http.responseText;

    document.getElementById('payment_form_here').innerHTML = results;
document.documentElement.style.cursor = '';
			for (var j=0; j < document.links.length; j++) {
					document.links[j].style.cursor='pointer';	/* reset the cursor for links */
			}
     }

}
