window.onload = getForm;
// internet explorer workaround - Array.indexOf 
if(!Array.indexOf){
  Array.prototype.indexOf = function(obj){
   for(var i=0; i<this.length; i++){
    if(this[i]==obj){
     return i;
    }
   }
   return -1;
  }
}
//
function CheckInput() {
//  for (i = 0; i < document.forms[0].elements.length; ++i)
//    if (document.forms[0].elements[i].value == "") {
//      alert("Es wurden nicht alle Felder ausgefuellt!");
//      document.forms[0].elements[i].focus();
//      return false;
//    }
//  return true;
	var form = document.getElementById(formid);
	var sendError = false;
	if (document.getElementsByTagName) {
		var vInput = document.getElementsByTagName("input");
		for (var vCount=0; vCount<vInput.length; vCount++) {
			if(vInput[vCount].className.indexOf("required") != -1) {
				if(vInput[vCount].value==""){
					document[vInput[vCount].id].src = "images/false.png";
					vError.push(vInput[vCount].id);
					sendError = true;
				}
//				alert(vInput[vCount].id);
			}
		}
		var vSelect = document.getElementsByTagName("select");
		for (var vCount=0; vCount<vSelect.length; vCount++) {
			if(vSelect[vCount].className.indexOf("required") != -1) {
				if(vSelect[vCount].value==""){
					document[vSelect[vCount].id].src = "images/false.png";
					vError.push(vSelect[vCount].id);
					sendError = true;
				}
//				alert(vInput[vCount].id);
			}
		}

	}

	if(document.anmform.agb.checked == false) {
		document.forms[0].agb_img.src = "images/false.png";
		sendError = true;
	} else {
		document.forms[0].agb_img.src = "images/true.png";
	}
	if (sendError == false) {
		var vButton = document.getElementById("submit");
		vButton.style.background = "#a8161c";
		vButton.style.color = "#fff";
		vButton.style.border = "1px dotted #fff";
		vButton.style.cursor = "pointer";
		vButton.disabled = false;
		return true;
	} else {
		var vButton = document.getElementById("submit");
		vButton.disabled = true;
		vButton.style.background = "#ccc";
		vButton.style.color = "#999";
		vButton.style.border = "1px dotted #000";
		vButton.style.cursor = "auto";
		return false;
	}
}

var vUrl = "validate_form.php";
var formid = "anmform";
var vError =  [];

function getForm() {
	var form = document.getElementById(formid);
	if (document.getElementsByTagName) {
		var vInput = document.getElementsByTagName("input");
		for (var vCount=0; vCount<vInput.length; vCount++)
			vInput[vCount].onchange = function() { return validateIt(this); }
		var vSelect = document.getElementsByTagName("select");
		for (var vCount=0; vCount<vSelect.length; vCount++)
			vSelect[vCount].onchange = function() { return validateIt(this); }
	}
}
var xmlhttp;
xmlhttp = GetXmlHttpObject();
	if (xmlhttp==null) {
	  alert ("Your browser does not support AJAX!");
	}

function GetXmlHttpObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function validateIt(vInput) {

	vId = vInput.id;
	vValue = vInput.value;

	getValue = vInput.className;
	if(getValue.indexOf(",") == -1 ) {
		vType = getValue;
		vRequired = "";
	} else {
		vRules = vInput.className.split(",");
		vRequired = vRules[0];
		vType = vRules[1];
	}

	var url = vUrl + "?value=" + (encodeURIComponent(vValue)) + "&required=" + (encodeURIComponent(vRequired)) + "&type=" + (encodeURIComponent(vType));
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = handleHttpResponse;
	xmlhttp.send(null);
}

function handleHttpResponse() {
	if(xmlhttp.readyState == 4) {
		if(xmlhttp.responseText == "false") {
			var sInput = document.getElementById(vId);
			var vButton = document.getElementById("submit");
			document[vId].src = "images/false.png";
			sInput.style.border = "1px dotted #ff0000";
			sInput.style.color = "#ff0000";
//			sInput.style.background = "#f7cbc2";
			vButton.disabled = true;
			vButton.style.background = "#ccc";
			vButton.style.color = "#999";
			vButton.style.border = "1px dotted #000";
			vButton.style.cursor = "auto";
			if(vError.indexOf(vId) == -1) {
				vError.push(vId);
			}
		}
		if(xmlhttp.responseText == "true") {
			var sInput = document.getElementById(vId);
			document[vId].src = "images/true.png";
			sInput.style.border = "1px dotted #fff";
			sInput.style.color = "#fff";
//			sInput.style.background = "#c7f7be";
			if(vError.indexOf(vId) != -1) {
				var aId = vError.indexOf(vId);
				vError.splice(aId, 1);

				if(vError.length > 0) {
					var vButton = document.getElementById("submit");
					vButton.disabled = true;
				} else {
					var vButton = document.getElementById("submit");
					vButton.style.background = "#a8161c";
					vButton.style.color = "#fff";
					vButton.style.border = "1px dotted #fff";
					vButton.style.cursor = "pointer";
					vButton.disabled = false;
				}
			}
		}

		if(xmlhttp.responseText == "none") {
			var sInput = document.getElementById(vId);
			document[vId].src = "images/blank.gif";
			sInput.style.border = "1px dotted #fff";
			sInput.style.color = "#fff";
			sInput.style.background = "transparent";
			if(vError.indexOf(vId) != -1) {
				var aId = vError.indexOf(vId);
				vError.splice(aId, 1);
				if(vError.length > 0) {
					var vButton = document.getElementById("submit");
					vButton.disabled = true;
				} else {
					var vButton = document.getElementById("submit");
					vButton.style.background = "#a8161c";
					vButton.style.color = "#fff";
					vButton.style.border = "1px dotted #fff";
					vButton.style.cursor = "pointer";
					vButton.disabled = false;
				}
			}
		}
	}
}
