// JavaScript Document
//These functions provide input masks eg for numeric form fields, to ensure correct and consistent treatment and/or conversion 
//when entereing data
//+ standard functions often used when checking forms
function doNumberInput(fld,lang) {
	//make sure that numbers use "," for decimal separators!
	//first convert any "." numbers to "," (this is just for representation in the field)
	fld.value = fld.value.replace('.',',')
	//Now replace back for number checking, because javascripts only works with english decimal conventions
	var val = fld.value.replace(',','.');
	
	switch(lang) {
		case 'nl': var msg = "Dit is geen geldig getal."; break ;
		case 'fr': var msg = "Ceci n'est pas un nombre valable."; break
		case 'en': var msg = "This is not a valid number."; break
		default:   var msg = "Dit is geen geldig getal.";         
	}	
	
	if (isNaN(val)) {
		alert(msg);
		fld.style.color = 'red';
		this.focus();
		return false; }
	else {
		fld.style.color = 'black';
		return true; }
}
function fNumToEng(num) {
	//convert European style to English style numbers
	num += ''; //make sure we have a string
	num = num.replace(',','.');
	return num;
}
function fNumToEur(num) {
	//convert English style to European style numbers
	num += ''; //make sure we have a string
	num = num.replace('.',',');
	return num;
}
function isEmpty() {
	//test if ANY ONE of a series of values (in the args) is empty and return true if so.
	for (var i=0; i<isEmpty.arguments.length; i++) {
		if (isEmpty.arguments[i] == '')
			return true;
	}
	return false;
}
function isEmail(str) {
  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  if (!reg1.test(str) && reg2.test(str)) { // syntax is valid
    return true;
  }
  return false;
}
function noSelection(fld) {
	//tests if the value of a SELECT selection = empty string and returns true if it is
	if(fld[fld.selectedIndex].value == '')
		return true;
	else
		return false;
}
function noRadioChecked(fldlist) {
	//test if, for a list of radio button groups, none of the radio buttons in ANY ONE or the list have been checked and if so, return true, otherwise, return false
	var noCheck;
	for (var i=0; i<noRadioChecked.arguments.length; i++) {
		fld = noRadioChecked.arguments[i]
		noCheck = true;
		for (var j = 0; j < fld.length ; j++) {
			if(fld[j].checked) {
				noCheck = false }
		}
		if (noCheck)
			return true; // no radio btn was checked in the current group
	}
	return false; // if no unchecked group has been found
}
function noRadioCheckedOLD(fld) {
	//test if none of the radio buttons in a radio button have been checked and if so, return true, otherwise, return false
	for (var i = 0; i < fld.length ; i++) {
		if(fld[i].checked) {
			return false; }
	}
	return true;
}
function doubleCheck(url,msg) {
	//ask confirmation before going to the link the user clicked!
	if(!msg) {
		msg = "Bent u zeker?"
	}
	if(confirm(msg)) {
		document.location.href=url;
	}
	
}
function fToInt(fld) {
	myval = parseInt(parseFloat(fld.value));
	if (!isNaN(myval)) {
		fld.value = myval;
	}
	else {
		fld.value = "0";
	}
}
function to_integer(fld) {
   var strnum = fld.value;
   len = strnum.length;
   var newstrnum = '';
   for (i = 0; i < len; i++)
		{
		if (strnum.charAt(i) != '.' && strnum.charAt(i) != ',' && strnum.charAt(i) != ' ')
			{ newstrnum = newstrnum + strnum.charAt(i); }
		}
	len = newstrnum.length;
   for (i = 0; i < len; i++)
		{
			if (newstrnum.charAt(i) < '0' || newstrnum.charAt(i) > '9')
				  {
				  fld.value = '';				  
				  alert('Vul een geldige getalwaarde in aub');
				  return false;
				  break;
				  }
		}
	// set the value of prijs to the new string
	fld.value = newstrnum;
	return true;
}	
