// 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,silent) {
	var silentmode = silent;
	//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 = "\"" + val + "\" is geen geldig getal."; break ;
		case 'fr': var msg = "\"" + val + "\" n'est pas un nombre valable."; break
		case 'en': var msg = "\"" + val + "\" is not a valid number."; break
		default:   var msg = "\"" + val + "\" is geen geldig getal.";
	}
	
	if (isNaN(val)) {
		if(silentmode != 0)
			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,4}|[0-9]{1,3})(\]?)$/; // valid
  if (!reg1.test(str) && reg2.test(str)) { // syntax is valid
    return true;
  }
  return false;
}

function noValue(fld) {
	//tests if the value of a given field = empty string and returns true if it is
	if(fld.value == '')
		return true
	else
		return false
}

function hasEmpties(fieldlist) {
	//test if the value of at least ONE in a list of form fields = empty string and returns true if it is
	for(i=0;i<arguments.length;i++){
	 fld = arguments[i]
	 	if (noValue(fld))
			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)) {
		location.href=url;
	}
}

function fToInt(fld) {
	myval = parseInt(parseFloat(fld.value));
	if (!isNaN(myval)) {
		fld.value = myval;
	}
	else {
		fld.value = "0";
	}
}


function isIMEI (s) {
var etal = /^[0-9]{15}$/;
  if (!etal.test(s))
    return false;
  sum = 0; mul = 2; l = 14;
  for (i = 0; i < l; i++) {
    digit = s.substring(l-i-1,l-i);
    tp = parseInt(digit,10)*mul;
    if (tp >= 10)
         sum += (tp % 10) +1;
    else
         sum += tp;
    if (mul == 1)
         mul++;
    else
         mul--;
    }
  chk = ((10 - (sum % 10)) % 10);
  if (chk != parseInt(s.substring(14,15),10))
    return false;
  return true;
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	return (((sign)?'':'-')  + num + '.' + cents);
}

