// ---------------------------------------------------------------------
function CheckCC(CC_Num,CC_Type)
// ---------------------------------------------------------------------
    {
// ---------------------------------------------------------------------
// CCN_digits stores just the digits from the Credit Card Number
// ---------------------------------------------------------------------
    var CCN_digits = ""

// ---------------------------------------------------------------------
// get the digits from the entered Card Number
// Note - the isNaN (Not a Number) function is not used 
// because it is not supported by JavaScript 1.0
// ---------------------------------------------------------------------
    for (var i = 0; i < CC_Num.length; i++)
        {
        if ((CC_Num.charAt(i) == "0") || 
            (CC_Num.charAt(i) == "1") ||
            (CC_Num.charAt(i) == "2") || 
            (CC_Num.charAt(i) == "3") ||  
            (CC_Num.charAt(i) == "4") || 
            (CC_Num.charAt(i) == "5") || 
            (CC_Num.charAt(i) == "6") || 
            (CC_Num.charAt(i) == "7") || 
            (CC_Num.charAt(i) == "8") || 
            (CC_Num.charAt(i) == "9"))
            {
             CCN_digits = CCN_digits + CC_Num.charAt(i);
            }
        }

// ---------------------------------------------------------------------
// validcard is the true/false indicator for a valid card 
//  - it is returned to the calling routine.
// ---------------------------------------------------------------------
    var validcard = false;

// ---------------------------------------------------------------------
// msgind is used to communicate the type of alert to 
// post in case of a problem
//    1=invalid prefix (prefix does not match card type)
//    2=invalid number of digits in card number 
//        for the card type selected
// ---------------------------------------------------------------------
    var msgind = 0;

// ---------------------------------------------------------------------
// Check the card for having a valid prefix and number of  
// digits (length) for the card type.
// Note - the if, else if construct was used here because 
// switch/case is not supported by JavaScript 1.0
// ---------------------------------------------------------------------
// VALID LENGTH AND PREFIX VALUES
// --------------------------------------------------------------------- //  AMEX Discover Master Card Visa 
// LENGTHS 15 16 16 13/16 
// PREFIXES 34 6011 51 4 
//  37  52  
//    53  
//    54  
//    55  
 

    if (CC_Type == "American Express")
        if (CCN_digits.length == 15)
            if ((CCN_digits.substring (0, 2) == "34") || 
                (CCN_digits.substring (0, 2) == "37"))
                validcard = true;
            else
                msgind = 1;
        else    
            msgind = 2;
    else if (CC_Type == "Discover")
        if (CCN_digits.length == 16)
            if (CCN_digits.substring (0, 4) == "6011")
                validcard = true;
            else
                msgind = 1;
        else    
            msgind = 2;
    else if (CC_Type == "Master Card")
        if (CCN_digits.length == 16)
            if ((CCN_digits.substring (0, 2) >= "51") && 
                (CCN_digits.substring (0, 2) <= "55"))
                validcard = true;
            else
                msgind = 1;
        else    
            msgind = 2;
    else if (CC_Type == "Visa")
        if ((CCN_digits.length == 16) || 
            (CCN_digits.length ==13))
            if (CCN_digits.substring (0, 1) == "4")
                validcard = true;
            else
                msgind = 1;
        else    
            msgind = 2;
    else
// ---------------------------------------------------------------------
// Invalid card type - this should be impossible to reach as 
// long  as all valid card types are in the list above....
// ---------------------------------------------------------------------
        alert ("Sorry, "+ CC_Type + " is not currently being accepted - please contact us by phone or email.");

    if (!validcard)
        {
        if (msgind == 1)
// ---------------------------------------------------------------------
//            Invalid prefix
// ---------------------------------------------------------------------
            alert ("The Card Number ("+CC_Num + ") and the Card Type (" + CC_Type + ") do not match.");

        else if (msgind == 2)
// ---------------------------------------------------------------------
//            Invalid number of digits (length)
// ---------------------------------------------------------------------
            alert ("The Card Number ("+CC_Num + ") is not the right length for the Card Type (" + CC_Type + ").");
        }

    if (!validcard)
        return (validcard);

// ---------------------------------------------------------------------
// Perform the mod10 check sum routine on the 
//  digits in the card number
//    1) Go through the Credit Card Number digits, starting on 
//        the RIGHT.
//            If the position is odd
//                 add the digit to the checksum tally. 
//            If the position is even
//                 multiply the digit by 2
//                 if the result is greater than 9
//                     divide the result by 10 
//                     and add the remainder 
//                         to the checksum tally
//                     add 1 to the checksum tally
//                 if the result is 9 or less
//                     add the result to the checksum tally
//        Repeat for each digit.
//    2) Divide the checksum tally by 10
//    3) If there is a remainder
//                     the Credit Card Number is not valid.
// ---------------------------------------------------------------------
    var CheckSum = 0;
// ---------------------------------------------------------------------
// for loop to look at the Credit Card Number
// ---------------------------------------------------------------------
    for (var x = 1; x <= CCN_digits.length; x++)
        {
// ---------------------------------------------------------------------
// x is subtracted from the length of the CCN 
// to point at the digits from RIGHT to LEFT
// ---------------------------------------------------------------------
        var CurrentDigit = CCN_digits.charAt
                                            (CCN_digits.length - x);
        if (x % 2 == 0)
            {
// ---------------------------------------------------------------------
// even position in credit card number 
// (2nd, 4th, etc. from RIGHT of Credit Card Number)
// ---------------------------------------------------------------------
            var WorkDigit = CurrentDigit * 2;    
            if (WorkDigit > 9)
                { 
                CheckSum = CheckSum + (1 - 0);
                CheckSum = CheckSum + (WorkDigit % 10);
                }
            else
                {
                CheckSum = CheckSum + (WorkDigit - 0);
                }     
            }
        else
            {
// ---------------------------------------------------------------------
// odd position in credit card number 
// (1st, 3rd, etc. from RIGHT of Credit Card Number)
// ---------------------------------------------------------------------
            CheckSum = CheckSum + (CurrentDigit - 0);
            }
        }
// ---------------------------------------------------------------------
// end for loop
// ---------------------------------------------------------------------

    if (CheckSum % 10) 
        { 
// ---------------------------------------------------------------------
// The CheckSum does not divide evenly by 10
// ---------------------------------------------------------------------
        validcard = false; 
        alert ("The Card Number ("+ CC_Num +") is not a valid credit card number."); 
        } 
    return (validcard); 
    }
// ---------------------------------------------------------------------
// end function 
// ---------------------------------------------------------------------

