/*
Ajax Object By Gareth Heyes for Ignition NBS (c) 2005
By Gareth Heyes (gareth@ignited.co.uk)
All rights reserved
*/

function Ajax() {
	var browser = navigator.appName;
	this.params = new Array;
	this.supported = 0;
	this.response = null;
	this.mode = "text";
	this.progress = "waiting";
	this.xmlCallBack = null;
	this.textCallBack = null;	
	this.isIE = 0;
	this.sendMode = null;
	this.additionalQueryString = 0;	
	
	if(browser == "Microsoft Internet Explorer") {
		if(window.ActiveXObject) {
			
			try {
			  this.http = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					this.http = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					this.http = false;
					this.supported = 0;						
				}
	 		}			
				
			this.supported = 1;
			this.isIE = 1;
		}
	} else {
		if(window.XMLHttpRequest) {
			this.http = new XMLHttpRequest();
			this.supported = 1;
		} 
	}
	
	if(this.supported == 0) {
		alert('Sorry you have an unsupported browser.');
	}
	
}
Ajax.prototype.setURL = function(url) {
	this.url = url;
}
Ajax.prototype.setMode = function(mode) {
	this.mode = mode;
}
Ajax.prototype.setParam = function(name, value) {
	this.params[name] = escape(value);
}
Ajax.prototype.post = function() {
	if(!this.supported) return false;
	this.sendMode = 'post';
	this.setParam("ts",new Date().getTime());	
	this.http.open('POST', this.url);
	this.http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	this.http.send(this.getParams());
	this.http.onreadystatechange = this.handleRequest;	
	this.reset();
}
Ajax.prototype.get = function() {
	if(!this.supported) return false;
	this.sendMode = 'get';	
	this.setParam("ts",new Date().getTime());	
	this.http.open('GET', this.url + this.getParams());
	this.http.send(null);	
	this.http.onreadystatechange = this.handleRequest;
	this.reset();	
}
Ajax.prototype.addQueryString = function(additionalQueryString) {
	this.additionalQueryString = additionalQueryString;
}
Ajax.prototype.getParams = function() {
var queryStr;	
	if(	this.sendMode = 'post') {
		var queryStr = '';
	} else {
		var queryStr = '?';
	}
	var pos = 0;
	for(var i in this.params) {
		if(typeof this.params[i] != 'string') {
		 continue;
		}
		if(pos > 0) queryStr += '&';
		queryStr += i + '=' + this.params[i];
		pos++;
	}
	if(this.additionalQueryString != 0) {
		queryStr += "&" + this.additionalQueryString;
	}
	return queryStr;
}
Ajax.prototype.handleRequest = function() {
	var readyState = 0;
	readyState = ajax.http.readyState;
	switch(readyState) {
			case 0://Uninitialised
				ajax.progress = "Uninitialised";
			break;
			case 1://Loading
				ajax.progress = "Loading...";			
			break;
			case 2://Loaded
				ajax.progress = "Loaded.";
			break;
			case 3://Interactive
				ajax.progress = "Preparing";			
			break;
			case 4://Finished
				ajax.progress = "Done";	
				if(ajax.http.status == 200) {
					if(ajax.mode == "text") {
						ajax.response = ajax.http.responseText;
						eval(ajax.textCallBack);
					} else {
						ajax.response = ajax.http.responseXML;
						eval(ajax.xmlCallBack);
					}
				} else {
					msg = 'Error connecting to XML:-\n';
					msg += 'Status text: ' + ajax.http.statusText + '\n';
					msg += 'Status: ' + ajax.http.status + '\n';	
					msg += 'URL: ' + ajax.url + '\n';
					//debug
					alert(msg);
				}
			break;
	}
}
Ajax.prototype.setXMLCallBack = function(xmlCallBack) {
	this.xmlCallBack = xmlCallBack;
}
Ajax.prototype.setTextCallBack = function(textCallBack) {
	this.textCallBack = textCallBack;
}
Ajax.prototype.isReady = function() {
	return this.ready;
}
Ajax.prototype.reset = function() {
	this.params = new Array;
	this.additionalQueryString = 0;
}
var ajax = new Ajax;

