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

function checkRequired (validateElement, errorElementId)   // element to validate and id of element to receive error msg
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server

  var elem = document.getElementById(errorElementId);
  if (!elem.firstChild)
    return true;  // not available on this browser 

  if (elem.firstChild.nodeType != node_text)
    return true;  // ifld is wrong type of node  

  if (emptyString.test(validateElement.value)) {
      msg (errorElementId, "error", "ERROR: Required");  
      setfocus(validateElement);
      return false;
  }

  return true;

}

function msg(errorElementId,     // 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 displaymessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

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

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

function setFocusDelayed()
{
  global_validateElement.focus()
}

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