﻿/*
Custom jQuery written by Angel Grablev for Permilia
*/
$(document).ready(function() {
    $("a.popup").click(function() {
        var href_link = $(this).attr("href");
        window.open(href_link, "popup", "menubar=no,width=430,height=360,toolbar=no,status=no,directories=no,scrollbars=yes");
        return false;
    });
    $(".more").live("click", function() {
        $(".extra_information").slideDown(); //  you can replace the slideDown with fadeIn or just show. you can also add slideDown("slow") for the effect to happen slower.		
        $(".extra_information input, .extra_information select").each(function() {
            $(this).attr("required", "required");
        });
    });
    $(".less").live("click", function() {
        $(".extra_information").slideUp(); //  you can replace the slideUp with fadeOut or just hide	
        $(".extra_information input, .extra_information select").each(function() {
            $(this).removeAttr("required");
        });
    });
    $("select[type=select]").change(function() {
        if ($(this).val() != 0) {
            $(this).removeClass("error");
        }
    });
    $.tools.validator.fn("[type=credit]", "", function(input, value) {
        return isValidCardNumber(value);
    });
    $.tools.validator.fn("[type=select]", "", function(input, value) {
        if (input.attr("required")) {
            return value != 0;
        } else {
            return true;
        }

    });
    // is valid credit card number
    function isValidCardNumber(strNum) {
        var nCheck = 0; var nDigit = 0; var bEven = false;

        for (n = strNum.length - 1; n >= 0; n--) {
            var cDigit = strNum.charAt(n);
            if (isDigits(cDigit)) {
                var nDigit = parseInt(cDigit, 10);
                if (bEven) {
                    if ((nDigit *= 2) > 9)
                        nDigit -= 9;
                }
                nCheck += nDigit;
                bEven = !bEven;
            }
            else if (cDigit != ' ' && cDigit != '.' && cDigit != '-') {
                return false;
            }
        }
        return (nCheck % 10) == 0;
    }

    // is valid number (just checks for actual numbers)
    function isDigits(val) {
        var intRegex = /^\d+$/;

        if (intRegex.test(val)) {
            return true;
        } else { return false }
    }

    // checks if the credit card number is a valid card format 
    function isCardTypeCorrect(ccnum) {
        if (ccnum.match("^6011[0-9]{12}$") || ccnum.match("^3[47][0-9]{13}$") || ccnum.match("^4([0-9]{15}$|[0-9]{12}$)") || ccnum.match("^5[1-5][0-9]{14}$"))
            return true;
        else
            return false;
    }
    $(".exp").change(function() {
        if (isDateValid()) {
            $(".exp").removeClass("error");
        }
    });
    $.tools.validator.fn("[type=date]", "", function(input) {
        return isDateValid();
    });
    // checks if todays date is in the past
    function isDateValid() {

        var d = new Date();
        var curr_month = d.getMonth();
        var curr_year = d.getFullYear();
        var month_input, year_input;

        var isYearValid, isMonthValid;

        $(".expiration").each(function(e) {
            if ($(this).attr('name') == "ExpirationYear") {
                year_input = "20" + $(this).val();
            } else {
                month_input = $(this).val();
            }
        });

        if (year_input > curr_year) {
            $(".expiration").removeClass("error");
            return true;
        } else if (year_input == curr_year) {
            if (month_input >= curr_month + 1) {
                $(".expiration").removeClass("error");
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }
    var darken = '<div class="darken" style="display:none;"></div>';
    var cvv_popup = '<div class="cvv_popup" style="display:none;"><div class="close_cvv">Close X</div><span class="cvv_header">What is the CVV Number?<br></span><!-- end cvv_header -->CARD VERIFICATION VALUE CODE (CVV)<br /><img src="https://jqueryunique.s3.amazonaws.com/images/cvv.gif" alt="" width="350" height="320" border="0"><span class="cvv_more_info">More Information:</span><!-- end cvv_more_info --><div class="cvv_right_col"><p>CVV is a new authentication procedure established by credit card companies to further efforts towards reducing fraud for internet transactions. It consists of requiring a card holder to enter the CVV number in at transaction time to verify that the card is on hand.The CVV code is a security feature for "card not present" transactions (e.g., Internet transactions), and now appears on most (but not all) major credit and debit cards. This new feature is a three- or four-digit code which provides a cryptographic check of the information embossed on the card. Therefore, the CVV code is not part of the card number itself.</p><p>The CVV code helps ascertain that the customer placing the order actually possesses the credit/debit card and that the card account is legitimate. Each credit card company has its own name for the CVV code, but it functions the same for all major card types. (VISA refers to the code as CVV2, MasterCard calls it CVC2, and American Express calls it CID.)</p><p>The back panel of most Visa/MasterCard cards contain the full 16-digit account number, followed by the CVV/CVC code. Some banks, though, only show the last four digits of the account number followed by the code. To aid in the prevention of fraudulent credit card use, we now require the 3 or 4 digit code on the back of your credit card. When you submit your credit card information your data is protected by Secure Socket Layer (SSL) technology certified by a digital certificate.</p><!-- END OF DB --></div></div>';
    $("body").append(cvv_popup).append(darken);
    $(".whats_cvv").live("click", function() {
        $(".cvv_popup").fadeIn();
        $(".darken").show().animate({ opacity: 0.6 });
    });
    $(".close_cvv, .darken").live("click", function() {
        $(".cvv_popup").fadeOut();
        $(".darken").hide();
    });
});
