// form check system
// =======================
// (c)2003 by Provident BV

function FormChecker(msg, standardBackground, erroredBackground) {

	this.elements = new Array();
	this.elementC = 0;
	this.errormessage = msg;
	this.standardBackground = standardBackground;
	this.erroredBackground = erroredBackground;
	this.addGuard = formChecker_addGuard;
	this.check = formChecker_check;
	this.submitCheck = formChecker_submitCheck;
	this.setStyle = formChecker_setStyle;
	this.focusOn = formChecker_focusOn;	
}

function formChecker_addGuard(formElement, guard, required) {
	required = (required==null?true:required);
	this.elements[this.elementC++] = new Array(formElement, guard);	
//	formElement.style.background = this.standardBackground;
	if (required) {
		this.setStyle(formElement, this.standardBackground);
	}
}

function formChecker_submitCheck(form) {
	if (this.check()) {
		form.submit();
	}
}

function formChecker_check() {
	hasErrored = false;
	errors = this.errormessage;
	focusable = null;	
	var i;
	
	for (i=0;i<this.elementC;i++) {
		guard = this.elements[i][1];
		frmElement = this.elements[i][0];

		if (!guard.isValid(frmElement)) {
			if (hasErrored == false) {
				focusable = frmElement;
			}
			hasErrored = true;
				
				this.setStyle(frmElement, this.erroredBackground);
			errors+='\n- '+guard.message;
		} else {
				this.setStyle(frmElement, this.standardBackground);
		}
	}
	if (hasErrored) {
		alert(errors);
		if (focusable!=null) this.focusOn(focusable);
		return false;
	} 
	
	return true;
}


function formChecker_setStyle(formElement, style) {
	if (formElement.length!=null) {
		// assume array
		for (j=0;j<formElement.length;j++) {
			//this.setStyle(formElement[i], style);
			formElement[j].style.background=style;
		}
	} else {
		// assume object
		formElement.style.background=style;		
	}

}

function formChecker_focusOn(formElement) {
	if (formElement.focus) {
		formElement.focus();
	} else if (formElement.length!=null) {
		// assume array
		//	formElement[0].focus();

	} 
}

// =========== empty field guard =============

function EmptyFieldGuard(message) {
	this.message = message;
	this.isValid = emptyFieldGuard_isValid;
}

function emptyFieldGuard_isValid(formElement) {
	return formElement.value!='';
}

// =========== email address guard ===========

function EmailGuard(message) {
	this.message = message;
	this.isValid = emailGuard_isValid;
}

function emailGuard_isValid(formElement)
{
	illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]\%]/;
		
	return (formElement.value.indexOf("@") + "" != "-1" &&
        formElement.value.indexOf(".") + "" != "-1" && formElement.value.indexOf(" ") +"" == "-1" &&
		formElement.value != "" && formElement.value.match(illegalChars) == null);
	
		/*
	return (formElement.value.indexOf("@") + "" != "-1" &&
        formElement.value.indexOf(".") + "" != "-1" && formElement.value.indexOf(" ") +"" == "-1" &&
		formElement.value != "");
		*/
}


// =========== weblink guard ===========
// added by	: Johan de Kruyf
// date		: May 13, 2003

function WeblinkGuard(message) {
	this.message = message;
	this.isValid = weblinkGuard_isValid;
}

function weblinkGuard_isValid(formElement) {
    return (formElement.value.indexOf("http://") + "" != "-1");
}

// =========== postcode guard ===========
// added by	: Johan de Kruyf
// date		: May 14, 2003

function PostcodeGuard(message) {
	this.message = message;
	this.isValid = postcodeGuard_isValid;
}

function postcodeGuard_isValid(formElement) {
   return typeof formElement.value=='string'?formElement.value.match(/ *[1-9][0-9]{3} ?[a-zA-Z]{2} */)==formElement.value:false;
}

// =========== bank/girorekening check ===========
// added by	: Vincent van Beveren
// date		: July 1, 2004

function BankGiroGuard(message, magLeeg) {
	this.message = message;
	this.magLeeg = (magLeeg?magLeeg:false);
	this.isValid = function(formElement) {
		num = formElement.value;
		if (num=='' && this.magLeeg) return true;
		// wegehalen whitespaces, P aan het begin, 
		// punten en alle eerste nullen (0)
		num = num.replace(/[(^P)\.\s]/gi,'');
		num = num.replace(/^0+/,'');		
		// onmogelijke lengtes (0, 8 of meer dan 10)
		l = num.length;
		if (l==8 || l>10 || l==0) return false;
		
		// minder dan 8 is altijd giro, dus altijd goed
		if (l<8) return true;	// giro
		
		// doe de 11 proef
		tot = 0;
		for (i=0;i<l;i++) {
			tot+=parseInt(num.substring(i,i+1))*(l-i);
		}
		return ((tot % 11)==0); // klopt die, true, anders false
	}

}


// =========== telefoon guard ===========
// added by	: Johan de Kruyf
// date		: June 4, 2003

function TelefoonGuard(message) {
	this.message = message;
	this.isValid = telefoonGuard_isValid;
}

function telefoonGuard_isValid(formElement) {
	var i;
	var numberCount = 0;
	aantalKarakters = formElement.value.length;
	for(i=0;i<aantalKarakters;i++)	{
		if(!isNaN(formElement.value.charAt(i)))	{
			numberCount++;
			}
		}
	return(numberCount < 10);
	//alert(numberCount);
	//return true;
   }


// =========== checkbox guard ===============
function CheckboxGuard(message) {
	this.message = message;
	this.isValid = checkboxGuard_isValid;
}

