﻿
function isBillingSame() {
    var hidden = $('BillingSame_ClientID');
    if (hidden != null) {
        if ($(hidden.value).checked) {
            return true;
        }
    }
    return false;
}

var _autoUpdateCardHolder;
function userFullName_Changed() {
    _autoUpdateCardHolder = ($defined(_autoUpdateCardHolder)) ? _autoUpdateCardHolder : ($($('CreditCardDiv_ClientID').value) != null);
    if (_autoUpdateCardHolder) {
        $($('CardName_ClientID').value).value =
            $($('UserFullName_ClientID').value).value;  
    }
}

function cardName_Changed() {
    _autoUpdateCardHolder = false;
}

function mainAddress_Changed() {
    if (isBillingSame()) {
        makeBillingAddressSame();
    }
}

function makeBillingAddressSame() {
    $($('BillingAddress_ClientID').value).value =
        $($('MainAddress_ClientID').value).value;

    $($('BillingAddress2_ClientID').value).value =
        $($('MainAddress2_ClientID').value).value;

    $($('BillingAddress3_ClientID').value).value =
        $($('MainAddress3_ClientID').value).value;

    $($('BillingCity_ClientID').value).value =
        $($('MainCity_ClientID').value).value;

    i = $($('MainCountry_ClientID').value).selectedIndex;
    $($('BillingCountry_ClientID').value).options[i].selected = true;

    i = $($('MainState_ClientID').value).selectedIndex;
    $($('BillingState_ClientID').value).options[i].selected = true;

    $($('BillingPostal_ClientID').value).value =
        $($('MainPostal_ClientID').value).value;
}

function formatPhone(n) {
    n = n.replace(/[^\d]/g, '');
    if (n.length == 10) {
        n = String.format("({0}) {1}-{2}", n.substring(0, 3), n.substring(3, 6), n.substring(6));
    } else if (n.length == 7) {
        n = String.format("{0}-{1}", n.substring(0, 3), n.substring(3));
    }

    return n;
}
//TODO: Remove when added to MooTools proper.
String.prototype.chunk = function(n) {
    if (typeof n=='undefined') n=2;
        return this.match(RegExp('.{1,'+n+'}','g'));
};

function formatCard(n) {
    n = n.replace(/[^\d]/g, '');
    n = n.chunk(4).join(' ');

    return n;
}

function formatPostal(n) {
    n = n.replace(/[^\d]/g, '');

    // Need update for Canadian postals
    //if (n.length > 5)
    //    n = String.format("{0}-{1}", n.substring(0, 5), n.substring(5));

    return n;
}

function fadeFormOnButtonClick() {
    var aspnetForm = $(document.aspnetForm);

    aspnetForm.addEvent('submit', window.blur);
    aspnetForm.getElements('input[type="submit"]')
    .each(function(input) {
        input.addEvent('click', function() {
            var div = new Element('div', {
                'opacity': .3,
                'styles': {
                    'background': 'white',
                    'position': 'absolute',
                    'top': 0,
                    'left': 0,
                    'width': '100%',
                    'height': '100%'
                }
            });

            var table = input.getParent('table');
            if ($chk(table)) {
                div.inject(table, 'after');
            }
        });

        input.blur();
    });
}

window.addEvent('domready', fadeFormOnButtonClick);
