/** class Sjaks
	- opdelen van content naar html en javascript
	- @params url (url pagina)
	- @params postval (POST string of form object)
	- @params callfunct (indien waarde 'id' dan moet args een html element ID zijn, hier kan ook een functie naam 
		gebruik eventueel externe functie javaEval(this.server.responseText); om in die functie de javascript uit te voeren)
	- @params string args (indien waarde 'id' bij callfunctie moet dit een html element zijn)
	- @params string varType
*/
function Sjaks(varType) {
	this.debug = true; /* debug alert */
	this.disableform = true; /* disable form elements on form post */
	this.callfunct = 'id';
	this.args = '';
	this.rtFunc = new Function();
	this.varType = varType;

	this.aState1 = new Function();
	this.aState2 = new Function();
	this.aState3 = new Function();
	this.aState4 = new Function();
};

Sjaks.prototype.doGet = function(url, rtFunc){
	if(rtFunc != null){
		this.rtFunc = rtFunc;
	}
	this.url = url;
	this.aState1(this.varType);
	this.init();
};

Sjaks.prototype.doPost = function(url, postval, rtFunc){
	if(rtFunc != null){
		this.rtFunc = rtFunc;
	}
	this.url = url;
	this.postval = postval;
	this.aState1(this.varType);
	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");
	};
	var self = this;
	this.server.onreadystatechange = function () {
		if (this.server.readyState == 2) { 
			this.aState2(this.varType);
		}
		if (this.server.readyState == 3) {
			this.aState3(this.varType);
		} 
		if (this.server.readyState == 4) { 
			this.aState4(this.varType);	
			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("Kan functie niet uitvoeren");
				};
			}; 
		};	
	}.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.abort = function(){
	if(this.server){
		this.aState4(this.varType);
		this.server.onreadystatechange = function(){};
		this.server.abort();
		this.server = null;
	}
};
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) {
	if(this.args != '' && this.args != null){
alert('duh');
		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("Toewijzen mislukt: " + ID);
			}
		} else {
			this.msg("Missing page Id: " + ID);
		}
	}else{
		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':''};
		this.rtFunc(output);
	}
};
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;
};

