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();
			var nav = navigator.appName;
			if (nav != 'Microsoft Internet Explorer') {
				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(String.fromCharCode(10),"<br>");
	value = value.replace("\n","<br>");
	oudewaarde = value;
}
function doemaf(val){
	valnew = val.replace("'","_squote_");
	if(valnew.length < val.length){
		valnew = doemaf(val);
	} 
	return valnew;
}

function update(tabel,obj,websites_id,id,type) {
	if (oudewaarde != obj.value) {
		var querystring = "";
		querystring = querystring + "kolom=" + obj.name;
		querystring = querystring + "&type=" + type;
		querystring = querystring + "&id=" + id;
		querystring = querystring + "&websites_id=" + websites_id;
		querystring = querystring + "&tabel=" + tabel;
		
		var element = obj;
		var spl = (element.value).split('%0D%0A');
		var iny = spl.join("_br_");
		spl = (iny).split("\n");
		iny = spl.join("_br_");
		/*iny = iny.replace(String.fromCharCode(13);,"<br>");
		iny = iny.replace(String.fromCharCode(10),"<br>");*/
		//iny = iny.replace(String.fromCharCode(10),"_br_");
		
		//iny = iny.replace("\r","_br_");
		iny = iny.replace(/\%/ig,"_procent_");
		iny = iny.replace("'","_squote_");
		//iny = iny.replace("&","_amp_");
		iny = iny.replace(/&/ig,"_amp_");
		oudewaarde = oudewaarde.replace("&","_amp_");
		oudewaarde = oudewaarde.replace(/\<br\>/ig,"_br_");
		oudewaarde = oudewaarde.replace(/\%/ig,"_procent_");
		/*iny = iny.replace(/\</ig,"&lt;");
		iny = iny.replace(/\>/ig,"&gt;");*/
		
		querystring = querystring + "&waarde=" + ((iny).split(" ")).join("%20");
		querystring = querystring + "&oudewaarde=" + oudewaarde;
		querystring = doemaf(querystring);
		
		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');
			obj.parentNode.appendChild(img);
			//alert(querystring);
			SERVER.GETrequest('/ajax/update.asp', querystring, update_result);
		} else {
			setTimeout("update('" + tabel + "','" + obj + "','"  + klantid + "','" + id + "','" + type + "');",1000);
		}
	}
}
function update_result() {
	var data = SERVER.Receive();
	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://admin.elementalite.nl/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://admin.elementalite.nl/images/kruisje.png';
				oudewaarde = oudewaarde.replace("_squote_","''");
				setTimeout('document.getElementById("'+r+'").value = "' + oudewaarde + '"',500);
			}
			SERVER.loaded = false;
		}
	}
	return false;
}
//function doAjax
