
var win = null;
var t = null;
var winuid = Math.round(Math.random() * 999999999);
function browse(url) {
    try {
        win = window.open(url, ('user_list_' + winuid), 'resizable=1, scrollbars=1,toolbar=0,location=0,width=750,height=650');
    }
    catch (e) {
        return;
    }
}
function check() {
    if (win == null && t != null) clearInterval(t);
    if (win != null) {
        try {
            var test = win.document;
        }
        catch (e) {
            win = null;
        }
    }
}

function cls() {
    win.close();
    win = null;
    if (t != null) {
        clearInterval(t);
    }
    t = null;
}


function clearField(controlID, value) {
    var control = document.getElementById(controlID);
    if (control.value == value) {
        control.value = '';
    }
}

function clearPasswordField(controlID) {
    var control = document.getElementById(controlID);
    if (control.value == '************') {
        control.value = '';
    }
}


function changeState(obj, catId) {
    var catContainer = document.getElementById('category_' + catId);
    if (catContainer.style.display == 'none') {
        catContainer.style.display = 'block';
        obj.className = obj.className.replace('collapse', 'expand');
    }
    else {
        catContainer.style.display = 'none';
        obj.className = obj.className.replace('expand', 'collapse');
    }
}

function changeStateTo(obj, catId, state) {
    var catContainer = document.getElementById('category_' + catId);
    if (state == 'block') {
        catContainer.style.display = 'block';
        obj.className = obj.className.replace('collapse', 'expand');
    }
    else {
        catContainer.style.display = 'none';
        obj.className = obj.className.replace('expand', 'collapse');
    }
}

function expandAll(button) {
    var expand = false; ;
    if (button.className == "expand-image-button") {
        button.className = "collapse-image-button";
        expand = true;
    }
    else {
        button.className = "expand-image-button";
        expand = false;
    }
    var menuContainer = document.getElementById('menu');
    if (menuContainer) {
        var controlList = menuContainer.getElementsByTagName("div");
        for (i = 0; i < controlList.length; i++) {
            if (controlList[i].id.indexOf("category_arrow_") >= 0) {
                var catId = controlList[i].id.split('_')[2];
                if (expand)
                    changeStateTo(controlList[i], catId, "block");
                else
                    changeStateTo(controlList[i], catId, "none");
            }
        }
    }
}

function $(id) {
    return document.getElementById(id);
}

function SetItems(ContainerID) {
    var el, el2;
    var array = document.getElementById(ContainerID).childNodes;
    var menuList = document.getElementById('menu').getElementsByTagName("input");

    for (j = 0; j < menuList.length; j++) {
        if (menuList[j].type != "button")
            menuList[j].value = '';
    }
    for (i = 0; i < array.length; i++) {
        el = array[i];
        if (el && el.id != '' && el.childNodes && el.childNodes.length > 0 && el.childNodes[0].tagName == 'TD') {

            var inputRadioList = el.getElementsByTagName("INPUT")
            var radioButton = null;
            for (g = 0; g < inputRadioList.length; g++) {
                if (inputRadioList[g].type == "radio") {
                    radioButton = inputRadioList[g];
                    break;
                }
            }



            if (radioButton && radioButton.checked == true) {
                var tableContainer = document.getElementById('category_' + radioButton.value + '_order').getElementsByTagName("TABLE")[0];
                var inputList = tableContainer.getElementsByTagName("input");
                for (k = 0; k < inputList.length; k++) {
                    if (inputList[k].type == "text" && inputList[k - 1].checked) {
                        objName = "countitem_" + inputList[k].id.split('_')[3] + "_" + inputList[k].id.split('_')[4];
                        obj = document.getElementById(objName);
                        if (obj) {
                            obj.value = inputList[k].value;
                        }
                    }
                }
            }
        }
    }
}

function SetValue() {

}


function changeQuantity(id, value) {
    var obj = $(id);
    var quantity = obj.value == '' ? 0 : parseInt(obj.value);
    quantity = quantity + value;
    quantity = quantity > 0 ? quantity : '';
    obj.value = quantity + '';
}

function runCombo() {
    jQuery('div.combo-item div.red-category').click(function() {
        if (jQuery(this).parent().find('div.combo-products:hidden').length > 0) {
            jQuery(this).parent().find('div.combo-products').show();
        } 
        else {
            jQuery(this).parent().find('div.combo-products').hide();
        }
    });

    jQuery('.combo-item-category').click(function() {
        var container = jQuery(this).next();
        while (container.hasClass('menu-item-template')) {
            if (jQuery(this).hasClass('open')) {
                container.hide();
            }
            else {
                container.show();
            }
            container = container.next();
        }
        if (jQuery(this).hasClass('open')) {
            jQuery(this).removeClass('open');
        }
        else {
            jQuery(this).addClass('open');
        }
    });

    jQuery('div.combo-item input[type="checkbox"]').click(function() {
        var counter = jQuery(this).parents('div.combo-item').find('span.count');
        var count = parseInt(counter.html());
        if (this.checked) {
            var item = jQuery(this).parents('div.menu-item-template').prevAll('div.combo-item-category').eq(0).next();
            while (item.hasClass('menu-item-template')) {
                if (item.find('input[type="checkbox"]').not(this).length > 0) {
                    item.find('input[type="checkbox"]').each(function() {
                        if (this.checked) {
                            this.checked = false;
                            count--;
                        }
                    });

                }
                item = item.next();
            };
            counter.html(count);
            if (jQuery(this).ValidateMaxCount()) {
                

                counter.html(count + 1);
            }
            else {
                this.checked = false;
            }
        }
        else {
            counter.html(count - 1);
        }
    });

    jQuery.fn.ValidateMaxCount = function() {
    var counter = jQuery(this).parents('div.combo-item').find('span.count').eq(0);
        var count = parseInt(counter.html());
        var maxCounter = jQuery(this).parents('div.combo-item').find('span.maxCount').eq(0);
        var maxCount = parseInt(maxCounter.html());
        return maxCount > count;
    };
}

