
// JScript File
function Mediachase_Checkout() {
    this.PaymentTabs = null;
    this.Validator = null;

    this.RedirectFromPopupWindow = function(url) {
        if (self.opener != null && !self.opener.closed && url != null) {
            self.opener.document.location.href = url;
            self.opener.focus();
        }
    };

    this.init = function() {
        /*
        $.validator.addMethod("required", function(value, element) {
        var $element = $(element)
        function match(index) {
        return false;  //current == index && $(element).parents("#sf" + (index + 1)).length;
        }
        if (match(0) || match(1) || match(2)) {
        return !this.optional(element);
        }
        return "dependency-mismatch";
        }, $.validator.messages.required);
        */
        
        this.Validator = $(theForm).validate({
            submitHandler: function(form) {
                return true;
            },
            onsubmit: false,
            ignore: ".ignore",

            highlight: function(element, errorClass, validClass) {
                $(element).addClass(errorClass);
                $(element).parent().addClass(errorClass);
                $(element).parent().parent().addClass(errorClass);
                $(element).parent().parent().removeClass(validClass);
            },

            unhighlight: function(element, errorClass, validClass) {
                $(element).removeClass(errorClass);
                $(element).parent().removeClass(errorClass);
                $(element).parent().parent().removeClass(errorClass);
                $(element).parent().parent().addClass(validClass);
            }
        });

        $("input.mui_checkout-button").bind("click", function() { return CSCheckout.Validator.form(); });
        this.initPaymentTabs();
        this.initAddresses();
        this.initBillingAddresses();
    };

    this.initPaymentTabs = function() {
        $("div.mui_checkout-payment-radio > input").each(function(i) {
            if (this.checked)
                CSCheckout.switchTab(i);
        });
    };

    this.initAddresses = function() {
        var radioList = $("input:radio[name=RadioButtonsAddressSelect]:last");
        var checked = true;
        
        if (radioList.length)
            checked = radioList[0].checked;
            
        this.toggleNewAddress(checked);
    };

    this.toggleNewAddress = function(show) {
        if (show)
            CSCheckout.hideShow(true, $("#NewShippingAddressPanel"));
        else
            CSCheckout.hideShow(false, $("#NewShippingAddressPanel"));
    };

    this.initBillingAddresses = function() {
        var radioList = $("input:radio[name=RadioButtonsBillingAddressSelect]:last");
        var checked = true;

        if (radioList.length)
            checked = radioList[0].checked;

        this.toggleNewBillingAddress(checked);
    };

    this.toggleNewBillingAddress = function(show) {
        if (show)
            CSCheckout.hideShow(true, $("#NewBillingAddressPanel"));
        else
            CSCheckout.hideShow(false, $("#NewBillingAddressPanel"));
    };

    this.switchTab = function(index) {
        $("div.mui_checkout-payment-panel").each(function(i) {
            if (index == i) {
                CSCheckout.hideShow(true, this);
            }
            else {
                CSCheckout.hideShow(false, this);
            }
        });
    };

    this.hideShow = function(show, element) {
        if (show) {
            $(element).show();
            $(element).find("input").removeClass("ignore");
            $(element).find("select").removeClass("ignore");

        }
        else {
            $(element).hide();
            $(element).find("input").addClass("ignore");
            $(element).find("select").addClass("ignore");
        }
    };
};

var CSCheckout = new Mediachase_Checkout();
