/* for IE
 * expects class(or classpart)-name "Submit" (nextSubmit or backSubmit)
 * replaces Submit with SubmitFOCUS or the other way round!
 */
 /*
function submitFOCUS(item) {
	if (item.className) {
		var currentClass = item.className;
		if (currentClass.indexOf('SubmitFOCUS') == -1) {
			item.className = currentClass.replace('Submit', 'SubmitFOCUS');
		} else {
			item.className = currentClass.replace('SubmitFOCUS', 'Submit');
		}
	}
}
*/


function toggleHeaderInfo(id) {
	var e = document.getElementById(id); // element to toggle
	if (e.style.display == "none" || e.style.display == "") {
		e.style.display = "block";
	} else {
		e.style.display = "none";
	}
}


/*-----------------------------------------------------*\
 *  Client-Form-Validation
 *	
 *	Version: 1.5
 *	Author : sebastian.krause@e-7.com
\*-----------------------------------------------------*/

//Variable to enable or disable Client-Form-Validation
doValidation = true;

//Array to store all required fields with their respective validation RegExp
requiredFields = new Array();

//Array for error output
errorsAll = new Array();

//Define RegExp Constands
RegExpConst = new Array();
RegExpConst["__EMAIL__"]	= "/^[a-zA-Z0-9]([\\._-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\\.-]?[a-zA-Z0-9]+)*(\\.[a-zA-Z0-9]{2,})+$/";
RegExpConst["__ZIPCODE__"] 	= "/^([0-9][0-9][0-9][0-9][0-9])+$/";

//Define standard errormessages.
errorFormat 	= "Der Eintrag in Feld ##pif_cv_fieldlabel## hat ein falsches Format.";
errorLengthMin 	= "Der Eintrag in Feld ##pif_cv_fieldlabel## ist zu kurz.";
errorLengthMax 	= "Der Eintrag in Feld ##pif_cv_fieldlabel## ist zu lang.";
errorNumMin 	= "Der Wert in Feld ##pif_cv_fieldlabel## ist zu klein.";
errorNumMax 	= "Der Wert in Feld ##pif_cv_fieldlabel## ist zu groß.";
errorRequired 	= "Das Feld ##pif_cv_fieldlabel## ist ein Pflichtfeld.";

//Function to fill the required fields array
function addRequiredField(field,model,required,datatype,min_length,max_length,min_value,max_value,pif_regexp) {
	aRFi = requiredFields.length;
	requiredFields[aRFi] = new Array();
	
	requiredFields[aRFi].field 		= field;
	requiredFields[aRFi].model 		= model;
	requiredFields[aRFi].required 	= required;
	requiredFields[aRFi].datatype 	= datatype;
	requiredFields[aRFi].min_length = min_length;
	requiredFields[aRFi].max_length = max_length;
	requiredFields[aRFi].min_value 	= min_value;
	requiredFields[aRFi].max_value 	= max_value;
	
	if(datatype == "pif:email" && pif_regexp == "") pif_regexp = "__EMAIL__";
	if(datatype == "pif:zipcode" && pif_regexp == "") pif_regexp = "__ZIPCODE__";
	if(pif_regexp.indexOf("/") != -1) {
		regExpStart = pif_regexp.indexOf("/");
		regExpEnd 	= pif_regexp.lastIndexOf("/")+1;
		
		requiredFields[aRFi].pif_regexp = pif_regexp.substring(regExpStart,regExpEnd);
	} else {
		requiredFields[aRFi].pif_regexp = pif_regexp;
	}
}

//Helper function to determine the type of an input field.
function getInputType(thisForm,thisInput) {
	thisInputType = eval("thisForm.elements['" + thisInput + "'].type");
	if(thisInputType == undefined && eval("thisForm.elements['" + thisInput + "'][0]")) {
		thisInputType	= eval("thisForm.elements['" + thisInput + "'][0].type");
	}
	
	return thisInputType;
}

//Helper function to determine the label id of a given form field.
function getLabelId(thisForm,thisField,thisType) {
	if(thisType == "radio") {
		thisLabelId = "label_" + thisForm.elements[thisField][0].id;
	} else {
		thisLabelId = "label_" + thisForm.elements[thisField].id;
	}
	
	return thisLabelId;
}

