var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

function setFocusDelayed()
{
  global_valfield.focus();
}

function setfocus(valfield)
{
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}

function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  
  
  elem.className = msgtype;   // set the CSS class to adjust appearance of message
}

// --------------------------------------------
//            commonCheck
// Common code for all validation routines to:
// (a) check for older / less-equipped browsers
// (b) check if empty fields are required
// Returns true (validation passed), 
//         false (validation failed) or 
//         proceed (don't know yet)
// --------------------------------------------

var proceed = 2;  

function commonCheck    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(infofield);
  if (!elem.firstChild) return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text) return true;  // infofield is wrong type of node  

  if (emptyString.test(valfield.value)) {
    if (required) {
      msg (infofield, "error", ''); 
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '<img src="http://www.infocash.nl/images/exclamation.png" alt="Let op!" onmouseover="Tip(\'Dit is een verplicht veld!\')" onmouseout="UnTip()" />';

      setfocus(valfield);
      return false;
    }
    else {
      msg (infofield, "warn", "");   // OK
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '';

      return true;  
    }
  }
  return proceed;
}

// --------------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so 
// --------------------------------------------

function validateAmountCharacters(valfield,   // element to be validated
                         infofield, minimum, maximum ) // id of element to receive info/error msg
{
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;
 var lengte = valfield.value.length;

if(lengte < minimum){
  msg (infofield, "error", "");  
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '<img src="http://www.infocash.nl/images/exclamation.png" alt="Let op!" onmouseover="Tip(\'Minimaal ' + minimum + ' karakters\')" onmouseout="UnTip()" />';
      setfocus(valfield);

return false;
}
if(lengte > maximum){

  msg (infofield, "error", "");  
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '<img src="http://www.infocash.nl/images/exclamation.png" alt="Let op!" onmouseover="Tip(\'Maximaal ' + maximum + ' karakters\')" onmouseout="UnTip()" />';
      setfocus(valfield);

return false;
}
      msg (infofield, "warn", "");   // OK
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '';

  return true;
}



// --------------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateEmail  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  if (!email.test(tfld)) {
    msg (infofield, "error", "");
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '<img src="http://www.infocash.nl/images/exclamation.png" alt="Let op!" onmouseover="Tip(\'Geen geldig e-mail adres\')" onmouseout="UnTip()" />';
    setfocus(valfield);
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
  if (!email2.test(tfld)) {
    msg (infofield, "warn", "");
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '<img src="http://www.infocash.nl/images/exclamation.png" alt="Let op!" onmouseover="Tip(\'Ongebruikelijk e-mail adres - controleer a.u.b.\')" onmouseout="UnTip()" />';
	return true;

  }else {
      msg (infofield, "warn", "");   // OK
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '';

return true;
}
}
function validateMoney(valfield, infofield, minimum, maximum) {
	var newValue = valfield.value;
	var newValue = newValue.replace(".", ",");
	var decAmount = "";
	var dolAmount = "";
	var decFlag = false;
	var aChar = "";
   

   // ignore all but digits and decimal points.
	for(i=0; i < newValue.length; i++) {
      aChar = newValue.substring(i,i+1);
      if(aChar >= "0" && aChar <= "9") {
         if(decFlag) {
            decAmount = "" + decAmount + aChar;
         }
         else {
            dolAmount = "" + dolAmount + aChar;
         }
      }
      if(aChar == ",") {
         if(decFlag) {
            dolAmount = "";
            break;
         }
         decFlag=true;
      }
   }
   
   // Ensure that at least a zero appears for the dollar amount.

   if(dolAmount == "") {
      dolAmount = "0";
   }
   // Strip leading zeros.
   if(dolAmount.length > 1) {
      while(dolAmount.length > 1 && dolAmount.substring(0,1) == "0") {
         dolAmount = dolAmount.substring(1,dolAmount.length);
      }
   }
   
   // Round the decimal amount.
   if(decAmount.length > 2) {
      if(decAmount.substring(2,3) > "4") {
         decAmount = parseInt(decAmount.substring(0,2)) + 1;
         if(decAmount < 10) {
            decAmount = "0" + decAmount;
         }
         else {
            decAmount = "" + decAmount;
         }
      }
      else {
         decAmount = decAmount.substring(0,2);
      }
      if (decAmount == 100) {
         decAmount = "00";
         dolAmount = parseInt(dolAmount) + 1;
      }
   }
   
   // Pad right side of decAmount
   if(decAmount.length == 1) {
      decAmount = decAmount + "0";
   }
   if(decAmount.length == 0) {
      decAmount = decAmount + "00";
   }
   
   // Check for negative values and reset valfield
   if(newValue.substring(0,1) != '-' ||
         (dolAmount == "0" && decAmount == "00")) {
valfield.value = dolAmount + "," + decAmount;
   }
   else{
      valfield.value = '-' + dolAmount + "," + decAmount;
   }
   var minimumrepl = parseFloat(minimum.replace(",", ""));
   var maximumrepl = parseFloat(maximum.replace(",", ""));
   var amount = parseFloat(valfield.value.replace(",", ""));
	if(amount >= minimumrepl && amount <= maximumrepl){
      msg (infofield, "warn", "");   // OK
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '';
return true;
}
if(amount < minimumrepl) {
  msg (infofield, "error", "");  
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '<img src="http://www.infocash.nl/images/exclamation.png" alt="Let op!" onmouseover="Tip(\'Minimaal \u20AC' + minimum +'\')" onmouseout="UnTip()" />';
    setfocus(valfield);
return false;   
   }
   if(amount > maximumrepl) {
  msg (infofield, "error", "");  
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '<img src="http://www.infocash.nl/images/exclamation.png" alt="Let op!" onmouseover="Tip(\'Maximaal \u20AC' + maximum +'\')" onmouseout="UnTip()" />';
    setfocus(valfield);
return false;   
   }   
}



function validateConfirm   (valfield,   // checkbox to be validated
                            infofield,
							required)   // id of element to receive info/error msg
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;
  if (valfield.checked){ 
   msg (infofield, "warn", "");  
		var kleurfield = infofield + '_kleur';
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '';

  return true;
  }
  else	{

  msg (infofield, "error", "");  
		var kleurfield = infofield + '_kleur';
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '<img src="http://www.infocash.nl/images/exclamation.png" alt="Let op!" onmouseover="Tip(\'Je bent niet akkoord gegaan\')" onmouseout="UnTip()" />';
      setfocus(valfield);
	return false;
}
}

function validateSameValue   (valfield1, valfield2,
                            infofield)
{
	
  if (valfield1.value == valfield2.value){ 
    if (validateAmountCharacters(valfield2, 'status_wachtwoord2', 6, 32)){
    if (validateAmountCharacters(valfield1, 'status_wachtwoord1', 6, 32)){
      msg (infofield, "warn", "");   // OK
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '';
return true;
	}
	}
	}
  else	{

  msg (infofield, "error", "");  
		var imagefield = infofield + '_image';
		document.getElementById(imagefield).innerHTML = '<img src="http://www.infocash.nl/images/exclamation.png" alt="Let op!" onmouseover="Tip(\'Wachtwoorden komen niet overeen\')" onmouseout="UnTip()" />';
      setfocus(valfield2);

return false;
}
}

