var SERVER = {
	loaded : false,
	requestobj : false,
	load : function() {
		if( SERVER.loaded == false ) {
			this.loaded = SERVER.loadrequestobj(); //probeer te laden
		}
		return this.loaded;
	},
	
	loadrequestobj : function () {
		if (window.XMLHttpRequest){ // if Mozilla, Safari etc or IE7
			this.requestobj = new XMLHttpRequest();
			if (this.requestobj.overrideMimeType) this.requestobj.overrideMimeType('text/xml');
			return true;
		} else if (window.ActiveXObject){ // if IE, not IE7
			try {
				this.requestobj = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					this.requestobj = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
					return false;
				}
			}
			return true;
		}
		return false;
	},
	
	GETrequest : function(url,data,func) {
		if( !this.load() ) return false;
		this.requestobj.open('GET', url+'?'+data, true);
		if( func  ) this.requestobj.onreadystatechange = func;
		this.requestobj.send(null);
		var nav = navigator.appName;
		if (nav != 'Microsoft Internet Explorer') {
			this.requestobj.overrideMimeType('text/html');
		}
		return true;
	},
	
	POSTrequest : function(url,data,func) {
		if( !this.load() ) return false;
		this.requestobj.open('POST', url, true);
		if( func ) this.requestobj.onreadystatechange = func;
		var nav = navigator.appName;
		if (nav != 'Microsoft Internet Explorer') {
			this.requestobj.overrideMimeType('text/html');
		}
		this.requestobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.requestobj.setRequestHeader("Content-length", data.length);
		this.requestobj.setRequestHeader("Connection", "close");
		this.requestobj.send(data);
		return true;
	},
	
	Receive : function() {
		if( this.requestobj.readyState != 4 ) {
			return false;
		} else if (this.requestobj.status != 200) {
			this.loaded = false;
			return 'Fout: '+this.requestobj.statusText;
		} else {
			return this.requestobj.responseText;
			this.loaded = false;
		}
	}
}

oudewaarde = "";
function update_onfocus(value) {
	value = value.replace("'","_squote_");
	value = value.replace("&","_amp_");
	oudewaarde = value;
}

function update_always(tabel,obj,gebruikers_id,id,type) {
	
	var iny = obj.value;
	for (i=0;i<50;i++) {
		iny = iny.replace("'","_squote_");
		oudewaarde = oudewaarde.replace("'","_squote_");
	}
	var element = obj;
	var spl = (element.value).split('%0D%0A');
	iny = spl.join("<br>");
	spl = (iny).split('\n');
	iny = spl.join("<br>");
	
	for (i=0;i<50;i++) {
		iny = iny.replace("'","_squote_");
		iny  = iny.replace("&","_amp_");
		iny  = iny.replace("%","_pcent_");
		iny = iny.replace("<br>","_br_");
		iny = iny.replace("#","_hekje_");
	}
	
	var querystring = "";
	querystring = querystring + "kolom=" + obj.name;
	querystring = querystring + "&type=" + type;
	querystring = querystring + "&id=" + id;
	querystring = querystring + "&gebruikers_id=" + gebruikers_id;
	querystring = querystring + "&tabel=" + tabel;
	
	querystring = querystring + "&waarde=" + ((iny).split(" ")).join("%20");
	//alert(querystring);
	//document.getElementById('content').innerHTML += querystring + '<br />';
	
	if (SERVER.loaded == false) {
		var titel = 'img_' + obj.name;
		if (document.getElementById(titel)) {
			clearTimeout(window[titel]);
			document.getElementById(titel).parentNode.removeChild(document.getElementById(titel));
		}
		var img = document.createElement('IMG');
		img.setAttribute('src', '/images/loadingAnimation.gif');
		img.setAttribute('alt', '');
		img.setAttribute('id', titel);	
		img.setAttribute('height', '25');
		img.setAttribute('width', '25');
		img.setAttribute('class', 'admin');
		obj.parentNode.appendChild(img);
		SERVER.GETrequest('/ajax/update.asp', querystring, update_result);
	} else {
		setTimeout("update('" + tabel + "','" + obj + "','"  + klantid + "','" + id + "','" + type + "');",1000);
	}
}

