﻿ajax = {
	xmlhttpobj : function()
	{
		var http = false;
		//Use IE's ActiveX items to load the file.
		if(typeof ActiveXObject != 'undefined') {
			try {http = new ActiveXObject("Msxml2.XMLHTTP");}
			catch (e) {
				try {http = new ActiveXObject("Microsoft.XMLHTTP");}
				catch (E) {http = false;}
			}
		//If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
		} else if (window.XMLHttpRequest) {
			try {http = new XMLHttpRequest();}
			catch (e) {http = false;}
		}
		return http;
	},

	send : function(opt)
	{
		var http = this.xmlhttpobj();
		if(!http||!opt||!opt.url) return;
		//XML Format need this for some Mozilla Browsers
		if (http.overrideMimeType) http.overrideMimeType('text/xml');
	
		if(!opt.method) method = "GET";//Default method is GET
		else method = opt.method;
		method = method.toUpperCase();

		var parameters = null;
	
		if(method=="POST") {
			parameters = opt.data;
		}
		http.open(method, opt.url, true);

		http.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		if (!opt.cache)
			http.setRequestHeader("If-Modified-Since", "Thu, 01 Jan 1970 00:00:00 GMT");
		if(method=="POST") {
			http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http.setRequestHeader("Content-length", parameters.length);
			http.setRequestHeader("Connection", "close");
		}

		var ths = this;// Closure
		if(opt.handler) { //If a custom handler is defined, use it
			http.onreadystatechange = function() { opt.handler(http); };
		} else {
			http.onreadystatechange = function () {//Call a function when the state changes.
				if (http.readyState == 4) {//Ready State will be 4 when the document is loaded.
					if(http.status == 200) {
						var result = "";
						if(http.responseText) result = http.responseText;
						else if (http.responseXML) result = http.responseXML;
	
						//Give the data to the callback function.
						if(opt.success) opt.success(result);
					} else {
						if(opt.error) opt.error(http.status);
					}
				}
			}
		}
		http.send(parameters);
	}
}
