//      Credit Card Validator
//      By Brendon Ely

function amexdiners()
	{
	return confirm('You have chosen to pay using an American Express or Diners Club card.  \n\nYou will be redirected to a secure web site hosted by our third party payment processor. \n\nYour information will be protected by 128-bit encryption and an SSL certificate.')
	}

function renderDown(num)        {   // bangs down multi-digit numbers into singles by splitting and adding
        var renHash = new Array(); if(num>9) { num=num.toString();
        for(x=0;x<16;x++)       { renHash[x] = num.charAt(x);}
        var res = parseInt(renHash[0]) + parseInt(renHash[1]);} else {
        res = num; } return res<10?res:renderDown(res);
}

function luhn(intCC)    {  // intCC is our credit card number as a 16-digit integer 
        
        var luhnHash = new Array(12);
				str = intCC.toString();
				
        for(x=0;x<16;x++)       {luhnHash[x] = str.charAt(x);}
				
				
        // first two lines populate the array of all 16 digits
        
        for(y=0;y<16;y=y+2)     {res = renderDown(parseInt(luhnHash[y]) * 2);   luhnHash[y] = res;      } 
        // above line cuts down double-digit figures    
        
        var sum = new Number(0);
        for(z=0;z<luhnHash.length;z++)  {sum = sum + parseInt(luhnHash[z]);     }
        // we just added all the digits together into the variable sum
        
        //return (sum%10)==0?true:false; // checks if the sum divides by ten to give an integer
        // function returns true for a good card, false for a bad card
        
       if (((sum%10)==0) == false) {
			//alert("Credit card is not Valid");
			return false;
       } else {
			//alert("Credit card is Valid");
			return true;
       }
}

//**********************************************************************
// Name:	isCcOfType
// Author:	Mark Micallef (mmicallef@powerserve.com.au)
// Purpose:	Return boolean indicating if card number matches type
// Args:	string	strCardType		- the card type
//			int		intCardNumber	- the credit card number
//**********************************************************************
// VERSION HISTORY 
// Ver		Date			Init		Description
// 1.0		02-NOV-2001		MEM			Initial Version
//**********************************************************************
function isCcOfType( strCardType, intCardNumber )
{
	strCardType = strCardType.toLowerCase() ;
	switch( strCardType )
	{
		case "americanexpress":
			if( intCardNumber.slice(0,2) == "37" || intCardNumber.slice(0,2) == "34") {
				return true ;
			} else {
				return false ;
			}
		case "amex":
			if( intCardNumber.slice(0,2) == "37" || intCardNumber.slice(0,2) == "34") {
				return true ;
			} else {
				return false ;
			}
		case "bankcard":
			if( intCardNumber.slice(0,1) == "5" ) {
				return true ;
			} else {
				return false ;
			}
		case "bc":
			if( intCardNumber.slice(0,1) == "5" ) {
				return true ;
			} else {
				return false ;
			}
		case "mastercard":
			if( intCardNumber.slice(0,1) == "5" ) {
				return true ;
			} else {
				return false ;
			}
		case "mc":
			if( intCardNumber.slice(0,1) == "5" ) {
				return true ;
			} else {
				return false ;
			}
		case "diners":
			if( intCardNumber.slice(0,2) == "36" || intCardNumber.slice(0,2) == "55") {
				return true ;
			} else {
				return false ;
			}	
		case "visa":
			if( intCardNumber.slice(0,1) == "4" ) {
				return true ;
			} else {
				return false ;
			}
		default: return false ;
	}
}