//Function to check the given form
function checkRequiredFields(thisForm,thisFormModel) {
	while(errorsAll.length) errorsAll.pop(); //Array leeren

	cRFimax = requiredFields.length;

	for(cRFi=0;cRFi<cRFimax;cRFi++) {
		if(thisFormModel == requiredFields[cRFi].model && eval("thisForm.elements['" + requiredFields[cRFi].field + "']") != null) {
			currentField		= requiredFields[cRFi].field;
			currentValue		= eval("thisForm.elements['" + currentField + "'].value");
			currentType			= getInputType(thisForm,currentField);
			currentLabelId		= getLabelId(thisForm,currentField,currentType);
			currentName			= document.getElementById(currentLabelId).innerHTML;
			currentRequired		= requiredFields[cRFi].required;
			currentRegExp		= requiredFields[cRFi].pif_regexp;
			currentMinLength	= requiredFields[cRFi].min_length;
			currentMaxLength	= requiredFields[cRFi].max_length;
			currentMinValue		= requiredFields[cRFi].min_value;
			currentMaxValue		= requiredFields[cRFi].max_value;
			currentDataType		= requiredFields[cRFi].datatype;
			
			eAAr_i = errorsAll.length;
			
			if(currentType == 'select-one') { //This applys to type "select" (Not multiple)
				currentIndex	 = eval("thisForm.elements['" + currentField + "'].selectedIndex");
				if(currentIndex > -1) currentValue	 = eval("thisForm.elements['" + currentField + "'].options["+currentIndex+"].value");
			}
			
			if(currentType == 'select-multiple') { //This applys to type "select" (With multiple)
				currentValue = "";
			
				currentOptionLength = eval("thisForm.elements['" + currentField + "'].options.length");
				
				for(sMi=0;sMi<currentOptionLength;sMi++) { //Loop the options within the select to see if at least one valid option has been selected
					currentOptionSelected	= eval("thisForm.elements['" + currentField + "'].options["+sMi+"].selected");
					currentOptionValue		= eval("thisForm.elements['" + currentField + "'].options["+sMi+"].value");
					
					if(currentOptionSelected && currentOptionValue != '') { //Break loop if a valid option has been selected
						currentValue = currentOptionValue;
						break;
					}
				}
			}
			
			if(currentType == 'text' || currentType == 'textarea' || currentType == 'password') {
				if(currentValue == '' && currentRequired) {
					errorsAll[eAAr_i] 				= new Array();
					errorsAll[eAAr_i].field 		= currentField;
					errorsAll[eAAr_i].labelid 		= currentLabelId;
					errorsAll[eAAr_i].name 			= currentName;
					errorsAll[eAAr_i].errorType 	= 'Required';
				} else if(currentValue != '') {
					if(currentDataType == "xs:double" || currentDataType == "xs:integer") {
						if(currentDataType == "xs:double") currentValue = currentValue.replace(/,/,".");
						if(isNaN(currentValue) == false) {
							if(currentMinValue && currentValue < currentMinValue) {
								errorsAll[eAAr_i] 				= new Array();
								errorsAll[eAAr_i].field 		= currentField;
								errorsAll[eAAr_i].labelid 		= currentLabelId;
								errorsAll[eAAr_i].name 			= currentName;
								errorsAll[eAAr_i].errorType 	= 'NumMin';
							} else if(currentMaxValue && currentValue > currentMaxValue) {
								errorsAll[eAAr_i] 				= new Array();
								errorsAll[eAAr_i].field 		= currentField;
								errorsAll[eAAr_i].labelid 		= currentLabelId;
								errorsAll[eAAr_i].name 			= currentName;
								errorsAll[eAAr_i].errorType 	= 'NumMax';
							}
						} else {
							errorsAll[eAAr_i] 				= new Array();
							errorsAll[eAAr_i].field 		= currentField;
							errorsAll[eAAr_i].labelid 		= currentLabelId;
							errorsAll[eAAr_i].name 			= currentName;
							errorsAll[eAAr_i].errorType 	= 'Format';
						}
					} else if(currentMinLength && currentValue.length < currentMinLength) {
						errorsAll[eAAr_i] 				= new Array();
						errorsAll[eAAr_i].field 		= currentField;
						errorsAll[eAAr_i].labelid 		= currentLabelId;
						errorsAll[eAAr_i].name 			= currentName;
						errorsAll[eAAr_i].errorType 	= 'LengthMin';
					} else if(currentMaxLength && currentValue.length > currentMaxLength) {
						errorsAll[eAAr_i] 				= new Array();
						errorsAll[eAAr_i].field 		= currentField;
						errorsAll[eAAr_i].labelid 		= currentLabelId;
						errorsAll[eAAr_i].name 			= currentName;
						errorsAll[eAAr_i].errorType 	= 'LengthMax';
					} else if(currentRegExp) { //The field is not empty, all other test were positive and there is a RegExp to check for valid input
						if(RegExpConst[currentRegExp]) currentRegExp = RegExpConst[currentRegExp];
						eval("currentCheckTest = " + currentRegExp + ".test(currentValue);");

						if(!currentCheckTest) { //Error if the field doesn't validate.
							errorsAll[eAAr_i] 				= new Array();
							errorsAll[eAAr_i].field 		= currentField;
							errorsAll[eAAr_i].labelid 		= currentLabelId;
							errorsAll[eAAr_i].name 			= currentName;
							errorsAll[eAAr_i].errorType 	= 'Format';
						}
					}
				}
			} else if((currentType == 'select-one' || currentType == 'select-multiple') && currentValue == ''  && currentRequired) {
				errorsAll[eAAr_i] 				= new Array();
				errorsAll[eAAr_i].field 		= currentField;
				errorsAll[eAAr_i].labelid 		= currentLabelId;
				errorsAll[eAAr_i].name 			= currentName;
				errorsAll[eAAr_i].errorType 	= 'Required';
			} else if(currentType == 'checkbox' && currentRequired) { //This applys to checkboxes
				if(!eval("thisForm.elements['" + currentField + "'].checked")) { //Error if it isn't checked
					errorsAll[eAAr_i] 				= new Array();
					errorsAll[eAAr_i].field 		= currentField;
					errorsAll[eAAr_i].labelid 		= currentLabelId;
					errorsAll[eAAr_i].name 			= currentName;
					errorsAll[eAAr_i].errorType 	= 'Required';
				}
			} else if(currentType == "radio" && currentRequired) { //This might be some Radio-Buttons
				rBFound = 0;
				for(rBi=0;rBi<thisForm.length;rBi++) {
					if(thisForm.elements[rBi].name == currentField && thisForm.elements[rBi].checked) {
						rBFound = 1;
						break;
					} 
				}
				if(!rBFound) {
					errorsAll[eAAr_i] 				= new Array();
					errorsAll[eAAr_i].field 		= currentField;
					errorsAll[eAAr_i].labelid 		= currentLabelId;
					errorsAll[eAAr_i].name 			= currentName;
					errorsAll[eAAr_i].errorType 	= 'Required';
				}
			}
		}
	}
	
	//Flush erroroutput
	if(document.getElementById("erroroutput_" + thisForm.id)) {
		errordoc = document.getElementById("erroroutput_" + thisForm.id);
		errordoc.innerHTML = "";
		errordoc.style.display = "none";
	}
	//Reset field colors
	for(i=0;i<requiredFields.length;i++) {
		if(thisFormModel == requiredFields[i].model) {
			currentField = requiredFields[i].field;
			currentType	 = getInputType(thisForm,currentField);
			currentClass = document.getElementById(getLabelId(thisForm,currentField,currentType)).className;
			currentClass = currentClass.replace(/ errorlabel/, "");
			currentClass = currentClass.replace(/errorlabel/, "");
			document.getElementById(getLabelId(thisForm,currentField,currentType)).className = currentClass;
		}
	}
	
	if(errorsAll.length > 0 && doValidation) {
		//Display errors
		for(i=0;i<errorsAll.length;i++) {
			//Generate errortext for current field
			eval("output = error" + errorsAll[i].errorType + ".replace(/##pif_cv_fieldlabel##/,\"" + errorsAll[i].name.replace(/\*/, "") + "\")");
			
			//Add current errortext to error container
			if(document.getElementById("erroroutput_" + thisForm.id)) {
				errordoc = document.getElementById("erroroutput_" + thisForm.id);
				errordoc.innerHTML = errordoc.innerHTML + output + "<br/>";
				errordoc.style.display = "block";
			}
			
			//Set color for current field
			if(document.getElementById(errorsAll[i].labelid)) {
				errorcaption = document.getElementById(errorsAll[i].labelid);
				//errorcaption.style.color = "red";
				errorcaption.className = errorcaption.className += " errorlabel";
			}
		}
		
		//Enable Button
		//if(disabledButton != null) enableSubmit(disabledButton);
		
		//There were errors, so don't send the form
		return false;
	} else {
		//No errors occured, ready to send the form
		return true;
	}
}

/* disble submit buttons to prevent second submit */
var disabledButton = null;
function disableSubmit(button,formID) {
	disabledButton = button;
	button.disabled = 'disabled';
	//xxx = checkRequiredFields(document.getElementById(formID),thisFormModel) {
	//document.getElementById(formID).submit();
	return true;
	}
	
function enableSubmit(button) {
	disabledButton = button;
	button.disabled = null;
	}
