// Function:  textCheck
//
// Modification Log
//
// 02/18/2002  BMO  Trim the field before making the check

function textCheck(field, fieldname, required, confirmation)
 {
   val = false;

   field.value = AllTrim(field.value, " ");

   // Check for No Entry
   if (field.value == "")
    {
      if (required == true)
       {
         alert("Nothing has been entered in the " + fieldname +
               " field.  Please correct it before continuing.");
         field.focus();
       }
      else
       {
         if (confirmation == true)
          {
            if (confirm("Nothing has been entered in the " + fieldname +
                " field.  Press OK to proceed, or CANCEL to correct it before continuing."))
             {
               val = true;
             }
          }
         else
          {
            val = true;
          }
       }
    }
   else
    {
      val = true;
    }

  return val;

 }

// Function:  dateCheck
//
// Modification Log
//
// 02/18/2002  BMO  Trim the field before making the check

function dateCheck(field, fieldname, required, confirmation)
 {
   val = false;

   field.value = AllTrim(field.value, " ");
   
   // Check for No Entry
   if (field.value == "")
    {
      if (required == true)
       {
         alert ("Nothing has been entered in the " + fieldname +
             " field.  Please correct it before continuing.");
         val = false;
       }
      else
       {
         if (confirmation == true)
          {
            if (confirm("Nothing has been entered in the " + fieldname +
                " field.  Press OK to proceed, or CANCEL to correct it before continuing."))
             {
               val = true;
             }
          }
         else
          {
            val = true;
          }
       }
    }
   else if ((field.value.length > 10) || (field.value.length < 6))
    {
      alert ("The format of the " + fieldname +
          " field is not correct.  The correct format is mm/dd/yyyy.  Please correct it before continuing.");
      val = false;
    }
   else
    {
      array = field.value.split (/[ \.\-\/]/);

      if (array[2] < 100)     // Handle 2 digit years
       {
         if (array[2] == 99)
          {
            array[2] += 1900;
          }
         else
          {
            array[2] += 2000;
          }
       }

      if ((isNaN(array[0])) || (array[0] < 1) || (array[0] > 12) ||    // Bad Month
          (isNaN(array[1])) || (array[1] < 1) || (array[1] > 31) ||    // Bad Day
          (isNaN(array[2])) || (array[2] < 1999))                      // Bad Year
       {
         alert (array[0] + "/" + array[1] + "/" + array[2] + " does not appear to be a valid date for the "+
                fieldname + " field.  "+
                "The correct format is mm/dd/yyyy.  Please correct it before continuing.");
       }
      else
       {
         val = true;
       }
    }
  
  return val;

 }

// Function:  zipCheck
//
// Modification Log
//
// 02/18/2002  BMO  Trim the field before making the check

function zipCheck(field, fieldname, required, confirmation)
{
   val = false;
   var tmp;

   field.value = AllTrim(field.value, " ");
   
   // Check for No Entry
   if (field.value == "")
    {
      if (required > 0)
       {
         alert("Nothing has been entered in the " + fieldname +
               " field.  Please correct it before continuing.");
         field.focus();
       }
      else
       {
         if (confirmation == true)
          {
            if (confirm("Nothing has been entered in the " + fieldname +
                " field.  Press OK to proceed, or CANCEL to correct it before continuing."))
             {
               val = true;
             }
          }
         else
          {
            val = true;
          }
       }
    }
   else 
   {
     tmp = field.value.replace("-","");
     if (isNaN(tmp))
      {
        alert(fieldname + " must contain only numbers and an optional '-'.  Please correct it before continuing.");
        field.focus();
      }
     else if (tmp.length != 5 && tmp.length != 9)
      {
        alert(fieldname + " must be either standard Zip or Zip+4 format.  Please correct it before continuing.");
        field.focus();
      }
     else
      {
        val = true;
      }
    }

  return val;
}

// Function:  numberCheck
//
// Modification Log
//
// 02/18/2002  BMO  Trim the field before making the check

function numberCheck(field, fieldname, required, confirmation)
{
   val = false;

   field.value = AllTrim(field.value, " ");
   
   // Check for No Entry
   if (field.value == "")
    {
      if (required > 0)
       {
         alert("Nothing has been entered in the " + fieldname +
               " field.  Please correct it before continuing.");
       }
      else
       {
         if (confirmation == true)
          {
            if (confirm("Nothing has been entered in the " + fieldname +
                " field.  Press OK to proceed, or CANCEL to correct it before continuing."))
             {
               val = true;
             }
          }
         else
          {
            val = true;
          }
       }
    }
   else if (isNaN(field.value))
    {
      alert(fieldname + " must be numeric.  Please correct it before continuing.");
    }
   else if (field.value.length < required)
    {
      alert(fieldname + " must be at least " + required + " digits long.  Please correct it before continuing.");
    }
   else
    {
      val = true;
    }

  return val;
}

