/*---------------------------------------------------------------*/
/* */
/* Function : isCreditCard() */
/* Purpose : Check if cc is LUHN10 */
/* */
/* */
/* Parameters: cc - the cc number */
/* */
/* Returns : boolean */
/* */
/* Usage : isCreditCard(cc) */
/*---------------------------------------------------------------*/

function isCreditCard( CC )
{
if (CC.length > 19)
return (false);

sum = 0; mul = 1; l = CC.length;
for (i = 0; i < l; i++)
{
digit = CC.substring(l-i-1,l-i);
tproduct = parseInt(digit ,10)*mul;
if (tproduct >= 10)
sum += (tproduct % 10) + 1;
else
sum += tproduct;
if (mul == 1)
	mul=mul+1;
else
	mul=mul-1;
}
if ((sum % 10) == 0)
return (true);
else
return (false);
}

/*—————————————————————*/
/* */
/* Function : isVisa() */
/* Purpose : Validate CC number following visa specs */
/* */
/* */
/* Parameters: cc - the cc number */
/* */
/* Returns : boolean */
/* */
/* Usage : isVisa(cc) */
/*—————————————————————*/

function isVisa( cc ){
	if( (cc.substring(0,1) == 4) && (cc.length == 16) || (cc.length == 13) ){
		return isCreditCard( cc );
	}
	return (false);
}

/*—————————————————————*/
/* */
/* Function : isMC() */
/* Purpose : Validate CC number following MasterCard specs */
/* */
/* */
/* Parameters: cc - the cc number */
/* */
/* Returns : boolean */
/* */
/* Usage : isMC(cc) */
/*—————————————————————*/
function isMC( cc )
{
if( (cc.length == 16) && (cc.substring(0,2) == 51)
|| (cc.substring(0,2) == 52) || (cc.substring(0,2) == 53)
|| (cc.substring(0,2) == 54) || (cc.substring(0,2) == 55) )
{
return isCreditCard( cc );
}
return (false);
}
/*—————————————————————*/
/* */
/* Function : isAmex() */
/* Purpose : Validate CC number following AMEX specs */
/* */
/* */
/* Parameters: cc - the cc number */
/* */
/* Returns : boolean */
/* */
/* Usage : isAmex(cc) */
/*—————————————————————*/
function isAmex( cc )
{
if( (cc.length == 15) && (cc.substring(0,2) == 34)
|| (cc.substring(0,2) == 37) )
{
return isCreditCard( cc );
}
return (false);
}
/*—————————————————————*/
/* */
/* Function : isDiscover() */
/* Purpose : Validate CC number following Discover specs */
/* */
/* */
/* Parameters: cc - the cc number */
/* */
/* Returns : boolean */
/* */
/* Usage : isDiscover(cc) */
/*—————————————————————*/
function isDiscover( cc )
{
if( (cc.length == 16) && (cc.substring(0,4) == 6011) )
{
return isCreditCard( cc );
}
return (false);
}

function validateCC(cc,cctype){
	if(cctype=="Visa"){
		if(!isVisa( cc )){
			//alert("Invalid credit card.");
			return false;
		}
	}else if(cctype=="MasterCard"){
		if(!isMC( cc )){
			//alert("Invalid credit card.");
			return false;
		}
	}else if(cctype=="Amex"){
		if(!isAmex( cc )) {
			//alert("Invalid credit card.");
			return false;
		}
	}
	return true;
}
