/* 
# Scriptname: SJAKS - An Ajax based connector script - Current release: 26 aug 07
# Sjaks is from scripts.titude.nl - 2007 (c) 
# For use leave above comment with the script - please comment changes
#
# This javascript is provided  "as is" and any express or implied warranties, 
# including, but not limited to, the implied warranties of merchantability 
# and fitness for a particular purpose are disclaimed. in no event shall the 
# writer be liable for any direct, indirect, incidental, special, exemplary, 
# or consequential damages (including, but not limited to, procurement of 
# substitute goods or services; loss of use, data, or profits; or business 
# interruption) however caused and on any theory of  liability, whether in 
# contract, strict liability, or tort (including negligence or otherwise) arising
# in any way out of the use of this javascript, even if advised of the possibility 
# of such damage.
*/
function Sjaks(url, postval, callfunct, args) {
	this.debug = true; /* debug alert */
	this.disableform = true; /* disable form elements on form post */
	this.url = url;
	this.postval = postval;
	this.callfunct = callfunct;
	this.args = args;
	this.init();
};
Sjaks.prototype.init = function() { 
	this.server = null;
	if (typeof this.postval == "object") {
		this.poststr = this.form();
	} else if (typeof this.postval != "string") {
		this.poststr = "";
	} else {
		this.poststr = this.postval;
		this.postval = "";
	};
	var argtypes = {'string':'', 'boolean':'', 'number':''};
	if (typeof this.args in argtypes) {
		this.callArgs = new Array(this.args);
	} else if (typeof this.args == "object") {
		this.callArgs = this.args; 
	} else {
		this.callArgs = new Array();
	};
	var XMLHTTPtype = new Array(
		'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0',
		'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP',
		'Microsoft.XMLHTTP');
	for (var i = 0; i < XMLHTTPtype.length; i++) {
		try {
			if (window.XMLHttpRequest) {
				this.server = new XMLHttpRequest(XMLHTTPtype[i]);
			} else if (window.ActiveXObject) {
				this.server = new ActiveXObject(XMLHTTPtype[i]);
			};
		} catch (e) {};	
		if (this.server != null) {
			break;
		};
	};
	if (this.server == null) {
		this.msg("XMLHTTP unsupported");
	};
	this.server.onreadystatechange = function () {
		if (this.server.readyState == 4) { 		
			if (this.server.status == 200 && this.server.responseText != null) {
				if (typeof this.postval == "object") {
					this.form(true);
				};
				if (typeof this.callfunct == "string") {
					try {
						callback = eval("this." + this.callfunct);
					} catch (e) {};
				} else {
					callback = (typeof this.callfunct == "function") ? this.callfunct : function () {};
				};
				try {
					callback.apply(this, this.callArgs);
				} catch(e) {
					this.msg("Cannot parse function");
				};
			}; 
		};	
	}.bind(this);
	var reqmeth = (this.poststr != "") ? "POST" : "GET";
	this.server.open(reqmeth, this.url, true);
	this.server.setRequestHeader("If-Modified-Since", "Mon, 1 Jan 2000 00:00:00 GMT"); 
	if (this.poststr != "") {
		this.server.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		this.server.setRequestHeader("Content-length", this.poststr.length); 
		this.server.setRequestHeader("Connection", "close");
		this.server.send(this.poststr);
	} else {
		this.server.send(null);
	};
};
Function.prototype.bind = function(object) { 
	var newbind = this; 
	return function() { 
		return newbind.apply(object); 
	};
};
Sjaks.prototype.msg = function(msg) { 
	if (this.debug === true) {
		alert(msg);
	};
	return false;
};
SjaksJsIDs = new Array();
Sjaks.prototype.js = function(ID) {
	if (typeof ID == "string") {
		for (var i in SjaksJsIDs) {
			if (i == ID) {
				try {
					document.getElementsByTagName('body')[0].removeChild(SjaksJsIDs[i]);
				} catch(e) {};
			};
		};
	};
	var input = this.server.responseText;
	var reg = /<script[^>]*>([\s\S]*?)<\/script>/gi;
	if (input.search(reg) != -1) {
		var reg1 = /<script?[^>]+>/gi;			
		var reg2 = /<\/script>/gi;				
		var reg3 = /<!--/;
		var reg4 = /\/\/-->/;
		jsparts = input.split(reg1);
		input = "";
		for (i =0; i < jsparts.length; i++) {
			if (jsparts[i].search(reg2) != -1) {
				jspart = jsparts[i].split(reg2);
				jspart[0] = jspart[0].replace(reg3, "");
				jspart[0] = jspart[0].replace(reg4, "");
				input += jspart[0];
			};
		};
	};
	try {
		DynaScript = document.createElement("script");
		if (typeof ID == "string") {
			SjaksJsIDs[ID] = DynaScript;
		};
		DynaScript.setAttribute("type", "text/javascript");
		DynaScript.text = "<!--\n" + input + "\n//-->";
		document.getElementsByTagName("body")[0].appendChild(DynaScript);
	} catch(e) {
		this.msg("Js error in Server response");
	};
};
Sjaks.prototype.id = function(ID) {
	var ObjID = null;
	if (typeof ID == "object") {
		var ObjID = ID;
	} else {
		if (document.getElementById && document.getElementById(ID)) {
			ObjID = document.getElementById(ID);
		} else if (document.all && document.all[''+ID+'']) {
			ObjID = document.all[''+ID+''];
		};
	};
	if (ObjID != null && typeof ObjID == "object") {
		var output = this.server.responseText;
		var reg = /<script[^>]*>([\s\S]*?)<\/script>/gi;
		if (output.search(reg) != -1) {
			this.js(ID);
		};
		output = output.replace(reg, "");
		var AsignVal = {'textarea':'', 'input':''};
		var AsignSrc = {'img':'', 'iframe':''};
		try {
			if (ObjID.tagName.toLowerCase() in AsignVal) {
				ObjID.value = output;
			} else if (ObjID.tagName.toLowerCase() in AsignSrc) {
				ObjID.src = output;
			} else {
				ObjID.innerHTML = output; 
			};
		} catch(e) {
			this.msg("Cannot assign to: " + ID);
		};
	} else {
		this.msg("Missing page Id: " + ID);
	};
};
Sjaks.prototype.form = function(free) {
	var poststr = "";
	if (typeof this.postval == "object") {
		try {
			for (i = 0; i < this.postval.elements.length; i++) {
				if (this.postval.elements[i].name != "" && free !== true) {
					var pName = this.postval.elements[i].name;
					var pVal = this.postval.elements[i].value;
					pVal = pVal.replace(/\+/g,"%2B");
					pVal = pVal.replace(/\=/g,"%3D");
					pVal = pVal.replace(/\&/g,"%26");
					if (this.postval.elements[i].type == "radio") {
						if (this.postval.elements[i].checked === true) {
							poststr += pName + "=" + pVal;
							poststr += (this.postval.elements.length - 1 ==  i) ? "" : "&";
						};
					} else {
						if (this.postval.elements[i].type == "checkbox") {
							if (this.postval.elements[i].checked !== true) {
								pVal = "";
							};
						};
						poststr += pName + "=" + pVal;
						poststr += (this.postval.elements.length - 1 ==  i) ? "" : "&"; 
					};
				};
				if (this.disableform === true) {
					this.postval.elements[i].disabled = (free === true) ? false : true;
				};				
			};
		} catch(e) {
			this.msg("Form error");
		};
	};
	return poststr;
};

