/*============================================================================*/



/*



This routine checks the credit card number. The following checks are made:



1. A number has been provided

2. The number is a right length for the card

3. The number has an appropriate prefix for the card

4. The number has a valid modulus 10 number check digit if required



If the validation fails an error is reported.



The structure of credit card formats was gleaned from a variety of sources on 

the web, although the best is probably on Wikepedia ("Credit card number"):



  http://en.wikipedia.org/wiki/Credit_card_number



Parameters:

            cardnumber           number on the card

            cardname             name of card as defined in the card list below



Author:     John Gardner

Date:       1st November 2003

Updated:    26th Feb. 2005      Additional cards added by request

Updated:    27th Nov. 2006      Additional cards added from Wikipedia

Updated:    18th Jan. 2008      Additional cards added from Wikipedia

Updated:    26th Nov. 2008      Maestro cards extended

Updated:    19th Jun. 2009      Laser cards extended from Wikipedia



*/



/*

   If a credit card number is invalid, an error reason is loaded into the 

   global ccErrorNo variable. This can be be used to index into the global error  

   string array to report the reason to the user if required:

   

   e.g. if (!checkCreditCard (number, name) alert (ccErrors(ccErrorNo);

*/



var ccErrorNo = 0;

var ccErrors = new Array ()



ccErrors [0] = "Unknown card type";

ccErrors [1] = "No card number provided";

ccErrors [2] = "Credit card number is in invalid format";

ccErrors [3] = "Credit card number is invalid";

ccErrors [4] = "Credit card number has an inappropriate number of digits";



function ccardvalidation()

	{		

		var userform = document.ccard;

		var cardname = userform.crdname.value;

		var cardno = userform.CardNumber.value;

		var cardEmon = userform.ExpMonth.value;

		if (userform.CardHname.value=='') { 
       		alert("Please enter card holder name!");
			userform.CardHname.focus()
		 	return false;
		}
		if (Trim(userform.CardHname.value)=='') { 
       		alert("Card holder name contains invalid characters!");
			userform.CardHname.focus()
		 	return false;
		}

		if (userform.crdname.value=='') { 
       		alert("Please select card name!");
			userform.crdname.focus()
		 	return false;
		}

		if (userform.CardNumber.value=='') { 
		   	alert("Please enter card number!");
			userform.CardNumber.focus()
		 	return false;
		}
		
		if (Trim(userform.CardNumber.value)=='') { 
		   	alert("Card number contains invalid characters!");
			userform.CardNumber.focus()
		 	return false;
		}

		if (!checkCreditCard (cardno, cardname)) {
			alert ("Please enter valid card number!");
			userform.CardNumber.focus()
		 	return false;
		}

		if (userform.CCVnum.value=='') { 
		   	alert("Please enter CCV number!");
			userform.CCVnum.focus()
		 	return false;
		}
		if (Trim(userform.CCVnum.value)=='') { 
		   	alert("CCV number contains invalid characters!");
			userform.CCVnum.focus()
		 	return false;
		}

		if (userform.ExpMonth.value=='') { 
		   	alert("Please enter card expiration month!");
			userform.ExpMonth.focus()
		 	return false;
		}

		if (userform.ExpMonth.value > 12 || userform.ExpMonth.value < 1) { 
		   	alert("Card expiration month is not valid!");
			userform.ExpMonth.focus()
		 	return false;

		}	

		if (userform.ExpYear.value=='') { 
		   	alert("Please enter card expiration year!");
			userform.ExpYear.focus()
		 	return false;
		}
		if (Trim(userform.ExpYear.value)=='') { 
		   	alert("Card expiration year contains invalid characters!");
			userform.ExpYear.focus()
		 	return false;
		}

		if (userform.ExpYear.value > 96) { 
			tmpyear = "19" + userform.ExpYear.value;
		}

		else if (userform.ExpYear.value < 21) { 
			tmpyear = "20" + userform.ExpYear.value;
		}

		else { 
			alert("Card expiration year is not valid.");
		 	return false;
		}

		if (!isExpiryDate(tmpyear, cardEmon)) {
			alert("This card has already expired.");
		 	return false;
		}

		if (userform.billaddress.value=='') { 
		   	alert("Please enter billing address!");
			userform.billaddress.focus()
		 	return false;
		}
		if (Trim(userform.billaddress.value)=='') { 
		   	alert("Billing address contains invalid characters!");
			userform.billaddress.focus()
		 	return false;
		}

		if (userform.stateProvience.value=='') { 
		   	alert("Please enter state or provience name!");
			userform.stateProvience.focus()
		 	return false;
		}
		if (Trim(userform.stateProvience.value)=='') { 
		   	alert("State or provience name contains invalid characters!");
			userform.stateProvience.focus()
		 	return false;
		}

		if (userform.zipCode.value=='') { 
		   	alert("Please enter zip or postal code!");
			userform.zipCode.focus()
		 	return false;

		}
		if (Trim(userform.zipCode.value)=='') { 
		   	alert("Zip or postal code contains invalid characters!");
			userform.zipCode.focus()
		 	return false;

		}

		if (userform.country.value=='') { 
		   	alert("Please select country name!");
			userform.country.focus()
		 	return false;
		}

		else {
		 		return true;
			 }

}



