// Load the getForm function while page is loading
//window.onload = getForm; // execute on site
 
// This is the array for error handling
var vError =  new Array;

// We attach to every input field a little js
function getForm(formid) {

	// Important: Our form has to got the "vform" id
	var form = document.getElementById(formid);

	if (document.getElementsByTagName) {
		var vInput = document.getElementsByTagName("input");
		for (var vCount=0; vCount<vInput.length; vCount++) {
		
			// Put required ids in Error array
			getValue = vInput[vCount].className;
			vId = vInput[vCount].id;
			if(getValue.indexOf(",") != -1 ) {
				// don't put in error array, if already in, else put it in (because of ajax it might be already inside)
				if(vError.indexOf(vId) == -1) {
					vError.push(vId);
				}
			}
		
			vInput[vCount].onkeyup = function() { return validateIt(this); }
			//vInput[vCount].onmouseout = function() { return validateIt(this); }
			vInput[vCount].onkeydown = handleEnterKey;
		}
	}
}

// lock enter button, that the form has to be send by clicking on enabled submit button ;-)
function handleEnterKey(Ereignis) {
	if (!Ereignis)
		Ereignis = window.event;
	if (Ereignis.which == 13) {
		/*Ereignis.which = 9; // 9 = TAB */
		return false;
	} else if (Ereignis.keyCode == 13) {
		/*Ereignis.keyCode = 9; // 9 = TAB */
		return false;
	}
}
 
 
// The main validation-function
function validateIt(vInput) {

	// Each input field's id
	vId = vInput.id;
	vValue = vInput.value;
 
	// Separate the class attr of each input field
	getValue = vInput.className;
	if(getValue.indexOf(",") == -1 ) {
		vType = getValue;
		vRequired = "";
	} else {
		vRules = vInput.className.split(",");
		vRequired = vRules[0];
		vType = vRules[1];
	}
	
	// vButton = form's submit button
	var vButton = document.getElementById("submit");
	
	// Ajax function for validation php script request
	var ajax = {
		
		checkInput : function(strObj, strValue, strRequired, strType)
		{
	
			new Ajax.Request(
				"/ajax/check_input.html",
				{
		
					method: 'post',
					parameters: {strObj : strObj, strValue : strValue, strRequired: strRequired, strType : strType},
		
					onLoading:function(r)
					{
						if(strObj == "CheckNewsLetterEmail") {
							$(strObj).innerHTML = '<img align="absmiddle" src="/templates/standard/gfx/ajax/loading_mini_black.gif"/> Eingabe wird &uuml;berpr&uuml;t..';
						}
						else {
							$(strObj).innerHTML = '<img align="absmiddle" src="/templates/standard/gfx/ajax/loading_mini.gif"/> Eingabe wird &uuml;berpr&uuml;t..';
						}
					},
		
					onFailure:function(r)
					{
						if(strObj == "CheckNewsLetterEmail") {
							
						}
						else {
							$(strObj).innerHTML = '<img align="absmiddle" src="/templates/standard/gfx/ajax/error_black.gif"/> Fehler beim Pr&uuml;fen der Eingabe.';
						}
					},
		
					onSuccess:function(r)
					{	
				 
						if(r.responseText == "true") {
				 
							var sInput = document.getElementById(vId);
				 
							if(strObj == "CheckNewsLetterEmail") {
								$(strObj).innerHTML = '<img align="absmiddle" src="/templates/standard/gfx/ajax/available_black.gif"/>';
							}
							else {
								$(strObj).innerHTML = '<img align="absmiddle" src="/templates/standard/gfx/ajax/available.gif"/>';
							}
				 
							// We do a check if our element is in the error array, and if
							// so, we can delete it from the array
							
							if(vError.indexOf(vId) != -1) {
								var aId = vError.indexOf(vId);
								vError.splice(aId, 1);
							}
							if(vError.length > 0) {
								vButton.disabled = true;
							} else {
								vButton.disabled = false;
							}
							document.NewsLetterForm.submit.value='Eintragen';
						}
						
						// NewsLetter bereits vorhanden (Submit = austragen)
						else if(r.responseText == "falseNewsLetter") {
							
							var sInput = document.getElementById(vId);

							$(strObj).innerHTML = '<img align="absmiddle" src="/templates/standard/gfx/ajax/available_black.gif"/> Email bereits vorhanden.<input type="hidden" name="NewsLetterDelete" value="true">';
				 
							// We do a check if our element is in the error array, and if
							// so, we can delete it from the array
							
							if(vError.indexOf(vId) != -1) {
								var aId = vError.indexOf(vId);
								vError.splice(aId, 1);
							}
							if(vError.length > 0) {
								vButton.disabled = true;
							} else {
								vButton.disabled = false;
							}
							document.NewsLetterForm.submit.value='Austragen';
						}
						else {
				 
							var sInput = document.getElementById(vId);
				 
							if(strObj == "CheckNewsLetterEmail") {
								$(strObj).innerHTML = '<img align="absmiddle" src="/templates/standard/gfx/ajax/error_black.gif"/> ' + r.responseText;
							}
							else {
								$(strObj).innerHTML = '<img align="absmiddle" src="/templates/standard/gfx/ajax/error.gif"/> ' + r.responseText;
							}
							vButton.disabled = true;
							
							// don't put in error array, if already in, else put it in
							if(vError.indexOf(vId) == -1) {
								vError.push(vId);
							}
							document.NewsLetterForm.submit.value='Eintragen';
				 
						}
						
					/*	alert(vError); */
	/*			 
						if(r.responseText == "none") {
				 
							var sInput = document.getElementById(vId);
				 
							document[vId].src = "img/blank.gif";
							vButton.disabled = false;
				 
						}
		*/				
					}
				}
			);	
		}
	};
	
	// Execute ajax function above
	ajax.checkInput('Check' + vId,vValue, vRequired, vType);
}