function update(tabel,obj,gebruikers_id,id,type) {
	
	var iny = obj.value;
	for (i=0;i<50;i++) {
		iny = iny.replace("'","_squote_");
		oudewaarde = oudewaarde.replace("'","_squote_");
	}
		
	if (oudewaarde != iny) {
		var element = obj;
		var spl = (element.value).split('%0D%0A');
		iny = spl.join("<br>");
		spl = (iny).split('\n');
		iny = spl.join("<br>");
		
		for (i=0;i<50;i++) {
			iny = iny.replace("'","_squote_");
			iny  = iny.replace("&","_amp_");
			iny  = iny.replace("%","_pcent_");
			iny = iny.replace("<br>","_br_");
			iny = iny.replace("#","_hekje_");
			oudewaarde = oudewaarde.replace("'","_squote_");
			oudewaarde = oudewaarde.replace("%","_pcent_");
			oudewaarde = oudewaarde.replace("&","_amp_");
			oudewaarde = oudewaarde.replace("<br>","_br_");
			oudewaarde = oudewaarde.replace("#","_hekje_");
		}
		
		var querystring = "";
		querystring = querystring + "kolom=" + obj.name;
		querystring = querystring + "&type=" + type;
		querystring = querystring + "&id=" + id;
		querystring = querystring + "&gebruikers_id=" + gebruikers_id;
		querystring = querystring + "&tabel=" + tabel;
		
		querystring = querystring + "&waarde=" + ((iny).split(" ")).join("%20");
		querystring = querystring + "&oudewaarde=" + oudewaarde;
		//alert(querystring);
		//document.getElementById('content').innerHTML += querystring + '<br />';
		
		if (SERVER.loaded == false) {
			var titel = 'img_' + obj.name;
			if (document.getElementById(titel)) {
				clearTimeout(window[titel]);
				document.getElementById(titel).parentNode.removeChild(document.getElementById(titel));
			}
			var img = document.createElement('IMG');
			img.setAttribute('src', '/images/loadingAnimation.gif');
			img.setAttribute('alt', '');
			img.setAttribute('id', titel);	
			img.setAttribute('height', '25');
			img.setAttribute('width', '25');
			img.setAttribute('class', 'admin');
			obj.parentNode.appendChild(img);
			SERVER.GETrequest('/ajax/update.asp', querystring, update_result);
		} else {
			setTimeout("update('" + tabel + "','" + obj + "','"  + klantid + "','" + id + "','" + type + "');",1000);
		}
	}
}
function update_result() {
	var data = SERVER.Receive();
	//alert(data);
	if(data) { 
		if (data.substr(0,2) == 'ja') {
			var r = data.substr(2,data.length);
			var titel = "img_" + r;
			if (document.getElementById(titel)) {
				document.getElementById(titel).src = 'http://news.elementa.com/images/vinkje.png';
				
				clearTimeout(window[titel]);
				window[titel] = setTimeout("document.getElementById('" + titel + "').parentNode.removeChild(document.getElementById('" + titel + "'))",3000);
			}
			 
			SERVER.loaded = false;
		} else {
			//document.getElementById('polldivke').innerHTML = data;
			var r = data.substr(3,data.length);
			var titel = "img_" + r;
			if (document.getElementById(titel)) {
				document.getElementById(titel).src = 'http://news.elementa.com/images/kruisje.png';
				oudewaarde = oudewaarde.replace("_squote_","''");
				setTimeout('document.getElementById("'+r+'").value = "' + oudewaarde + '"',500);
			}
			SERVER.loaded = false;
		}
	}
	return false;
}

function GetXmlHttpObject() {
		var xmlHttp3=null;
		try { // Firefox, Opera 8.0+, Safari
					xmlHttp3=new XMLHttpRequest();
		} catch (e) { // Internet Explorer
					try {
								xmlHttp3=new ActiveXObject("Msxml2.XMLHTTP");
					} catch (e) {
								xmlHttp3=new ActiveXObject("Microsoft.XMLHTTP");
					}
		}
		return xmlHttp3;
	}
	function AJAXterug() {
		if (xmlHttp.readyState==4) {
			//alert(xmlHttp.responseText);
		}
	}

function LoadPage(page,el) {
		 // Set up request varible
		 xmlhttp2 = GetXmlHttpObject();
		 //Show page is loading
		 el.innerHTML = 'Loading Page...';
		 //scroll to top
		 //scroll(0,0);
		 //send data
		 xmlhttp2.onreadystatechange = function(){
				 //Check page is completed and there were no problems.
				 if ((xmlhttp2.readyState == 4) && (xmlhttp2.status == 200)) {
						//Write data returned to page
						el.innerHTML = xmlhttp2.responseText;
				 }
		 }
		 xmlhttp2.open("GET", page);
		 xmlhttp2.send(null);
		 //Stop any link loading normaly
		 return false;
	}


