function fun_js_ValidEmailAddress(email)
{
  AtPos = email.indexOf("@");
  StopPos = email.lastIndexOf(".");
  intNoOfErrors = 0;
  
  if (AtPos == -1 || StopPos == -1) 
  {
    intNoOfErrors = intNoOfErrors + 1;
  }
  else if (StopPos < AtPos) 
  {
    intNoOfErrors = intNoOfErrors + 1;
  }
  else if (StopPos - AtPos == 1) 
  {
    intNoOfErrors = intNoOfErrors + 1;
  } 
  return intNoOfErrors;
}


function fun_js_GenericFormValidation(thisform, strHTMLColorCode)
{
  var intErrorCounter = 0
  var strErrorMessage = ""
  
  for (i=0; i< thisform.elements.length; i++)
  {
    var frmElementType = thisform.elements[i].type;
    var frmElementName = thisform.elements[i].name;
    var frmElementValue = thisform.elements[i].value;
    var frmElement = thisform.elements[i]
    
    if (frmElementType=="text" || frmElementType=="textarea" || frmElementType =="select-one")
    {
      if (frmElementName.indexOf("_(") > -1)
      {
        var arrValidationOptions = frmElementName.substring(frmElementName.indexOf("_(")).replace("_(", "").replace(")","").split("|");
        
        for (intValidationOption = 0; intValidationOption < arrValidationOptions.length; intValidationOption++)
        {
          if (arrValidationOptions[intValidationOption] == "$")
          {
            if (frmElement.length == 0 || frmElement.value==null || frmElement.value=="")
            {
              intErrorCounter = intErrorCounter + 1;
              strErrorMessage += "- " + "Please Enter a value into " + frmElementName.substring(0,frmElementName.indexOf("_(")) + " form field" + "\n";
              frmElement.style.background = strHTMLColorCode;                   
            }
            else
            {
              frmElement.style.background = 'white';
            }            
          }
          if (frmElementValue.length > 0 || frmElementValue != "")
          {
            if (arrValidationOptions[intValidationOption] == "@")
            {
              if (fun_js_ValidEmailAddress(frmElementValue) > 0)
              {
                intErrorCounter = intErrorCounter + 1;
                strErrorMessage += "- " + "Invalid Email Address Entered" + "\n";
                frmElement.style.background = strHTMLColorCode;           
              }
              else
              {
                frmElement.style.background = 'white';
              }
            } 
            if (arrValidationOptions[intValidationOption] == "#")
            {
              if ((!frmElementValue.match(/^([0-9])*$/)))
              {
                intErrorCounter = intErrorCounter + 1;
                strErrorMessage += "- " + "Invalid Number Enter Into " + frmElementName.substring(0,frmElementName.indexOf("_(")) + " form field" + "\n";
                frmElement.style.background = strHTMLColorCode;           
              }
              else
              {
                frmElement.style.background = 'white';
              }
            }
            if (arrValidationOptions[intValidationOption] == "D")
            {
              if (!frmElementValue.match(/^([0-9]{4})-([0-9]{2})-([0-9]{2})*$/))
              {
                intErrorCounter = intErrorCounter + 1;
                strErrorMessage += "- " + "Invalid Date Enter Into " + frmElementName.substring(0,frmElementName.indexOf("_(")) + " form field. Please Re-Format Date to be YYYY-MM-DD." + "\n";
                frmElement.style.background = strHTMLColorCode;           
              }
              else
              {
                frmElement.style.background = 'white';
              }
            }
            if (arrValidationOptions[intValidationOption].match(/^([0-9])*$/))
            {
               if (frmElementValue.length > arrValidationOptions[intValidationOption])
               {
                intErrorCounter = intErrorCounter + 1;
                strErrorMessage += "- " + "You Have Entered Too Many Characters into " + frmElementName.substring(0,frmElementName.indexOf("_(")) + " form field. Please ensure there are no more than " + arrValidationOptions[intValidationOption] + " Entered" + "\n";
                frmElement.style.background = strHTMLColorCode;           
              }
              else
              {
                frmElement.style.background = 'white';
              }
            }
                                  
          }

        }
      }
            
    }
  }
  if (intErrorCounter != 0)
  {
    alert("The following Error(s) have occured when submitting the form" + "\n\n" + strErrorMessage + "\n" + "Please correct these error(s) and re-submit.");
    return false;
  }

return true;

}
