/* Formchecker 1.02 (date fix), MDi, okt. 2003, Fabrique [design, nieuwe media & communicatie] */

function setText(thefield){
if (thefield.value=="")
thefield.value = thefield.defaultValue
}
function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}
// filled in
function isEmpty (o){
	trimField(o);
	return o.value == '';
}

// trims field
function trimField (o){
	var v = o.value.replace(/^ +| +$/g,'');
	o.value = v;
}

// checkbox checked;
function validCheckbox (o){
	return o.checked;
}

// radio checked;
function validRadio (o){
	for (var i = 0; i < o.length; i++){
		if (o[i].checked){ return 1; }	
	}
	return 0;
}

// not 1st option selected;
function validDD (o){
	return o.options.selectedIndex > 0;
}

// checks for spaces in input
function hasSpaces(o){
	return o.value.indexOf(' ') != -1;
}

// more chars then;
function longerThen(o,maxL){
	trimField(o);
	return o.value.length > maxL; 
}

// less chars then;
function shorterThen(o,minL){
	trimField(o);
	return o.value.length < minL; 
}

// valid integer, with optional min and max range (for age)
var regExpInt = /^\d+$/;
function isInteger (o,minR,maxR){
	if (isEmpty (o)){ return 1; }
	var v = o.value;
	return ((regExpInt.test(v)) && (!minR || v >= minR) && (!maxR || v <= maxR));
}

// valid email
var regExpEmail = /^\S+@+\S+\.+\S+$/;
function validEmail(o){
	if (isEmpty (o)){ return 1; }
	return regExpEmail.test(o.value);
}

// valid dutch zipcode, formats field
var regExpPC = /^[1-9]{1}\d{3} *[A-Z]{2}$/;
function validPC(o){
	if (isEmpty (o)){ return 1; }
	var v = o.value.toUpperCase();
	if (!regExpPC.test(v)){ return 0; }
	o.value = v.replace (/ /g,'');
	return 1;
}

// valid phone 10 digits, formats field
var regExpPhone = /^[\d+ ][\d ]+$/;
function validPhone(o){
	if (isEmpty (o)){ return 1; }
	var v = o.value.replace(/[-\/. ]+/g, ' ').replace(/^ +| +$/g, '');
	if ((!regExpPhone.test(v)) || v.replace(/[-\/. +]+/g, '').length < 10){
		return 0;
	}
	o.value = v;
	return 1;
}

// gets value from dropdown menu's
function getDDValue(o){
	return o.options[o.selectedIndex].value;
}

// valid date for dropdownmenu's (also leap year), needs functions validDD() and getDDValue()
function validDate(d,m,y){
	if (!validDD(d) && !validDD(m) && !validDD(y)){ return 1; }
	if (!validDD(d) || !validDD(m) || !validDD(y)){ return 0; }
	var d = parseInt(getDDValue(d),10), m = parseInt(getDDValue(m),10), y = parseInt(getDDValue(y),10);
	daysAMonth = new Array (null,31,28,31,30,31,30,31,31,30,31,30,31);
	return (d <= daysAMonth[m] || (m == 2 && d == 29 && y%4 == 0));
}

// extended valid date, accepts no wrong input at all, needs functions isInteger() and isLonger()
function validDateExt(d,m,y){
	if (isEmpty (d) && isEmpty (m) && isEmpty (y)){ return 1; }
	if (	isEmpty (d) || !(isInteger(d,1,31)) || (longerThen(d,2)) ||
			isEmpty (m) || !(isInteger(m,1,12)) || (longerThen(m,2)) ||
			isEmpty (y) || !(isInteger(y,1900,9999)) || (longerThen(y,4))
		){ return 0; }
	var d = parseInt(d.value,10), m = parseInt(m.value,10), y = parseInt(y.value,10);
	daysAMonth = new Array (null,31,28,31,30,31,30,31,31,30,31,30,31);
	return (d <= daysAMonth[m] || (m == 2 && d == 29 && y%4 == 0));
}

// validateURL check if input starts with http//
// if not: add it
function validateURL(o){
	if( !isEmpty(o) && o.value.indexOf('http://')!=0 ){
		o.value = 'http://'+o.value;
	}
}

//<form action="mailer.php" method="post" onsubmit="return checkForm(this)">
/*
function checkForm(f){
		var intro = 'De volgende fouten zijn geconstateerd:';
		with (f){
			var w = '';
			if (isEmpty(naam))																	{ w += '\n- Naam niet ingevuld'; }
			if (isEmpty(adres))																	{ w += '\n- Adres niet ingevuld'; }
			if (isEmpty(postcode))																{ w += '\n- Postcode niet ingevuld'; }
			if (!validPC(postcode))																{ w += '\n- Postcode niet correct'; }
			if (isEmpty(plaats))																	{ w += '\n- Plaats niet ingevuld'; }
			if (isEmpty(email))																	{ w += '\n- Email niet ingevuld'; }
			if (!validEmail(email))																{ w += '\n- Email niet correct'; }
			if (isEmpty(telefoon))																{ w += '\n- Telefoon niet ingevuld'; }
			if (!validPhone(telefoon))															{ w += '\n- Telefoon niet correct'; }
			
			if (isEmpty(persoonsnaam))															{ w += '\n- Persoonsnaam niet ingevuld'; }

			if (isEmpty(hoofd_verzekerde_naam))													{ w += '\n- Naam niet ingevuld'; }

			if (isEmpty(gebDag1) && isEmpty(gebMaand1) && isEmpty(gebJaar1))					{ w += '\n- Geen geboortedatum gekozen'; }
			if (!validDateExt(gebDag1,gebMaand1,gebJaar1))										{ w += '\n- Geen geldige geboortedatum'; }

			if (isEmpty(voorletters))															{ w += '\n- Voorletters niet ingevuld'; }
			if (isEmpty(achternaam))															{ w += '\n- Achternaam niet ingevuld'; }	
		}
		if (w != ''){ alert(intro+w); return false; }
		return true;
	}
*/

