var good = true;

function altercontent(myText, col){
	// call your function to remove all the children from your element
	so_clearInnerHTML(document.getElementById(col + "_error"));
	
	//string converteren naar DOM object	
	newstring = stringToDOM(myText);
	document.getElementById(col + "_error").appendChild(newstring);	

}

function show_message(myText, col){
	// call your function to remove all the children from your element
	so_clearInnerHTML(document.getElementById(col));
	
	//string converteren naar DOM object	
	newstring = stringToDOM(myText);
	document.getElementById(col).appendChild(newstring);	

}

function so_clearInnerHTML(obj) {
	// so long as obj has children, remove them
	while(obj.firstChild) obj.removeChild(obj.firstChild);
}

function validate(modulename, classname, col, val, language, allcheck) {		
	
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	  xmlhttp = new XMLHttpRequest();
	}
	
	suffix = col.substring(col.length - 2, col.length);
	
  if ((suffix == "_d")||(suffix == "_m")||(suffix == "_y")) {		
		col = col.substring(0, col.length-2);	

		if ((document.getElementById(col + "_d").value != "default")&&(document.getElementById(col + "_m").value != "default")&&(document.getElementById(col + "_y").value != "default")){
			val = document.getElementById(col + "_d").value + document.getElementById(col + "_m").value + document.getElementById(col + "_y").value;
		}else {
			val = "";
		}
  }
  
	val = escape(val);
	
	xmlhttp.open("GET", basepath + "modules/validate/validate.php?col=" + col + "&class=" + classname + "&val=" + val + "&lang=" + language + "&module=" + modulename, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			xmlhttp_ready(xmlhttp, val, col, false);
		}
	}
	xmlhttp.send(null);
}

function check_repeat(modulename, classname, col, language, allcheck) {		
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	  xmlhttp = new XMLHttpRequest();
	}
	
	//get original id from repeat fields (i.e. password_repeat, email_repeat..)	
	var re = new RegExp("\b_repeat\b");
  if (!col.match(re)) {
  	original_id = new String(col);
	original_id = original_id.replace("_repeat","");
  }

	val1 = escape(document.getElementById(original_id).value);
	val2 = escape(document.getElementById(col).value);
	xmlhttp.open("GET", basepath + "modules/validate/validate.php?col=" + col + "&class=" + classname + "&val=" + val1 + "&lang=" + language + "&val2=" + val2 + "&check_repeat=true" + "&module=" + modulename, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			xmlhttp_ready(xmlhttp, val1, col, false);
		}
	}
	xmlhttp.send(null);
}
function submit_form(n, formname, language) {
	var exception;
	exception = "";
	
	val = escape(document.getElementById(columns[n]).value);
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	  xmlhttp = new XMLHttpRequest();
	}
	
	suffix = columns[n].substring(columns[n].length - 2, columns[n].length);
	
  if ((suffix == "_d")||(suffix == "_m")||(suffix == "_y")) {		
		col = columns[n].replace (suffix, "");	

		if ((document.getElementById(col + "_d").value != "default")&&(document.getElementById(col + "_m").value != "default")&&(document.getElementById(col + "_y").value != "default")){
			val = document.getElementById(col + "_d").value + document.getElementById(col + "_m").value + document.getElementById(col + "_y").value;
		}else {
			val = "";
		}
  }else {
  	col = columns[n];
	}

	val = escape(val);

	//exceptions for editing passwords (unrecoverable due to encryption)
	for(z=0;z<exceptions.length;z++){
		if (exceptions[z] == col){
			exception = exceptions[z]
		}
	}

	xmlhttp.open("GET", basepath + "modules/validate/validate.php?col=" + col + "&class=" + classes[n] + "&val=" + val + "&lang=" + language + "&exception=" + exception, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			val = escape(document.getElementById(col).value);
			xmlhttp_ready(xmlhttp, val, col, true);
			if (n == (columns.length-1)) {
				if (good == true) {
					document.getElementById(formname).submit();
				} else {
					//alert('fout');	
				}
			} else {
				submit_form((n+1), formname, language);
			}
		}
	}
	xmlhttp.send(null);
}	  
function xmlhttp_ready(xmlhttp, val, col, allcheck) {
	if (val != ""){
		if (xmlhttp.responseText != "") {
			for (n=0; n<columns.length; n++) {
				if (columns[n] == col) {
					check[n] = false;
					message[n] = xmlhttp.responseText;
				}
			} 				
   		document.getElementById(col).style.border = '1px solid red';
   		good = false;
  	} else { 				
			for (n=0; n<columns.length; n++) {
				if (columns[n] == col) {
					check[n] = true;	
				}
			} 	
  		document.getElementById(col).style.border = '1px solid #6C8896';
  	}
  	altercontent(xmlhttp.responseText, col);
	} else {
		if (xmlhttp.responseText != "" && allcheck == true) { //required field empty	  		
			for (n=0; n<columns.length; n++) {
				if (columns[n] == col) {
					altercontent(xmlhttp.responseText, col);
					check[n] = false;
					message[n] = xmlhttp.responseText;
				}
			} 	
			document.getElementById(col).style.border = '1px solid red';
			
			good = false;
	  	}	else {
	  		altercontent("", col);
	  		document.getElementById(col).style.border = '1px solid #6C8896';
	  	}
	}
}