/* 
	PAVIT AJAX Framework (pAJAX) v1.2.1
	2006 © Pavel 'pasha' Vladimiroff
	http://www.pavit.ru
	Supported Browsers: IE 6.0+, Mozilla based, Opera
	NOT FOR COMERCICAL USE
*/

function pAJAX()
{

	/* AJAX functions */

	var xmlobj = null;

	// Set up variables
	this.frameworkDescription = 'PAVIT AJAX Framework version';
	this.frameworkVersion = '1.0.0';
	this.postMethod = 'GET';
	this.sendURL = null;
	this.postParams = null;
	this.execResponse = false;
	this.failed = false;

	// Response
	this.respStatus = new Array(2);

	// Functions
	this.onLoading = function() { };
	this.onLoaded = function() { };
	this.onInteractive = function() { };
	this.onComplete = function() { };
	this.onFail = function() { };
	this.onError = function() { };


	// Get version of framework
	this.getVersion = function() {
		return this.frameworkVersion;
	}


	// Get full version of framework
	this.getFullVersion = function() {
		return this.frameworkDescription + ' ' + this.frameworkVersion;
	}


	// Execure response
	this.runResponse = function() {
		try {
			eval(this.response); }
		catch (e) { }
	}


	// Init XMLHttpRequest
	this.initAJAX = function() {
		try {
			xmlobj = new ActiveXObject('Msxml2.XMLHTTP'); }
		catch (e) {
			try {
				xmlobj = new ActiveXObject('Microsoft.XMLHTTP'); }
			catch (ee) {
				if (!xmlobj && typeof XMLHttpRequest != 'undefined')
					xmlobj = new XMLHttpRequest();
				else
					this.failed = true;
			}
		}
	}


	// Send request
	this.sendRequest = function() {
		if (!xmlobj || this.failed)
			this.onFail();
		else {
			try {
				xmlobj.open(this.postMethod, this.sendURL,  true);
				xmlobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				xmlobj.send(this.postParams);
				var self = this;
				xmlobj.onreadystatechange = function() {
					switch (xmlobj.readyState) {
						case 0:
							self.onError();
							break;
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;
						case 3:
							self.onInteractive();
							break;
						case 4:
							self.response = xmlobj.responseText;
							self.responseXML = xmlobj.responseXML;
							self.respStatus[0] = xmlobj.status;
							self.respStatus[1] = xmlobj.statusText;

							if (self.execResponse)
								self.runResponse();

							if (self.respStatus[0] == '200')
								self.onComplete();
							else
								self.onError();
							break;
					}
				}
			}
			catch (e) {
				this.onError(); }
		}
	}

	/* Start service */
	this.initAJAX();
}
