// Scriptlibrary zur Pflichtfelder- und Dateinamen-\u00DCberpr\u00FCfung
var bCheckFields=true;
var bCheckFilename=true;
var strBGColorInvalid="#FFAA99";		// k\u00F6nnen mit konfigurierten Werten \u00FCberschrieben werden
var strBGColorValid="#FFFFFF";			// k\u00F6nnen mit konfigurierten Werten \u00FCberschrieben werden
var strFileMessage="";
//**********************************************
// Variablen initialisieren
//**********************************************
function initFieldchecker() {
	bCheckFields=true;
}
//**********************************************
// R\u00FCckgabewert
//**********************************************
function areFieldsValid() {
	return bCheckFields;
}
//**********************************************
// Feld demarkieren
//**********************************************
function resetField(strIdField) {
	document.getElementById(strIdField).style.backgroundColor=strBGColorValid;
}
//**********************************************
// Textfeld auf Inhalt pr\u00FCfen
//**********************************************
function checkText(strIdField) {
	// Ist Feld vorhanden?
	if (document.getElementById(strIdField)) {
		// Ist Feld gef\u00FCllt?
		if (document.getElementById(strIdField).value=="") {
			// Feld leer -> Meldung und einf\u00E4rben
			bCheckFields = false;
			document.getElementById(strIdField).style.backgroundColor=strBGColorInvalid;
		} else {
			// Feld gef\u00FCllt -> etwaige vorige Einf\u00E4rbung entfernen
			document.getElementById(strIdField).style.backgroundColor=strBGColorValid;
		}
	}
}
//**********************************************
// checkSelectBox auf Inhalt pr\u00FCfen
//**********************************************
function checkSelectBox(strIdField) {
	// Ist Feld vorhanden?
	if (document.getElementById(strIdField)) {
		// Ist Feld gef\u00FCllt?
		if (document.getElementById(strIdField).selectedIndex == -1) {
			// Feld leer -> Meldung und einf\u00E4rben
			bCheckFields = false;
			document.getElementById(strIdField).style.backgroundColor=strBGColorInvalid;
		} else {
			if (document.getElementById(strIdField).options[document.getElementById(strIdField).selectedIndex].text == "") {
				// Feld leer -> Meldung und einf\u00E4rben
				bCheckFields = false;
				document.getElementById(strIdField).style.backgroundColor=strBGColorInvalid;
			} else {
				// Feld gef\u00FCllt -> etwaige vorige Einf\u00E4rbung entfernen
				document.getElementById(strIdField).style.backgroundColor=strBGColorValid;
			}
		}
	}
}
//**********************************************
// Email auf Inhalt \u00FCberpr\u00FCfen
//**********************************************
function IsEmailValid(strEmail) {
	var myEMailIsValid = true;
	var myAtSymbolAt = strEmail.indexOf('@');
	var myLastDotAt = strEmail.lastIndexOf('.');
	var mySpaceAt = strEmail.indexOf(' ');
	var myLength = strEmail.length;
	//@Symbol fehlt
	if (myAtSymbolAt < 1 )  {myEMailIsValid = false}
	//Punkt vor @Symbol
	if (myLastDotAt < myAtSymbolAt)  {myEMailIsValid = false}
	//mehr als 3 Zeichen hinter @Symbol fehlt
	if (myLength - myLastDotAt <= 2) {myEMailIsValid = false}
	//Leerzeichen vorhanden
	if (mySpaceAt != -1)  {myEMailIsValid = false}
	return myEMailIsValid
}
function checkEMail(strIdField) {
	// Ist Feld vorhanden?
	if (document.getElementById(strIdField)) {
		// Ist Email g\u00FCltig?
		if (IsEmailValid(document.getElementById(strIdField).value)==false)  {
			//Email ung\u00FCltig -> Fehler und einf\u00E4rben
			bCheckFields=false;
			document.getElementById(strIdField).style.backgroundColor=strBGColorInvalid;
		} else {
			// Email g\u00FCltig -> etwaige vorige Einf\u00E4rbung entfernen
			document.getElementById(strIdField).style.backgroundColor=strBGColorValid;
		}
	}
}
//**********************************************
// Radio und Checkboxen auf Inhalt pr\u00FCfen
//**********************************************
function checkRadio(strNameField, strIdDiv) {
	var n;
	var elBox=document.getElementsByName(strNameField);
	//Unterscheiden zwischen mehrfachen und einfachen Auswahloptionen
	if (elBox.length > 0) {
		//Alle Auswahloptionen auf Selektion \u00FCberpr\u00FCfen
		for(n = 0; n < elBox.length; ++n) {
			if (elBox[n].checked == true) {
				//Eine Auswahl gefunden -> Feld gef\u00FCllt -> etwaige vorige Einf\u00E4rbung entfernen
				document.getElementById(strIdDiv).style.backgroundColor=strBGColorValid;
				return;
			}
		}
	} else {
		//Nur eine Auswahl
		if (varelBox.checked == true) {
			//Auswahl markiert -> Feld gef\u00FCllt -> etwaige vorige Einf\u00E4rbung entfernen
			document.getElementById(strIdDiv).style.backgroundColor=strBGColorValid;
			return;
		}
	}
	//Keine Selektion gefunden -> Feld leer -> Meldung und einf\u00E4rben
	bCheckFields=false;
	document.getElementById(strIdDiv).style.backgroundColor=strBGColorInvalid;
}

