//<!-- =================================================================== -->
// trims whitespace off text
function trim(theText)
{
   return theText.replace(/^\s+|\s+$/g, '')
   /*
      var theRegExp = /^\s+|\s+$/g;
      ^\s+  leading whitespace
      |     and
      \s+$  ending whitespace
   */
};

//<!-- =================================================================== -->
// checks textfield is answered
function answeredField(theTextField)
{
   if(!theTextField) // doesn't exist
   {
      return true;
   }
   else
   {
      if (theTextField.value.length == 0) // if theText does not exist
      {
         return false;
      }
      else // else theText exists
      {
         if (trim(theTextField.value).length ==0) // if text only contains blank space
         {
         return false;
         }
      }
      return true;
   }
}


//<!-- =================================================================== -->
// checks text for alphanumeric characters only
function validAlphaChars(theText, minChars, maxChars)
{
   if (theText.length == 0) // if theText does not exist
   {
      if (!minChars) // if no min, return true (not required)
      {
         return true;
      }
      else //  if min, return false (required, but no theText)
      {
         return false;
      }
   }
   else // else theText exists
   {
      if (!minChars) // if no min, set to 0
      {
         minChars = 0;
      }
      if (!maxChars) // if no max, set to length of text
      {
         maxChars = theText.length;
      }
   }

/*
   if (!minChars) // if no min, set to 0 (not required)
   {
      minChars = 0;
   }
   if (!maxChars) // if no max
   {
      if (theText.length != 0) // if theText value exists, set to length of text
      {
         maxChars = theText.length;
      }
      else // else if theText value does not exist
      {
         if (minChars == 0) // and field not required, return true
         {
            return true;
         }
         else // and field is required, return false
         {
            return false;
         }
      }
   }
*/

   pattern = "^[A-Za-z0-9]{"+minChars+","+maxChars+"}$";
   flags = "g";
   theRegExp = new RegExp(pattern, flags);
   /*
      var theRegExp = /^[A-Za-z0-9]{6,15}$/g;
      ^              begins with
      [A-Za-z0-9]    alphanumeric character
      {min,max}      min-max characters
      $              (and ends with)
   */

   if (theRegExp.test(trim(theText)) > 0)
   {
      return true;
   }
   else
   {
      return false;
   }
}

//<!-- =================================================================== -->
// checks textfield for alphanumeric characters only
function validAlphaCharsField(theTextField, minChars, maxChars)
{
   if(!theTextField) // doesn't exist
   {
      return true;
   }
   return validAlphaChars(theTextField.value, minChars, maxChars);
}

//<!-- =================================================================== -->
// checks text for alphanumeric characters and/or some symbols
function validAlphaCharSym(theText, minChars, maxChars)
{
   if (theText.length == 0) // if theText does not exist
   {
      if (!minChars) // if no min, return true (not required)
      {
         return true;
      }
      else //  if min, return false (required, but no theText)
      {
         return false;
      }
   }
   else // else theText exists
   {
      if (!minChars) // if no min, set to 0
      {
         minChars = 0;
      }
      if (!maxChars) // if no max, set to length of text
      {
         maxChars = theText.length;
      }
   }

   pattern = "^[A-Za-z0-9\s\\-\_\.\,\'\(\)\&\/\: ]{"+minChars+","+maxChars+"}$";
   flags = "g";
   theRegExp = new RegExp(pattern, flags);
   /*
      var theRegExp = /^[A-Za-z0-9\s\\-\_\.\,\'\(\)\&\/\:]{6,15}$/g;
      ^                             begins with
      [A-Za-z0-9                    alphanumeric character or
         \s\\-\_\.\,\'\(\)\&\/\:]         <space> - _ . , ' ( ) & / :
      {min,max}                     min-max characters
      $                             (and ends with)
   */

   if (theRegExp.test(trim(theText)) > 0)
   {
      return true;
   }
   else
   {
      return false;
   }
}

//<!-- =================================================================== -->
// checks textfield for alphanumeric characters and/or some symbols
function validAlphaCharSymField(theTextField, minChars, maxChars)
{
   if(!theTextField) // doesn't exist
   {
      return true;
   }
   return validAlphaCharSym(theTextField.value, minChars, maxChars);
}    

