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


// --------------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// --------------------------------------------

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


// --------------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// --------------------------------------------

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 );
}


// --------------------------------------------
//                  msg
// Display warn/error message in HTML element.
// commonCheck routine must have previously been called
// --------------------------------------------

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", "required");  
      setfocus(valfield);
      return false;
    }
    else {
      msg (infofield, "warn", "");   // OK
      return true;  
    }
  }
  return proceed;
}


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

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

  msg (infofield, "warn", "");  
  return true;
}


// --------------------------------------------
//            validateTelnr
// Validate telephone number
// Returns true if so (and also if could not be executed because of old browser)
// Permits spaces, hyphens, brackets and leading +
// --------------------------------------------

function validateTelnr  (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 telnr = /^\+?[0-9 ()-]+[0-9]$/  ;
  if (!telnr.test(tfld)) {
    msg (infofield, "error", "ERROR: not a valid telephone number. Characters permitted are digits, space ()- and leading +");
    setfocus(valfield);
    return false;
  }

  var numdigits = 0;
  for (var j=0; j<tfld.length; j++)
    if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;

  if (numdigits<6) {
    msg (infofield, "error", "ERROR: " + numdigits + " digits - too short");
    setfocus(valfield);
    return false;
  }

  if (numdigits>14)
    msg (infofield, "warn", numdigits + " digits - check if correct");
  else { 
    if (numdigits<10)
      msg (infofield, "warn", "Only " + numdigits + " digits - check if correct");
    else
      msg (infofield, "warn", "");
  }
  return true;
}


// 061219 --------------------------------------------
//               isvalid_name
// Validate if name is valid, regardless of country
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function isvalid_name  (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 regex = /^[a-zA-Z0-9/ \-]{3,60}$/ ;
  
  if (!regex.test(tfld)) {
    msg (infofield, "error", "Your name contains special characters unrecognised by our system");
    setfocus(valfield);
    return false;
  } else
    msg (infofield, "warn", ""); // OK

  return true;
}


// 071206 --------------------------------------------
//               isvalid_email
// Validate if email is valid, regardless of country
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function isvalid_email  (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 regex = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$/ ;
  
  if (!regex.test(tfld)) {
    msg (infofield, "error", "E-mail is invalid");
    setfocus(valfield);
    return false;
  } else
    msg (infofield, "warn", ""); // OK

  return true;
}


// 061219 --------------------------------------------
//               isvalid_mobileprovider
// Validate if mobileprovider is valid, country specific
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

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

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var regex;
  
  if (country == "SG")
    regex = /^((SingTel)|(StarHub)|(M1))$/
  else if (country == "MY")
    regex = /^((Digi)|(Celcom)|(Maxis))$/
  
  if (!regex.test(tfld)) {
    msg (infofield, "error", "Mobile service provider is invalid");
    setfocus(valfield);
    return false;
  } else
    msg (infofield, "warn", ""); // OK

  return true;
}


// 061219 --------------------------------------------
//               isvalid_mobilenumber
// Validate if mobilenumber is valid, country specific
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

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

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var regex;
  
  if (country == "SG")
    regex = /^(6|8|9)[0-9]{7}$/
  else if (country == "MY")
    regex = /^(01)[0-9]{8}$/
  
  if (!regex.test(tfld)) {
    msg (infofield, "error", "Mobile number is invalid");
    setfocus(valfield);
    return false;
  } else
    msg (infofield, "warn", ""); // OK

  return true;
}


// 071206 --------------------------------------------
//               isvalid_egoldamountbuy
// Validate if egoldamountbuy is valid, country specific
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

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

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var regex;
  
  if (country == "SG") {
    regex = /^[0-9]{1,5}([\.]{1}[0-9]{1,2})?$/

    if (!regex.test(tfld)) {
      msg (infofield, "error", "Amount is invalid");
      //msg (infofield, "error", "e-gold amount is invalid");
      setfocus(valfield);
      return false;
    } else
      msg (infofield, "warn", ""); // OK

  } else if (country == "MY") {
    regex = /^[0-9]{1,5}([\.]{1}[0-9]{1,2})?$/

    if (!regex.test(tfld)) {
      msg (infofield, "error", "Amount must contain .00 to .99 cents");
      //msg (infofield, "error", "e-gold amount must contain .00 to .99 cents");
      setfocus(valfield);
      return false;
    } else
      msg (infofield, "warn", ""); // OK

  };
  
  // check min / max
  var min = 10.00;
  var max = 50000.00;

  if (tfld<min) {
    msg (infofield, "error", "Amount must be more than US$10");
    //msg (infofield, "error", "e-gold amount must be more than US$10");
    setfocus(valfield);
    return false;
  }

  if (tfld>max) {
    msg (infofield, "error", "Amount must be less than US$20,000");
    //msg (infofield, "error", "e-gold amount must be less than US$20,000");
    setfocus(valfield);
    return false;
  }

  // if MY, check cents
  if ((country == "MY") && (!isNull(egoldaccountnumber))) {
    var eg2 = document.forms.orderform.egAccountNumber.value.substr(document.orderform.egAccountNumber.value.length-2);

    var dp = document.forms.orderform.egAmount.value.lastIndexOf('.');

    if (dp<0) {dp=0} else {dp+=1};
    dp = document.forms.orderform.egAmount.value.substr(dp);

    if (dp.length!=2) {
      msg (infofield, "error", "Amount must contain .00 to .99 cents");
      //msg (infofield, "error", "e-gold amount must contain .00 to .99 cents");
      setfocus(valfield);
      return false;
    } else if (dp!=eg2) {
      msg (infofield, "error", "Amount's cents must match last 2 digits of account number");
      //msg (infofield, "error", "e-gold amount's cents must match last 2 digits of e-gold account number");
      setfocus(valfield);
      return false;
    };
  };
    
  return true;
}