function UpdateSelectedComboItems()
{ }

function runMenuItems() {
    jQuery('.header').click(function() {
        jQuery('div.selectionGroupContent div.item').hide();
        jQuery('div.selectionGroupContent div.item[value = ' + $(this).attr('value') + ']').show();
    });

    jQuery('div.red-category,div.orange-category').click(function() {
        jQuery(this).parent().find('div.products').hide();
        if (jQuery(this).hasClass('open')) {
            jQuery(this).removeClass('open');
        }
        else {
            jQuery(this).next().show();
            jQuery(this).addClass('open');
        }
    });

    (function(jQuery) {
        jQuery.fn.CheckboxClickHandler = function() {
            jQuery(this).each(function() {
                jQuery(this).click(function() {
                    var price = parseFloat(jQuery(this).parent().find('input[type="hidden"]').attr('value'));
                    var totalContainer = jQuery(this).parents('.products').eq(0).prev().find('span.totalAmount');
                    if (totalContainer.length == 0) {
                        var totalContainer = jQuery(this).parents('.products').prev().find('span.totalAmount');
                    }
                    var totalString = totalContainer.html();
                    var r = /[0-9]+.[0-9]+/;
                    var totalPriceString = r.exec(totalString);
                    var totalPrice = parseFloat(totalPriceString);
                    if (this.checked) {
                        totalPrice += price;
                        $(this).parents('td').eq(0).addClass('checked');
                    }
                    else {
                        totalPrice -= price;
                        $(this).parents('td').eq(0).removeClass('checked');
                    }
                    totalContainer.html(totalContainer.html().replace(totalPriceString, roundNumber(totalPrice, 2)));
                })
            })
        }
    })(jQuery);

    jQuery(function() {
    var items = jQuery('table.modifierInnerItems input[type="checkbox"]');
        if (items != null) {
            items.CheckboxClickHandler();
        }
    });

    (function(jQuery) {
        jQuery.fn.DropdownSelectedIndexChangeHandler = function() {
            jQuery(this).each(function() {
                jQuery(this).change(function() {
                    var previosPriceContainer = jQuery(this).parent().find('input[type="hidden"]').eq(0);
                    var previosPrice = parseFloat(previosPriceContainer.val());
                    var totalContainer = jQuery(this).parents('.products').eq(0).prev().find('span.totalAmount');
                    if (totalContainer.length == 0) {
                        var totalContainer = $(this).parents('.products').prev().find('span.totalAmount');
                    }
                    var totalString = totalContainer.html();
                    var r = /[0-9]+.[0-9]+/;
                    var totalPriceString = r.exec(totalString);
                    var totalPrice = parseFloat(totalPriceString);
                    totalPrice -= previosPrice;
                    var currentPriceContainer = jQuery(this).parent().find('input[tabindex="' + this.value + '"]');
                    var price = 0;
                    if (currentPriceContainer.length > 0) {
                        price = parseFloat(currentPriceContainer.eq(0).val());
                    }
                    totalPrice += price;
                    totalContainer.html(totalContainer.html().replace(totalPriceString, roundNumber(totalPrice, 2)));
                    previosPriceContainer.val(price);
                })
            })
        }
    })(jQuery);

    jQuery(function() {
        var items = jQuery('table.modifierInnerItems select');
        if (items != null) {
            items.DropdownSelectedIndexChangeHandler();
        }
    });

    (function(jQuery) {
        jQuery.fn.RadioClickHandler = function() {
            jQuery(this).each(function() {
                $(this).change(function() {
                    var previosPriceContainer = jQuery(this).parents('.item').find('input[type="hidden"]').eq(0);
                    var previosPrice = parseFloat(previosPriceContainer.val());
                    var totalContainer = jQuery(this).parents('.products').eq(0).prev().find('span.totalAmount');
                    if (totalContainer.length == 0) {
                        var totalContainer = jQuery(this).parents('.products').prev().find('span.totalAmount');
                    }
                    var totalString = totalContainer.html();
                    var r = /[0-9]+.[0-9]+/;
                    var totalPriceString = r.exec(totalString);
                    var totalPrice = parseFloat(totalPriceString);
                    totalPrice -= previosPrice;
                    var currentPriceContainer = jQuery(this).parent().find('input[type="hidden"]').eq(0);
                    var price = 0;
                    price = parseFloat(currentPriceContainer.eq(0).val());
                    totalPrice += price;
                    totalContainer.html(totalContainer.html().replace(totalPriceString, roundNumber(totalPrice, 2)));
                    previosPriceContainer.val(price);
                    jQuery(this).parents('table').find('td').removeClass('checked');
                    jQuery(this).parents('td').eq(0).addClass('checked');
                })
            })
        }
    })(jQuery);

    jQuery(function() {
        var items = jQuery('table.modifierInnerItems input[type="radio"]');
        if (items != null) {
            items.RadioClickHandler();
        }
    });

    jQuery(function() {
        var items = jQuery('div.selectionGroupContent input[type="radio"]');
        if (items != null)
            items.each(function() { jQuery(this).click(function() { this.value = this.id; }) });
    });

}

function roundNumber(num, dec) {
    var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
    return result;
}