//<!-- =================================================================== -->
// checks text for any character except...
function validAllCharExcept(theText, minChars, maxChars)
{
   if (theText.length == 0) // if theText does not exist
   {
      if (!minChars) // if no min, return true (not required)
      {
         return true;
      }
      else //  if min, return false (required, but no theText)
      {
         return false;
      }
   }
   else // else theText exists
   {
      if (!minChars) // if no min, set to 0
      {
         minChars = 0;
      }
      if (!maxChars) // if no max, set to length of text
      {
         maxChars = theText.length;
      }
   }

   pattern = "^[^\#\%\*\=\"]{"+minChars+","+maxChars+"}$";
   flags = "g";
   theRegExp = new RegExp(pattern, flags);
   /*
      var theRegExp = /^[^\#\%\*\=\"]{6,15}$/g;
      ^                       begins with
      [^\#\%\*\=\"]           anything except # % * = "
      {min,max}               min-max characters
      $                       (and ends with)
   */

   if (theRegExp.test(trim(theText)) > 0)
   {
      return true;
   }
   else
   {
      return false;
   }
}

//<!-- =================================================================== -->
// checks textfield for any character except...
function validAllCharExceptField(theTextField, minChars, maxChars)
{
   if(!theTextField) // doesn't exist
   {
      return true;
   }
   return validAllCharExcept(theTextField.value, minChars, maxChars);
}    

//<!-- =================================================================== -->
// checks for valid phone
function validPhone(theText, minChars, maxChars)
{
   if (theText.length == 0) // if theText does not exist
   {
      if (!minChars) // if no min, return true (not required)
      {
         return true;
      }
      else //  if min, return false (required, but no theText)
      {
         return false;
      }
   }
   else // else theText exists
   {
      if (!minChars) // if no min, set to 0
      {
         minChars = 0;
      }
      if (!maxChars) // if no max, set to length of text
      {
         maxChars = theText.length;
      }
   }

   pattern = "^[\+]?[0-9 \(\)\-.]{"+minChars+","+maxChars+"}$";
   flags = "g";
   theRegExp = new RegExp(pattern, flags);
   /*
      var theRegExp = /^[\+]?[0-9 \(\)\-.]{6,15}$/g;
      ^              begins with
      [\+]?          + (optional)
      [0-9 \(\)\-.]  valid numbers and chars: <space>()-.
      {min,max}      min-max characters
      $              (and ends with)
   */

   if (theRegExp.test(trim(theText)) > 0)
   {
      return true;
   }
   else
   {
      return false;
   }
}

//<!-- =================================================================== -->
// checks for valid phone field
function validPhoneField(theTextField, minChars, maxChars)
{
   if(!theTextField) // doesn't exist
   {
      return true;
   }
   return validPhone(theTextField.value, minChars, maxChars);
}    

//<!-- =================================================================== -->
// checks for valid email
function validEmail (theText, minChars, maxChars)
{
   if (theText.length == 0) // if theText does not exist
   {
      if (!minChars) // if no min, return true (not required)
      {
         return true;
      }
      else //  if min, return false (required, but no theText)
      {
         return false;
      }
   }
   else // else theText exists
   {
      if (!minChars) // if no min, set to 0
      {
         minChars = 0;
      }
      if (!maxChars) // if no max, set to length of text
      {
         maxChars = theText.length;
      }
   }

   pattern = "^[^@]{1,}@[^@.]{1,}\.[^@]{1,}[a-zA-Z0-9_]$";
   flags = "g";
   theRegExp = new RegExp(pattern, flags);
   /*
      var theRegExp = /^[^@]{1,}@[^@.]{1,}\.[^@]{1,}[a-zA-Z0-9_]$/g;
      ^              begins with
      [^@]{1,}       anything but @ (min 1)
      @              @
      [^@.]{1,}      anything but @ or . (min 1)
      \.             .
      [^@]{1,}       anything but @ (min 1)
      [a-zA-Z0-9_]   alphanumeric character
      $              (and ends with)
   */

   if (theRegExp.test(trim(theText)) > 0)
   {
      return true;
   }
   else
   {
      return false;
   }
}

//<!-- =================================================================== -->
// checks for valid email field
function validEmailField (theTextField, minChars, maxChars)
{
   if(!theTextField) // doesn't exist
   {
      return true;
   }
   return validEmail(theTextField.value, minChars, maxChars);
}    

//<!-- =================================================================== -->
// checks if button group (radio button or checkbox group) option is selected
function answeredButtongrp(buttongrp)
{
   if(!buttongrp) // doesn't exist
   {
      return true;
   }
   for (i=0; i<buttongrp.length; i++)
   {
      if (buttongrp[i].checked)
      {
         return true;
      }
   }
   return false;
}

//<!-- =================================================================== -->
// checks if select field option is selected
function answeredSelectList(selectList)
{
   if(!selectList) // doesn't exist
   {
      return true;
   }
   if( (selectList.selectedIndex!=-1) && (selectList.options[selectList.selectedIndex].value!="") )
   {
      return true;
   }
   return false;
}

