<!--
/***********************************************
* Form validation by Tom
***********************************************/
//	Description: 
//  	Variables for the validation are set 
//  	in the form field elements
//  Form setup:
//		<form name=".." onSubmit="return formCheck(this);">
//	Form field elements to set: 
// 		id=""    for mandatory fields
//		msg=""   error message to return
//		regex="" regular expression to validate
	


function formCheck(formobj){
	
		// dialog message
	var alertMsg = "Controleer de volgende velden:\n";  
		// Form field color
	var bgBad = "#FF8080";
	var bgGood = "#FFFFFF";
	var emailregex="^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
	
	var fieldnr = formobj.length;
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldnr; i++){
		
		var obj = formobj.elements[i];
		var sMsg=obj.getAttribute("msg"); 		// Get error message
		var sReg=obj.getAttribute("regex");		// Regular expression
		if (!(obj.id==null || obj.id=="" || obj.id.substr(0,10)=="mce_editor")){ //the mce_editor is exception fo rich text editor for
			switch(obj.type){
			case "select-one":					// Check Dropdowns
				if (obj.selectedIndex == 0){
					alertMsg += " - " + sMsg +"\n";
					setColor(obj, bgBad);
				} else {
					setColor(obj, bgGood);
				}
				break;
			case "select-multiple":				// Check multiple
				if (obj.selectedIndex == -1){
					alertMsg += " - " + sMsg + "\n";
				}
				break;
			case "password":
				if (obj.value=="" || obj.value==null || !(checkPasswords())){
					if (obj.value==""){
						alertMsg += " - " + sMsg + "\n";
					}else{
						if (obj.id=="password2" && !(checkPasswords())){
							alertMsg += " - Passwords are not the same \n";
						}
					}
					setColor(obj, bgBad);
				} else {
					setColor(obj, bgGood);
				}
				break;
			case "text":						// Check text fields
			case "textarea":
				if(!(sReg=="" || sReg==null)){ //if text field contains regEx
					var sRegEx=new RegExp(sReg);
					var chkReg=sRegEx.exec(obj.value);
						if (chkReg==null){
							alertMsg += " - " + sMsg + "\n";
							setColor(obj, bgBad);
						} else {
							setColor(obj, bgGood);
						}	
				} else {
					if (obj.id=='sptBankAccount') {
						if (check11(obj)) {
							setColor(obj, bgGood);
						}
						else {
							alertMsg += " - " + sMsg + "\n";
							setColor(obj, bgBad);
						}
					}
					else {
						if (obj.value=="" || obj.value==null){
							alertMsg += " - " + sMsg + "\n";
							setColor(obj, bgBad);
						} else {
							setColor(obj, bgGood);
						}
					}
				}
				break;
			default:
				if (obj.type == undefined){
					var blnchecked = false;
					for (var j = 0; j < obj.length; j++){
						if (obj[j].checked){
							blnchecked = true;
						}
					}
					if (!blnchecked){
						alertMsg += " - " + sMsg + "\n";
					}
				}		
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}

	// Set color of the form field
function setColor(oel,bg) {
	oel.style.backgroundColor=bg;
}

	// Check password fields
	// Note: from field ids should be;
	// password1 and password2
function checkPasswords() {
	if (document.getElementById){
		if (document.getElementById('password1').value==document.getElementById('password2').value){
			return true;
		} else {
			return false;
		}
	}	
}


function check11(oel){
var check=parseFloat(oel.value);
var i;
var sum;
var bReturn = true;

	if(bReturn=(check>0)) {										// At least some kind of number
		check=10000000000+check;								// Place leading zeroes
		check=check.toString();
		check=check.substr(1);
		if (check.substr(0,3)!='000'){							// Not a GIRO
			if(bReturn=(check.substr(0,2)!='00')){				// Starting with 00 is forbidden
				sum=0;											// Check the 11-sum
				for (var i=0; i<=9; i++){
					sum+=parseInt(check.substr(i,1))*(10-i)	;
				}
				bReturn = (parseInt(sum/11)*11==sum);
			}
		}
	}
	
	if (bReturn){
		oel.value=check;
	}
	return bReturn;	
}


// -->



