
var connection;
targets = new Array(); /* array that will hold ids of targeted divs */

function showXHR(myXHR){var msg = 'The following is exposed in the XMLHttpRequest';/*<br />*/msg += ' Object in your current browser:\n';/*<br />*/for(var i in myXHR){/*<br />*/try{msg += i + ': is a ' + typeof(myXHR[i]) + '\n';/*<br />*/}catch(e){alert('Error: '+e);}}/*<br />*/alert(msg);}

function createRequestObject() {
    var ro = false;
	
	if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
	  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
	  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
	  try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
	  try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
	  return false;
	};

	try {
		ro = new XMLHttpRequest();
	} catch (e) {};
	/*showXHR(ro);*/
	connection = ro;
    return ro;
}

var http = createRequestObject();
var scrollUp;
var linkObj_0;

if (connection && !$_hh['aflag']) {
	setTimeout(function() {
		httpSend("target=set_ajax_flag",handleResponse2);
	}, 2000);
}

function httpSend(vars,respFn) {
	
	if (!respFn) respFn = handleResponse;
	http.open('post', $_hh['base'] + "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);
	
	//showXHR(http);
}

function httpSendC(vars,respFn) {
	
	if (!respFn) respFn = handleResponse;
	
	var v = vars.split('&'); parts = [];
	for (var i = v.length; i > 0; i--) {
		if (v[i-1]!="") {
			parts.push(v[i-1].split('=')[1]);
		}
	}
	
	var url = $_hh['base'] + "ajax/" + parts.join("/") + ".html";
	
	http.open('get', url);	
	//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);
	http.send(null);
	
	//showXHR(http);
}

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 {
			var $href = $(linkObj).attr("rev") || linkObj.href;
			
			if ($href.indexOf("?") == -1) {
			
			} else {
				$href = $href.substring($href.indexOf("?") + 1);
			}
			a = $href + '&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 + " ...</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 sndReqC(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 {
			var $href = $(linkObj).attr("rev") || linkObj.href;
			
			if ($href.indexOf("?") == -1) {
			
			} else {
				$href = $href.substring($href.indexOf("?") + 1);
			}
			a = $href + '&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';


		httpSendC(a);

		if (menusect_name) {
			document.getElementById('menu_panel_title').innerHTML = "<b>Loading " + menusect_name + " ...</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 */
		try {
			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 += encodeURIComponent(el.name) + "=" + encodeURIComponent(el.value) + "&";
		
				} else if (el.type != "submit" && el.type != "image") {
		
					str += encodeURIComponent(el.name) + "=" + encodeURIComponent(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';
			
			if (document.documentElement) {
				document.documentElement.style.cursor = 'wait';
			}
			if (document.links) {
				for (var j=0; j < document.links.length; j++) {
					document.links[j].style.cursor='wait';	/* all links should also 'wait' */
				}
			}
		} catch(e) {
			return true;
		}
		
		httpSend (str);
		
		try {
			document.getElementById('menu_panel_title').innerHTML = "<b>Adding to my order...</b>";
		} catch(e) {}
		
		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){
		try {
			resp = new String();
			response = new Array();
			tempx = new String();
			
			if (!http.responseText && linkObj_0) {
				// if there is no responce but we are waiting for it -- just forward to location needed
				location.href=linkObj_0;
				return;
			}
			
			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;
				
				/* 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[i]).trigger("load", resp);
			}
			
			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';
				linkObj_0 = null;
			}
			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;
					}
			}
			
		} catch(e) {
			// if something wrong -- reload page
			location.reload();
		}
		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 */
			}
     }

}