//<!-- =================================================================== -->
// checks for valid date
function validDate(theText, minChars, maxChars)
{
   if (theText.length == 0) // if theText does not exist
   {
      if (!minChars) // if no min, return true (not required)
      {
         return true;
      }
      else //  if min, return false (required, but no theText)
      {
         return false;
      }
   }
   else // else theText exists
   {
      if (!minChars) // if no min, set to 0
      {
         minChars = 0;
      }
      if (!maxChars) // if no max, set to length of text
      {
         maxChars = theText.length;
      }
   }
              
   pattern = "^((0?2(-|\/)29(-|\/)((19(0[48]|[2468][048]|[13579][26])|20(0[048]|[2468][048]|[13579][26]))|((0[048]|[2468][048]|[13579][26]))))|(((0?[13578]|1[02])(-|\/)(3[01]|[12][0-9]|0?[1-9]))|((0?[469]|11)(-|\/)(30|[12][0-9]|0?[1-9]))|((0?2)(-|\/)([12][0-8]|19|0?[1-9])))(-|\/)((19\d{2}|20\d{2})|(\d{2})))$";
   flags = "g";
   theRegExp = new RegExp(pattern, flags);
   /*
      var theRegExp = /^((0?2(-|\/)29(-|\/)((19(0[48]|[2468][048]|[13579][26])|20(0[048]|[2468][048]|[13579][26]))|((0[048]|[2468][048]|[13579][26]))))|(((0?[13578]|1[02])(-|\/)(3[01]|[12][0-9]|0?[1-9]))|((0?[469]|11)(-|\/)(30|[12][0-9]|0?[1-9]))|((0?2)(-|\/)([12][0-8]|19|0?[1-9])))(-|\/)((19\d{2}|20\d{2})|(\d{2})))$/g;
      ^                                   begins with
      ((                                  LEAP YEAR:
      0?2                                 month (feb)
      (-|\/)                              - /
      29                                  day (29)
      (-|\/)                              - /
      ((                                  4-digit year:
      19                                  19
      (0[48]|[2468][048]|[13579][26])     xx
      |                                   or
      20                                  20
      (0[048]|[2468][048]|[13579][26])    xx
      )|(                                 or
      (0[048]|[2468][048]|[13579][26])    2-digit year (xx)
      )))
      |                                   or 
      ((                                  NON-LEAP YEAR:
      (0?[13578]|1[02])                   month (jan/mar/may/jul/aug/oct/dec)
      (-|\/)                              - /
      (3[01]|[12][0-9]|0?[1-9])           day (could have up to 31 days)
      )|(                                 or
      (0?[469]|11)                        month (apr,jun,sep,nov)
      (-|\/)                              - /
      (30|[12][0-9]|0?[1-9])              day (could have up to 30 days)
      )|(                                 or
      (0?2)                               month (feb)
      (-|\/)                              - /
      ([12][0-8]|19|0?[1-9])              day (could have up to 28 days)
      ))
      (-|\/)                              - /
      (
      (19\d{2}|20\d{2})                   4-digit year (19xx or 20xx)
      |                                   or
      (\d{2})                             2-digit year (xx)
      ))
      $                                   (and ends with)
   */

   if (theRegExp.test(trim(theText)) > 0)
   {
      return true;
/*
      earliestDate = new Date("April 1, 2002") // date not valid if before this date
      inputDate = new Date(year,month-1,day)
      if (inputDate < earliestDate)
      {
         return false;
      }
      else
      {
         return true;
      }
*/
   }
   else
   {
      return false;
   }
}

//<!-- =================================================================== -->
// checks for valid date field
function validDateField(theTextField, minChars, maxChars)
{
   if(!theTextField) // doesn't exist
   {
      return true;
   }
   return validDate(theTextField.value, minChars, maxChars);
}

//<!-- =================================================================== -->
// checks for valid filename
function validFileName(theText)
{
   pattern = "^[A-Za-z0-9]{1,24}\.[A-Za-z]{1,3}$";
   flags = "g";
   theRegExp = new RegExp(pattern, flags);
   /*
      var reg = /^[A-Za-z0-9]{1,24}\.[A-Za-z]{1,3}$/g;
      ^                    begins with
      [A-Za-z0-9]{1,24}    alphanumeric characters (min 1, max 24)
      \.                   .
      [A-Za-z]{1,3}        alpha characters (min 1, max 3)
      $              (and ends with)
   */

   if (theRegExp.test(trim(theText)) > 0)
   {
      return true;
   }
   else
   {
      return false;
   }
}

//<!-- =================================================================== -->