﻿/*
 * jQuery creditcard2 extension for the jQuery Validation plugin (http://plugins.jquery.com/project/validate).
 * Ported from http://www.braemoor.co.uk/software/creditcard.shtml by John Gardner, with some enhancements.
 *
 * Author: Jack Killpatrick
 * Copyright (c) 2008 iHwy, Inc.
 *
 * Version 1.0.0 (11/17/2008)
 * Tested with jquery 1.2.6, but will probably work with earlier versions.
 *
 * History:
 * 1.0.0 - released 2008-11-17
 *
 * VJ edit: removed validation of card type, since that isn't applicable for our application.
 *
 * Visit http://www.ihwy.com/labs/jquery-validate-credit-card-extension.aspx for usage information
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
*/
 
jQuery.validator.addMethod("creditcard2", function(value, element, param) {
    value = value.replace (/[\s-]/g, "");
    if (value.length == 0) { return false; }
    var cardNo = value;
    var cardexp = /^[0-9]{13,19}$/;
    if (!cardexp.exec(cardNo)) { return false; }
    cardNo = cardNo.replace(/\D/g, "");
    var checksum = 0;
    var mychar = "";
    var j = 1;
    var calc;
    for (i = cardNo.length - 1; i >= 0; i--) {
        calc = Number(cardNo.charAt(i)) * j;
        if (calc > 9) {
            checksum = checksum + 1;
            calc = calc - 10;
        }
        checksum = checksum + calc;
        if (j ==1) {j = 2} else {j = 1};
    } 
    if (checksum % 10 != 0) {return false; }
    return true;
}, jQuery.validator.messages.creditcard);
