// ----------------------------------------------------------------------

// Javascript form validation routines.

// Author: Stephen Poley

//

// Simple routines to quickly pick up obvious typos.

// All validation routines return true if executed by an older browser:

// in this case validation must be left to the server.

//

// Update Jun 2005: discovered that reason IE wasn't setting focus was

// due to an IE timing bug. Added 0.1 sec delay to fix.

//

// Update Oct 2005: minor tidy-up: unused parameter removed

//

// Update Jun 2006: minor improvements to variable names and layout

// ----------------------------------------------------------------------



var nbsp = 160;	

var node_text = 3;	

var emptyString = /^\s*$/ ;

var global_valfield;	

var proceed;	





function trim(str)

{

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

}







function setFocusDelayed()

{

  global_valfield.focus();

}



function setfocus(valfield)

{

  global_valfield = valfield;

  setTimeout( 'setFocusDelayed()', 100 );

}







function msg(fld, msgtype, message) 

{ 

  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;   

var proceed = 2;  

}

function commonCheck    (valfield, infofield, required)

{

  if (!document.getElementById) 

    return true;  

  var elem = document.getElementById(infofield);

  if (!elem.firstChild) return true;  

  if (elem.firstChild.nodeType != node_text) return true; 



  if (emptyString.test(valfield.value)) {

    if (required) {

      msg (infofield, "error", "错误（*）");  

      setfocus(valfield);

      return false;

    }

    else {

      msg (infofield, "warn", "");   // OK

      return true;  

    }

  }

  return proceed;

}

function validatePresent(valfield, infofield ) 

{

  var stat = commonCheck (valfield, infofield, true);

  if (stat != proceed) return stat;



  msg (infofield, "warn", "");  

  return true;

}



function validateEmail  (valfield, infofield, required)   

{

  var stat = commonCheck (valfield, infofield, required);

  if (stat != proceed) return stat;



  var tfld = trim(valfield.value);  

  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;

  if (!email.test(tfld)) {

    msg (infofield, "error", "ERROR: not a valid e-mail address");

    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", "Unusual e-mail address - check if correct");

  else

    msg (infofield, "warn", "");

  return true;

}



function validateTelnr  (valfield, infofield, required)   

{

  var stat = commonCheck (valfield, infofield, required);

  if (stat != proceed) return stat;



  var tfld = trim(valfield.value);  

  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;

}

  function validateOnSubmit() {

    var elem;

    var errs=0;

    if (!validateEmail  (document.forms.demo.email, 'inf_email', true)) errs += 1;  

    if (!validateEmail  (document.forms.demo.email2, 'inf_email2', true)) errs += 1;  

    if (!validatePresent (document.forms.demo.from, 'inf_from'))  errs += 1; 

    if (!validatePresent (document.forms.demo.to, 'inf_to'))  errs += 1; 

    

    if (errs>1)  alert('There are fields which need correction before sending');

    if (errs==1) alert('There is a field which needs correction before sending');



    return (errs==0);

  }
