// 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(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)) {
		window.location=url;
	}
	return false;
}

//alternative (older and better) email check

function CheckMail(str)
{
   if (str != '')
   {
		if (str.indexOf('@') == -1)
			 return false;
		len = str.length;
		for (i = 0; i < len; i++)
		{
			 if (  ( str.charAt(i) < 'a' || str.charAt(i) > 'z' ) && isNaN(str.charAt(i)) )
				  if (str.charAt(i) < 'A' || str.charAt(i) > 'Z' || str.charAt(i) == " ")
					   if (str.charAt(i) != '.' && str.charAt(i) != '-' && str.charAt(i) != '_' && str.charAt(i) != '@' && str.charAt(i) != '\'')
							return false;
		}
   }
   return true;
}


//__________FUNCTIONS USED TO CHECK CONFORMITY OF TELEPHONE NUMBERS_____________________//

function stripChars(str,bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < str.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = str.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkTel(tel) {
     // Check for correct phone number
		 if (tel != "") {
			 rePhoneNumber = new RegExp(/^(\+[0-9]{1,3}\.[0-9]{5,14})$/);
			 if (!rePhoneNumber.test(tel)) {
						return false;
			 }
			}
 return true;
}

function configTel(telfield) {
	var tel = telfield.value;
	var ret = '';
	// non-digit characters allowed as user input, but to be stripped from phone numbers
	var strDelimiters = "/()- ";
	if(tel != '') {
		ret = stripChars(tel,strDelimiters);
		telfield.value = ret;
		if (!checkTel(ret)) {
			 alert("Gelieve het nummer in te geven volgens de internationale standaard: \"+\"-teken gevolgd door de landcode, een \".\" en tenslotte het oproepnummer.\nbv.: +32.33261500");
			 telfield.style.color = "#FF0000"
		}
	}
}
//__________    END  _____________________//




