//////////////////////////////////////////////////
// Prüft die Richtigkeit der Eingaben in Formularen
// by Maciej Homziuk
///////////////////////////////////////////////////

// Naming of a field oenName of field
// Empty values: text and select-one "".
	function checkForm (iForm, paramNameSubmit) {
		for (x=0; x<document.forms[iForm].elements.length; x++) {
			blnObligatory = false;
			blnEmail = false;
			blnDate = false;
			blnNumeric = false;
			blnEmpty = false;
			blnAt = false;
			blnPoint = false;
			blnCheckNumeric = true;
			blnCheckDate = true;
			strMaxLength = "";
			strTextAreaName = "";

			fieldName = document.forms[iForm].elements[x].name;
			fieldType = document.forms[iForm].elements[x].type;

			//Checking if the field is obligatory.
			if (fieldName.charAt(0) == "o") {blnObligatory = true}
			//Checking if the field is a date field.
			if (fieldName.charAt(1) == "d") {blnDate = true}
			//Checking if the field is an email field.
			if (fieldName.charAt(1) == "e") {blnEmail = true}
			//Checking if the field is "only numeric values" field.
			if (fieldName.charAt(2) == "n") {blnNumeric = true}

			strRealName = fieldName.slice(3);
			if (strRealName == "Email") {
				strRealName = "E-Mail";
			} else if (strRealName == "Email2") {
				strRealName = "Bestätigung der E-Mail";
			} else if (strRealName == "Dienst") {
				strRealName = "Innen- / Außendienst";
			} else if (strRealName == "Vermittler Profil") {
				strRealName = "Vermittlerprofil";
			} else if (strRealName == "Passwort2") {
				strRealName = "Bestätigung des Passworts";
			} else if (strRealName == "VJBeschreibung") {
				strRealName = "Kurzbeschreibung zur Veröffentlichung im VersicherungsJournal";
			} else if (strRealName == "Wogehoert") {
				strRealName = "\"Wo gehört\"";
			}
			strValue = document.forms[iForm].elements[x].value;
			// Whole procedure for the obligatory field.
			if (blnObligatory) {
				//Checking if the obligatory field wans't filled in the form.
				if (fieldType == "text" || fieldType == "textarea" || fieldType == "password") {
					if (strValue == "") {
						blnEmpty = true;
						emptyAlert = "Bitte füllen Sie das Feld \""+strRealName+"\" aus!";
					}
				} else if (fieldType == "select-one") {
					if (strValue == "") {
						blnEmpty = true;
						emptyAlert = "Bitte treffen Sie im Feld \""+strRealName+"\" eine Wahl!";
					}
				} else if (fieldType == "checkbox") {
					if (document.forms[iForm].elements[x].checked == false) {
						blnEmpty = true;
						emptyAlert = "Bitte akzeptieren Sie unsere Teilnahmebedingungen. Sollten Sie mit diesen nicht einverstanden sein, setzen Sie sich bitte mit uns in Verbindung.";
					}
				} else if (fieldType == "radio") {
					blnRadioOK = false;
					intAnzahlElemente = document.getElementsByName(document.forms[iForm].elements[x].name).length;
					for (var i=0; i<intAnzahlElemente; i++) {
						if (document.getElementsByName(document.forms[iForm].elements[x].name)[i].checked) {
							blnRadioOK = true;
						}
					}
					if (!blnRadioOK) {
						blnEmpty = true;
						emptyAlert = "Bitte eine Option im Feld \""+strRealName+"\" auswählen !!!";
					}
				}
				
				if (blnEmpty == true) {
					alert (emptyAlert);
					document.forms[iForm].elements[x].focus();
					return false;
				}
			}

			// Whole procedure for the email field.
			if (blnEmail && (strValue != "")) {
				for (y = 0; y < document.forms[iForm].elements[x].value.length; y++) {
					if (document.forms[iForm].elements[x].value.charAt(y) == "@")
						{blnAt = true}
					if (document.forms[iForm].elements[x].value.charAt(y) == ".")
						{blnPoint = true}
				}
				if (! (blnAt && blnPoint)) {
					alert("Geben Sie bitte im Feld "+strRealName+" eine Emailadresse ein !");
					document.forms[iForm].elements[x].focus();
					return false;
				}
			}

			// Whole procedure for the date field.
			if (blnDate && (strValue != "")) {
				for (y = 0; y < document.forms[iForm].elements[x].value.length; y++) {
					if (document.forms[iForm].elements[x].value.length != 10) {
						blnCheckDate = false;
					}
					if (y == 4 || y == 7) {
						if (document.forms[iForm].elements[x].value.charAt(y) != "-") {
							blnCheckDate = false;
						}
					} else {
						if (document.forms[iForm].elements[x].value.charAt(y) < "0" || 
							document.forms[iForm].elements[x].value.charAt(y) > "9") {
							blnCheckDate = false;
						}
					}
					if (! blnCheckDate) {
						alert("Geben Sie bitte im Feld "+strRealName+" ein Datum im Format JJJJ-MM-TT ein !");
						document.forms[iForm].elements[x].focus();
						return false;
					}
				}
			}			
			
			// Whole procedure for numeric field.
			if (blnNumeric && (strValue != "")) {
				for (y = 0; y < document.forms[iForm].elements[x].value.length; y++) {
					if ((document.forms[iForm].elements[x].value.charAt(y) < "0" ||
						document.forms[iForm].elements[x].value.charAt(y) > "9"))
						{
							blnCheckNumeric = false;
						}
					if (! blnCheckNumeric) {
						alert("Geben Sie bitte im Feld "+strRealName+" einen numerischen Wert ein !");
						document.forms[iForm].elements[x].focus();
						return false;
					}
				}
			}
			
			//Checking if the text in textarea is not too long.
			if (fieldType == "textarea") {
				//Checking if this textarea has limited characters number allowed.
				for (y = 0; y < fieldName.length; y++) {
					if (fieldName.charAt(y) >= 0 || fieldName.charAt(y) <= 9) {
						strMaxLength = strMaxLength + fieldName.charAt(y);
					} else if (y > 2) {
						strTextAreaName = strTextAreaName + fieldName.charAt(y);
					}
				}
				//Checking if the value entered in the textarea is not too long.
				if (strMaxLength != "" && strMaxLength > 9) {
					if (document.forms[iForm].elements[x].value.length > strMaxLength) {
						document.forms[iForm].elements[x].focus();
						alert("Der Text im Feld "+strTextAreaName+" kann maximal aus "+strMaxLength+" Zeichen bestehen.");
						return false;
					}
				}
			}
			
			// Checking if the email adresses and passwords are in booths fields the same.
			if (strRealName == "Bestätigung der E-Mail") {
				strEmail1 = document.forms[iForm].oexEmail.value;
				strEmail2 = document.forms[iForm].oexEmail2.value;
		  		if (strEmail1 != strEmail2) {
					alert("Die Werte im Feld \"E-Mail\" und \"E-Mail-Bestaetigung\"\nstimmen nicht ueberein !!!");
					document.forms[iForm].oexEmail.focus();
					return false;
				}
			}

			if (strRealName == "Bestätigung des Passworts") {
				strEintrag1 = document.forms[iForm].oxxPasswort.value;
				strEintrag2 = document.forms[iForm].oxxPasswort2.value;
		  		if (strEintrag1 != strEintrag2) {
					alert("Die Werte im Feld \"Passwort\" und \"Passwort - Bestaetigung\"\nstimmen nicht ueberein !!!");
					document.forms[iForm].oxxPasswort.focus();
					return false;
				}
			}
		}
		
		// If everything is ok then submit the form.
		if (paramNameSubmit) {
			if (window.navigator.appName == "Microsoft Internet Explorer") {
				document.getElementById(paramNameSubmit).disabled = true;
			}
		}
		document.forms[iForm].submit();
		return true;
	}
	
