//<!--
/******************************************************************
   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 + "." );
      }
      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 + "." );
   }
   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;
}

/******************************************************************
   Validates that entry is a valid credit card number
   The first parameter is the ID of the credit card number form field.
   DEPENDS ON: getObjectFormat, addErrors
*******************************************************************/
function validateCreditCardNumber( numberId ) 
{
   var returnValue = false;
   var cardNumberString = getObjectFormat( numberId ).value.trim( ).replace(/[^0-9]/g,''); // card number as string
   
   if ( cardNumberString.trim( ) == '' || isNaN( parseInt( cardNumberString.trim( ) ) ) ) 
   {
      addError( "The credit card number provided is not valid." );
   }
   else
   {
      var cardLength = cardNumberString.length; // length of the card number
      var cardNumberInt = parseInt( cardNumberString.trim( ) ); // number as int
      var iTotal = 0;  // integer total set at zero
      var isValid = false;  // by default assume it is NOT a valid cc
      var calc;  // used for calculation of each digit
      
      // Determine if it is the proper length 
      if ( cardLength >= 15 )
      {  
         // 15 or 16 for Amex or V/MC
         for ( var i = cardLength; i > 0; i-- )
         {  
            // LOOP throught the digits of the card
            calc = parseInt( cardNumberInt ) % 10;  // right most digit
            calc = parseInt( calc );  // assure it is an integer
            iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
            i--;  // decrement the count - move to the next digit in the card
            
            cardNumberInt = cardNumberInt / 10; // subtracts right most digit from ccNumb
            calc = parseInt(cardNumberInt) % 10 ;    // NEXT right most digit
            calc = calc *2;                                 // multiply the digit by two
            
            // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
            // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
            switch( calc )
            {
               case 10: calc = 1; break;       // 5*2=10 & 1+0 = 1
               case 12: calc = 3; break;       // 6*2=12 & 1+2 = 3
               case 14: calc = 5; break;       // 7*2=14 & 1+4 = 5
               case 16: calc = 7; break;       // 8*2=16 & 1+6 = 7
               case 18: calc = 9; break;       // 9*2=18 & 1+8 = 9
               default: calc = calc;           // 4*2= 8 & 8 = 8  -same for all lower numbers
            }                                               
          
            cardNumberInt = cardNumberInt / 10;  // subtracts right most digit from ccNum
          
            iTotal += calc;  // running total of the card number as we loop
        }  
        // END OF LOOP
        
        if ( ( iTotal % 10 ) == 0 )
        {  // check to see if the sum Mod 10 is zero
          returnValue = true;  // This IS (or could be) a valid credit card number.
        }
        else
        {
           addError( 'the credit card number you entered is invalid.' );
        }
   
      }
      else
      {
         addError( 'please verify that you have entered your complete credit card number.' );
      }
   }
   
   return returnValue;
}
//-->