// Function:  dollarCheck
//
// Modification Log
//
// 02/18/2002  BMO  Trim the field before making the check

function dollarCheck(field, fieldname, required, confirmation)
{
   val = false;

   field.value = AllTrim(field.value, " ");
   
   // Check for No Entry
   if (field.value == "")
    {
      if (required == true)
       {
         alert("Nothing has been entered in the " + fieldname +
               " field.  Please correct it before continuing.");
       }
      else
       {
         if (confirmation == true)
          {
            if (confirm("Nothing has been entered in the " + fieldname +
                " field.  Press OK to proceed, or CANCEL to correct it before continuing."))
             {
               val = true;
             }
          }
         else
          {
            val = true;
          }
       }
    }
   else
    {
      newfield = field.value.replace(/[ ,\$]/g, "");
      array = newfield.split(/\./);

      if (isNaN(array[0]))
       {
         alert(fieldname + " must be numeric.  Please correct it before continuing.");
       }
      else
       {
         if (array[1])
          {
            if (isNaN(array[1]))
             {
               alert(fieldname + " must be numeric.  Please correct it before continuing.");
             }
            else
             {
               if ((array[1] > 99) || (array[1] < 0))
                {
                  alert(fieldname + " must be a valid dollar amount.  Please correct it before continuing.");
		}
               else
                {
                  val = true;
                }
             }
          }
         else
          {
            val = true;
          }
       }
    }

  return val;
}

// Function:  emailCheck
//
// Modification Log
//
// 01/27/2000  BMG  New algorithm created to handle vvv.www@xxx.yyy.zzz addresses
//
// 10/11/2001  HES  Made change so error doesn't occur if there isn't an @
//
// 02/18/2002  BMO  Trim the field before making the check

function emailCheck(field, fieldname, required, confirmation)
 {
   val = false;

   field.value = AllTrim(field.value, " ");

   // Check for No Entry
   if (field.value == "")
    {
      if (required == true)
       {
         alert("Nothing has been entered in the " + fieldname +
               " field.  Please correct it before continuing.");
       }
      else
       {
         if (confirmation == true)
          {
            if (confirm("Nothing has been entered in the " + fieldname +
                " field.  Press OK to proceed, or CANCEL to correct it before continuing."))
             {
               val = true;
             }
          }
         else
          {
            val = true;
          }
       }
    }
   else
    {
      if (field.value.indexOf("@") != -1)
       {
         var array = field.value.split("@");


         if ((array[0].length < 1) ||  // no userid
             (array[1].length < 1) ||  // no domain
             (array.length > 2))       // more than one @ sign
          {
            alert("Your email address does not appear to be correct.  The correct "+
                  "format is xxxxx@yyyyy.zzz.");
          }
         else
          {
            var domarray = array[1].split(".");

            if (domarray.length < 2)
             {
               alert("The domain name of your email address does not appear to be correct.  The correct "+
                     "format is xxxxx@yyyyy.zzz.");
             }
            else
             {
               val = true;

               for (var loop = 0; loop < domarray.length; loop++)
                {
                  if (domarray[loop].length < 1)
                   {
                     val = false;
                   }
                }

               if (val == false)
                {
                  alert("The domain name of your email address does not appear to be correct.  The correct "+
                        "format is xxxxx@yyyyy.zzz.");
                }
             }
          }
       }
      else
       {
         alert("Your email address does not appear to be correct.  The correct " +
               "format is xxxxx@yyyyy.zzz.");
       }
    }

  return val;
}


// Function:  phoneCheck
//
// Modification Log
//
// 02/18/2002  BMO  Trim the field before making the check

function phoneCheck(field, fieldname, required, confirmation)
{
   val = false;

   field.value = AllTrim(field.value, " ");
   
   // Check for No Entry
   if (field.value == "")
    {
      if (required == true)
       {
         alert("Nothing has been entered in the " + fieldname +
               " field.  Please correct it before continuing.");
       }
      else
       {
         if (confirmation == true)
          {
            if (confirm("Nothing has been entered in the " + fieldname +
                " field.  Press OK to proceed, or CANCEL to correct it before continuing."))
             {
               val = true;
             }
          }
         else
          {
            val = true;
          }
       }
    }
   else
    {
      newfield = field.value.replace(/[ \-()\.]/g, "");

      if (isNaN(newfield))
       {
         alert(fieldname + " is not correct.  Please use the (xxx) xxx-xxxx format.");
       }
      else if (newfield.length < 10)
       {
         alert(fieldname + " is not correct.  Please use the (xxx) xxx-xxxx format .");
       }
      else
       {
         val = true;
       }
    }

  return val;
}

