/*
 * Plugin: Identify Credit card type entered in a text field and calls back a function when the type is identified.
 * Description: Binds on a textfield in order to allow automatic identification of a credit card type (as user enters it).
 *
 * Dependency: ba-throttle-debounce (http://benalman.com/code/projects/jquery-throttle-debounce/docs/files/jquery-ba-throttle-debounce-js.html)
 *
 *
 * Example usage :

    $(document).ready(function() {
        $("#cardNumber").identifyCreditCardType(function(element, foundType) {
            $("#cardtype").removeClass().addClass(foundType);
        })
    });
 *
 * Author:
 *      David Marquis 2010 (david.marquis@nurun.com)
 *
 */

;(function($) {
    var CARDTYPE_NONE = "none",
        // supported card types:
        CARDTYPE_VISA = "visa",
        CARDTYPE_MASTERCARD = "mc";

    /**
     * Binds the automatic identification of credit card type to the source input fields.
     * @param cardTypeCallback the callback to be called when correct card type is identified. Callback function needs
     * two parameters :
     *      element: source element (where number was entered)
     *      string: card type identified (one of the supported card types, or 'none' if unidenfied.
     */
    $.fn.identifyCreditCardType = function(cardTypeCallback, options) {
        var settings = $.extend({
            // should we check length of credit card number before identifying the card type ? If false, card type will be identified only from first digits.
            checkLengths: false,
            // should we debounce execution of the card type identification (so that the callback is not called on every keypress)
            debounce: true
        }, options);

        return this.each(function(idx, el) {
            var onKeypress = function() {
                cardTypeCallback(this, getCardType($(this).val(), settings.checkLengths));
            };
            if (settings.debounce) {
                // setup debounce function to prevent calling our keypress method too often
                onKeypress = $.debounce(250, onKeypress);
            }
            $(el).keypress(onKeypress);
        });
    };

    /**
     * Identifies the type of card from the card number provided.
     * @param num card numbe from which to identify the card type
     * @param checkLengths card should we check lengths as well as start of number ?
     * @return one of the supported card types
     */
    function getCardType(num, checkLengths) {
        var cctype = CARDTYPE_NONE;
        if (num.substring(0, 1) == '4')
            if (!checkLengths || ((num.length == '13') || (num.length == '16'))) {
                cctype = CARDTYPE_VISA;
            }
        if ((num.substring(0, 2) >= '51') && (num.substring(0, 2) <= '55'))
            if (!checkLengths || (num.length == '16')) {
                cctype = CARDTYPE_MASTERCARD;
            }
//        if (num.substring(0, 4) == '6011')
//            if (!checkLengths || (num.length == '16')) {
//                cctype = CARDTYPE_DISCOVER;
//            }
//        if ((num.substring(0, 1) == '3') || (num.substring(0, 4) == '1800') || (num.substring(0, 4) == '2131'))
//            if (!checkLengths || ((num.length == '15') || (num.length == '16'))) {
//                cctype = "jcb";
//            }
//        if ((num.substring(0, 2) == '34') || (num.substring(0, 2) == '37'))
//            if (!checkLengths || (num.length == '15')) {
//                cctype = CARDTYPE_AMEX;
//            }
//        if ((num.substring(0, 3) == '300') || (num.substring(0, 3) == '305') || (num.substring(0, 2) == '36') || (num.substring(0, 2) == '38'))
//            if (!checkLengths || (num.length == '14')) {
//                cctype = "diners";
//            }
        return cctype;
    }
})(jQuery);
