/*
Form validation functions
Copyright (c) 2002-2009 Ylab, www.ylab.nl
*/
function errormsg(errorcode, guiName, val){
	guiName = '`' + guiName + '`';
	switch (errorcode){
		case 'err_bsn'   : alert('In het veld ' + guiName + ' is geen geldig BSN ingevuld.'); break;
		case 'err_date'  : alert('In het veld ' + guiName + ' is geen geldige datum ingevuld.\nGebruik het formaat dd-mm-jjjj om een datum in te voeren.'); break;
		case 'err_time'  : alert('In het veld ' + guiName + ' is geen geldige tijd ingevuld.\nGebruik het formaat uu:mm om een tijd in te voeren.'); break;
		case 'err_email' : alert('Het e-mailadres is onvolledig.'); break;
		case 'err_empty' : alert('Het veld ' + guiName + ' moet ingevuld zijn.'); break;
		case 'err_gte'   : alert('Het veld ' + guiName + ' moet groter dan of gelijk aan ' + val + ' zijn.'); break;
		case 'err_later' : alert('De datum in het veld ' + guiName + ' moet na ' + val + ' liggen.'); break;
		case 'err_length': alert('Het veld ' + guiName + ' moet uit ' + val + ' cijfers bestaan.'); break;
		case 'err_lte'   : alert('Het veld ' + guiName + ' moet kleiner dan of gelijk aan ' + val + ' zijn.'); break;
		case 'err_number': alert('Het veld ' + guiName + ' moet een getal zijn.'); break;
		case 'err_url'   : alert('In het veld ' + guiName + ' is geen geldig internetadres ingevuld.'); break;
		case 'err_zip'   : alert('In het veld ' + guiName + ' is geen geldige postcode ingevuld.'); break;
		default: alert('Ongedefineerde foutcode: ' + errorcode); break;
	}
}

function testRegExp(field, re){
	return new RegExp(re).test(field.value);
}

function validateEmail(field){
	field.value = field.value.trim();
	return !testRegExp(field, '[<>"\;\, ]') && testRegExp(field, '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$');
}

function validateUrl(field){
	field.value = field.value.trim();
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(field.value);
}

function isNotNull (field, guiName, f){
	//validates if a field contains a value
	//field: input element text|hidden
	//guiName: fieldname to communicate with user
	if( (!field.value) || (field.value == "") ){
		setFocus(field, f);
		errormsg('err_empty', guiName);
		return false;
	}
	return true;
}

function isEmail (field, guiName, f){
	//validates if a textbox contains a valid email address
	if(!field.value) return true;
	if(!validateEmail(field)){
		setFocus(field, f);
		errormsg('err_email', guiName);
		return false;
	}
	return true;
}

function isUrl(field, guiName, f){
	//validates if a textbox contains a valid url
	if(!field.value) return true;
	if(!validateUrl(field)){
		setFocus(field, f);
		errormsg('err_url', guiName);
		return false;
	}
	return true;
}

function isInRange(field, guiName, minval, maxval, f){
	//validates if a field contains a value with a certain range
	if(!field.value) return true;
	if((minval != null) && ((Number(field.value)||0) < minval)){
		setFocus(field, f);
		errormsg('err_gte', guiName, minval);
	}
	else if((maxval != null) && ((Number(field.value)||0) > maxval)){
		setFocus(field, f);
		errormsg('err_lte', guiName, maxval);
	}
	else{
		return true;
	}
	return false;
}

function hasCorrectLen(field, guiName, charlen, f){
	//validates if a field contains a value with a certain length
	//field: input element text|hidden
	//guiName: fieldname to communicate with user
	if(!field.value) return true;
	if(field.value.length != charlen){
		setFocus(field, f);
		errormsg('err_length', guiName, charlen);
		return false;
	}
	return true;
}

function isNumeric(field, guiName, bReal, f){
	//validates if a field contains a numeric value
	//bReal: also accept decimal points
	var i, validChars = "0123456789";

	if(!field.value) return true;
	if(bReal){
		//decimal point accepted
		validChars += ".,";
	}
	for (i = 0; i < field.value.length; i++){
		if(validChars.indexOf(field.value.charAt(i)) == -1){
			setFocus(field, f);
			errormsg('err_number', guiName);
			return false;
		}
	}
	return true;
}

function isDate(field, guiName, f){
	//validates if a field contains a valid date
	if (!field.value){
		return true;
	}
	d = field.value.toDate();
	if (!d){
		setFocus(field, f);
		errormsg('err_date', guiName);
		return false;
	}
	return true;
}

function isTime(field, guiName, f){
	//validates if a field contains a valid date
	if (!field.value){
		return true;
	}
	d = field.value.toTime();
	if (!d){
		setFocus(field, f);
		errormsg('err_time', guiName);
		return false;
	}
	return true;
}

function isBSN(field, guiName, f){
	//validates if a field contains a valid Dutch bsn number
	if (!field.value){
		return true;
	}
	var reBSN = /(\d)(\d)(\d)(\d).?(\d)(\d).?(\d)(\d)(\d)/;
	var isValid = false;
	var i, numbers;
	var lSom = 0;

	if (reBSN.test(field.value)){
		//11 test
		numbers = (field.value.match(reBSN));
		for (i=1;i<9;i+=1){
			lSom += (numbers[i] * (9-(i-1)));
		}
		isValid = (((lSom -= numbers[9]) % 11) == 0)
	}
	if (!isValid){
		setFocus(field, f);
		errormsg('err_bsn', guiName);
	}
	return (isValid);
}

function isZipCode (field, guiName, f){
	//validates if a field contains a valid Dutch zip code
	var str = field.value;
	if (!str) return true;

	var codePattern = /[1-9][0-9]{3} *[A-Za-z]{2}/;
	if (!str.match(codePattern)){
		setFocus(field, f);
		errormsg('err_zip', guiName);
		return false;
	}
	return true;
}

function isLater(field, fieldStart, guiName, f){
	//validates if a field contains a later date then fieldStart
	if(!field.value || !fieldStart.value){
		//don't validate empty fields
		return true;
	}
	if(field.value.toDate() < fieldStart.value.toDate()){
		setFocus(field, f);
		errormsg('err_later', guiName, fieldStart.value);
		return false;
	}
	return true;
}

function isBeforeDate(field, refDate){
	//validates if a field contains a later date then refDate
	//doesnot produce an alertbox
	if(!field.value){
		//don't validate empty fields
		return true;
	}
	d = field.value.toDate();
	return (d < refDate);
}

function isAfterDate(field, refDate){
	//validates if a field contains a earlier date then refDate
	//doesnot produce an alertbox
	if(!field.value){
		//don't validate empty fields
		return true;
	}
	d = field.value.toDate();
	return (d > refDate);
}

function ignoreNonNumKey(event){
	event = event || window.event;
	//8,9,18,37,39,46 49-56,96-105 event.keyCode < 48 ||
	if((event.shiftKey && event.keyCode > 48 && event.keyCode < 57) || (event.keyCode > 57 && event.keyCode < 96) || event.keyCode > 105){
		event.returnValue = false;
		if (event.preventDefault){
			event.preventDefault();
		}
	}
}

function setFocus(field, f){
	try{
		if(f){
			if(typeof(f) != 'function'){
				f = new Function(f);
			}
			f();
		}
		field.focus();
	}
	catch(ex){}
}
