

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) {
	this.elements[this.elementC++] = new Array(formElement, guard);	
//	formElement.style.background = this.standardBackground;
	this.setStyle(formElement, this.standardBackground);
}

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

function formChecker_check() {
	hasErrored = false;
	errors = this.errormessage;
	focusable = null;	

	
	for (i=0;i<this.elementC;i++) {
		guard = this.elements[i][1];
		


		if (!guard.isValid(this.elements[i][0])) {
			if (hasErrored == false) {
				focusable = this.elements[i][0];
			}
			hasErrored = true;
				
				this.setStyle(this.elements[i][0], this.erroredBackground);
			errors+='\n- '+guard.message;
		} else {
				this.setStyle(this.elements[i][0], 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.length!=null) && (formElement.tagName!="SELECT")) {
		// assume array
//			this.focusOn(formElement[0]);
			formElement[0].focus();
	} else {
		// assume object
		formElement.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) {
    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 guards ===========
// 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;
}

/** cijfers en letters apart */

function PostcodeCijfersGuard(message)	{
	this.message = message;
	this.isValid = postcodeCijfersGuard_isValid;
}
function postcodeCijfersGuard_isValid(formElement)	{
	return typeof formElement.value=='string'?formElement.value.match(/[1-9][0-9]{3}/)==formElement.value:false;
}
function PostcodeLettersGuard(message)	{
	this.message = message;
	this.isValid = PostcodeLettersGuard_isValid;
}
function PostcodeLettersGuard_isValid(formElement)	{
	return typeof formElement.value=='string'?formElement.value.match(/[a-zA-Z]{2}/)==formElement.value: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 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);
// STATUS: EXPERIMENTAL!
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;
	}

}

// =========== 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;
}



// ============ 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;
	}
}






