/**
*	Various AJAX tools.
*
*	@author Giorgio Sintichakis
*	Copyright (c) 2007 HitGrab Inc. and Giorgio Sintichakis
*/

function createHttpRequest() { try { return new XMLHttpRequest(); } catch (e) { return new ActiveXObject("Msxml2.XMLHTTP"); } }

var http = createHttpRequest();
var sync = true;

function getResp() { return http.responseText; }
//function launchHttpError(id, msg) { ziporDown(id, msg); }

function doGetHttpRequest(url, params, respFunction, respParams)
{
	http = createHttpRequest();
	http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
			if (http.status == 200) respFunction.call(this, respParams);
			//else launchHttpError('errorMsg', http.status);
			cancelHttpRequest();
		}
	};
	http.open("GET", url+"?"+params, sync);
	http.send(null);
	sync = false;
}

function doPostHttpRequest(url, params, respFunction, respParams)
{
	http = createHttpRequest();
	http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
			if (http.status == 200) respFunction.call(this, respParams);
			//else launchHttpError('errorMsg', http.status);
			cancelHttpRequest();
		}
	};	
	http.open("POST", url, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	http.send(params);
	sync = false;
}

function cancelHttpRequest() { http.abort(); http = createHttpRequest(); sync = true; }


/**
*	GoAjax - AJAX object. "GoJax!"
*	
*	@author Giorgio Sintichakis
*	Copyright (c) 2007 HitGrab Inc. and Giorgio Sintichakis
*
*	Object with prepared functions for executing an XMLHttpRequest.
*
*	Example of Use:
*	var go = new GoAjax();
*	// Mandatory arguments for execution
*	go.setUrl("myManager.php");
*	go.setFunc("findUser"); // Optional field - denotes a PHP function name to run using call_user_func
*	go.setParams(["name="+clientName, "phoneNum="+clientNum]);
*
*	// Optional arguments for code execution before/after XMLHttpRequest has completed
*	go.setLoading([Object function]); // Executes the provided "loading" function before the request
*	go.setElement('profileBlock'); // Fills the innerHTML of an element called "profileBlock" with the responseText of the XHR object
*	go.setResponse([Object function]); // Executes the provided function object after the request
*
*	// Begins the XHR
*	go.start();
*/
var GoAjax = function() {
	var http, method='GET', url, func='', params='', element='', loading, reaction='', reactionParams, sync=true;

	this.setMethod = function(v) { method = v; }; // Sets the send method (either GET or POST) - default is GET
	this.setUrl = function(v) { url = v; }; // Sets the PHP script which receives parameters from the XHR Object
	this.setFunc = function(v) { func = "func="+v; }; // Optional field - denotes a PHP function name to run using call_user_func
	
	this.setParams = function(p) { 
		//for (i=0; i<arguments.length; i++) params += arguments[i] + "&"; 
		params = p;
	}; // Sets params using an initialized array of keys and values
	
	this.setElement = function(v) { element = v; }; // Fills the innerHTML of an element called "profileBlock" with the responseText of the XHR object
	this.setLoading = function(v) { loading = v; }; // Executes the provided "loading" function before the request is completed
	
	this.setReaction = function(v, p) { 
		reaction = v; 
		reactionParams = p;
	}; // Executes the provided function object after the request is completed
	
	this.setSync = function(v) { sync = v; }; // Sets the asynchronous state of the XHR object

	// Begins the XHR - returns false if Url has not been set, or the XHR object is invalid
	this.start = function() {
		http = createHttpRequest();
		if (!http || !url) return false;
		else { beginRequest(); return true; }
	};
	
	// Continues the request, sending using the appropriate method
	var beginRequest = function() {
		if (loading) loading.call();
		http.onreadystatechange = handleResponse;
		if (method=="GET") {
			http.open("GET", url+"?"+params+"&"+func, sync);
			http.send(null);
		}
		else if (method=="POST") {
			http.open("POST", url, sync);
			http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http.setRequestHeader("Content-length", params.length + func.length);
			http.setRequestHeader("Connection", "close");
			http.send(params+"&"+func);
		}
	};
	
	// Executes each time the readyState of the XHR object changes
	var handleResponse = function() {
		if (http.readyState == 4) {
			if (http.status == 200) {
				if (reaction!='') reaction.call(this, getResponse(), reactionParams);
				if (element!='') fillElementHTML(element, http.responseText);
			}
		}
	};
	
	var getResponse = function() { return http.responseText; };
	
	var createHttpRequest = function() { try { return new XMLHttpRequest(); } catch (e) { return new ActiveXObject("Msxml2.XMLHTTP"); } };
	var cancelHttpRequest = function() { http.abort(); http = this.createHttpRequest(); };
};

/**
*	Class based ajax object.
*	
*	@author Giorgio Sintichakis (Ever-so-Slightly Modified by Dave Vanderburg)
*	Copyright (c) 2007 HitGrab Inc. and Giorgio Sintichakis
*	www.hitgrab.com
*/
function AjaxRequest() {

	var http = createHttpRequest();
	var sync = true;
	
	this.createHttpRequest = createHttpRequest;
	this.cancelHttpRequest = cancelHttpRequest;
	this.getResp = getResp;
	this.doGetHttpRequest = doGetHttpRequest;

	function createHttpRequest() { try { return new XMLHttpRequest(); } catch (e) { return new ActiveXObject("Msxml2.XMLHTTP"); } }
	function cancelHttpRequest() { http.abort(); http = createHttpRequest(); sync = true; }
	function getResp() { return http.responseText; }
	
	function doGetHttpRequest(url, params, respFunction, respParams)
	{
		http = createHttpRequest();
		http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				if (http.status == 200) respFunction.call(this, getResp(), respParams);
				else alert('AJAX Request Error: '+http.status);
				cancelHttpRequest();
			}
		};
		http.open("GET", url+"?"+params, sync);
		http.send(null);
		sync = false;
	}

	function doPostHttpRequest(url, params, respFunction, respParams)
	{
		http = createHttpRequest();
		http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				if (http.status == 200) respFunction.call(this, respParams);
				else alert('AJAX Request Error: '+http.status);
				cancelHttpRequest();
			}
		};	
		http.open("POST", url, true);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
		http.send(params);
		sync = false;
	}
}