// 071206 --------------------------------------------
//               isvalid_egoldamountsell
// Validate if egoldamountsell is valid, regardless of country
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function isvalid_egoldamountsell  (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 regex = /^[0-9]{1,5}([\.]{1}[0-9]{1,2})?$/
  
  if (!regex.test(tfld)) {
    msg (infofield, "error", "Amount is invalid");
    //msg (infofield, "error", "e-gold amount is invalid");
    setfocus(valfield);
    return false;
  } else
    msg (infofield, "warn", ""); // OK

  // check min / max
  var min = 10.00;
  var max = 50000.00;

  if (tfld<min) {
    msg (infofield, "error", "Amount must be more than US$10");
    //msg (infofield, "error", "e-gold amount must be more than US$10");
    setfocus(valfield);
    return false;
  }

  if (tfld>max) {
    msg (infofield, "error", "Amount must be less than US$20,000");
    //msg (infofield, "error", "e-gold amount must be less than US$20,000");
    setfocus(valfield);
    return false;
  }


  return true;
}


// 071008 --------------------------------------------
//               isvalid_egoldaccountnumber
// Validate if egoldaccountnumber is valid, regardless of country
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function isvalid_egoldaccountnumber  (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 regex = /^[A-Za-z1-9]{1}[0-9]{2,7}$/ ;
  
  if (!regex.test(tfld)) {
    msg (infofield, "error", "Account number is invalid");
    //msg (infofield, "error", "e-gold account number is invalid");
    setfocus(valfield);
    return false;
  } else
    msg (infofield, "warn", ""); // OK

  return true;
}


// 070615 --------------------------------------------
//               isvalid_egoldaccountname
// Validate if egoldaccountname is valid, regardless of country
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function isvalid_egoldaccountname  (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 regex = /^.{2,60}$/ ;
  
  if (!regex.test(tfld)) {
    msg (infofield, "error", "Account name is invalid");
    //msg (infofield, "error", "e-gold account name is invalid");
    setfocus(valfield);
    return false;
  } else
    msg (infofield, "warn", ""); // OK

  return true;
}


// 061219 --------------------------------------------
//               isvalid_bankname
// Validate if bankname is valid, country specific
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

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

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var regex;
  
  if (country == "SG")
    regex = /^((DBS)|(POSB)|(UOB)|(OCBC))$/
  else if (country == "MY")
    regex = /^(Maybank)$/
  
  if (!regex.test(tfld)) {
    msg (infofield, "error", "Bank name is invalid");
    setfocus(valfield);
    return false;
  } else
    msg (infofield, "warn", ""); // OK

  return true;
}


// 061219 --------------------------------------------
//               isvalid_bankaccounttype
// Validate if bankaccounttype is valid, country specific
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

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

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var regex;
  
  if (country == "SG")
    regex = /^((DBS Savings Plus)|(DBS Current)|(DBS Autosave)|(DBS Corporate Plus)|(POSB Current)|(POSB Savings)|(UOB)|(OCBC))$/
  else if (country == "MY")
    regex = /^(Maybank)$/
  
  if (!regex.test(tfld)) {
    msg (infofield, "error", "Bank account type is invalid");
    setfocus(valfield);
    return false;
  } else
    msg (infofield, "warn", ""); // OK

  return true;
}


// 061228 --------------------------------------------
//               isvalid_bankaccountnumber
// Validate if bankaccountnumber is valid, country specific
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

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

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var regex;
  
  if (country == "SG")
    regex = /^[0-9/ \-]{9,14}$/
  else if (country == "MY")
    regex = /^[0-9]{12}$/
  
  if (!regex.test(tfld)) {
    msg (infofield, "error", "Bank account number is invalid");
    setfocus(valfield);
    return false;
  } else
    msg (infofield, "warn", ""); // OK

  return true;
}


// 061219 --------------------------------------------
//               isvalid_transfertime
// Validate if transfertime is valid, regardless of country
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function isvalid_transfertime  (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 regex = /(^([1-9]|1[0-2]){1}(\.[0-5][0-9])?((am)|(pm)){1}$)|(^$)/ ;
  
  if (!regex.test(tfld)) {
    msg (infofield, "error", "Preferred time of fund transfer is invalid");
    setfocus(valfield);
    return false;
  } else
    msg (infofield, "warn", ""); // OK

  return true;
}


// 061219 --------------------------------------------
//               isNull
// --------------------------------------------
function isNull(a) 
{
  return typeof a == 'object' && !a;
}

