function checkCreditCard()
    {
   	cc=document.GeneralRegForm;
   	
var eroare='';
var no="";
var lg=1;

	f=document.GeneralRegForm.elements;
	
	

		for(var j = f.length-1; j >= 0; j--)
			{
              if (f[j].type!="radio")
			f[j].style.backgroundColor=elemFormDefaultBackColor;
			}


			for(i=0;i<cc.length;i++)
                {
				e	=	cc.elements[i];
				
				if(e.type	==	"radio" && e.checked == true){
				
				break;
			}
		}




		if (e.value != "Check")
        {
var iCreditCardPrefix = "This is not a valid "
var iCreditCardSuffix = " credit card number."

var creditCardDelimiters = " "
var cardType = e.value;
var cardMonth=cc.startmonth.value;
var cardYear=cc.startyear.value;
var normalizedCCN = stripCharsInBag(cc.cc_number.value, creditCardDelimiters)
if (!isCardMatch(cardType, normalizedCCN))
    {
eroare+=iCreditCardPrefix + cardType + iCreditCardSuffix+"\n";
set_error_form(cc.cc_number);
    }

if (e.value=="Amex" || e.value=="Discover" || e.value=="MasterCard" || e.value=="Visa")
		{

		if(cc.cc_cvv.value.length<3)
            {
			eroare+="Please provide a corect Card Code (CVV)\n";
			set_error_form(cc.cc_cvv);
			}
			
	if( expired( cardMonth, cardYear ) ) {							// check if entered date is already expired.
eroare+="Sorry! The expiration date you have entered would make this card invalid.\n";	
set_error_form(cc.startmonth);                  

	}
		
	}

 /* if (cc.cc_name.value.length<2)
    {
			eroare+="Please provide a valid Cardholder Name \n";
			set_error_form(cc.cc_name);
	}
*/
	//take out the bank for the moment
 // if (cc.cc_bank.value.length<2)
   // {
	//		eroare+="Please provide a valid Bank \n";
		//	set_error_form(cc.cc_bank);
	//}
    
//take out address for the moment
  //  chkel(cc.cc_address,cc.cc_address.value,"address",3);




if (eroare != "") {alerter(eroare);  return false;}
		else
		{
	n=cc.cc_number.value.length;


		for(i=0;i<n;i++){
			if(cc.cc_number.value.charAt(i)>="0" && cc.cc_number.value.charAt(i)<="9")
				no+=cc.cc_number.value.charAt(i);
	}


	//  cc.cc_number.value=no.substr(no.length-4,4);
	cc.cc_number.value=no;
		//cc.submit();
		
		}

    }




}

   function expired( month, year ) {
        	var now = new Date();							
        	var expiresIn = new Date(year,month,0,0,0);		
        	expiresIn.setMonth(expiresIn.getMonth()+1);		
        	if( now.getTime() < expiresIn.getTime() ) return false;
        	return true;								
    }


// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag)

{ var i;
var returnString = "";

// Search through string's characters one by one.
// If character is not in bag, append to returnString.

for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}

return returnString;
}


/*
isCardMatch()

INPUT: cardType - a string representing the credit card type
cardNumber - a string representing a credit card number

RETURNS: true, if the credit card number is valid for the particular
credit card type given in "cardType".

false, otherwise
================================================================ */

function isCardMatch (cardType, cardNumber)
{

//cardType = cardType.toUpperCase();
var doesMatch = true;

if ((cardType == "Visa") && (!isVisa(cardNumber)))
doesMatch = false;
if ((cardType == "MasterCard") && (!isMasterCard(cardNumber)))
doesMatch = false;
if ( ( (cardType == "Amex") || (cardType == "AMEX") )
&& (!isAmericanExpress(cardNumber))) doesMatch = false;
if ((cardType == "Discover") && (!isDiscover(cardNumber)))
doesMatch = false;
if ((cardType == "JCB") && (!isJCB(cardNumber)))
doesMatch = false;
if ((cardType == "DINERS") && (!isDinersClub(cardNumber)))
doesMatch = false;
if ((cardType == "CARTEBLANCHE") && (!isCarteBlanche(cardNumber)))
doesMatch = false;
if ((cardType == "ENROUTE") && (!isEnRoute(cardNumber)))
doesMatch = false;
return doesMatch;

} // END FUNCTION CardMatch()


     

