var defaultEmptyOK = false;
/* Function to validate field value on onBlur() event */
function checkField(obj)
{
 if(obj.value == "")
 {
  return false;
 } else {
  return true;
 } 
}

function formValidationElement(objForm, strProperName, objFieldType) {
 /***************** SETUP CORE PROPERTIES ************************/
 
 /* Properties in need of error handling */
 
 this.objForm = objForm; 
 this.fieldType = objFieldType;
 
 /* End of properties in need of error handling */
 
 if (strProperName == '') {
  this.properName = objForm.name.findReplace('_', ' ');
 } else {
  this.properName = strProperName.findReplace('_', ' ');
 }
 this.validationPrompt = ''; //Default state
 this.validated = false; //Default state
 this.hasFocusMethod = false; //Default state
 
 /****************** END OF CORE PROPERTY SETUPS *****************/
 
 /****************************************************************
   BEGIN Error handling block for this object
 *****************************************************************/
 var errString;
 var astrType = new Array('textbox', 'date', 'zip', 'decimal', 'number', 'email', 'radio_checkbox', 'phone');
 var blnErr = false; // Default state
 if (arguments.length != 3) {
  blnErr = true;
  errString = "Error: Calls to the formValidation object must contain 3 arguments!\n";
  errString += "USAGE: finalNewCheck(new formValidationElement(objForm, strProperName, objFieldType);"
  errString += "\nDESCRIPTION:\n\tobjForm <form object>\n\t\tExample: document.forms[0].First_Name\n";
  errString += "\n\tstrProperName <proper name of validation element>\n\t\tExample: 'First Name'\n";
  errString += "\n\tobjFieldType <element type>\n\t\tExample: 'textbox'";
 }
 if (validateObjType(astrType, objFieldType)) {
  blnErr = true;
  errString = "Error: Invalid element type!\n";
  errString += "VALID ELEMENT TYPES: \n";
  for (var i = 0; i < astrType.length; i++) {
   errString += "\t" + astrType[i] + "\n";
  }
 }
 if (!(typeof objForm == "object")) {
  blnErr = true;
  errString = "Error: Invalid object type in objForm argument !\n";
  errString += "Only form elements should be placed into the objForm argument.";
 }
 /****************************************************************
    END Error handling block for this object
 *****************************************************************/
 
 if (blnErr) {
  /****************************************************************
     IF ERROR OCCURRED: stop processing validation
  *****************************************************************/
  this.validationPrompt = errString + "\nValidation will not continue.";
 } else {
  /****************************************************************
        IF NO ERROR OCCURRED: continue processing validation
  *****************************************************************/
  
  switch (this.fieldType) {
   case 'email':
    if (checkEmail(this.objForm)) {
     this.validated = true;
    } else {
     this.validated = false;
     this.validationPrompt = "Email address must be provided in the correct format to proceed!";
     this.objForm.value = '';
    }
    this.hasFocusMethod = true;
    break;
   case 'radio_checkbox':
    this.validated = isOneSelected(this.objForm);
    if (!this.validated) {
     this.validationPrompt = "The field " + this.properName + " must be checked!";
    }
    this.hasFocusMethod = false;
    break;
   case 'select':
    this.validated = isOptionSelected(this.objForm);
    if (!this.validated) {
     this.validationPrompt = "The field " + this.properName + " must have a value selected!";
    }
    this.hasFocusMethod = true;
    break;
   case 'phone':
    this.validated = validPhone(this.objForm);
    if (!this.validated) {
     this.validationPrompt = this.properName + " must include numbers, dots(.) or dashes(-)!"
    }
    this.hasFocusMethod = true;
    break;
   case 'number':
    this.validated = isNumeric(this.objForm);
    if (!this.validated) {
     this.validationPrompt = this.properName + " must include numbers only!"
    }
    this.hasFocusMethod = true;
    break;
   case 'decimal':
    this.validated = isDouble(this.objForm);
    if (!this.validated) {
     this.validationPrompt = this.properName + " must include decimal numbers only!"
    }
    this.hasFocusMethod = true;
    break;
   case 'zip':
    this.validated = validZipCode(this.objForm);
    if (!this.validated) {
     this.validationPrompt = this.properName + " must include decimal numbers only!"
    }
    this.hasFocusMethod = true;
    break;
   case 'date':
    this.validated = validDate(this.objForm);
    if (!this.validated) {
     this.validationPrompt = this.properName + " must include numbers, slashes(/\) or dashes(-)!"
    }
    this.hasFocusMethod = true;
    break;
   default: //'textbox' type is the default.
    this.validated = checkField(this.objForm);
    if (!this.validated) {
     this.validationPrompt = "The field " + this.properName + " cannot be blank!";
    }
    this.hasFocusMethod = true;
  }
 }
 
}
function validateObjType(astrValidTypes, strType) {
 for (var i = 0; i < astrValidTypes; i++) {
  if (astrValidTypes[i] == strType || strType == '') {
   return true;
  }
 }
 return false;
}
function finalNewCheck(aobjValidation) { 
// This function takes a dynamic number of formValidationElement arguments
// USAGE: finalNewCheck(new formValidationElement(<form object>, <proper name of validation element>, <element type>));
// VALID TYPES: 'textbox', 'email'
 for (var i = 0; i < aobjValidation.length; i++) {
  if (!aobjValidation[i].validated) {
   alert(aobjValidation[i].validationPrompt);
   if (aobjValidation[i].hasFocusMethod) {
    aobjValidation[i].objForm.focus();
    aobjValidation[i].objForm.select();
   }
   return aobjValidation[i].validated;
   
  }
 }
 // If all aobjValidation have been validated return true;
 return true;  
}
function isOneSelected(oSelect) {
 var bReturn = false;
 for (var j = 0; j < oSelect.length; j++) {
  if (oSelect[j].checked) {
   
   bReturn = true;
   break;
  }
    
 }
 return bReturn;
}
function isOptionSelected(oOption) {
 if (oOption.selectedIndex > 0) {
  return true;
 } else {
  return false;
 }
}
function unloadForm(oForm, aobjFormValidation) {
 if (finalNewCheck(aobjFormValidation)) {
  oForm.submit();
 }
}
function RequiredFieldText(obj)
{
 window.status="The " + obj.name + " field is required.";
}
// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s)
{   var i;
     var whitespace = " \t\n\r"; 
    // Is s empty?
    if (isEmpty(s)) return true;
    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    // All characters are whitespace.
    return true;
}
function isEmail(s){
if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;
    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }
    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }
    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}
