/*
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
		
		//document.location = url;
		
		xmlHttp=GetXmlHttpObject(stateChanged)
		xmlHttp.open("GET", url , true)
		xmlHttp.send(null)
		TheTarget=target
		TheFollowup=followup
		//document.getElementById(TheTarget).innerHTML="Loading..."
	} 
	else
	{ 
		document.getElementById(TheTarget).innerHTML=""
	} 
} 

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if(TheTarget != "")
			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
		}
	}
}


function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null

	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		eval(ErrorFunction)
		return 
	}
	
	
	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 
		} 
	} 
		
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest()
		if (objXmlHttp.overrideMimeType) {
				objXmlHttp.overrideMimeType('text/xml');
			}
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
	
	eval(ErrorFunction)
} 

//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()
	{
	alert("Sorry, you really must have AJAX to view this site. Please use FireFox, IE 6+ or Safari browsers.");
	}
