
/*  ================================================================
    FUNCTION	:  	isCreditCard(pmlngCreditCardNo)
    INPUT		:   pmlngCreditCardNo - a string representing a credit card number
    RETURNS		:  	true, if the credit card number passes the Luhn Mod-10 test.
					false, otherwise
  ================================================================ */
function isCreditCard(pmlngCreditCardNo) 
{
	//#--	Encoding only works on cards with less than 19 digits
	if (pmlngCreditCardNo.length > 19)
		return (false);
	//alert("the card no is "+pmlngCreditCardNo);	
	//#--	Initialization the Local Variable
	vcurSum = 0; 
	vintMul = 1; 
	//#--	Assign the Length of the give Crediti Card No
	vintCardLength = pmlngCreditCardNo.length;

	//#--	Starting the Loop upto Length of the CradCardNo	
	for (lintIndex = 0; lintIndex < vintCardLength; lintIndex++) 
	{
		//#--	Assign the One Digit Number from the Give Credit Card No
		vintDigit = pmlngCreditCardNo.substring(vintCardLength-lintIndex-1,vintCardLength-lintIndex);
		
		//#--	Coverting into Decimal of Base 10 and Mutiple by 1 or 2	
		vfltTproduct = parseInt(vintDigit ,10)	*	vintMul;
		
		//#--	Checking the Condition for Calculate Credit Card No is Greater than Ten or Nor
		if (vfltTproduct >= 10)
			vcurSum += (vfltTproduct % 10) + 1;
		else
			vcurSum += vfltTproduct;
			
		//#--	Checking the Mutiple Value is Equal to o"One" or Not	
		if (vintMul == 1)
			vintMul++;
		else
			vintMul--;
	}
	//#--	End of the Loop
	
	//#--	Uncomment the following line to help create credit card numbers
	//#--	1. Create a dummy number with a 0 as the last digit
	//#--	2. Examine the sum written out
	//#--	3. Replace the last digit with the difference between the sum and
	//#--	the next multiple of 10.
   	if ((vcurSum % 10) == 0)
    	return (true);
	else
    	return (false);
} //#-- END FUNCTION isCreditCard(pmlngCreditCardNo)

		
	
/*  ================================================================
    FUNCTION	:  isVisa(pmlngCCNo)
    INPUT		:  pmlngCCNo - a string representing a credit card number
    RETURNS		:  true, if the credit card number is a valid VISA number.
	      		   false, otherwise
    Sample number: 4111 1111 1111 1111 (16 digits)
    ================================================================ */
function isVisa(pmlngCCNo)
{
  if (((pmlngCCNo.length == 16) || (pmlngCCNo.length == 13)) && (pmlngCCNo.substring(0,1) == 4))
    return isCreditCard(pmlngCCNo);
  return false;
}  
//#--	END FUNCTION isVisa(pmlngCCNo)

/*  ================================================================
    FUNCTION	:  isMasterCard(pmlngCCNo)
    INPUT		:  pmlngCCNo - a string representing a credit card number
    RETURNS		:  true, if the credit card number is a valid MasterCard   number.
			      false, otherwise
				  Sample number: 5500 0000 0000 0004 (16 digits)
   ================================================================ */
function isMasterCard(pmlngCCNo)
{
  vintFirstDig = pmlngCCNo.substring(0,1);
  vintSecondDig = pmlngCCNo.substring(1,2);
  if ((pmlngCCNo.length == 16) && (vintFirstDig == 5) && ((vintSecondDig >= 1) && (vintSecondDig <= 5)))
    return isCreditCard(pmlngCCNo);
  return false;

} 
//#--	END FUNCTION isMasterCard(pmlngCCNo)

/*  ================================================================
    FUNCTION	:  isAmericanExpress()
    INPUT		:  pmlngCCNo - a string representing a credit card number
    RETURNS		:  true, if the credit card number is a valid American  Express number.
			      false, otherwise
    Sample number: 340000000000009 (15 digits)
    ================================================================ */

function isAmericanExpress(pmlngCCNo)
{
  vintFirstDig = pmlngCCNo.substring(0,1);
  vintSecondDig = pmlngCCNo.substring(1,2);
  if ((pmlngCCNo.length == 15) && (vintFirstDig == 3) &&   ((vintSecondDig == 4) || (vintSecondDig == 7)))
    return isCreditCard(pmlngCCNo);
  return false;

} //#-- End FUNCTION isAmericanExpress()


/*  ================================================================
    FUNCTION	:  isDiscover()
    INPUT		:  pmlngCCNo - a string representing a credit card number
    RETURNS		:  true, if the credit card number is a valid Discover  card number.
			      false, otherwise
    Sample number: 6011000000000004 (16 digits)
    ================================================================ */

function isDiscover(pmlngCCNo)
{
  first4digs = pmlngCCNo.substring(0,4);
  if ((pmlngCCNo.length == 16) && (first4digs == "6011"))
    return isCreditCard(pmlngCCNo);
  return false;

} //#-- End FUNCTION isDiscover()

function validateccnum(cardtype,cardnum)
{
			var result = true;
			var cardlen = cardnum.length;
			var firstdig = cardnum.substring(0,1);
			var seconddig = cardnum.substring(1,2);
			var first4digs = cardnum.substring(0,4);
			if (isNaN(cardnum))
			{
				return false;
			}
			
			cardtype = cardtype.toUpperCase();
			switch (cardtype)
			{
				// 1 - mastercard, 2 - visa, 3 - amex, 4 - diners, 5 - discover(old)
				// 3 - mastercard, 3 - visa, 3 - amex, 4 - diners, 5 - discover(new)
				case "1" :
					       result = isVisa(cardnum) || isMasterCard(cardnum);
					       break;
				case "2":
					       result = isAmericanExpress(cardnum);
					       break;
				case "3":
					       result = isDiscover(cardnum);
					       break;
			}
			 return result;
} 


function validateCreditCard(s) 
		{
			//alert("the number passed is "+s);
			var v = "0123456789";
			var w = "";
			for (var i=0; i < s.length; i++) 
			{
				x = s.charAt(i);
				if (v.indexOf(x,0) != -1)
				w += x;
			}
			var j = w.length / 2;
			if (j < 6.5 || j > 8 || j == 7) return false;
			var k = Math.floor(j);
			var m = Math.ceil(j) - k;
			var c = 0;
			for (var i=0; i<k; i++) 
			{
				a = w.charAt(i*2+m) * 2;
				c += a > 9 ? Math.floor(a/10 + a%10) : a;
			}
			for (var i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
				return (c%10 == 0);
	}	