var nbsp = 160;	
var node_text = 3;
var emptyString = /^\s*$/ ;
var global_valfield;
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
}
--------------------------------------------

var proceed = 2;  

function common_check    (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", "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;
}


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

  var tfld = trim(valfield.value);
  var age_re =/^0?\.?[0-9]{1,2}$/
  if (!age_re.test(tfld)) {
    msg (infofield, "error", "ERROR: not a valid age");
    setfocus(valfield);
    return false;
  }

  if (tfld>=100) {
    msg (infofield, "error", "ERROR: Our maximum age is 99; please use 99 for anyone 100 or older.");
    setfocus(valfield);
    return false;
  }
}

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

  var tfld = trim(valfield.value);
  
  var lbs_re =/^[0-9]{1,3}(.[0-9])?$/
  if (!lbs_re.test(tfld)) {
    msg (infofield, "error", "ERROR: not a valid weight.");
    setfocus(valfield);
    return false;
  }

  if (tfld>699) { msg (infofield, "error", "ERROR: sorry, maximum weight for this chart is 699 lbs.");
    setfocus(valfield);
    return false;
  } else {
  	msg (infofield, "error", "")
  }
    if (tfld<4) { msg (infofield, "error", "ERROR: sorry, minimum weight for this chart is 4 lbs.");
    setfocus(valfield);
    return false;
  } else {
  	msg (infofield, "error", "")
  }
}
function validate_kg    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  var stat = common_check (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);
  
  var kg_re =/^[0-9]{2,3}.?[0-9]?$/
  if (!kg_re.test(tfld)) {
    msg (infofield, "error", "ERROR: not a valid weight.");
    setfocus(valfield);
    return false;
  }

  if (tfld>499) { msg (infofield, "error", "ERROR: sorry, maximum weight for this chart is 499 kg.");
    setfocus(valfield);
    return false;
  } else {
  	msg (infofield, "error", "")
  }
    if (tfld<25) { msg (infofield, "error", "ERROR: sorry, minimum weight for this chart is 25kg.");
    setfocus(valfield);
    return false;
  } else {
  	msg (infofield, "error", "")
  }
}