////////////////////////////////////////////////////////////////////
//Prüft die Länge der Eingabe im Textarea.
//by Maciej Homziuk
////////////////////////////////////////////////////////////////////

	function limitTextarea(paramFeld,paramLimit)
	{
		if (paramFeld.value.length > paramLimit) {
			alert("Sie können maximal "+(paramLimit+1)+" Zeichen eingeben !");
			paramFeld.focus();
			strValue = paramFeld.value;
			strNewValue = strValue.slice(0,paramLimit);
			paramFeld.value = strNewValue;
			return false;
		}
	}
	
////////////////////////////////////////////////////////////////////
//Setzt den Zeiger auf das n-te (mögliche) Element im m-ten Formular
//by Maciej Homziuk
////////////////////////////////////////////////////////////////////

	function focusOn(n,m) {
		var blnGesetzt = false;
		while (!blnGesetzt && document.forms[m]) {
			if (document.forms[m].elements[n].type == "hidden" || document.forms[m].elements[n].value != "") {
				n++;
				if (n > document.forms[m].elements.length - 1) {
					return false;
				}
			} else {
				document.forms[m].elements[n].focus();
				blnGesetzt = true;
			}
		}
	} 
	
////////////////////////////////////////////////////////////////////
//Öffnet das Bestätigungsfenster, wenn bestätigt öffnet die URL
//by Maciej Homziuk
////////////////////////////////////////////////////////////////////
	
	function warnung(paramText,paramURL) {
		blnAction = confirm(paramText);
		if (blnAction == true) {
			window.location.href = paramURL;
		}
	}	

////////////////////////////////////////////////////////////////////
//Öffnet das popup Fenster mit und platziert es in der Mitte des
//Bildschirms
//by Maciej Homziuk
////////////////////////////////////////////////////////////////////

	function mypopup(strURL,intSzer,intWys,scroll,namewindow) {
		//Prüfen, ob popup Fenster blockiert sind.
		var objChild;                           // Window
		var reWork = new RegExp('object','gi');	// Regular expression
		try {
			objChild = window.open('','child','width=50,height=50,status=no,resizable=yes'); 
			objChild.close();
		}
		catch(e) { }
		if (reWork.test(String(objChild))) {
			windowPopup = open(strURL,namewindow,"width="+intSzer+",height="+intWys+",scrollbars="+scroll+",status=no,resizable=no,left="+(screen.availWidth/2-0.5*intSzer)+",top="+(screen.availHeight/2-0.5*intWys));
		} else {
			alert("Achtung!!! Sie haben in Ihrem Browser Popups deaktiviert.\nAktivieren Sie bitte Popups, um alle Inhalte zu sehen.");		
		}
	}