/**
*	@fileoverview Simple example that shows how to encapsulate XMLHTTPRequestCalls
*
*	@author Mike Chambers (mesh@adobe.com)
*   @modified by Kenneth Skovhede (ks@geograf.dk)
*/


/**
*	Constructor for the class.
*
*	@param {String} dataURL The path to the data that the class
*	will load (OPTIONAL)
*	@param {function} callbacksuccess The function to call when data arrives
*	@param {function} callbackerror The function to call in case of an error
*	@param {boolean} cachebuster If set, the function will add random data to 
*   the url in a parameter called nocache, and nocacheguid
*
*	@constructor
*/
function AjaxRequest(dataURL, callbacksuccess, callbackerror, cachebuster)
{
	if(dataURL != undefined)
		this._dataURL = dataURL;

	if(callbacksuccess != undefined)
		this._callbacksuccess = callbacksuccess;

	if(callbackerror != undefined)
		this._callbackerror = callbackerror;

	if (cachebuster)
	{
		this._cachebuster = true;
		this._cachebusterindex = 0;
		this._cachebusterguid = this._randomString(15);
	}

	if (this._dataURL != null)
		this.load();
}

//where to load the data from
AjaxRequest.prototype._dataURL = null;

//var to hold an instance of the XMLHTTPRequest object
AjaxRequest.prototype._request = null;

//Function to call with the resultant data
AjaxRequest.prototype._callbacksuccess = null;

//Function to call in case of an error
AjaxRequest.prototype._callbackerror = null;

//Indicates if the cachebuster is active
AjaxRequest.prototype._cachebuster = false;

//The current cachebusterindex
AjaxRequest.prototype._cachebusterindex = 0;

//The current cachebuster GUID
AjaxRequest.prototype._cachebusterguid = null;

//Private list of allowed cachebuster GUID characters
AjaxRequest.prototype._cachebusterguidchars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";


/**************** Public APIs **********************/


/**
*	Tells the class to load its data
*/
AjaxRequest.prototype.load = function(url, postdata)
{
	//get a new XMLHTTPRequest and store it in an instance var.
	this._request = this._getXMLHTTPRequest();
	
	//set the var so we can scope the callback
	var _this = this;
	
	//callback will be an anonymous function that calls back into our class
	//this allows the call back in which we handle the response (_onData())
	// to have the correct scope.
	var generatedurl;

	if (url != null && url != undefined)
		generatedurl = url;
	else
		generatedurl = this._dataURL;

	if (this._cachebuster)
	{
		this._cachebusterindex++;
		if (generatedurl.indexOf('?') > 0)
			generatedurl += '&nocache=' + this._cachebusterindex + '&nocacheguid=' + this._cachebusterguid;
		else
			generatedurl += '?nocache=' + this._cachebusterindex + '&nocacheguid=' + this._cachebusterguid; 
	}

	if (postdata != null)
	{
		this._request.onreadystatechange = function(){_this._onData()};
		this._request.open("POST", generatedurl, true);
		this._request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this._request.send(postdata);
	}
	else
	{
		this._request.onreadystatechange = function(){_this._onData()};
		this._request.open("GET", generatedurl, true);
		this._request.send(null);
	}

}


/***************Private Data Loading Handlers*******************/


//callback for when the data is loaded from the server
AjaxRequest.prototype._onData = function()
{
	if(this._request != null && this._request.readyState == 4)
	{
		if(this._request.status == "200" && this._callbacksuccess != null)
		{
			var text = this._request.responseText;
			var status = this._request.status;
			delete this._request;
			
			this._callbacksuccess(text, status, this);
		}
		else if (this._callbackerror != null) 
		{
			var text = this._request.statusText;
			var status = this._request.status;
			delete this._request;

			this._callbackerror(text, status, this);
		}
	}
}

/***************Private Data Util Functions ********************/


//returns an XMLHTTPRequest instance (based on browser)
AjaxRequest.prototype._getXMLHTTPRequest = function()
{
	var xmlHttp;
	try
	{
		xmlHttp = new ActiveXObject("Msxml2.XMLHttp");
	}
	catch(e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		}
		catch(e2)
		{
		}
	}
	
	if(xmlHttp == undefined && (typeof XMLHttpRequest != 'undefined'))
	{
		xmlHttp = new XMLHttpRequest();
	}
	
	return xmlHttp;
}

// returns a random string
AjaxRequest.prototype._randomString = function(string_length) 
{
	
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * this._cachebusterguidchars.length);
		randomstring += this._cachebusterguidchars.substring(rnum,rnum+1);
	}
	return randomstring;
}

AjaxRequest.prototype.SubmitForm = function(form, method) 
{
	if (!form.elements)
		form = document.getElementById(form);
		
	var q='';
	for(var i=0;i<form.elements.length;i++)
		q+='&' + form.elements[i].name + '=' + encodeURIComponent(form.elements[i].value);
	
	q = q.substring(1);
	
	if (method == "GET")
		this.load(form.action + '?' + q);
	else
		this.load(form.action, q);
}


