﻿
function takeoutMenuInitialize() {
    var needUpdateCufon = true;
    $('.takeout-menu li .category-name').click(function() {
        $('.takeout-menu ul li').removeClass('open');
        $(this).parent().addClass('open');
        //Cufon.replace('.menu-top-level .category-name');
        //Cufon.replace('.category-name,.product-name');
        //Cufon.now();
    });

    $('.takeout-menu li li').mouseover(function() {
        $(this).addClass('hover');
        //Cufon.replace($(this).parent().find('.product-name'));
        //Cufon.now();
    });

    $('.takeout-menu li li').mouseout(function() {
        $(this).removeClass('hover');
        //Cufon.replace($(this));
        //Cufon.now();
    });
}

function openFirstCategory() {
    $('.takeout-menu li .category-name').first().click();
}

function initErrorPopup(cssClass) {

    //var btnOk = 'КнопкаРус';
    var buttonOpts = {};
    buttonOpts[btnOk] = function() {
        $(this).dialog("close");
    };

    //jQuery('.error-box').remove();
    jQuery(cssClass).not('form ' + cssClass).remove();
    jQuery(cssClass).dialog({
        bgiframe: true,
        resizable: false,
        autoOpen: false,
        modal: true,
        position: 'center',
        dialogClass: 'error-box',
        buttons: buttonOpts

    });
}

function tip() {
    var customerName = $('.order-name').val();
    var message = 'Order is complete.<br/> Click here to add these item(s) to your bag.';
    if (customerName != '') {
        message = customerName + "'s order is complete.<br/> Click here to add these item(s) to your bag.";
    }
    $('#bag_it').html(message);
    TagToTip('bag_it');
}

function deletebuttontip(customerName) {
    var message = 'Click here to delete '+ customerName + "'s bag.";
    $('#bag_it').html(message);
    TagToTip('bag_it');
}

function ChangeHideInputValue() { }
function initLoadingHint() { }
function autoMove(length, maxlength, controlID) {
    if (length == maxlength) {
        controlID.focus();
    }
}

function initCustomerChoose(creationType) {
    $('.customer-form input[type="radio"]').click(function() {
        $('table.signUpForm').hide();
        $(this).parent().find('table').show();
    });
    $('.customer-form input[value="' + creationType + '"]').not('input[type="button"]').click();
}

function initScroll() { }

function changeOptionsVisibility(sender,cssClass) {
    var options = $(cssClass);
    var image = $(sender).parent().parent().find('img.collaps');
    if (options.is(":visible")) {
        image.attr('src','files/images/icon_plus.png');
        options.hide();
    }
    else {
        image.attr('src', 'files/images/icon_minus.png');
        options.show();
    }
}

function changeStartpointVisibility() {
    var startPoint = $('.start-point');
    if (startPoint.is(':visible')) {
        startPoint.hide();
    }
    else {
        startPoint.show();
    }
}

function initDrivingDirections() {
    var cb = $('input[id$="sendDirection"]:checked');
    if (cb.length >0) {
        changeStartpointVisibility();   
    }
}

function initPaymentType() {
    if ($('#PaymentType').val() == 1) {
        $('input[type="radio"][id="r2"]').attr('checked', 'checked');
    } else {
        $('input[type="radio"][id="r1"]').attr('checked', 'checked');
    }
    var paymentType = $('input[type="radio"][id^="r"]:checked');
    if (paymentType.length == 0) {
        $('input[type="radio"][id="r1"]').attr('checked', 'checked');
    }
    paymentType = $('input[type="radio"][id^="r"]:checked');
    changePaymentFormVisibility(parseInt(paymentType.val()));
};

function changeOptionsVisibilityImg(img, cssClass) {
    var options = $(cssClass);
    var image = $(img);
    if (options.is(":visible")) {
        image.attr('src', 'files/images/icon_plus.png');
        options.hide();
    }
    else {
        image.attr('src', 'files/images/icon_minus.png');
        options.show();
    }
}

function changePrevOrderVisibility(sender) {
    $('table.prev-order table').hide();
    $('.order' + sender.value).show();
}

function initMenu() {
    $('ul.nav span.categoryName').click(function() {
        var ul = $(this).next();
        if (ul.is(':visible')) {
            ul.hide();
        }
        else {
            ul.show();
        }
    });
 }
 function scrollToTop() {
    Sys.Application.add_init(appl_init);

    function appl_init() {
       var pgRegMgr = Sys.WebForms.PageRequestManager.getInstance();       
       pgRegMgr.add_endRequest(EndHandler);
    }   

    function EndHandler() {
       window.scroll(0,100);
    }
 }

 function productAddedToOrder() {
    $('.productAddedToOrder').show();
 }

