/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function ajax() {
	this.headers		= new Array();
	this.options		= new String();
	this.async		= false;
	this.url		= '';
	this.method		= 'GET';
	this.format		= 'txt';
	this.callback		= '';
	this.data		= '';
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
ajax.prototype.execute = function() {

	if (this.method != 'GET' && this.method != 'POST') {
		return false;
	}

	if (this.callback != '' && !this.function_exists(this.callback)) {
		return false;
	}

	if (this.url == '') {
		return false;
	}

	var url = this.url
	if (this.method == 'GET') {
		if (this.data != '') {
			url += '?' + this.data;
		}
	}

	var XHR = this.createXHR();

	XHR.open(this.method, url, this.async);

	if (this.method == 'POST') {
		XHR.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	} else {
	        if (this.headers) {
	        	for (var key in this.headers) {
	        		XHR.setRequestHeader(key, this.headers[key]);
	        	}
	        }
	}

	if (this.async) {
        	var myXHR 	= XHR;
        	var myAjax 	= this;
        	XHR.onreadystatechange = function() {
			if (myXHR.readyState == 4 && myXHR.status == 200) {
				switch(myAjax.format) {
					case 'txt' :
						myAjax.response = myXHR.responseText;
					break;
					case 'xml' :
						myAjax.response = myXHR.responseXML;
					break;
					default :
						myAjax.response = myXHR.responseText;
					break;
				}

				if (myAjax.response) {
					if (myAjax.callback != '') {
						if (myAjax.arg) {
							eval(myAjax.callback + '(myAjax.response,' + myAjax.arg + ')');
						} else {
							eval(myAjax.callback + '(myAjax.response)');
						}
					}
				}
	                }
	        }

		if (this.method == 'POST') {
			XHR.send(this.data);
		} else {
			XHR.send(null);
		}
	} else {
		if (this.method == 'POST') {
			XHR.send(this.data);
		} else {
			XHR.send(null);
		}

		if (XHR.status == 200) {
			switch(this.format) {
				case 'txt' :
					this.response = XHR.responseText;
				break;
				case 'xml' :
					this.response = XHR.responseXML;
				break;
				default :
					this.response = XHR.responseText;
				break;
			}

			if (this.response) {
				if (this.callback != '') {
					if (this.arg) {
						eval(this.callback + '(this.response,' + this.arg + ')');
					} else {
						eval(this.callback + '(this.response)');
					}
				} else {
					return this.response;
				}
			}
		} else {
			return false;
		}
	}

	delete XHR;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
ajax.prototype.addHeader = function(name, value) {
	this.headers[name] = value;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
ajax.prototype.setParam = function(name, value) {
	switch(name) {
		case 'url' :
			this.url = value;
		break;
		case 'method' :
			this.method = value.toUpperCase();
		break;
		case 'async' :
			this.async = value;
		break;
		case 'callback' :
			this.callback = value;
		break;
		case 'format' :
			this.format = value;
		break;
		case 'arg' :
			this.arg = value;
		break;
	}
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
ajax.prototype.setData = function(name, value) {
	if (typeof name == 'string') {
		if (this.data != '') {
			this.data += '&';
		}
		this.data += name + '=' + escape(value);
	} else {
		if (typeof value == 'object') {
			for (key in value) {
				if (this.data != '') {
					this.data += '&';
				}
				this.data += key + '=' + escape(value[key]);
			}
		}
	}
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
ajax.prototype.getOption = function(name) {
	if (this.options != '') {
		var arr = this.options.split('&');
		if( !arr || (arr.constructor !== Array && arr.constructor !== Object) ) {
			return false;
		}
		return name in arr;
	}
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
ajax.prototype.function_exists = function(name) {
	if (typeof name == 'string') {
		return ((typeof window[name] == 'function') || typeof window[name] == 'object');
	} else{
		return (name instanceof Function);
	}
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
ajax.prototype.createXHR = function () {
	var xhr;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xhr;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/