/*
	this object is responsable for creating the ajax object and starting the XML HTTP request
*/


var Ajax = {
	//object properties
	xmlHttp: null,
	url: "",
	mehod: "GET",
	params: null,
	async: true,
	//creates ajax object
	createObject: function () {
		try {
			this.xmlHttp = new XMLHttpRequest();
		}
		catch (ie) {
			try {
				this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (anotherie) {
				try {
					this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (error) {
					this.xmlHttp = null;
				}
			}
		}
		finally {
			return this.xmlHttp;
		}
	},
	//get ajax object
	getAjax: function () {
		return this.createObject();
	},
	//send request to the server
	startRequest: function () {
		var xmlHttp = this.getAjax();
		xmlHttp.open(this.method, this.url, this.async);
		if (this.method === "GET") {
			xmlHttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			xmlHttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			xmlHttp.setRequestHeader("Content-Type", "text/xml");
			xmlHttp.setRequestHeader("Pragma", "no-cache");
			xmlHttp.setRequestHeader("Expires", "now");
		} else if (this.method === "POST") {
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", this.params.length);
			xmlHttp.setRequestHeader("Connection", "close");
		}
		xmlHttp.onreadystatechange = function () {
			Ajax.getResponse(xmlHttp);
		};
		xmlHttp.send(this.params);
	},
	//get server response
	getResponse: function (xmlHttp) {
		var status = this.getStatus;
		if (xmlHttp !== null) {
			if (xmlHttp.readyState === 4) {
				if (xmlHttp.status === 200) {
					Search.getDocument(xmlHttp);
				}
			} else {
				document.getElementById("results").innerHTML = "<p><strong>Loading...</strong></p>";
			}
		}
	},
	//create ajax request using the methods above
	createRequest: function (method, url, params) {
		this.method = method;
		this.url = url;
		this.params = params;
		this.startRequest();
	}
};