/*---------------------------------------------------------------*/
/* */
/* 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++;
else
mul--;
}
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 : checkCard() */
/* Purpose : Validate CC number following */
/* */
/* */
/* Parameters: cc - the cc number */
/* */
/* Returns : boolean */
/* */
/* Usage : checkCard(cc) */
/* This function is added by Sree
/*—————————————————————*/

function checkCard(cc, cc_type){
//onClick="return checkCard(document.Form1.CreditCard.value);"
if(!isCreditCard( cc )) { alert("Incorrect card number");return false;}

if(cc_type == 'amex'){
	if(!isAmex( cc )) { alert("Incorrect American Expresss Number");return false;}
}else if(cc_type == 'mc'){
	//if(!isDiscover( cc ) { alert("Incorrect Dicover Card number");return false;}
	if(!isMC( cc )) { alert("Incorrect Master Card number");return false;}
}else if(cc_type == 'visa'){
	if(!isVisa( cc )) { alert("Incorrect card number");return false;}
}else{
	return true;
}
}