function isExpiryDate() {

var argv = isExpiryDate.arguments;

var argc = isExpiryDate.arguments.length;



year = argc > 0 ? argv[0] : this.year;

month = argc > 1 ? argv[1] : this.month;



if (!isNum(year+""))

return false;

if (!isNum(month+""))

return false;

today = new Date();

expiry = new Date(year, month);

if (today.getTime() > expiry.getTime())

return false;

else

return true;

}



function isNum(argvalue) {

argvalue = argvalue.toString();



if (argvalue.length == 0)

return false;



for (var n = 0; n < argvalue.length; n++)

if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")

return false;



return true;

}







function checkCreditCard (cardnumber, cardname) {

     

  // Array to hold the permitted card characteristics

  var cards = new Array();



  // Define the cards we support. You may add addtional card types.

  

  //  Name:      As in the selection box of the form - must be same as user's

  //  Length:    List of possible valid lengths of the card number for the card

  //  prefixes:  List of possible prefixes for the card

  //  checkdigit Boolean to say whether there is a check digit

  

  cards [0] = {name: "Visa", 

               length: "13,16", 

               prefixes: "4",

               checkdigit: true};

  cards [1] = {name: "MasterCard", 

               length: "16", 

               prefixes: "51,52,53,54,55",

               checkdigit: true};

  cards [2] = {name: "AmEx", 

               length: "15", 

               prefixes: "34,37",

               checkdigit: true};

  cards [3] = {name: "Discover", 

               length: "16", 

               prefixes: "6011,622,64,65",

               checkdigit: true};

               

  // Establish card type

  var cardType = -1;

  for (var i=0; i<cards.length; i++) {



    // See if it is this card (ignoring the case of the string)

    if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {

      cardType = i;

      break;

    }

  }

  

  // If card type not found, report an error

  if (cardType == -1) {

     ccErrorNo = 0;

     return false; 

  }

   

  // Ensure that the user has provided a credit card number

  if (cardnumber.length == 0)  {

     ccErrorNo = 1;

     return false; 

  }

    

  // Now remove any spaces from the credit card number

  cardnumber = cardnumber.replace (/\s/g, "");

  

  // Check that the number is numeric

  var cardNo = cardnumber

  var cardexp = /^[0-9]{13,19}$/;

  if (!cardexp.exec(cardNo))  {

     ccErrorNo = 2;

     return false; 

  }

       

  // Now check the modulus 10 check digit - if required

  if (cards[cardType].checkdigit) {

    var checksum = 0;                                  // running checksum total

    var mychar = "";                                   // next char to process

    var j = 1;                                         // takes value of 1 or 2

  

    // Process each digit one by one starting at the right

    var calc;

    for (i = cardNo.length - 1; i >= 0; i--) {

    

      // Extract the next digit and multiply by 1 or 2 on alternative digits.

      calc = Number(cardNo.charAt(i)) * j;

    

      // If the result is in two digits add 1 to the checksum total

      if (calc > 9) {

        checksum = checksum + 1;

        calc = calc - 10;

      }

    

      // Add the units element to the checksum total

      checksum = checksum + calc;

    

      // Switch the value of j

      if (j ==1) {j = 2} else {j = 1};

    } 

  

    // All done - if checksum is divisible by 10, it is a valid modulus 10.

    // If not, report an error.

    if (checksum % 10 != 0)  {

     ccErrorNo = 3;

     return false; 

    }

  }  



  // The following are the card-specific checks we undertake.

  var LengthValid = false;

  var PrefixValid = false; 

  var undefined; 



  // We use these for holding the valid lengths and prefixes of a card type

  var prefix = new Array ();

  var lengths = new Array ();

    

  // Load an array with the valid prefixes for this card

  prefix = cards[cardType].prefixes.split(",");

      

  // Now see if any of them match what we have in the card number

  for (i=0; i<prefix.length; i++) {

    var exp = new RegExp ("^" + prefix[i]);

    if (exp.test (cardNo)) PrefixValid = true;

  }

      

  // If it isn't a valid prefix there's no point at looking at the length

  if (!PrefixValid) {

     ccErrorNo = 3;

     return false; 

  }

    

  // See if the length is valid for this card

  lengths = cards[cardType].length.split(",");

  for (j=0; j<lengths.length; j++) {

    if (cardNo.length == lengths[j]) LengthValid = true;

  }

  

  // See if all is OK by seeing if the length was valid. We only check the 

  // length if all else was hunky dory.

  if (!LengthValid) {

     ccErrorNo = 4;

     return false; 

  };   

  

  // The credit card is in the required format.

  return true;

}



/*============================================================================*/