/* ================================================================
FUNCTION: isCreditCard(st)

INPUT: st - a string representing a credit card number

RETURNS: true, if the credit card number passes the Luhn Mod-10
test.
false, otherwise
================================================================ */

function isCreditCard(st) {
// Encoding only works on cards with less than 19 digits
if (st.length > 19)
return (false);

sum = 0; mul = 1; l = st.length;
for (i = 0; i < l; i++) {
digit = st.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--;
}
// 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.

// document.writeln("<BR>Sum = ",sum,"<BR>");
// alert("Sum = " + sum);

if ((sum % 10) == 0)
return (true);
else
return (false);

} // END FUNCTION isCreditCard()



/* ================================================================
FUNCTION: isVisa()

INPUT: cc - 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(cc)
{
if (((cc.length == 16) || (cc.length == 13)) &&
(cc.substring(0,1) == 4))
return isCreditCard(cc);
return false;
} // END FUNCTION isVisa()




/* ================================================================
FUNCTION: isMasterCard()

INPUT: cc - 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(cc)
{
firstdig = cc.substring(0,1);
seconddig = cc.substring(1,2);
if ((cc.length == 16) && (firstdig == 5) &&
((seconddig >= 1) && (seconddig <= 5)))
return isCreditCard(cc);
return false;

} // END FUNCTION isMasterCard()





/* ================================================================
FUNCTION: isAmericanExpress()

INPUT: cc - 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(cc)
{
firstdig = cc.substring(0,1);
seconddig = cc.substring(1,2);
if ((cc.length == 15) && (firstdig == 3) &&
((seconddig == 4) || (seconddig == 7)))
return isCreditCard(cc);
return false;

} // END FUNCTION isAmericanExpress()




/* ================================================================
FUNCTION: isDinersClub()

INPUT: cc - a string representing a credit card number

RETURNS: true, if the credit card number is a valid Diner's
Club number.

false, otherwise

Sample number: 30000000000004 (14 digits)
================================================================ */

function isDinersClub(cc)
{
firstdig = cc.substring(0,1);
seconddig = cc.substring(1,2);
if ((cc.length == 14) && (firstdig == 3) &&
((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
return isCreditCard(cc);
return false;
}



/* ================================================================
FUNCTION: isCarteBlanche()

INPUT: cc - a string representing a credit card number

RETURNS: true, if the credit card number is a valid Carte
Blanche number.

false, otherwise
================================================================ */

function isCarteBlanche(cc)
{
return isDinersClub(cc);
}




/* ================================================================
FUNCTION: isDiscover()

INPUT: cc - 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(cc)
{
first4digs = cc.substring(0,4);
if ((cc.length == 16) && (first4digs == "6011"))
return isCreditCard(cc);
return false;

} // END FUNCTION isDiscover()





/* ================================================================
FUNCTION: isEnRoute()

INPUT: cc - a string representing a credit card number

RETURNS: true, if the credit card number is a valid enRoute
card number.

false, otherwise

Sample number: 201400000000009 (15 digits)
================================================================ */

function isEnRoute(cc)
{
first4digs = cc.substring(0,4);
if ((cc.length == 15) &&
((first4digs == "2014") ||
(first4digs == "2149")))
return isCreditCard(cc);
return false;
}



/* ================================================================
FUNCTION: isJCB()

INPUT: cc - a string representing a credit card number

RETURNS: true, if the credit card number is a valid JCB
card number.

false, otherwise
================================================================ */

function isJCB(cc)
{
first4digs = cc.substring(0,4);
if ((cc.length == 16) &&
((first4digs == "3088") ||
(first4digs == "3096") ||
(first4digs == "3112") ||
(first4digs == "3158") ||
(first4digs == "3337") ||
(first4digs == "3528")))
return isCreditCard(cc);
return false;

} // END FUNCTION isJCB()



/* ================================================================
FUNCTION: isAnyCard()

INPUT: cc - a string representing a credit card number

RETURNS: true, if the credit card number is any valid credit
card number for any of the accepted card types.

false, otherwise
================================================================ */

function isAnyCard(cc)
{
if (!isCreditCard(cc))
return false;
if (!isMasterCard(cc) && !isVisa(cc) && !isAmericanExpress(cc)
    && !isDinersClub(cc) && !isDiscover(cc) && !isEnRoute(cc) && !isJCB(cc)) {
return false;
}
return true;

} // END FUNCTION isAnyCard()