function yearCheck(field, fieldname, required, confirmation)
{
   val = false;

   // Check for No Entry
   if (field.value == "")
    {
      if (required == true)
       {
         alert("Nothing has been entered in the " + fieldname +
               " field.  Please correct it before continuing.");
       }
      else
       {
         if (confirmation == true)
          {
            if (confirm("Nothing has been entered in the " + fieldname +
                " field.  Press OK to proceed, or CANCEL to correct it before continuing."))
             {
               val = true;
             }
          }
         else
          {
            val = true;
          }
       }
    }
   else
    {
      val = numberCheck(field, fieldname, 4, confirmation);
    }

  if (val == false)
   {
     field.focus();
     field.select();
   }

  return val;
}

function percentCheck(field, fieldname, required, confirmation)
{
   val = false;

   // Check for No Entry
   if (field.value == "")
    {
      if (required == true)
       {
         alert("Nothing has been entered in the " + fieldname +
               " field.  Please correct it before continuing.");
       }
      else
       {
         if (confirmation == true)
          {
            if (confirm("Nothing has been entered in the " + fieldname +
                " field.  Press OK to proceed, or CANCEL to correct it before continuing."))
             {
               val = true;
             }
          }
         else
          {
            val = true;
          }
       }
    }
   else
    {
      field.value = field.value.replace(/[ %]/g, "")
      val = numberCheck(field, fieldname, false, confirmation);
    }

  if (val == false)
   {
     field.focus();
     field.select();
   }

  return val;
}

function CompareDollar (d1, d2)
 {

   dollar1 = parseFloat(d1.value.replace(/[ ,\$]/g, ""));
   dollar2 = parseFloat(d2.value.replace(/[ ,\$]/g, ""));

   if (dollar1 > dollar2)
    {
      return 1;
    }
   else if (dollar1 == dollar2)
    {
      return 0;
    }
   else
    {
      return -1;
    }

 }

function LeftTrim(String,TrimChar)
{
 // **********************************************************
 // Placed in the public domain by Affordable Production Tools
 // April 27, 1998
 // Web site: http://www.aptools.com
 //
 // December 2, 1998 -- Modified to allow specification of
 // character to be trimmed.
 //
 // This function trims spaces (default) or the specified
 // character from the left of a string or form field.
 // **********************************************************
 String += ""         // Force argument to string.
 TrimChar += ""       // Force argument to string.
 if((TrimChar == "") || (!(TrimChar.length == 1)))
  TrimChar = " "
 if(String.length == 0)
  return(String)
 var Count = 0
 for(Count = 0;Count < String.length;Count++)
 {
  if(!(String.charAt(Count) == TrimChar))
   return(String.substring(Count,String.length))
 }
 return("")
}

function RightTrim(String,TrimChar)
{
 // **********************************************************
 // Placed in the public domain by Affordable Production Tools
 // April 27, 1998
 // Web site: http://www.aptools.com
 //
 // December 2, 1998 -- Modified to allow specification of
 // character to be trimmed.
 //
 // This function trims spaces (default) or the specified
 // character from the right of a string or form field.
 // **********************************************************
 String += ""        // Force argument to string.
 TrimChar += ""      // Force argument to string.
 if((TrimChar == "") || (!(TrimChar.length == 1)))
  TrimChar = " "
 if(String.length == 0)
  return(String)
 var Count = 0
 for(Count = String.length -1;Count >= 0;Count--)
 {
  if(!(String.charAt(Count) == TrimChar))
   return(String.substring(0,Count + 1))
 }
 return("")
}

function AllTrim(String,TrimChar)
{
 // **********************************************************
 // Placed in the public domain by Affordable Production Tools
 // April 27, 1998
 // Web site: http://www.aptools.com
 //
 // December 2, 1998 -- Modified to allow specification of
 // character to be trimmed.
 //
 // This function trims spaces (default) or the specified
 // character from the left and the right of a string or form field.
 //
 // Note that this functions uses two other library functions,
 // LeftTrim() and RightTrim().
 // **********************************************************
 String += ""        // Force argument to string.
 TrimChar += ""      // Force argument to string.
 if((TrimChar == "") || (!(TrimChar.length == 1)))
  TrimChar = " "
 return(RightTrim(LeftTrim(String,TrimChar),TrimChar))
}
