/********************************************************************\
*	util.js
*	utility javascript function 
\********************************************************************/


//	blankSpace represents any string consisting of all white space
//	If an exec used on this string it will return null if the param passed isn't all whitespace
var blankSpace = /^\s*$/;

//	returns true if the string passed in the parameter is made up entirely of white space
function isBlank (str)
{	
    if (blankSpace.exec (str) == null)
    {	
	return false;
    }
    else
    {	
   	return true;
    }
} // end isBlank



function checkEnter(event)
{
    var NS4 = (document.layers) ? true : false;

    var code = 0;

    if (NS4)
        code = event.which;
    else
        code = event.keyCode;
    if (code==13)
        document.searchForm.submit();
}




/*
 * function to pop up a window
 * param winURL		the URL that will be requested in the new window
 * param winName	name of the new window
 * param winAttr	attributes of the new window 
 *                      (i.e. "toolbar=no,resizable=no,scrollbars=no,status=yes,width=540,height=300" )
 */
function myPopUp(winURL, winName, winAttr)
{
	window.open(winURL, winName, winAttr);
}


/*
 * Open a new window using on form field as the URL
 * param formField      the form field we get the URL from
 * param fieldLabel     the lable of the text field that will show in error message if any
 * param winAttr        attributes of the new window
 *                      (i.e. "toolbar=no,resizable=no,scrollbars=no,status=yes,width=540,height=300" )
 */
function newWindowFromOneFieldURL(formField, fieldLabel, winAttr) {

    var URL = formField.value;
    if (URL == null || isBlank(URL)) {
         alert('Please enter a value for the "' + fieldLabel +'" field.');
         formField.focus();
    } else {

         myPopUp(URL, 'newWindow', winAttr);

    }

}


/*
 * Enter some text into a text field
 * param formField      the form field you want to enter text to
 * param addedText      the text added to the form field
 */
function addToTextField(formField, addedText) {

    formField.value = addedText;
    formField.focus();

}


/*******************************\
* HTML form validation
\*******************************/

/* 
 * check to see whether an e-mail address format is correct or not
 * param email	the e-mail address to check
 * return "true" if the e-mail format is correct
 *        "false" if the e-mail format is NOT correct
 */
function isEmail(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0) {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1)) {
        result = true;
    }
  }
  return result;
}



/*
 * validate a "text field" for e-mail address in HTML form
 * param formField	the name of the text field in the form
 * param fieldLable     the lable of the text field that will show in error message if any.
 * param required	"true" if this is a required field, "false" if this is not a required field.
 * return "true" if text in this "text field" is valid
 *        "false" if text in this "text field" is invalid
 */
function validEmail(formField,fieldLabel,required)
{
  var result = true;
  
  if (required) { // if the email is a "required" field, check empty
    validRequired(formField,fieldLabel);
  }

  if (formField.value != "") {// check the email if it is not empty
    if (formField.value.length < 3 || !isEmail(formField.value) ) {
      alert('Please enter a complete email address in the form: yourname@yourdomain.com for the"' + fieldLabel +'" field.');
      formField.focus();
      result = false;
    }
  } 
  return result;

}





/*
 * validate a "text field" for "required field" in HTML form 
 * param formField      the name of the text field in the form
 * param fieldLable     the lable of the text field that will show in error message if any.
 * return "true" if the form field pass the required field check
 *        "false" if text is required but missing in the field 
 */
function validateRequired(formField,fieldLabel)
{
  var result = true;

  if (formField.value == null || isBlank(formField.value))
  {
    alert('Please enter a value for the "' + fieldLabel +'" field.');
    formField.focus();
    result = false;
  }

  return result;
}


function validNumber(formField,fieldLabel)
{

  var result = true;

  if (isNaN(formField.value)) 
  {
    alert('Please enter a numeric value for the"' + fieldLabel +'" field.'); 
    formField.focus();
    result = false;
  }
  return result;
}