function orderCollapsor() {
    $(this).parent().children('.dropBlock').slideToggle(200);
    $(this).parent().toggleClass('collapsed');
    if ($(this).html() == '+') {
        $(this).html('&mdash;');
    }
    else {
        $(this).html('+');
    }
}

function deletePrevOrder(bag) {
    var bags = $(bag);
    var prevInOrder = bags.eq(0).parent().parent().find('tr').length;
    if (prevInOrder > bags.length) {
        bags.remove();
    }
    else {
        Tip('You can not remove the last Bag.');
        setTimeout(function() { UnTip(); }, 2000);
    }
}

function setLocationFormTop(locationId) {
    var input = $('input[value="location_' + locationId + '"]');
    $('.currentLocationContainer').css('top',input.position().top + 'px');
}

function changeStyle(elem) {
    if (elem.type == "checkbox") {
        var itemSel = $(elem).parent();
        if (itemSel.hasClass('checked')) {
            itemSel.removeClass('checked');
        } else {
            itemSel.addClass('checked');
        }
    } else {
        var radioBlock = $(elem).parent().parent();
        radioBlock.children(".checked").removeClass('checked');
        var itemSel = $(elem).parent();
        itemSel.addClass('checked');
    }
}

(function($) {
    $.fn.collapsor = function(openClass, panelElement) {
        var trigger = this;
        var panel = panelElement;
        return this.each(function() {
            $(this).click(function() {
                if ($(this).hasClass(openClass)) {
                    $(this).removeClass(openClass);
                    $(this).next(panel).hide();
                }
                else {
                    $(this).addClass(openClass);
                    $(this).next(panel).show();
                }
            });
        });
    };
})
(jQuery);
$(function() {
    if ($('table.collapsableTable thead') != null) {
        $('table.collapsableTable thead').collapsor('opened', '.collapsableTable tbody');
    }
});

(function($) {
    $.fn.collapseAll = function(openClass, panelElement, headerElement, buttonValueOpened, buttonValueClosed) {
        var trigger = this;
        var panel = panelElement;
        var header = headerElement;
        return this.each(function() {
            $(this).click(function() {
                if ($(headerElement).hasClass(openClass)) {
                    $(headerElement).removeClass(openClass);
                    $(headerElement).next(panel).hide();
                    $(this).attr('value', buttonValueClosed);
                }
                else {
                    $(headerElement).addClass(openClass);
                    $(headerElement).next(panel).show();
                    $(this).attr('value', buttonValueOpened);
                }
            });
        });
    };
})
(jQuery);

$(function() {
if ($('#collapseAll') != null) {
    $('#collapseAll').collapseAll('opened', '.collapsableTable tbody', '.collapsableTable thead', '- Collapse All', '+ Expand All');
    }
});
function MyClass() {
    this.UpdateEditorFormValue = function() {
        for (i = 0; i < parent.frames.length; ++i)
            if (parent.frames[i].FCK)
            parent.frames[i].FCK.UpdateLinkedField();
    };
    this.UpdateEditorFormValueParent = function(site) {
        for (i = 0; i < site.parent.frames.length; ++i) {
            if (site.parent.frames[i].FCK) {
                site.parent.frames[i].FCK.UpdateLinkedField();
            }
        }
    };
}

var MyObject = new MyClass();

function ShowSavePopup(sender, okScript, canselScript) {
    canselScript = canselScript.replace('this.id', '\'' + sender.id + '\'');
    eval(canselScript);
}

function doAjaxControlPostBack(containerID, controlID, eventArgs) {
    document.getElementById('CustomPostBackEventArgs' + containerID).value = eventArgs;
    __doPostBack(controlID, '');
}

function setClass(obj, cl) {
    if (obj != null && obj.className != cl) {
        obj.className = cl;
    }
}

