/* Formchecker 1.02 (date fix), MDi, okt. 2003, Fabrique [design, nieuwe media & communicatie] */

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

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