/* Credit card LUHN number checker - coded '05 shaman - www.planzero.org *
 * This code has been released into the public domain, however please    *
 * give credit to the original author wherever possible.                 */

function validatecc( x )
{
  var ccnumber = x.value.replace( /\D/g, '' );
  var cclength = ccnumber.length;
  var parity = cclength % 2;
  var sum = 0;
  var ccdigit;

  for( i = 0 ; i < cclength ; i++ )
  {
    ccdigit = ccnumber.charAt( i );

    if( i % 2 == parity )
    {
      ccdigit = ccdigit * 2;
    }

    if( ccdigit > 9 )
    {
      ccdigit = ccdigit - 9;
    }

    sum = sum + parseInt( "0" + ccdigit, 10 );
  }

  return ( sum % 10 == 0 );
}

function checkForm( theForm )
{
  if( theForm.trackcode.value.length != 18 ||
      ( theForm.trackcode.value.indexOf( "98269368" ) != 0 &&
        theForm.trackcode.value.indexOf( "98269380" ) != 0 &&
        theForm.trackcode.value.indexOf( "98269386" ) != 0 ) ||
      !validatecc( theForm.trackcode ) )
  {
    alert( "Please enter a valid Veri-fy form code.\nThe code must be 18 digits long and start with 98269368, 98269380 or 98269386." );
    return false;
  }

  return true;
}