function passwordStrength(password) {
    var desc = new Array();
    desc[0] = "Very Weak";
    desc[1] = "Weak";
    desc[2] = "Better";
    desc[3] = "Medium";
    desc[4] = "Strong";
    desc[5] = "Strongest";

    var score = 0;

    //if password bigger than 6 give 1 point
    if (password.length > 6) score++;

    //if password has both lower and uppercase characters give 1 point
    if ((password.match(/[a-z]/)) && (password.match(/[A-Z]/))) score++;

    //if password has at least one number give 1 point
    if (password.match(/\d+/)) score++;

    //if password has at least one special caracther give 1 point
    if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/)) score++;

    //if password bigger than 12 give another 1 point
    if (password.length > 12) score++;

    document.getElementById("passwordDescription").innerHTML = desc[score];
    document.getElementById("passwordStrength").className = "strength" + score;
}

function clearPasswordField(controlID) {
    var control = document.getElementById(controlID);
    if (control != null && control.value == '************') {
        control.value = '';
    }
}

function tipProductName(name) {
    Tip(name);
}

function checkQuantity(elem) {
   //return;
   var checkBoxId = $(elem).attr('id');
   var quantityId = checkBoxId.substring(0, checkBoxId.indexOf("checkbox")) + "quantity";
   var quantityValue = $("#" + quantityId).val();
   var selectionGroup = $(elem).parents(".selBlock");
   var maxQuantity = selectionGroup.children("input:hidden").val();
   var maxId = selectionGroup.children("input:hidden").attr('id');
   var currentValue = selectionGroup.find('span[id$=currentCount]');
   var groupTitle = selectionGroup.find('h2').text();
   var guestNote = selectionGroup.find('span[id$=guestNote]').text();
   if (maxQuantity == "Infinity") return;
   var remainValue;
   var intCurrentValue;
   if (($(elem).parent().attr('class') == "selItem  checked") || ($(elem).parent().attr('class') == "selItem checked")) {
      remainValue = parseInt(maxQuantity) - parseInt(quantityValue);
      if (remainValue < 0) {

         //alert("Warning, too many modifiers");
         var errorTitle = 'Error: ' + groupTitle;
         if (guestNote == '') {
            guestNote = 'Too many modifiers';
         }
         $(selectionGroup).append("<div class='modifierWarning' title='" + errorTitle+ "'>" + guestNote + "</div>");
         initErrorPopup('.modifierWarning');
         jQuery('.modifierWarning').dialog('open');
         
         $('#' + checkBoxId).attr('checked', false);
         changeStyle(elem);
         return;
      }          
      intCurrentValue = parseInt(currentValue.text()) + parseInt(quantityValue);           
   }
   else {
      remainValue = parseInt(maxQuantity) + parseInt(quantityValue);      
      intCurrentValue = parseInt(currentValue.text()) - parseInt(quantityValue);      
   }
   $('#' + maxId).attr('value', remainValue);
   currentValue.text(intCurrentValue);
}

function updateDeliveryTip(sender) {
    $('.deliveryTip').val($(sender).val());
}

function changePaymentFormVisibility(id) {
    switch (id) {
        case 0:
            $('#credit_card_form').show();
            $('#pay_by_cash_form').hide();
            $('#order_button').hide();
            
            break;
        case 1:
            $('#credit_card_form').hide();
            $('#pay_by_cash_form').show();
            $('#order_button').show();
            break;
    }
}
function setShippingAddressTheSame() {
    $('.shipping_address_form input').attr('disabled', $('.shipping_address_form input[type="checkbox"]').attr('checked'));
    $('.shipping_address_form select').attr('disabled', $('.shipping_address_form input[type="checkbox"]').attr('checked'));
    $('.shipping_address_form input[type="checkbox"]').attr('disabled', false);
    if ($('.shipping_address_form input[type="checkbox"]').attr('checked')) {
        $('.shippingElements').hide();
    } else {
        $('.shippingElements').show();
    }


}

function showHideElement(elementID, elementHelperID, showClassName, hideClassName) {
    element = document.getElementById(elementID);
    elementHelper = document.getElementById(elementHelperID);
    if (!element || !elementHelper) {
        return;
    }
    if (element.style.display == 'none') {
        element.style.display = '';
        elementHelper.className = showClassName;
    }
    else {
        element.style.display = 'none';
        elementHelper.className = hideClassName;
    }
}

function selCollapsor() {
    $(this).parent().toggleClass('opened');
    if ($(this).parent().is('.opened')) {
        $(this).find('.arrow-bottom').removeClass('arrow-bottom').addClass('arrow-top');
    }
    else {
        $(this).find('.arrow-top').removeClass('arrow-top').addClass('arrow-bottom');
    }
}
