
/*---------------------------------------------------
	Generic validation script for FORM
	
	author	: acdhirr
	company	: witchbiet
	created	: 8-jan-2004	
	
	INPUT elements must have either one of these
	classnames to be validated:
	- txtNumeric
	- txtDate
	- txtCurrency	
---------------------------------------------------*/

function validate(oForm) {
	var cMessage = "";
	var colInput = oForm.getElementsByTagName("INPUT");
	
	for (var i=0; i<colInput.length; i++) {
	
		// Check required fields
		if (colInput[i].getAttribute('required') && wbTrim(colInput[i].value) == '') {
			cMessage = cMessage + "Veld '" + colInput[i].getAttribute('title') + "' mag niet leeg blijven.\n\n";
			doFocus(colInput[i]);
		}
		
		// Check non empty fields on format 
		if (wbTrim(colInput[i].value) != '') {
			
			switch (colInput[i].className) {
				case 'txtNumeric': 
					_valNumeric(colInput[i]);
					break;
				case 'txtDate':		
					_valDate(colInput[i]);
					break;
				case 'txtTime':
					_valTime(colInput[i]);
					break;
				case 'txtCurrency':
					_valCurrency(colInput[i]);
					break;
				case 'txtCustom':
					_valCustom(colInput[i]);
					break;
				default:
					break;
			}
		}
		if (wbTrim(colInput[i].value) == ''){
			// PostFormat empty fields
			switch (colInput[i].className) {
				case 'txtNumeric': 
					colInput[i].value="0";
					break;
				case 'txtCurrency':
					colInput[i].value="0,00";
			}
		}
	}
	
	// Notify user when there are messages
	if (cMessage != "") {
		alert(cMessage);
		return false;
	}
	return true;
	
	/*----------------------------------------
		testing sub numeric
	----------------------------------------*/
	function _valNumeric(oInput) {
		// Remove leading and trailing spaces
		oInput.value = wbTrim(oInput.value);
		var re = /^\s*[0-9]+\s*$/;
		if (!oInput.value.match(re)) {
			cMessage += "'" + wbTrim(oInput.value) + "' is geen geldige waarde! \nNumerieke gegevens invoeren als een geheel getal.\n\n";
			doFocus(oInput);
			doSelect(oInput);
			return false;
		}
		if (!oInput.value>32000) {
			cMessage += "'" + wbTrim(oInput.value) + "' heeft een te grote waarde (maximum=32000)";
			doFocus(oInput);
			doSelect(oInput);
			return false;
		}	
		return true;
	}

	
	/*----------------------------------------
		testing sub date
	----------------------------------------*/
	function _valDate(oInput) {
		// Remove leading and trailing spaces
		oInput.value = wbTrim(oInput.value);
		if (!checkDate(oInput.value)) {
			cMessage += "'" + oInput.value + "'" + " is geen geldige datum!\n"
			+ "Data dienen ingevoerd te worden als 'dd-mm-jjjj'."
			+ "\nVoorbeeld: 19-09-2001.\n\n"
			doFocus(oInput);
			doSelect(oInput);
			return false
		}	
		return true;
	}

	/*----------------------------------------
		testing sub time
	----------------------------------------*/
	function _valTime(oInput) {
		// Remove leading and trailing spaces
		oInput.value = wbTrim(oInput.value);
		if(oInput.value.length==3 || (oInput.value.length==4 && oInput.value.match(":")))
			oInput.value="0"+oInput.value;
		if(!oInput.value.match(":"))
			if(oInput.value.length==4)
				oInput.value=oInput.value.substr(0,2)+":"+oInput.value.substr(2,2);
		var re = /^\s*[0-9]{2}:[0-9]{2}\s*$/;
		if (!oInput.value.match(re)
			|| parseInt(oInput.value.substr(0,2)) > 23
			|| parseInt(oInput.value.substr(3,2)) > 59
			) {
			cMessage += "'" + oInput.value + "'" + " is geen geldige tijd!\n"
			+ "Tijd dient ingevoerd te worden als 'uu:mm'."
			+ "\nVoorbeeld: 23:45.\n\n"
			doFocus(oInput);
			doSelect(oInput);
			return false
		}	
		return true;
	}

	/*----------------------------------------
		testing sub currency
	----------------------------------------*/
	function _valCurrency(oInput) {
		// Leading and trailing spaces allowed (\s)
		
		oInput.value=oInput.value.replace(".",",");
		if(oInput.value.match(",")==null)oInput.value=oInput.value+",00";
		
		var re = /^\s*[0-9]+,[0-9]{2}\s*$/;
		if (!oInput.value.match(re)) {
			cMessage += "'" + wbTrim(oInput.value) + "' is geen bedrag! \nBedragen invoeren als bv. 12,95.\n\n";
			doFocus(oInput);
			doSelect(oInput);
			return false;
		}
	}

	/*----------------------------------------
		testing sub custom
	----------------------------------------*/
	function _valCustom(oInput) {
		var re=new RegExp(oInput.getAttribute("wbValidation"));
		if (!oInput.value.match(re)) {
			cMessage += oInput.name + " is niet goed ingevuld (" + wbTrim(oInput.value) + ").\n\n";
			doFocus(oInput);
			doSelect(oInput);
			return false;
		}
		return true;
	}
}	


/*---------------------------------------------------
	Leading and trailing spaces verwijderen: we 
	willen niet	gefoold worden door wiseguys die 
	spaties invoeren. 
---------------------------------------------------*/
function wbTrim(cString) {
	
	return cString.replace(/(^\s*)|(\s*$)/g, "");
	
}


/*---------------------------------------------------
	Aleer men een gegeven deleteert eerst bevestiging
---------------------------------------------------*/
function confirmDel(cMessage) {
	if (cMessage == '') cMessage = 'Weet u zeker dat u dit gegeven wilt verwijderen?';
	return confirm(cMessage);
}



/*---------------------------------------------------
	Datumveld datum moet vorm hebben: DD-MM-YYYY
---------------------------------------------------*/
function checkDate(val) { 

	if (val == '') return null;
	
	nSecs70 = '';
	dtStart = new Date(1962,11,11);

	//Invoerformaat van datum: 20-06-1968
	var cRegExp = /[0-9]{2}-[0-9]{2}-[0-9]{4}/gi;
	lValidDate = cRegExp.test(val)

	if (lValidDate) {

		nDay	= val.substr(0,2);
		nMonth	= val.substr(3,2)-1;	// maanden 0-11!
		nYear	= val.substr(6,4);

		dtDate		= new Date(nYear, nMonth, nDay, 0, 0, 0, 0);

		/* Javascript doet niet moeilijk over onmogelijke
		data, want telt gewoon door (32 juni wordt 2 juli).
		Wij wel, dus vragen we van het geconstrueerde 
		datumobject de werkelijke datum op en kijken of
		dag en maand nog hetzelfde zijn. Dit werkt ook
		voor 29 februari-gevallen. */

		nJSDay 		= dtDate.getDate();
		nJSMonth	= dtDate.getMonth();

		if ((nJSDay != nDay) || (nJSMonth != nMonth)) {

			lValidDate = false;	
		}
	}
	
	if (!lValidDate) return false;
	return true;
}


/* try to set focus at oInput. This may not be possible,
if oInput is in a div or tab that is not visible */
function doFocus(oInput) {

	try {
		oInput.focus();
	}
	catch (e) {;}
}

/* try to select oInput. This may not be possible,
if oInput is in a div or tab that is not visible */
function doSelect(oInput) {

	try {
		oInput.select();
	}
	catch (e) {;}
}

