function toggleSameBilling(select) {
    switch (select.value) {
        case 'cod':{
            document.getElementById('sameBilling').disabled = true;
            document.getElementById('sameBilling').setAttribute('checked', 'checked');
            break;
        }
        case 'cc':{
            document.getElementById('sameBilling').disabled = false;
            document.getElementById('sameBilling').removeAttribute('checked');
            break;
        }
    }
}

function toggleBillingOption() {
    var options = document.getElementById('billingOption');
    if (options != null) {
        if (options[options.selectedIndex] != null) {
            $$('.payment-option-pane').each(function (element, index) {
                if (element.hasClass(options[options.selectedIndex].value + '-form-fields')) {
                    element.style.display = '';
                } else {
                    element.style.display = 'none';
                }
            });
        }
    }
}
function isInteger( string ) {
    return !isNaN( parseInt( string ) );
}
function checkLinkFollow(ev) {
	if(!confirm("Are you sure you want to delete the following " + this.title + "?")) {
		new Event(ev).stop()
	}
}

function toggleTab(tabName){
    $$('.tab').each(function (element, index) {
        //set the tabs to selected state
        if (element.hasClass('tab-' + tabName)) {
            element.addClass('selected');
        } else {
            element.removeClass('selected');
        }
    });

    $$('.pane').each(function (element, index) {
       if (element.hasClass(tabName)) {
           element.style.display = '';
       } else {
           element.style.display = 'none';
       }
    });
}

function updateCartAmount() {

    updating = document.getElementById('updating-container');
    cartTotal = document.getElementById('navCartTotal');
    if (updating != null && cartTotal != null) {
        updating.style.display = '';
        cartTotal.style.display = 'none';
        url = "blank.html?controller=UIFrontend&action=getCartSubTotal";
        new Ajax(url, {
            method: 'get',
            update: $('amount-in-cart'),
            onComplete: function(){
                updating.style.display = 'none';
                cartTotal.style.display = '';
            }
        }).request();
    }
    return false;
}

window.addEvent('domready', function() {
    var selectedCategoryIndex = document.getElementById('selectedCategoryIndex');
    var accordionIndex = null;
    if (selectedCategoryIndex != null) {
        accordionIndex = selectedCategoryIndex.getAttribute('title');
    }
    accordionIndex = ((isInteger(accordionIndex))?parseInt(accordionIndex):-1);

    var accordion = new Accordion('a.atStart', 'div.atStart', {
                                    opacity: false,
                                    alwaysHide: true,
                                    show: accordionIndex,
                                    onActive: function(toggler, element) {
                                        toggler.addClass('subnav-selected');
                                        toggler.addClass('subnav-active');
                                        $$('a.no-toggler').each(function (element, index) {
                                            element.removeClass('subnav-selected');
                                        });
                                    },

                                    onBackground: function(toggler, element) {
                                        if (toggler != null) {
                                            toggler.removeClass('subnav-selected');
                                            toggler.removeClass('subnav-active');
                                        }
                                    }

                                }, $('accordion'));

    $$('a.atStart').each(function (element, index) {

        element.onmouseover = function(event) {
            this.addClass('subnav-selected');
        }

        element.onmouseout = function(event) {
            if (!this.hasClass('subnav-active')) {
                this.removeClass('subnav-selected');
            }
        }
    });

	$$('a.confirm').addEvent('click', checkLinkFollow);

});