/*
This is built to be flexible. The GetAjax function is given the page it needs to get, 
the string and target ID. For example, to change the div PriceBox using page gifts.asp and
a size of 3, then followup with an optional function 
Example of use GetAjax("gifts.asp", "size=3", "PriceBox", "Move()")
will get the response from page gifts.asp?size=3, put that text into PriceBox, then execute the
function Move() upon completion. Any field can be left blank. 
*/
var xmlHttp;
var TheTarget; // this is the global variable that gets assigned the target ID name
var TheFollowup; // the name of the function to follow the completion of the AJAX function.
var DefaultErrorFunction = "error_box()";
var ErrorFunction = DefaultErrorFunction; //what to do if there is an error. This is set by the more complex ajax function, and also serves as a means to prevent double calling an ajax link
function GetAjax(page, str, target, followup)
{ 
	if (str.length > 0)
	{ 
		//var url="gift_sizes.asp?sid=" + Math.random() + "&size=" + str
		var url=page +"?sid="+ Math.random() + "&" + str;
		xmlHttp=GetXmlHttpObject2(stateChanged);
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
		TheTarget=target;
		TheFollowup=followup;
		document.getElementById(TheTarget).innerHTML="Loading...";
	} 
	else
	{ 
		TheTarget=target;
		TheFollowup=followup;
		document.getElementById(TheTarget).innerHTML="";
	} 
} 

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		try
		{
			//check for server side errors
			if(xmlHttp.responseText.substring(0,6) == "<Error")
			{

				//load the response xml (different for ff and IE)
				var oXML;
				if (navigator.userAgent.indexOf("MSIE")>=0)
				{ 
					var oXML = new ActiveXObject("Msxml2.DOMDocument.3.0");
					oXML.loadXML(xmlHttp.responseText);
				}
				else
					oXML = xmlHttp.responseXML;
					

				//execute the action
				eval(oXML.getElementsByTagName('Action').item(0).firstChild.nodeValue);
				return;
			}
		
			//write the output to the target
			document.getElementById(TheTarget).innerHTML=xmlHttp.responseText 
		
		
			//The followup function, if specified, run the function
			if(TheFollowup!=""){
				eval(TheFollowup);
				//evals the followup, then resets the ErrorFunction to default... 
				//if it gets here, there obviously wasn't an AJAX error!
				ErrorFunction = DefaultErrorFunction;
			}
			
		}
		catch(err)
		{
			eval(ErrorFunction);
		}
	} 
} 

function GetXmlHttpObject2(handler)
{ 
	var objXmlHttp=null

	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		eval(ErrorFunction);
		return null;
	}
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
		{
			strName="Microsoft.XMLHTTP"
		} 
		try
		{ 
			objXmlHttp=new ActiveXObject(strName);
			objXmlHttp.onreadystatechange=handler;
			return objXmlHttp;
		} 
		catch(e)
		{ 
			eval(ErrorFunction)
			return null;
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		try
		{
			objXmlHttp=new XMLHttpRequest();
			if (objXmlHttp.overrideMimeType) {
				objXmlHttp.overrideMimeType('text/xml');
			}
			objXmlHttp.onload=handler;
			objXmlHttp.onerror=handler;
			
			return objXmlHttp
		}		
		catch(e)
		{ 
			eval(ErrorFunction)
			return null;
		} 
	}
} 

//This is an alternate calling of the ajax function, which requires that an error function or string is attached. For example, you could have "window.redirect=cart.asp?additem=23" as the OnFail variable
function GetAjaxCustom(page, str, target, followup, OnFail){
 //Since ErrorFunction is reset to default... this function will know if this is a resend
 //of the AJAX call that is currently executing and skips a second execution!

	if(OnFail!=ErrorFunction){
		ErrorFunction=OnFail;
		GetAjax(page, str, target, followup)
	}
}

//This is the popup box that allows users to go to the old gifts page if AJAX does not work for their browser
function error_box()
	{
	input_box=confirm("Sorry, there was an error. Would you like to view our basic gift page? \n\n Click OK to view the old gift page. Click cancel to stay on this page");
	if (input_box==true)
	{ 
	// OK will redirect. Cancel will just close the alert box.
	window.location='oldgifts.asp'
	}

}

//HELP functions
function GetHelp(page, topic, target){
				if(document.getElementById(target).getAttribute('open')==0){ //if it's closed
					document.getElementById(target).style.display='block';
					GetAjax('help/'+page+'.asp', 'topic='+topic+'&id='+target, target, '');
					document.getElementById(target).setAttribute('open',1)
				}
			}
			function HelpClose(target){
				if(document.getElementById(target).getAttribute('open')==1){ //if it's open
					document.getElementById(target).style.display='none';
					document.getElementById(target).setAttribute('open',0)
				}
			}
