function Ajax() {

	this.toString = function() { return "Ajax"; }
  	this.http = new HTTP();

	this.makeRequest = function(_method, _url, _vars, _callbackMethod) {
		
		this.request = (window.XMLHttpRequest)? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP");
	    this.request.onreadystatechange = _callbackMethod;
    	
	    if (_method == 'GET') {
			var t = /\?/;
			if (t.test(_url)) {
				_url += '&'+_vars;
			} else {
				_url += '?'+_vars;
			}
	    }
	    this.request.open(_method, _url, true);
    	
	    if (_method == 'POST') {
	    	this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		    this.request.send(_vars);
	    } else if (_method == 'GET') {
		    this.request.send(null);
	    }
	}
	this.checkReadyState = function(_id, _1, _2, _3) {
		var res = this.request.readyState;
		switch(this.request.readyState) {
    		case 1:
        		defaultStatus = _1;
        		break;
      		case 2:
        		defaultStatus = _2;
        		break;
      		case 3:
        		defaultStatus = _3;
				break;
      		case 4:
      			defaultStatus = '';
        		res = this.http.status(this.request.status);
    	}
    	return res;
  	}
  	this.dataReflection = function(_delay, _callbackMethod) {
  		setTimeout(_callbackMethod, _delay);
	}
}
	
function HTTP() {
	this.toString = function() { return "HTTP"; }
	this.status = function(_status)   {
		switch(_status) {
			case 200:
				return "success";
				break;
			case 404:
				return "File not found.";
				break;
			default:
				return "HTTP Status: " + _status;
		}
	}
}



































// DEPRICATED DO NOT USE IT!

var __sTargetID = '';
var __sToEvalBefore = '';
var __sToEvalAfter = '';
var undefined = undefined;

function getContent(url, objID, postVars, toEvalBefore, toEvalAfter, objAjax) {

	if (objAjax.EvalBefore != null && objAjax.EvalBefore.length>0) {
		eval(objAjax.EvalBefore);
	}

	var ct = getControl('busy');
	if (ct != undefined) {
		showBusy(ct);
	}

	objAjax.Request = requestObject();
	objAjax.Request.onreadystatechange = onResponseContent;
	
	if(objAjax.PostVars != null && objAjax.PostVars.length > 0) {
		objAjax.Request.open("POST", url, true);
		objAjax.Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	} else {
		objAjax.Request.open("GET", url, true);
	}
	
	objAjax.Request.send(postVars);
	
}

function requestObject() {
	var req =false;

	if(window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		req = new ActiveXObject("MSXML2.XMLHTTP");
	}
	return req;
}

function checkReadyState(obj, alertID)
{
	var div = getControl(alertID);
	
	if(obj.readyState == 0) { defaultStatus = "Sending Request..."; }
	if(obj.readyState == 1) { defaultStatus = "Loading Response..."; }
	if(obj.readyState == 2) { defaultStatus = "Response Loaded..."; }
	if(obj.readyState == 3) { defaultStatus = "Response Ready..."; }
	if(obj.readyState == 4)
	{
		if(obj.status == 200)
		{
			defaultStatus = '';
			return true;
		}
		else if(obj.status == 404)
		{
			// Add a cu__sTom message or redirect the user to another page
			//document.getElementById('copy').innerHTML = "File not found";
			div.innerHTML = 'Requested page could not be found. ' + obj;
			alignPage();
		}
		else
		{
			//document.getElementById('copy').innerHTML = "There was a problem retrieving the XML.";
			div.innerHTML = 'There was a problem retrieving the page. ' + obj.url;
			alignPage();
		}
	}
}

function onResponseContent(x) 
{
	alert('@@'+x);
	var div = getControl(__sTargetID);
	
	if(checkReadyState(request, div))
	{
		div.innerHTML = request.responseText;

		alignPage();

		var ct = getControl('busy');
		if (ct != undefined) {
			hideBusy(ct);
		}

		if (__sToEvalAfter != null && __sToEvalAfter.length>0) {
			eval(__sToEvalAfter);
		}
	}
}


function submitFormAjax(url, objID, frm, toEvalBefore, toEvalAfter) {
	var q = formElementsToArray(frm);

	getContent(url, objID, q.join('&'), toEvalBefore, toEvalAfter);
}


function formElementsToArray(frm) {

	var nw = new Array();
	var len = frm.elements.length;
	var str='';
	for(i=0;i<len;i++) {
		nw[i] = frm.elements[i].name + '=' + frm.elements[i].value;
	}
	
	return nw;	

}

