/*
	ajax class by cybernetix@gmail.com
*/

var W3CDOM = (document.getElementById)?true:false;

var ajaxConn = function(fontFamily, fontSize, fontWeight, fontStyle, fontColor) {
	if (!W3CDOM) alert("This function requires W3CDOM compatible browser!" );
	this.responseMessages = ["Please wait", "Initializing request", "Sending request", "Processing", "Ready"];
	this.fontFamily = fontFamily || "Verdana, Arial, Helvetica, sans-serif";
	this.fontSize   = (fontSize || 10) + "px";
	this.fontWeight = fontWeight || "normal";
	this.fontStyle  = fontStyle || "normal";
	this.fontColor  = fontColor || "#000000";
}

ajaxConn.prototype = {
	connect: function() {
		this.XHRObj = null;
		try {
			this.XHRObj = new XMLHttpRequest();
		}
		catch (ie) {
			var i, iex = [ "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP" ];
			for(i = 0; i < iex.length && !this.XHRObj; i++) {
				try {
					this.XHRObj = new ActiveXObject(iex[i]);
				}
				catch (failed) {
				}
			}
		}
		return this.XHRObj;
	},
	sendGET: function(url, idObjOrIdName, callBackFunc, javaScriptsExecute) {
		this.coreGetPost(url, null, idObjOrIdName, callBackFunc, javaScriptsExecute, "GET");
		if (this.connect()) {
			this.url += (this.url.indexOf("?")>-1?"&":"?") + "rn=" + (Math.round(Math.random()*100000));
			this.XHRObj.open("GET", this.url, true);
			var self = this;
			this.XHRObj.onreadystatechange = function() { self.readyState(); }
			this.XHRObj.send(null);
		}
		else {
			this.displayAlert("Error: Ajax connection failed!");
		}
	},
	sendPOST: function(formObjectOrFormName, url, idObjOrIdName, callBackFunc, javaScriptsExecute) {
		this.coreGetPost(url, formObjectOrFormName, idObjOrIdName, callBackFunc, javaScriptsExecute, "POST");
		if (this.connect()) {
			this.XHRObj.open("POST", this.url, true);
			var self = this;
			this.XHRObj.onreadystatechange = function() { self.readyState(); }
			this.XHRObj.setRequestHeader("Method", "POST " + url + " HTTP/1.1");			
			this.XHRObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.XHRObj.setRequestHeader("Content-length", this.queryString.length);
			this.XHRObj.setRequestHeader("Connection", "close");
			this.XHRObj.send(this.queryString);
		}
		else {
			this.displayAlert("Error: Ajax connection failed!");
		}
	},
	coreGetPost: function(url, formObjectOrFormName, idObjOrIdName, callBackFunc, javaScriptsExecute, method) {
		this.idObj = this.getObject(idObjOrIdName);
		this.displayMessage(this.responseMessages[0]);
		this.method = method;
		this.url = url;
		this.javaScriptsExecute = javaScriptsExecute || false;
		this.callBackFunc = callBackFunc || false;
		if (this.method == "POST") {
			this.formObjectOrFormName = formObjectOrFormName;
			this.queryString = this.form2QueryString();
		}
	},
	readyState: function() {
		this.displayMessage(this.responseMessages[this.XHRObj.readyState]);
		if (this.XHRObj.readyState == 4) {
			if (this.XHRObj.status == 0 || this.XHRObj.status == 200) {
				if (this.callBackFunc) {
					 this.callBackFunc(this.XHRObj.responseText)
				}
				else if (this.idObj) {
					this.idObj.innerHTML = this.XHRObj.responseText;
					if (this.javaScriptsExecute)
						this.executeJavaScripts();
				}
				else {
					return this.XHRObj.responseText;
				}
				for (prop in this)
					delete this[prop];
			}
			else {
				this.displayAlert("A broken link! Error: " + this.XHRObj.status);
			}
		}
	},
	displayMessage: function(message) {
		if (this.idObj)
			this.idObj.innerHTML = "<span style='font-family:"+this.fontFamily+";font-size:"+this.fontSize+";font-weight:"+this.fontWeight+";font-style:"+this.fontStyle+";color:"+this.fontColor+";'>::" + message.toUpperCase() + "::</span>";
	},
	displayAlert: function(message) {
		if (this.idObj)
			this.displayMessage("<strong>"+message+"</strong>");
		else
			alert(message);
	},
	getObject: function(IdOrObject) {
		if (typeof(IdOrObject) == "object")
			return IdOrObject;
		else
			return document.getElementById(IdOrObject);
	},
	executeJavaScripts: function() {
		var scripts = this.idObj.getElementsByTagName("script");
		for (var i=0; i<scripts.length; i++) {
			_script = scripts[i];
			if (_script.text)
				eval(_script.text);
			else if (_script.textContent)
				eval(_script.textContent);
			else if (_script.innerHTML)
				eval(_script.innerHTML);
		}
	},
	form2QueryString: function() {
		if (typeof(this.formObjectOrFormName) == "object")
			var theForm = this.formObjectOrFormName;
		else
			var theForm = document.forms[this.formObjectOrFormName];	
		var qstr = "", multiElementString = "", multiElementName = "", formLength = theForm.elements.length;
		for (i = 0; i < formLength; i++) {
			switch (theForm.elements[i].type) {
				case "text": case "hidden":	case "password": case "textarea": case "select-one":
					qstr += theForm.elements[i].name + "=" + encodeURI(theForm.elements[i].value) + "&"
					break;
				case "select-multiple":
					multiElementName = theForm.elements[i].name;
					multiElementString = "";
					for(var j = 0; j < theForm.elements[i].options.length; j++) {
						if(theForm.elements[i].options[j].selected)
							multiElementString += encodeURI(theForm.elements[i].options[j].value) + ",";
					}
					multiElementString = multiElementString.substr(0, multiElementString.length - 1);
					qstr += multiElementName + "=" + multiElementString + "&";
					break;
				case "radio":
					if (theForm.elements[i].checked) {
						qstr += theForm.elements[i].name + "=" + encodeURI(theForm.elements[i].value) + "&"
					}
					break;
				case "checkbox":
					multiElementName = theForm.elements[i].name;
					multiElementString = "";				
					while (theForm.elements[i].name == multiElementName) {
						if (theForm.elements[i].checked)
							multiElementString += encodeURI(theForm.elements[i].value) + ",";
						i++;
					} i--;
					multiElementString = multiElementString.substr(0, multiElementString.length - 1);
					qstr += multiElementName + "=" + multiElementString + "&";
					break;
			}
		}
		qstr = qstr.substr(0, qstr.length - 1);
	return qstr;
	}
}


