//***********************************************************************************************
// Ajax class for handling AJAX requests and posts
// 		example invokation: 

///	var ajax1 = new Vormo.Ajax();
//	ajax1.URL = "someurl.xml";
//	ajax1.ExecuteAfter = function() {
//		Code to execute when the repsonse comes back
//		alert('Hijacked the function call after');
//	}
//	async1.GetXML();
//***********************************************************************************************
Vormo.Ajax = function () {
	Vormo.Ajax.count++;
	this.ID = Vormo.Ajax.count;
	this.http_request = false;
	this.RequestStatusCode = -1;
	this.RequestComplete = false;
	this.URL = "";
	this.PostData = "";
	this.ResponseText = "";
	this.ResponseXML;
}
Vormo.Ajax.count = 0;

Vormo.Ajax.prototype.GetXML = function (method) {
    var tmpMethod = 'get'
    if (typeof(method)=='string') {
        if (method=='post') tmpMethod = 'post';
    }
    method = tmpMethod;
    
	this.http_request = false;
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		this.http_request = new XMLHttpRequest();
		if (this.http_request.overrideMimeType) {
			this.http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			this.http_requestuest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!this.http_request) {
		alert('Cannot create an XMLHTTP instance');
		return false;
	}
	
	//Define the function that gets called when the readystate changes right
	//here so it's internal to the object
	var asynxml = this;
	this.http_request.onreadystatechange = function () {
    	if (asynxml.http_request.readyState == 4) {
			if (asynxml.http_request.status == 200 | asynxml.http_request.status == 304) {
			    //if (asynxml.http_request.responseXML.childNodes.length < 1) {
				//	alert('Could not retrieve XML');
				//	return false;
				//}
				asynxml.RequestStatus = asynxml.http_request.readyState;
				asynxml.ResponseText = asynxml.http_request.responseText;
				asynxml.ResponseXML = asynxml.http_request.responseXML;
				asynxml.RequestComplete = true;
				
    			asynxml.ExecuteAfter.call();
			} else {
				asynxml.RequestStatus = asynxml.http_request.readyState;
				//alert('There was a problem with the request.');
			}
		}
	}
	if (this.URL=='' | this.URL==null) {
		alert('Error: invalid url');
		return;
	}
	if (method=='post') {
	    this.http_request.open('POST', this.URL, true);
    	this.http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	this.http_request.send(this.PostData);
	} else {
	    this.http_request.open('GET', this.URL, true);
    	this.http_request.send(null);
	}
}

Vormo.Ajax.prototype.ExecuteAfter = function () {
	alert('XML request complete');
}

//***********************************************************************************************