function checkEmail(sEmail){
 if (!isEmail(sEmail.value)) {
  return false
 } else {
  return true;
 }
}
function validPhone(s) {
 var checkOK = "0123456789-.";
 var checkStr = s.value;
 var allValid = (checkStr.length == 0) ? false : true;
 for (i = 0;  i < checkStr.length;  i++)
 {
  ch = checkStr.charAt(i);
  for (j = 0;  j < checkOK.length;  j++) { 
   if (ch == checkOK.charAt(j)) {
    break;
   } 
  }
  if (j == checkOK.length)
  {
   allValid = false;
   break;
  }
 }
 return allValid; //'allValid;
}
function validZipCode(s) {
 var checkOK = "0123456789-";
 var checkStr = s.value;
 var allValid = (checkStr.length == 0) ? false : true;
 for (i = 0;  i < checkStr.length;  i++)
 {
  ch = checkStr.charAt(i);
  for (j = 0;  j < checkOK.length;  j++) {
   if (ch == checkOK.charAt(j)) {
    break;
   } 
  }
  if (j == checkOK.length)
  {
   allValid = false;
   break;
  }
 }
 return allValid; //'allValid;
}
function isNumeric(s) {
 var checkOK = "0123456789";
 var checkStr = s.value;
 var allValid = (checkStr.length == 0) ? false : true;
 for (i = 0;  i < checkStr.length;  i++)
 {
  ch = checkStr.charAt(i);
  for (j = 0;  j < checkOK.length;  j++) { 
   if (ch == checkOK.charAt(j)) {
    break;
   } 
  }
  if (j == checkOK.length)
  {
   allValid = false;
   break;
  }
 }
 return allValid; //'allValid;
}
function isDouble(s) {
 var checkOK = "0123456789.";
 var checkStr = s.value;
 var allValid = (checkStr.length == 0) ? false : true;
 for (i = 0;  i < checkStr.length;  i++)
 {
  ch = checkStr.charAt(i);
  for (j = 0;  j < checkOK.length;  j++) { 
   if (ch == checkOK.charAt(j)) {
    break;
   } 
  }
  if (j == checkOK.length)
  {
   allValid = false;
   break;
  }
 }
 return allValid; //'allValid;
}
function validDate(s) {
 var checkOK = "0123456789\\\/.-";
 var checkStr = s.value;
 var allValid = (checkStr.length == 0) ? false : true;
 for (i = 0;  i < checkStr.length;  i++)
 {
  ch = checkStr.charAt(i);
  for (j = 0;  j < checkOK.length;  j++) { 
   if (ch == checkOK.charAt(j)) {
    break;
   } 
  }
  if (j == checkOK.length)
  {
   allValid = false;
   break;
  }
 }
 return allValid; //'allValid;
}

function findReplace(strFindVal, strReplaceVal) {
 if (arguments.length != 2) {
  return this;
 } else {
  var intFind = this.indexOf(strFindVal);
  var strRight = this;
  var strTemp = '';
  var strLeft = '';
  while (intFind > -1) {
  if (intFind > 0) {
     strTemp = strRight.substring(0, intFind);
  }
  strLeft = strRight.substring((intFind + 1), strRight.length);
  strLeft =  strTemp + strReplaceVal + strLeft;
  strTemp = '';
  strRight = strLeft;
  intFind = strLeft.indexOf(strFindVal);
   }
   return strRight;
 }
}
String.prototype.findReplace = findReplace;
 