//**********************************************
// Dateinamen auf Inhalt \u00FCberpr\u00FCfen
//**********************************************
function validateFilename(strFilePathName) {
	var strFileName="";
	var bIsFileNameValid = true;
	for(var i = 0; i <= strFilePathName.length -1; i++) {
		strFileName = strFileName + strFilePathName.substr(i,1);
	 	// Bei Sonderzeichen R\u00FCckgabewert auf false setzen
 		if (!(	((strFilePathName.charCodeAt(i)>=65) && (strFilePathName.charCodeAt(i)<=90)) ||
				((strFilePathName.charCodeAt(i)>=97) && (strFilePathName.charCodeAt(i)<=122)) ||
				((strFilePathName.charCodeAt(i)>=48) && (strFilePathName.charCodeAt(i)<=57)) ||
				(strFilePathName.charCodeAt(i)==45) || (strFilePathName.charCodeAt(i)==46) ||
				(strFilePathName.charCodeAt(i)==95) ||
				(strFilePathName.charCodeAt(i)==32))) {
					 bIsFileNameValid = false;
		}
		// Wenn Slash oder Backslash gefunden wird wieder auf True setzen
		if ((strFilePathName.charCodeAt(i)==47) || (strFilePathName.charCodeAt(i)==92)) {
			strFileName = '';
			bIsFileNameValid = true;
	 	 }
	}
	return bIsFileNameValid
}
function checkFilename(strIdField, strIdUpload) {
	if (document.getElementById(strIdUpload)) {
		document.getElementById(strIdUpload).style.backgroundColor=strBGColorValid;
		if (document.getElementById(strIdUpload).value!="") {
			if (!validateFilename(document.getElementById(strIdUpload).value)) {
					bCheckFields=false;
					document.getElementById(strIdUpload).style.backgroundColor=strBGColorInvalid;
			}
			document.getElementById(strIdField).value = document.getElementById(strIdUpload).value;
		}
	}
}
function checkFile(strIdField, strIDUpload) {
	if (document.getElementById(strIdField).value=="") {
			bCheckFields=false;
			document.getElementById(strIDUpload).style.backgroundColor=strBGColorInvalid;
	}
}

function buttonMouseover(id) {
	if(id) {
		if(document.getElementById(id)) {
			document.getElementById(id).style.backgroundColor = '#DDDDDD';
		}
	}
}
function buttonMouseout(id) {
	if(id) {
		if(document.getElementById(id)) {
			document.getElementById(id).style.backgroundColor= 'White';
		}
	}
}

function openPopup(strURL, iWidth, iHeight) { 
	if (winPop) {
		winPop.close();
	}
	var iLeft = (screen.width - iWidth) / 2;
	var iTop = (screen.height - iHeight) / 2;
	var winPop = window.open(strURL, "WIN", "directories=no,location=no,menubar=no,resizable=no,status=no,scrollbars=yes,toolbar=no,width=" + iWidth + ",height=" + iHeight + ",left=" + iLeft + ",top="+(iTop -15 ));	
	winPop.focus();
}
