/*
	Function Name: ContentLoader
	Arguments: 
		URL: the URL to which the request is sent
		METHOD: the method used to send the request.  If this is set to JSON or XML, the POST method will be used.
		CALLBACK: the function to call when the server response is complete
		RESULTDIV: the name of the div that will contain the output
		Additional Optional Arguments:
			After the fourth argument, there may be one additional argument (an object with properties and values, an XML string, or a JSON string) or an even number of string arguments, where string1=string2,string3=string4, etc.
	Returns: Nothing
	Notes: Used to create and send an XMLHttpRequest object and call a specified callback function when the server response is complete.
*/
function ContentLoader(URL,METHOD,CALLBACK,RESULTDIV)
{
	this.wrongArgsMsg = "WARNING: In ContentLoader.js, after the fourth argument, there may be one additional argument (an object, an XML string, or a JSON string) or an even number of string arguments, where string1=string2,string3=string4, etc";
	this.xmlhttp=null;
	this.url = URL;
	this.method = (typeof METHOD == "undefined" || typeof METHOD == null) ? "GET" : METHOD.toUpperCase();
	this.callBack=CALLBACK;
	if (typeof RESULTDIV == "string")
		this.resultDiv=document.getElementById(RESULTDIV);
	else
		this.resultDiv=RESULTDIV;
	
	var args=new Object();
	if (arguments.length == 5) //last argument must be an object, an XML string, or a JSON string
	{
		if (typeof arguments[4] == "string" && this.method != "XML" && this.method != "JSON");
		args=arguments[4];
	}
	else if (arguments.length>4) //populate object property-value pairs: 5=6,7=8,9=10,etc.
	{
		if (arguments.length % 2 != 0) alert(this.wrongArgsMsg);
		for (var i=4;i<arguments.length;i+=2)
		{
			args[arguments[i]]=encodeURIComponent(arguments[i+1]);
		}
	}
	this.loadAjax(args);
}


ContentLoader.prototype.loadAjax=function(PARAMS)
{	
	var PostQS;
	var DataType="string";
	if (this.method=="JSON" || this.method=="XML")
	{
		DataType=this.method;
		PostQS = PARAMS;
		this.method="POST"; //Sets method to post for sending data to server.
	}
	else if (this.method=="POST")
	{
		PostQS = _createQueryString(PARAMS);
	}
	else //GET OR HEAD
	{
		this.url = this.url + "?" + _createQueryString(PARAMS);
		PostQS = null;
	}
	
	if ( this.xmlhttp=_createXHR() )
	{
		try
		{
			var loader=this;
			this.xmlhttp.onreadystatechange=function() 
			{
       			loader.onReadyState.call(loader);
			}
			this.xmlhttp.open(this.method,this.url,true);
			
			if (DataType == "XML")
			{ 
				this.xmlhttp.setRequestHeader("Content-Type", "text/xml"); 
			}
			else if (this.method=="POST")
			{
				this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
			}
			this.xmlhttp.send(PostQS);
		}
		catch(exc)
		{
			alert("ERROR: " + exc.message);
		}
	}
	else
	{
		alert("ERROR: AJAX NOT SUPPORTED");
	}
}

ContentLoader.prototype.onReadyState=function()
{
	if (this.xmlhttp.readyState==4 && this.xmlhttp.status == 200)
	{
		if (this.callBack) this.callBack.call(this);
	}
	else if (this.xmlhttp.readyState==4)
	{
		alert("Error: " + this.xmlhttp.statusText);
	}
}

/*
	Function Name: _createXHR
	Arguments: none
	Returns: browser-specific xmlhttp object or false
*/
function _createXHR()
{
	try
	{
		xmlhttp = new XMLHttpRequest();
	}
	catch(exc1)
	{
		var ieXmlHttpVersions = new Array();
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";
		
		var i;
		for (i=0; i < ieXmlHttpVersions.length; i++)
		{
			try
			{
				xmlhttp = new ActiveXObject(ieXmlHttpVersions[i]);
				break;
			}
			catch (exc2)
			{
				alert(ieXmlHttpVersions[i] + " not supported.");
			}
		}
	}
	return xmlhttp;
}

function _createQueryString(ARR)
{
	var qs="timestamp=" + new Date().getTime();
	if (typeof ARR == "object") //if no arguments are passed, arguments has a length of 1 and arguments[0] is undefined.
	{
		for (i in ARR)
		{
			qs += "&" + i + "=" + ARR[i];
		}
	}
	return qs;
}