/**
 * Bewell.ro Ajax Framkework
 * -------------------------
 * @author andrei rusu <andrei.rusu@activesoft.ro>
 */

/**
 * Class contructor
 * @param method		: GET / POST
 * @param url			: the request url
 * @param callbackMethod: function that handles the http request results
 * @param params		: for POST request; empty for GET requests
 * @param objId			: the HTML element in which will the result will be displayed
 * @param loading		: true if 'loading...' will be displayed during the http request, false otherwise
 */
AjaxRequest = {};


/**
 * creates an XMLHttpRequest instance
 */

AjaxRequest.createXmlHttpRequestObject = function ()
{
	// will store the reference to the XMLHttpRequest object
  	var xmlHttp;
  	// this should work for all browsers except IE6 and older
  	try {
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	} catch(e) {
	    // assume IE6 or older
	    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
	                                    "MSXML2.XMLHTTP.5.0",
	                                    "MSXML2.XMLHTTP.4.0",
	                                    "MSXML2.XMLHTTP.3.0",
	                                    "MSXML2.XMLHTTP",
	                                    "Microsoft.XMLHTTP");

	    // try every prog id until one works
	    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
	      try {
	        // try to create XMLHttpRequest object
	        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
	      }
	      catch (e) {}
	    }
  }

  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else
    return xmlHttp;
}

/**
 * sends the XMLHttpRequest
 */

AjaxRequest.makeRequest = function()
{
	this.request.open(this.method, this.url, true);
	
    // if the send method is POST, sets the request headers
    if (this.method == 'POST') {
		this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    this.request.setRequestHeader("Content-length", this.params.length);
	    this.request.setRequestHeader("Connection", "close");
	}
	
	this.request.onreadystatechange = this.callback;

	switch (this.method) {
		case 'POST':
			    this.request.send(this.params);
		    	break;
		case 'GET':
				this.request.send(this.url);
				break;
	}
}

/**
 * handles the ready state
 */
AjaxRequest.checkReadyState = function()
{
	switch (this.loading)
	{
		case false:
			
			switch(this.request.readyState)
			{
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
				case 4:
					this.isUpdating = false;
					return this.request.status;
			}
			break;

		case true:
			switch(this.request.readyState)
			{
				case 1:
					document.getElementById(this.objId).innerHTML = '<img src="pics/load_calendar.gif" border="0" style="margin:50px 0px 0px 80px;">';
					document.getElementById(this.objId).style.display = 'block';
					break;
				case 2:
					document.getElementById(this.objId).innerHTML = '<img src="pics/load_calendar.gif" border="0" style="margin:50px 0px 0px 80px;">';
					break;
				case 3:
					document.getElementById(this.objId).innerHTML = '<img src="pics/load_calendar.gif" border="0" style="margin:50px 0px 0px 80px;">';
					break;
				case 4:
					this.isUpdating = false;
					return this.request.status;
			}
			break;
	}
}

/**
 * gets the http response
 */
AjaxRequest.getResponse = function()
{
    if(this.request.getResponseHeader('Content-Type').indexOf('xml') != -1){

        return this.request.responseXML.documentElement;

    } else {

        return this.request.responseText;

    }
}

/**
 * updates the html with the http response
 */
AjaxRequest.Update = function(method, url, params, objId, loading, callbackMethod)
{	
	this.request 	= AjaxRequest.createXmlHttpRequestObject();
	this.isUpdating = false;
	this.method		= method;
	this.url		= url;
	this.params		= params;
	this.objId		= objId;
	this.loading	= loading;
	
	AjaxRequest.callback = callbackMethod;
	
    AjaxRequest.makeRequest();
    this.isUpdating = true;
}

