﻿function jumpToPageTop() {
    window.location.hash = 'PageTop';
}

/**
* checks for datein the format MM/YY or MM/YYYY against the current date
* @param month a number representing the month to be validated against.
* @param year a number representing the year to be validated against.
*/
function isValidExpDate(month, year) {
    var result = true;
    var now = new Date();
    var nowMonth = now.getMonth() + 1;
    var nowYear = now.getFullYear();
    if ((nowYear > year) || ((nowYear == year) && (nowMonth > month))) {
        result = false;
    }

    return result;
}

/**
* checks for valid credit card format using the Luhn check and known digit
* about various cards
* @ccType a string indicating the entered credit card name
* @ccNum a string indicating the credit card number being used.
*/
function isValidCreditCardNumber(ccType, ccNum) {
    var result = true;
    if (ccNum.length > 0) {
        if (isNaN(Number(ccNum))) {
            result = false;
        }
        if (result) {
            if (!luhnCheck(ccNum) || !validateCCNum(ccType, ccNum)) {
                result = false;
            }
        }
    }
    return result;
}

function getCCType(ccNum) {
    // initially the card type is unknown
    cardtype = "UNKNOWN";

    // define card names and their matching patterns
    ccArray = { "Visa": "^4[0-9]{12}(?:[0-9]{3})?$", "MasterCard": "^5[1-5][0-9]{14}$", "Discover": "^6(?:011|5[0-9]{2})[0-9]{12}$", "American Express": "^3[47][0-9]{13}$", "Diner's Club": "^3(?:0[0-5]|[68][0-9])[0-9]{11}$", "JCB": "^(?:2131|1800|35\d{3})\d{11}$" };

    // identify the card type
    for (key in ccArray) {
        regex = new RegExp(ccArray[key]);
        if (regex.test(ccNum)) {
            cardtype = key;
            break;
        }
    }

    return cardtype;
}

function luhnCheck(str) {
    var result = true;
    var sum = 0;
    var mul = 1;
    var strLen = str.length;
    for (var i = 0; i < strLen; i++) {
        var digit = str.substring(strLen - i - 1, strLen - i);
        var tproduct = parseInt(digit, 10) * mul;
        if (tproduct >= 10) {
            sum += (tproduct % 10) + 1;
        } else {
            sum += tproduct;
        }

        if (mul == 1) {
            mul++;
        } else {
            mul--;
        }
    }

    if ((sum % 10) != 0) {
        result = false;
    }

    return result;
}

function validateCCNum(cardType, cardNum) {
    var result = false;
    cardType = cardType.toUpperCase();
    var cardLen = cardNum.length;
    var firstdig = cardNum.substring(0, 1);
    var seconddig = cardNum.substring(1, 2);
    var first4digs = cardNum.substring(0, 4);

    switch (cardType) {
        case "VISA":
            result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
            break;
        case "AMEX":
            var validNums = "47";
            result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig) >= 0);
            break;
        case "MASTERCARD":
            var validNums = "12345";
            result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig) >= 0);
            break;
        case "DISCOVER":
            result = (cardLen == 16) && (first4digs == "6011");
            break;
        case "DINERS":
            var validNums = "068";
            result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig) >= 0);
            break;
        case "IGNORE":
            //Don't perform credit card type check
            result = true;
            break;
    }

    return result;
}

function copyToClipboard(s) {
    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData("Text", s);
    }
    else {
        // You have to sign the code to enable this or allow the action in about:config by changing
        user_pref("signed.applets.codebase_principal_support", true);
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

        var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip) return;

        // create a transferable
        var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
        if (!trans) return;

        // specify the data we wish to handle. Plaintext in this case.
        trans.addDataFlavor('text/unicode');

        // To get the data from the transferable we need two new objects
        var str = new Object();
        var len = new Object();

        var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);

        var copytext = meintext;

        str.data = copytext;

        trans.setTransferData("text/unicode", str, copytext.length * [[[[2]]]]);

        var clipid = Components.interfaces.nsIClipboard;

        if (!clip) return false;

        clip.setData(trans, null, clipid.kGlobalClipboard);
    }
}

// Helper functions using curvycorners.js library
function ApplyRoundCornersDefault(elementName) {
    // Set default corner radii values
    TopLeftDefault = 8;
    TopRightDefault = 8;
    BotLeftDefault = 8;
    BotRightDefault = 8;

    ApplyRoundCorners(elementName, TopLeftDefault, TopRightDefault, BotLeftDefault, BotRightDefault);
}

function ApplyRoundCorners(elementName, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius) {
    if (typeof curvyCorners == 'function') {
        settings = { antiAlias: true, tl: {}, tr: {}, bl: {}, br: {} };
        settings["tl"] = (topLeftRadius == 0) ? false : { radius: topLeftRadius };
        settings["tr"] = (topRightRadius == 0) ? false : { radius: topRightRadius };
        settings["bl"] = (bottomLeftRadius == 0) ? false : { radius: bottomLeftRadius };
        settings["br"] = (bottomRightRadius == 0) ? false : { radius: bottomRightRadius };

        var element = document.getElementById(elementName);
        var elementBox = new curvyCorners(settings, element);
        elementBox.applyCornersToAll();

    } else {
        alert("CurvyCorners library not found");
    }
}