function checkboxGuard_isValid(formElement) {
   return formElement.checked;
}
/*
// =========== multicheckboxguard ===============
// usage: new MutliCheckboxGuard("<message>", <minimal # of checked boxes inclusive>,
//        maximum # of checked boxes inclusive>);

// for example, a array of checkbox objects of which 3 exactly should be checked use:
//  new MultiCheckboxGaurd("You need to check 3 boxes", 3,3);
function MultiCheckboxGuard(message, minChecked, maxChecked) {

	this.NOT_SPECIFIED=-1;
	this.minChecked = (minChecked==null?this.NOT_SPECIFIED:minChecked);
	this.maxChecked = (maxChecked==null?this.NOT_SPECIFIED:maxChecked);
	this.message = message;
	var i;
	this.isValid = function(formElement) {
		if (formElement.length) {
			c = 0;
			for (i=0;i<formElement.length;i++) {
				if (formElement.item) {
					if (formElement.item(i).checked) c++;
				} else {
					if (formElement[i].checked) c++;
				}
			}
			if (minChecked>=0) if (c<minChecked) return false;
			if (maxChecked>=0) if (c>maxChecked) return false;			
		} 
		return true;
	}

}
*/


// =========== comperator guard ===============
// requires two elements:
// addGuard(new Array(element1, element2), "The two don't match");
function CompairGuard(message, additionalguard) {
	this.amessage = message;
	this.message = this.amessage;
	this.additionalguard = additionalguard;
	this.isValid = CompairGuard_isValid;
}

function CompairGuard_isValid(formElement) {
	this.message = this.amessage;
	if (formElement.length) {
		if (formElement.length==2) {
		
			if (formElement[0].value!=formElement[1].value) {
				formElement[0].value='';
				formElement[1].value='';
				return false;
			} else if ((this.additionalguard) && (!this.additionalguard.isValid(formElement[0]))) {
				this.message = this.additionalguard.message;
				return false;
			} else return true;
		} else return false;
	} else return false;
}


// =========== radiobutton guard ===============
function RadioGuard(message) {
	this.message = message;
	this.isValid = radioGuard_isValid;
}

function radioGuard_isValid(formElement) {
	if (formElement.length>0) {
		for (j=0;j<formElement.length;j++) {
			if (formElement[j].checked) return true;
		}
	}
	return false;
}


// =========== number guard =============

function NumberGuard(message) {
	this.message = message;
	this.isValid = NumberGuard_isValid;
}

function NumberGuard_isValid(formElement) {
	return isNaN(formElement.value)==false;
}


// ============ conditional checker =============
// conditional checker works in the following way:
// new ConditionalGuard('condition', ifCorrectGuard, ifNotCorrectGuard);
// for example:
// new ConditionalGuard('document.form.hasPhoneNumber.checked', new EmptyFieldGuard());
// This checks a phone number if the checkbox is checked

function ConditionalGuard(condition, guard1, guard2) {
	this.condition = condition;
	this.message = '???';
	this.isValid = conditionalGuard_isValid;
	this.trueGuard = guard1;
	this.falseGuard = guard2;
}

function conditionalGuard_isValid(formElement) {
	if (eval(this.condition)) {
		if (this.trueGuard!=null) {
			this.message = this.trueGuard.message;
			return this.trueGuard.isValid(formElement);
		}
		return true;
	} else {
		if (this.falseGuard!=null) {
			this.message = this.falseGuard.message;
			return this.falseGuard.isValid(formElement);
		}
		return true;
	}
}



// =========== multicheckboxguard ===============
// usage: new MutliCheckboxGuard("<message>", <minimal # of checked boxes inclusive>,
//        maximum # of checked boxes inclusive>);

// for example, a array of checkbox objects of which 3 exactly should be checked use:
//  new MultiCheckboxGaurd("You need to check 3 boxes", 3,3);
function MultiCheckboxGuard(message, minChecked, maxChecked) {
	
	this.NOT_SPECIFIED=-1;
	this.minChecked = (minChecked==null?this.NOT_SPECIFIED:minChecked);
	this.maxChecked = (maxChecked==null?this.NOT_SPECIFIED:maxChecked);
	this.message = message;
	var i;
	this.isValid = function(formElement) {
		
		if (formElement.length) {
			c = 0;
			
			for (i=0;i<formElement.length;i++) {
				if (formElement.item) {
					if (formElement.item(i).checked) c++;
				} else {
					if (formElement[i].checked) c++;
				}
			}
			if (minChecked>=0) if (c<minChecked) return false;
			if (maxChecked>=0) if (c>maxChecked) return false;			
		} 
		return true;
	}

}



// =========== comperator guard ===============
// requires two elements:
// addGuard(new Array(element1, element2), "The two don't match");
function CompairGuard(message, additionalguard) {
	this.amessage = message;
	this.message = this.amessage;
	this.additionalguard = additionalguard;
	this.isValid = CompairGuard_isValid;
}

function CompairGuard_isValid(formElement) {
	this.message = this.amessage;
	if (formElement.length) {
		if (formElement.length==2) {
		
			if (formElement[0].value!=formElement[1].value) {
				formElement[0].value='';
				formElement[1].value='';
				return false;
			} else if ((this.additionalguard) && (!this.additionalguard.isValid(formElement[0]))) {
				this.message = this.additionalguard.message;
				return false;
			} else return true;
		} else return false;
	} else return false;
}

fc = new FormChecker('Niet alle gegevens zijn (correct) ingevuld:\n', '#FFFFFF', '#FAF6B6');



