/******************--------- Ajax call helpers ----------*************************/
ajaxCall = {
	// The createXMLHttpRequest creates the actual object, the conditions within the method just determine how to create it.
	createXMLHttpRequest:function(value, callfunction) 
	{

		// If the brwser supports ActiveX objects, use these to create the XMLHttpRequest object
		if (window.ActiveXObject) 
		{
			ajaxCall.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		}
		else if (window.XMLHttpRequest) 
		{
			ajaxCall.xmlHttp = new XMLHttpRequest();
		}
		
		var method = "POST";
		// Set Status to 1 which means the upload is starting
		//var parameters = "iTotal="+iTotal+"&iRead="+iRead+"&iStatus="+status+"&sessionid="+ajaxCall.sid+"&dtnow="+dtnow+"&dtstart="+dtstart;
		var parameters = value;
		// Progress url
		var url = "/library/ajax_products.php";
		// track(link);

		if(callfunction == 'getprice') {
			var method = "GET";
			var url = "/library/ajax_products.php?"+value;
		}	
		// Set up a call to the server. 
		// Info: 'open' method assigns HTTP type (GET, POST or PUT), URL can be relative or absolute
		// IMPORTANT: Use POST because GET has a limited string length
		// Last argument determines is call should by asynchronous or not
		ajaxCall.xmlHttp.open(method, url, false);
		
		if(method == "POST") {
			//Send the proper header information along with the request
			ajaxCall.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			ajaxCall.xmlHttp.setRequestHeader("Content-length", parameters.length);
			ajaxCall.xmlHttp.setRequestHeader("Connection", "close");
		}

		// The event handler that fires on every state change (onreadystatechange stores the pointer to the callback function so the callback knows where its at and what its got to do).
		
		// +++++++++++++++++++ NEW: FIREFOX 3 fix
		// Check to see it addEventListener() method is supported (ff 3 supports this)
		if(ajaxCall.xmlHttp.addEventListener) 
		{
			ajaxCall.xmlHttp.addEventListener("load", ajaxCall.transferComplete, false);
		}
		// +++++++++++++++++++ NEW: FIREFOX 3 fix
		else
		{
			// NOTE: This doesnt work in firefox 3
			ajaxCall.xmlHttp.onreadystatechange = ajaxCall.goCallback; 
		}
		
		// Send the request to the detination resource (url var)
		// null is used when using the GET method. If using post we need to insert the post parameters here
		ajaxCall.xmlHttp.send(parameters);
	},
	
	// Check the readyState property on the XMLHttpRequest object
	goCallback:function() 
	{
		// Once the response is complete, check the response header to make sure everything is ok
		if (ajaxCall.xmlHttp.readyState == 4) 
		{
			// If all is well, run the function (200 is server code 'ok')
			if (ajaxCall.xmlHttp.status == 200) 
			{
				//alert(ajax.xmlHttp.responseText);
				ajaxCall.returnedContent = ajaxCall.xmlHttp.responseText;
				//return returnedContent;
				//setTimeout("pollServer()", 1000); // Call pollserver() function
			}
		}
	},
	
	// Firefox 3.1 adds support for DOM progress event monitoring of XMLHttpRequest transfers; this follows the Web API specification for progress events.
	transferComplete: function() {		
		ajaxCall.setFileValues();
		return;
	},
	
	// Set the file vakues for the PERL script
	setFileValues: function() {	
	
		// ajaxCall.returnedContent = ajaxCall.xmlHttp.responseText;
		//var data = eval('(' + ajaxCall.xmlHttp.responseText + ')');
		ajaxCall.returnedContent = ajaxCall.xmlHttp.responseText;
//		ajaxCall.percent = Math.round(data.percent);
//		ajaxCall.remaining = data.remaining;
//		ajaxCall.elapsed = data.elapsed;
//		ajaxCall.status = data.status;
//		ajaxCall.iRead = data.iRead;
//		ajaxCall.iTotal = data.iTotal;
//		
//		ajaxCall.dtstart = data.dtstart;
//		ajaxCall.dtnow = data.dtnow;
//		
//		var container = document.getElementById("complete");
//		container.innerHTML = "[WHY IS THIS NOT WORKING?]Est time left: "+ajaxCall.remaining+"<br />Elapsed time: "+ajaxCall.elapsed+"<br />";
		return;
	}
}
