//<!--
/******************************************************************
   Validates a US zip code is 5 or 10 digits long and will fit
   either the xxxxx or xxxxx-xxxx format mask.
   The first parameter is the ID of the form field to validate.
   The second parameter is the Name of the field on the form that 
   this error message applies to ( the field label )
   DEPENDS ON: getObjectFormat, addErrors
*******************************************************************/
function validateZip( id, label )
{
   var returnVal = false;
   
   if ( !getObjectFormat( id ).value.trim( ).match( /^([0-9]{5}|[0-9]{5}-[0-9]{4})$/ ) )
   {
      addError( "You have provided an invalid " + label + "." );
   }
   else
   {
      returnValue = true;
   }

   return returnVal;   
}

/******************************************************************
   Validates a US phone number will fit one of the following masks
   xxx-xxxx, xxx-xxx-xxxx, x-xxx-xxx-xxxx.
   The first parameter is the ID of the form field to validate.
   The second parameter is the Name of the field on the form that 
   this error message applies to ( the field label )
   The third parameter is to specify it the area code is required,
   thus causing phone numbers in the format xxx-xxxx to fail as well
   DEPENDS ON: getObjectFormat, addErrors
*******************************************************************/
function validatePhone( id, label, areaCodeRequired )
{
   var returnVal = false;
   
   if ( areaCodeRequired )
   {
      if ( !getObjectFormat( id ).value.trim( ).match( /^([0-9]{3}-[0-9]{3}-[0-9]{4}|[0-9]{1}-[0-9]{3}-[0-9]{3}-[0-9]{4})$/ ) )
      {
         addError( "You have provided an invalid " + label + ".  The number sould be in the format 555-555-5555" );
      }
      else
      {
         returnValue = true;
      }
   }
   else if ( !getObjectFormat( id ).value.trim( ).match( /^([0-9]{3}-[0-9]{4}|[0-9]{3}-[0-9]{3}-[0-9]{4}|[0-9]{1}-[0-9]{3}-[0-9]{3}-[0-9]{4})$/ ) )
   {
      addError( "You have provided an invalid " + label + ".  The number should be in the format 555-5555" );
   }
   else
   {
      returnValue = true;
   }

   return returnVal;   
}

/******************************************************************
   Validates an email address will fit the following mask
   (x?.)x+@xx*(.x?).x{2,3}
   The first parameter is the ID of the form field to validate.
   The second parameter is the Name of the field on the form that 
   this error message applies to ( the field label )
   DEPENDS ON: getObjectFormat, addErrors
*******************************************************************/
function validateEmail( id, label )
{
   var returnVal = false;
   
   if ( !getObjectFormat( id ).value.trim( ).match( /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/ ) )
   {
      addError( "You have provided an invalid " + label + "." );
   }
   else
   {
      returnValue = true;
   }

   return returnVal;   
}

/******************************************************************
   Validates an social security number will fit the following mask
   xxx-xx-xxxx
   The first parameter is the ID of the form field to validate.
   The second parameter is the Name of the field on the form that 
   this error message applies to ( the field label )
   DEPENDS ON: getObjectFormat, addErrors
*******************************************************************/
function validateSSN( id, label )
{
   var returnVal = false;
   
   if ( !getObjectFormat( id ).value.trim( ).match( /^([0-9]{3}-[0-9]{2}-[0-9]{4})$/ ) )
   {
      addError( "You have provided an invalid " + label + "." );
   }
   else
   {
      returnValue = true;
   }

   return returnVal;   
}

/******************************************************************
   Validates an field contains a valid date fitting the following mask
   MM/DD/YYYY
   The first parameter is the ID of the form field to validate.
   The second parameter is the Name of the field on the form that 
   this error message applies to ( the field label )
   DEPENDS ON: getObjectFormat, addErrors
*******************************************************************/
function validateDate( id, label )
{
   var returnVal = false;
   
   if ( getObjectFormat( id ).value.trim( ).match( /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/ ) )
   {
      var buffArray = getObjectFormat( id ).value.trim( ).split("/");
      var month = parseInt( buffArray[0] - 0 ) - 1;
      var day = parseInt( buffArray[1] - 0 );
      var year = parseInt( buffArray[2] );
      var checkDate = new Date( year, month, day, 0, 0, 0 );
      
      if ( checkDate.getMonth() == month && checkDate.getFullYear() == year && checkDate.getDate() == day )
      {
         returnVal = true;
      }
   }
   
   if ( !returnVal )
   {
      addError( "You have provided an invalid " + label + "." );
   }
   
   return returnVal;   
}

/******************************************************************
   Validates that entry is numeric
   The first parameter is the ID of the form field to validate.
   The second parameter is the Name of the field on the form that 
   this error message applies to ( the field label )
   The third parameter accepts 'int' or 'float' as values, and will
   validate the input accordingly
   DEPENDS ON: getObjectFormat, addErrors
*******************************************************************/
function validateNumeric( id, label, intOrFloat ) 
{
   var returnValue = false;
   
   if ( intOrFloat.toLowerCase( ) == 'int' )
   {
      if ( getObjectFormat( id ).value.trim( ).match(/[^0-9]/) ) 
      {
         addError( "The value provided for " + label + " is not a Whole Number." );
      }
      else
      {
         returnValue = true;
      }
   }
   else
   {
      if ( getObjectFormat( id ).value.trim( ).match(/[^0-9\.,]/) || isNaN( parseFloat( getObjectFormat( id ).value.trim( ) ) ) ) 
      {
         addError( "The value provided for " + label + " is not a Number." );
      }
      else
      {
         returnValue = true;
      }
   }
   
   return returnValue;
}
//-->

