var lib_simple_form_timer;
var req;
var not_save;
lib_simple_form_timer = setInterval("lib_simple_form_save_value();",20000);
function lib_simple_form_save_value() {
	var fields = document.getElementsByTagName("input")
	var pos = 0;
	var values = new Array(10);
	var ok=1;
	for(i in fields) {
		if(fields[i] == "[object HTMLInputElement]")
			if(fields[i].name.substring(0,15) == "lib_simple_form") {
				ok=1;
				for(j in lib_simple_form_not_save)
					if(lib_simple_form_not_save[j] == fields[i].name.substring(0,lib_simple_form_not_save[j].length))
						ok = 0;
			if(ok) {
				values[pos] = new Array(2);
				values[pos][0] = fields[i].name;
				values[pos][1] =  encode_ansi(fields[i].value);
				pos++;
			}
		}	
	}		
	
	var fields = document.getElementsByTagName("textarea")
	var ok=1;
	for(i in fields) {
		if(fields[i] == "[object HTMLTextAreaElement]")
			if(fields[i].name.substring(0,15) == "lib_simple_form") {
				ok=1
				for(j in lib_simple_form_not_save)
					if(lib_simple_form_not_save[j] == fields[i].name.substring(0,lib_simple_form_not_save[j].length))
						ok = 0;
				if(ok) {
					values[pos] = new Array(2);
					values[pos][0] = fields[i].name;
					values[pos][1] = encode_ansi(fields[i].value);
					pos++;
				}
			}	
			
	}
	quest = "ajax=true";
		
	if(pos) {
		for(i in values)
			quest += "&field["+i+"]="+values[i][0]+"&value["+i+"]="+values[i][1];
					

		req = getXMLHttpRequest();
		if(req) {
			req.onreadystatechange = lib_simple_form_save_value2;
			req.open("POST", "libraries/simple_form/simple_form_ajax.php",true);
			req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			req.setRequestHeader("Content-length", quest.length);
			req.setRequestHeader("Connection", "close"); 
			//alert(values[1][0]);
					
			req.send(quest);
		}
	}
}
function lib_simple_form_save_value2() {
	if(req.readyState == 4) {
		
		if(typeof(window.new_system_message) == "function")
			if(req.responseText == "OK") {
				new_system_message("success",message_success);
			} else if(req.responseText == "NO_CHANGE") { 
				//new_system_message("success","nichts geändert");
			} else {
				new_system_message("error",req.responseText);
			}
	}
				
				
}
function getXMLHttpRequest () {
	var httpReq = null;
	if(window.XMLHttpRequest) {
		httpReq = new XMLHttpRequest();
	} else if (typeof ActiveXObject != "undefined") {
		httpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return httpReq;
}

function encode_ansi (text1) {
	text2 = "";
	for(i=0;i<text1.length;i++) {
		if(text1.charCodeAt(i) > 9999) {
			text2 += text1.charCodeAt(i);
		} else if(text1.charCodeAt(i) > 999 && text1.charCodeAt(i) < 10000) {
			text2 += "0"+text1.charCodeAt(i);
		} else if(text1.charCodeAt(i) > 99 && text1.charCodeAt(i) < 1000) {
			text2 += "00"+text1.charCodeAt(i);
		} else if(text1.charCodeAt(i) < 100) {
			text2 += "000"+text1.charCodeAt(i);
		}
		
	}
	return text2;
}