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
var proceed = 2; 
 
/**
 * Displays warn/error mesage in DOM element
 * @param fld the id of the element to display the message in
 * @param msgtype The class to give the element ie warn or error
 * @param message The string message to dislplay
 */
function msg(fld,msgtype,message) 
{
	var elem = g_el(fld);
	
	if (!emptyString.test(message)) 
	{
		elem.innerHTML = message; 
	}		 	
	elem.className = msgtype;   // set the CSS class to adjust appearance of message	
}

/**
 * Validate that a radio button has been selected
 * @param valfield the element to be validated
 * @param infofield The id of element to receive info/error msg
 * @return True if a radio button has been selected
 */
function validateRadioOption(valfield,infofield) 
{
	for (var i = 0; i < valfield.length; i++)
	{
		if(valfield[i].checked)
		{
			msg (infofield, "fbpg", "");  
			return true;
		}
	}
	
	msg (infofield, "error fbpg", "");	
	return false;	
}

/**
 * Validate that a option has been selected
 * @param valfield the element to be validated
 * @param infofield The id of element to receive info/error msg
 * @return True if a state has been selected
 */
function validateSelectOption(valfield,infofield) 
{	
	if (valfield.value == "")
	{
		msg (infofield, "error", "Error: please make your selection");
		setfocus(valfield);
		return false;
	}
		msg (infofield, "warn", "");  
		return true;
}

/**
 * Validate that a state has been selected
 * @param valfield the element to be validated
 * @param infofield The id of element to receive info/error msg
 * @return True if a state has been selected
 */
function validateZip(valfield,infofield) 
{
	var stat = commonCheck (valfield, infofield, true);
	if (stat != proceed) return stat;

	var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
	var zip = /^\+?[0-9 -]+[0-9]$/  ;
	if (!zip.test(tfld)) 
	{
		msg (infofield, "error", "Error: not a valid zip code.");
		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<5) 
	{
		msg (infofield, "error", "Error: " + numdigits + " digits - too short");
		setfocus(valfield);
		return false;
	}
	if ((numdigits>5 && numdigits<9) || numdigits>9)
		msg (infofield, "warn", numdigits + " digits - check if correct");
	else 
	{
		msg (infofield, "warn", "");
	}
	return true;	
}


/**
 * Standard check routines for all types of validation. (Check for older browers, check for empty fields)
 * @param valfield the element to be validated
 * @param infofield The id of element to receive info/error msg
 * @param required True if required
 * @return True if validation passed false if validation failed
 */
function commonCheck(valfield,infofield,required)   
{
	if (!g_el) 
	return true;  // not available on this browser - leave validation to the server
	
	var elem = g_el(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)) //The test() method checks if a pattern exists within a string, and returns true if so, and false()
	{
		if (required) 
		{
		  msg (infofield, "error", "Error: required");  
		  setfocus(valfield);
		  return false;
		}
	}
	return proceed;
}

/**
 * Validate that something has been entered in to the input box
 * @param valfield the element to be validated
 * @param infofield The id of element to receive info/error msg
 * @return True if something has been entered into the input box
 */
function validatePresent(valfield,infofield) 
{
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;

  msg (infofield, "warn", "");  
  return true;
}

/**
 * Validate the email address
 * @param valfield the element to be validated
 * @param infofield The id of element to receive info/error msg
 * @param required True if required
 * @return True if the email is valid or if the function could not be excuted bacuas of an old browser.
 */
function validateEmail(valfield,infofield,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", "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;
}

/**
 * Validate the telephone number
 * @param valfield the element to be validated
 * @param infofield The id of element to receive info/error msg
 * @param required True if required
 * @return True if the telephone number is valid or if the function could not be excuted bacuas of an old browser.
 */
function validateTelnr(valfield,infofield,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.");
		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;
}

/**
 * Replacement method for document.getElementById(elementId).
 * @param e The id of the element to retrieve.
 * @return A DOM element corresponding to the provided id, or null if that object does not exist.
 */
function g_el(e)
{
    return document.getElementById(e);
}

/**
 * Trim leading/trailing whitespace off string
 * @param str The string to Trim leading/trailing whitespace off
 * @return String that is trimed. 
 */
 function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

/**
 * Delayed set focus function
 * @param valfield The element to set the focus to.
 */
function setfocus(valfield)
{
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}

/**
 * Delayed set focus function setting to get around IE bug
 */
function setFocusDelayed()
{
  global_valfield.focus();
}
