﻿var aMDT = new Array();
var aP2M = new Array();
var aTPS = new Array();
var arrayFilling = false;
var CCSelectedStoreID = 0;
var CCSelectedCategoryID = 0;

var StoreID = 0;
var CanRemoveSauce = true;
var greekCaps = '0,1,2,3,4,5,6,7,8,9,Α,Β,Γ,Δ,Ε,Ζ,Η,Θ,Ι,Κ,Λ,Μ,Ν,Ξ,Ο,Π,Ρ,Σ,Τ,Υ,Φ,Χ,Ψ,Ω,.,-,’, ' // μην βγάλεις το space απο εδώ
var txtHalfPizza;
 function isGreek(c)
 {
    c = convertToUpper(c);
    var arr = greekCaps.split(",");
    var res = false;
    if(arr.findValue(c) > -1 || arr.findValue( c.toUpperCase() ) > -1 )
        res = true;
    return res
 }
 
 function convertValueToUpper(el)
 {
    if(el)
        el.value = convertToUpper(el.value);
    
 }
 
 function convertToUpper(el)
 {
       return  el.replace(/ά/g,'Α').replace(/έ/g,'Ε').replace(/ή/g,'Η').replace(/ί/g,'Ι').replace(/ό/g,'Ο').replace(/ύ/g,'Υ').replace(/ώ/g,'Ω').replace(/ς/g,'Σ').toUpperCase();
 
 }
 
 /*##### ORDERS ###############*/
 
 /*Kanei to effe tou custom checkbox me ta divs kai bazei kai timh sto element me to targetID*/
function check(el,targetID)
{
                       
    var dest = document.getElementById(targetID);
    if(el.src.indexOf('_ON.png') > -1)
    {
        el.src = el.src.replace('_ON.png','.png');
        dest.value=0;
    }
    else
    {
        el.src = el.src.replace('.png','_ON.png');
        dest.value=1;
    }
}  

function Trigger(elID,event)
{

    $('#' + elID).get(0).click();
}

var isCheckBoxParent = false
function checkModalCart(el,targetID,value)
{
    var img;
    
    if (el.tagName == 'INPUT')
    {
        img=el;
    }
    else
    {
        img = $(el).find('input:image').first().get(0);
        isCheckBoxParent = true;
    }     
    
    $('.ModalCartBody').find('input[src$=_ON.png]').each(function(){
        if(img.id!=this.id)
            this.src = this.src.replace('_ON.png','.png');
    });  
                    
    var dest = document.getElementById(targetID);
    if(img.src.indexOf('_ON.png') > -1)
    {
        img.src = img.src.replace('_ON.png','.png');
        dest.value='';
    }
    else
    {
        img.src = img.src.replace('.png','_ON.png');
        dest.value=value;
    }
    return false;
}         

function doSlideDown(elID)
{
    $(document).ready(function() {
        $("#" + elID).slideDown("slow");
        return
    });
}

function close(elID)
{

    $(document).ready(function() {
            $("#" + elID).slideUp("slow");
        });
}

function JustHide(elID)
{
    $(document).ready(function() {
           document.getElementById(elID).style.display='none';
        });
}

function JustShow(elID)
{
     $(document).ready(function() {
           document.getElementById(elID).style.display='';
        });
}

function Hide(elID)
{

    $(document).ready(function() {
       $("#" + elID).hide('fast');
    });
}

function Show(elID)
{
    $(document).ready(function() {
        $("#" + elID).show('fast');
    });
}

function HideTooltip(event)
{
    $(document).ready(function() {
        $('.tooltipMain').hide();
    });
}

function ShowToolTip(elID,openerID)
{
    $(document).ready(function() {
        
        var tooltip =  $("#" + elID);
        var opener =  $("#" + openerID);
        
        $('.tooltipMain').hide();
        
        var position = opener.position();
        tooltip.get(0).style.top = parseInt(position.top) + 10 -  parseInt(tooltip.height()) + 'px'; 
        tooltip.get(0).style.left = parseInt(position.left) - 20 + 'px'; 
        tooltip.show('fast'); 
    });
}
var toolitipOn = false
function ShowCarouselTooltip(opnr,title,text,e)
{
    $(document).ready(function(){
           
        var opener = $(opnr);
        var tooltip = $('#PizzaTooltipMain');
        var container =  tooltip.parent();
        $('#pizzaToolTipTitle').get(0).innerHTML = title;
        $('#pizzaToolTipText').get(0).innerHTML = text;
        
        opener.children().each( function(){
            $(this).hover(  function(){ var a=1 },function(){ var a=1 } );
        });
        
        var cont = $('#Content');
        var contPos = cont.offset();
        var openerPos = opener.offset();
        var ev = window.event? event : e;
        var position = opener.offset();
        var styleTop = position.top; 
        
        var lala = getPosition(ev);
        
        tooltip.get(0).style.left = lala.x - 46 + 'px';
        tooltip.get(0).style.top = lala.y  -10- tooltip.height() + 'px'; 
        tooltip.show('fast'); 
    });
    
}


function getPosition(e) {
    var ev = window.event? event : e 
    if(ev)
    {
        var cursor = {x:0, y:0};
        if (ev.pageX || ev.pageY) {
            cursor.x = ev.pageX;
            cursor.y = ev.pageY;
        } 
        else {
            var de = document.documentElement;
            var b = document.body;
            cursor.x = ev.clientX + 
                (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
            cursor.y = ev.clientY + 
                (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
        }
    }
 
   
    return cursor;
}

function HideCarouselTooltip(el,e)
{
    return false
     var ev = window.event? event : e 
     
     if(ev && ev.srcElement.id == el.id)
     {
        var tooltip = $('#PizzaTooltipMain');
        tooltip.hide('fast'); 
     }
}


function toggle(elID)
{
    $(document).ready(function() {
        $("#" + elID).toggle('fast');
    });
}
    
function doSlideUp(elID)
{
    $(document).ready(function() {
        $("#" + elID).slideUp("fast");
     });
}
 
function fireTrigger(triggerID)
{
    var el = document.getElementById(triggerID);
    if(el)
     el.click();
}

/*Sto numeric box me ta koumpakia gia thn ayksomeiwsh, ayksanei h meiwnei
        num einai o ba8mos ayksomeiwshs
        elID einai to ID tou textbox
        priceTagID einai to ID tou div pou deixnei thn timh
        thePrice einai h timh bash ths opoias ypologizei
        e einai to event an einai mouse event tote ayksomeiwnei kata to num
        an einai keyup agnoei to num kai ypologizei basei tou ari8mou sto textbox
    */    
function Increase(elID,num,priceTagID,thePrice,e,targetID)
{
    
    var el = document.getElementById(elID);
    var priceTag = document.getElementById(priceTagID);
    
    var ev = window.event? event : e 
    var type=ev.type;
    
    if((ev && ev.keyCode == 0 && el.value) || type=='click')
    {
        if(el && el.value && parseInt(el.value) > 0)
        {
            el.value = parseInt(el.value) + parseInt(num);
            if(parseInt(el.value) + parseInt(num) < 0)
                el.value = 1;

            if (parseInt(el.value) + parseInt(num) > 99)
                el.value = 99;
        }
    }
   
 
    if(el.value.length == 0)
        el.value = 0;                
    /*if(thePrice && priceTag && (! isNaN(el.value)))
    {
      priceTag.innerHTML = (parseFloat(thePrice) * parseFloat(el.value)).toFixed(2); 
      priceTag.innerHTML = priceTag.innerHTML.replace('.',',');
    }*/
    if(targetID)
        fireTrigger(targetID);
    return true;
}

    
function ShowPizzaDow(elID)
{
    var el =  $('#' + elID).parent();
    el.removeClass('SelSize');
    el.addClass('SelSizeON');
    $('.SelSizecenter').slideDown('1000');
}    
function HidePizzaDow(elID)
{
    
    $('.SelSizecenter').slideUp('1000',function(){
        var el =  $('#' + elID).parent();
        el.removeClass('SelSizeON');
        el.addClass('SelSize');
    });
}      

/*ANOIGOKLEINEI TO DIV POU EXEI TO DATALIST ME TA TOPPINGS*/
function slideToggleToppings(elID,elID2,className)
{
    
    if($('#' + elID2).hasClass(className))
    {
        sildeDownToppings(elID,elID2,className);
    }
    else
    {
        $('#' + elID).slideUp('fast',function(){
            $('#' + elID2).removeClass(className + 'ON');
            $('#' + elID2).addClass(className);
        });
     }    
    
}

function sildeDownToppings(elID,elID2,className)
{
    $('#' + elID).slideDown('fast',function(){
            $('#' + elID2).removeClass(className);
            $('#' + elID2).addClass(className + 'ON'); 
            
        });
}


function resetIngrid(elID)
{
    var el = document.getElementById(elID);
    el.className = 'center';
    
}

/*Otan epilegeis pizza apo to carousel, epilegei thn idia pizza sto dropdown*/

function clearCarousel()
{
    var carouselHLD = document.getElementById('mycarousel');
     $('#mycarousel').find('input[src$=_ON.png]').each(function(){
            this.src = this.src.replace('_ON.png','.png');
            
    });
   
}
function checkCarousel(el,targetID,theID)
{
    resetPrice();
    var dest = document.getElementById(targetID);
    
    var image = $(el).find('input:first').get(0);
        
    if(image.src.indexOf('_ON.png') > -1)
    {
        image.src = image.src.replace('_ON.png','.png');
        dest.value='';
    }
    else
    {
        image.src = image.src.replace('.png','_ON.png');
        dest.value=theID;
    }
    
    var carouselHLD = document.getElementById('mycarousel');
    
    $('#mycarousel').find('input[src$=_ON.png]').each(function(){
        
        if(this != image)
            this.src = this.src.replace('_ON.png','.png');
    });
    

   setTimeout('__doPostBack(\''+ dest.name +'\',\'\')', 0);
   
}   

  /*Otan epilegeis pizza apo to drop down epilegei thn antistoixh pizza apo to carousel*/
function checkCarouselDP(el) {

    if (el.value.length = 0)
        return false;
    resetPrice();
    var carouselHLD = document.getElementById('mycarousel');
    var imgs = carouselHLD.getElementsByTagName('IMG')
    for(j=0;j<imgs.length;j++)
    {
        imgs[j].src = imgs[j].src.replace('_ON.png','.png');
         if(imgs[j].id == el.value)
         {
            imgs[j].src = imgs[j].src.replace('.png','_ON.png');
         }
    }
    
}


    

/*FTIANXEI MIA REMOVE FUNTION STO ARRAY OBJECT*/
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

Array.prototype.removeValue = function(value)
{
    for(i=0;i<this.length;i++)
    {
        if(this[i] == value)
            this.remove(i);
    }
} 

Array.prototype.findValue = function(value)
{
    var index = -1;
    for(indx = 0;indx < this.length;indx++)
    {
        if(this[indx] == value)
            index = indx;
    }
    return index;
}


/* OLA TA CONTROLS POU DINOUN VALUES STH ORDERS EXOUN TO VALUE TOUS STO FORMAT ID-TIMH-FPA-POSOTHTA.
    OI PARAKATW FUNCTIONS LEITOURGOUN BASEI AYTOU. TO TI  KANEI H KA8EMIA EINAI AYTONOHTO
*/
function GetPrice(el)
{
    if(el.length > 0)
        return parseFloat(el.split('-')[1].replace(",","."));
    else
        return 0;
}
function GetQuantity(el)
{
    if(el.length > 0)
        return parseInt(el.split('-')[3].replace('_S',''));
    else
        return 0;
}

function GetID(el)
{
    if(el.length > 0)
        return parseInt(el.split('-')[0]);
    else
        return 0;
}

function GetVat(el)
{
    if(el.length > 0)
        return parseFloat(el.split('-')[2].replace(",","."));
    else
        return 0;
}

function isSauce(el)
{
    if(el.length > 0 && el.indexOf('_S') > -1)
        return true;
    else
        return false;
}


function GetIDNoQuan(el)
{
    return  el.substring(0,el.lastIndexOf('-'))
}

/*PSAXNEI STO ARRAY NA BREI TO ID ENOS TOPPING XWRIS OMWS TO QUANTITY*/
function findIndex(arr,el)
{
    var index = -1;
    for(j=0;j<arr.length;j++)
    {
        var valueA = GetIDNoQuan(arr[j]);
        var valueB = GetIDNoQuan(el);
        if(valueA == valueB)
        {
            index = j;
            break;
        }
    }
    return index;
}



/*###################### CALL CENTER #####################*/
var SelectedPizza;
var FadeEffect = true;

function InitializeCCPizza() {    
    InitializeElement('td.btn', '#divZymes', 'td', true);
    InitializeElement('div.Menuitem', '#divMenusHLD', 'div',true);
    InitializeElement('#halFtab0,#halFtab1', '.halfHalfHLD', 'div', true);
    InitializeElement('div.NumberButton', '.finalOrderItemQuantity', 'div', true);
    InitializeTopping('td.ToppingX');
    $('#tcategory_1').next().attr('sauce', 'sauce');
    if(BAseMaterialsCategoryID != 1)
        $('.halfHalfHLD').get(0).style.display = 'none';
    else
        $('.halfHalfHLD').get(0).style.display = '';

    //$('#divZymes').find('td:first').click();
}

function InitializeEdit(BaseMaterialId, MenuId, BaseMaterialId2, MenuId2, BaseMaterialSizeId, Description) {
    
    FadeEffect = false;
    $('#ZYMH_' + BaseMaterialId).click();
    setZymh(BaseMaterialId, BaseMaterialSizeId, Description);
    $('#halFtab0').click();
    setHalfPizza(0);    
    
    /*
    if (BaseMaterialId2 == null)
        setHalfPizza(0);
    else
        setHalfPizza(1);

    $('#Menu_' + MenuId).click();
    setMenu(MenuId, $('#Menu_' + MenuId));
    */
}

function InitializeProducts() {
    InitializeElement('div.Menuitem', '#divMenusHLD', 'div', true);
    InitializeElement('div.NumberButton', '.finalOrderItemQuantity', 'div',true);
    setQuantity($('#Q1').get(0));
    calculateProduct();
}

function InitializeElement(selector,containerSelector,childrenFilterSelector,isProduct)
{    
    
    if(isTouchScreen == false)
    {
        $(containerSelector).find(selector).mouseover(function () {
            $(this).addClass('selected');
        })
        $(containerSelector).find(selector).mouseleave(function () {
            $(this).removeClass('selected');
        })
    }
    if(SelectedPizza || isProduct)
    {
        $(selector).click(function () {

            setSelectedView(this, containerSelector, childrenFilterSelector);
        })
    }
}

function InitializeTopping(selector)
{
    if(isTouchScreen == false) {
        if ($(selector).attr('disabled'))
            return false;
        $(selector).mouseover(function(){
            $(this).addClass('selectedON');
        })
        $(selector).mouseleave(function(){
            $(this).removeClass('selectedON');
        })
    }
}
var abortSelectedView = false;
function setSelectedView(el,containerSelector,childrenFilterSelector) {    
    if (abortSelectedView == true && el.id.indexOf('halFtab') > -1)
        return 
     $(containerSelector).find(childrenFilterSelector).each(function(){
            $(this).removeClass('selected');
            $(this).mouseleave(function(){
                 $(this).removeClass('selected');
            })
        })
        
        $(el).addClass('selected');
        $(el).mouseleave(function(){
            $(this).addClass('selected');
        })
}

function setSelectedTopping(el)
{
    var theElement = $(el);
    var categoryTable = theElement.parent().parent().parent();
    if (categoryTable.attr('sauce') == 'sauce' && BAseMaterialsCategoryID == 1 && theElement.attr("ON") != "ON")
    {
        categoryTable.find('td').removeClass('selected');
        categoryTable.find('td').removeAttr('ON');
        categoryTable.find('td').mouseleave(function(){
                $(this).removeClass('selectedON');
        })
    }
     
    theElement.parent().find('td.ToppingX').each(function(){
        var theBrother = $(this);
        
        if(theElement.get(0) != theBrother.get(0))
        {
            theBrother.removeClass('selected');
            theBrother.mouseleave(function(){
                    $(this).removeClass('selectedON');
            })
        }
        else
        {
            theBrother.toggleClass('selected');     
        }
    })
}
function setProduct(pID, price, el) {
    SelectedProductPrice = parseFloat(price);
    txtProductID.val(pID);
    calculateProduct();

}
//function setZymh(BaseMaterialsId, BaseMaterialsSizeID, clickedElement) {
function setZymh(BaseMaterialsId, BaseMaterialsSizeID, Description) {

    $('#divMenusHLD').slideDown('fast');        
    if (txtHalfPizza.val() == "0")
        txtBaseMaterial.val(BaseMaterialsId);
    else
        txtBaseMaterialHalf.val(BaseMaterialsId);
    selectedZymhID = BaseMaterialsId;
    selectedBaseMaterialsSizeID = BaseMaterialsSizeID;
    txtBaseMaterialsID.val(selectedZymhID);

    if (SelectedPizza == null || SelectedPizza == 'undefined')
        SelectedPizza = new Pizza(selectedBaseMaterialsSizeID, null, selectedZymhID);

    SelectedPizza.baseMaterialsID = selectedZymhID;         
    SelectedPizza.BasematerialsSizeID = selectedBaseMaterialsSizeID;

    txtPizzaSizeID.val(BaseMaterialsSizeID);
    var txt = Description.replace('<br/>','<BR>');  //$(clickedElement).html();
    SelectedPizza.Basematerial = txt.split('<BR>')[0];
    SelectedPizza.BasematerialSize = txt.split('<BR>')[1];
    
    $('#Q1').addClass('selected');
    $('#Q1').mouseleave(function() {
        $(this).addClass('selected');
    })    
    txtQuantity.val(1);
    calculate();
}

function setMenu(menuID, clickedElement) 
{

    if (SelectedPizza == null || SelectedPizza == 'undefined') {
        //$('#divZymes').find('td:first').click();
        alert('Επιλέξτε μέγεθος και ζύμη')
        return
    }

    var partindx = GetPizzaPartIndex(txtHalfPizza.val());
    if (partindx > -1) {
        SelectedPizza.pizzaParts[partindx].menuID = menuID;
    }
    else {
        SelectedPizza.pizzaParts = new Array();
        SelectedPizza.pizzaParts.push(new PizzaPart(0, menuID));
        partindx = 0;
        $('#halFtab0').addClass('selected1');
    }
    //alert('Looking for default toppings');
    arrDefaultToppings = GetDefaultToppingsByMenuID(menuID, SelectedPizza.BasematerialsSizeID, SelectedPizza.baseMaterialsID)
    if (arrDefaultToppings.length == 0) {
        //alert(':' + menuID + ', ' + SelectedPizza.BasematerialsSizeID + ', ' + SelectedPizza.baseMaterialsID);
        alert('Δεν βρέθηκαν default toppings')
    }
    SelectedPizza.pizzaParts[partindx].Toppings = new Array();
    SelectedPizza.pizzaParts[partindx].MenuDescr = $(clickedElement).text();    
    
    for (i = 0; i < arrDefaultToppings.length; i++) {
        var SelectedTopping = GetToppingByID(arrDefaultToppings[i].toppingID, SelectedPizza.BasematerialsSizeID);
        if (SelectedTopping) {
            SelectedTopping.defQuant = arrDefaultToppings[i].defaultQuantity;
            SelectedTopping.quant = arrDefaultToppings[i].defaultQuantity;
            SelectedTopping.disabled = arrDefaultToppings[i].disabled;
            SelectedPizza.pizzaParts[partindx].Toppings.push(SelectedTopping);
        }
        else {
            alert('Δεν βρέθηκε τιμή (Price) για το topping με ID ' + arrDefaultToppings[i].toppingID + ' και BasematerialsSizeID ' + SelectedPizza.BasematerialsSizeID );
        }
    }
    
    abortSelectedView = false;
    ShowSelectedToppings(SelectedPizza.pizzaParts[partindx].Toppings);
    if ( FadeEffect ) $('#divMenusHLD').slideUp('fast');
    calculate();
}

function setHalfPizza(PizzaSide) {
 
     if ( FadeEffect ) $('#divMenusHLD').slideDown('fast');
     if (PizzaSide == txtHalfPizza.val()) {
         
         return
     }
     if (SelectedPizza.pizzaParts && SelectedPizza.pizzaParts.length > 0)
         txtHalfPizza.val(PizzaSide);
    
    if (SelectedPizza == null || SelectedPizza == 'undefined') 
    {
        SelectedPizza = new Pizza(0);
        SelectedPizza.pizzaParts = new Array();
        for (hpindx = 0; hpindx <= PizzaSide; hpindx++) {
            SelectedPizza.pizzaParts.push(new PizzaPart(hpindx));
        }
        if (PizzaSide > 0)
            hpindx = 0;

        abortSelectedView = false;
    }
    else {
        if (SelectedPizza.pizzaParts && SelectedPizza.pizzaParts.length - 1 < PizzaSide) 
        {
            if (PizzaSide > 0 && SelectedPizza.pizzaParts.length > 0)
                $('#halFtab' + PizzaSide).removeClass('selected');
            for (hpindx = SelectedPizza.pizzaParts.length; hpindx <= PizzaSide; hpindx++) {
                SelectedPizza.pizzaParts.push(new PizzaPart(hpindx));
            }
            var partindx = GetPizzaPartIndex(PizzaSide)
            ShowSelectedMenu(0);
            if (partindx > -1)
                ShowSelectedToppings(SelectedPizza.pizzaParts[partindx].Toppings);
            
            abortSelectedView = false;
        }
        else {
            var partindx = GetPizzaPartIndex(PizzaSide)
            if (partindx > -1) {
                ShowSelectedMenu(SelectedPizza.pizzaParts[partindx].menuID);
                ShowSelectedToppings(SelectedPizza.pizzaParts[partindx].Toppings);                
                abortSelectedView = false;
            }
            else {
                abortSelectedView = true;
                $('#halFtab' + PizzaSide).removeClass('selected');
            }
        }
    }
   
    calculate();
}

function setTopping(el) {

    var parent = $(el).parent();
    var tdTitle = parent.children().first();
    var Quantity = parseInt(el.id.split('_')[2]);  //el.id;
    var ToppingID = parseInt(el.id.split('_')[1]); //var ToppingID = parseInt(tdTitle.attr("id").replace('Topping_', ''));
    
    if (SelectedPizza && SelectedPizza.pizzaParts && SelectedPizza.pizzaParts.length > 0) {
        var SelectedTopping = GetToppingByID(ToppingID, selectedBaseMaterialsSizeID);
        if (SelectedTopping) {
            var partindx = GetPizzaPartIndex(txtHalfPizza.val());
            if (partindx > -1) {
                var theID = SelectedTopping.id;
                var theMenuID = SelectedPizza.pizzaParts[partindx].menuID;
                var theBasematerialsSizeID = SelectedPizza.BasematerialsSizeID;
                var thebaseMaterialsID = SelectedPizza.baseMaterialsID;

                SelectedTopping.defQuant = GetDefaultQuantity(theID, theMenuID, theBasematerialsSizeID, thebaseMaterialsID);

                if ($(el).attr('ON') == 'ON') {
                    if ((SelectedTopping.isSauce == false) || (SelectedTopping.isSauce == true && CanRemoveSauce == true)) {
                        SelectedTopping.quant = Quantity;
                        setSelectedTopping(el);
                        if (SelectedPizza.pizzaParts[partindx].Toppings) {
                            SelectedTopping.quant = 1; //kolpo grosso
                            var topp = findToppingByID(SelectedPizza.pizzaParts[partindx].Toppings, SelectedTopping.id);

                            if (topp.defQuant > 0) {
                                topp.quant = 0 //SelectedPizza.pizzaParts[partindx].Toppings[idx].quant = 0;                                
                            }
                            else {
                                SelectedPizza.pizzaParts[partindx].Toppings.removeValue(topp);                                
                            }   
                            $(el).removeAttr('ON');
                        }
                    }
                }
                else {                    
                    var theTotalToppingsCount = 0;

                    if (SelectedPizza.pizzaParts[partindx].menuID == null || SelectedPizza.pizzaParts[partindx].menuID == 'undefined') {
                        alert('Επιλέξτε μενού')
                        return
                    }

                    if (SelectedTopping.isSauce && ($(el).attr('ON') == 'ON') == false && BAseMaterialsCategoryID == 1) {                        
                        RemoveSauce(partindx, SelectedTopping.id, Quantity);
                    }
                    
                    SelectedTopping.quant = Quantity;
                    for (ai = 0; ai < SelectedPizza.pizzaParts[partindx].Toppings.length; ai++) {
                        if (SelectedPizza.pizzaParts[partindx].Toppings[ai].isSauce == false && SelectedPizza.pizzaParts[partindx].Toppings[ai].quant > 0)
                            theTotalToppingsCount += (1 * SelectedPizza.pizzaParts[partindx].Toppings[ai].quant);
                    }
                    if (SelectedTopping.isSauce == false)
                        theTotalToppingsCount += 1 * Quantity;
                    
                    if (theTotalToppingsCount > MaxToppings && BAseMaterialsCategoryID == 1) {
                        alert('Μπορείτε να βάλετε μέχρι και ' + MaxToppings + ' Toppings συνολικά');
                        return
                    }
                    setSelectedTopping(el);

                    if (SelectedPizza.pizzaParts[partindx].Toppings == null || SelectedPizza.pizzaParts[partindx].Toppings == 'undefined')
                        SelectedPizza.pizzaParts[partindx].Toppings = new Array();

                    var lTopping = findToppingByID(SelectedPizza.pizzaParts[partindx].Toppings, SelectedTopping.id);

                    if (lTopping == null) {                        
                        SelectedPizza.pizzaParts[partindx].Toppings.push(SelectedTopping);
                    }
                    else {
                        if (SelectedTopping.quant != lTopping.quant)
                            lTopping.quant = SelectedTopping.quant;

                    }
                    parent.children().removeAttr('ON')
                    $(el).attr('ON', 'ON');
                }
            }
            calculate();
        }
        else {
            alert('Το topping δεν βρέθηκε.')
        }
    }
    else {
        alert('Επιλέξτε συνταγή και τύπο ζύμης');
        $(el).removeClass('selected');
        $(el).removeAttr('ON');
    }
}

function setTopping_Old(el) {
       
    var parent = $(el).parent();
    var tdTitle = parent.children().first();
    var Quantity = parseInt(el.id.split('_')[2]);  //el.id;
    var ToppingID = parseInt(el.id.split('_')[1]); //var ToppingID = parseInt(tdTitle.attr("id").replace('Topping_', ''));
    if (SelectedPizza && SelectedPizza.pizzaParts && SelectedPizza.pizzaParts.length > 0) {
        var SelectedTopping = GetToppingByID(ToppingID, selectedBaseMaterialsSizeID);        
        if (SelectedTopping) {
            var partindx = GetPizzaPartIndex(txtHalfPizza.val());            
            if (partindx > -1) {                
                var theID = SelectedTopping.id;
                var theMenuID = SelectedPizza.pizzaParts[partindx].menuID;
                var theBasematerialsSizeID = SelectedPizza.BasematerialsSizeID;
                var thebaseMaterialsID = SelectedPizza.baseMaterialsID;

                SelectedTopping.defQuant = GetDefaultQuantity(theID, theMenuID, theBasematerialsSizeID, thebaseMaterialsID);
                
                if ($(el).attr('ON') == 'ON') {
                    if ((SelectedTopping.isSauce == false) || (SelectedTopping.isSauce == true && CanRemoveSauce == true)) {
                        SelectedTopping.quant = Quantity;
                        setSelectedTopping(el);
                        if (SelectedPizza.pizzaParts[partindx].Toppings) {
                            SelectedTopping.quant = 1; //kolpo grosso
                            var idx = SelectedPizza.pizzaParts[partindx].Toppings.findValue(SelectedTopping);
                            var topp = SelectedPizza.pizzaParts[partindx].Toppings[idx];
                            if (topp.defQuant > 0)
                                SelectedPizza.pizzaParts[partindx].Toppings[idx].quant = 0;
                            else
                                SelectedPizza.pizzaParts[partindx].Toppings.removeValue(topp);
                            $(el).removeAttr('ON');
                        }
                    }                    
                }
                else {
                    var theTotalToppingsCount = 0;

                    if(SelectedPizza.pizzaParts[partindx].menuID == null || SelectedPizza.pizzaParts[partindx].menuID =='undefined')
                    {
                        alert('Επιλέξτε μενού')
                        return
                    }
                    
                    if (SelectedTopping.isSauce && ($(el).attr('ON') == 'ON') == false && BAseMaterialsCategoryID == 1)
                        RemoveSauce(partindx, SelectedTopping.id, Quantity);

                    SelectedTopping.quant = Quantity;                    
                    for (ai = 0; ai < SelectedPizza.pizzaParts[partindx].Toppings.length; ai++) {
                        if (SelectedPizza.pizzaParts[partindx].Toppings[ai].isSauce == false && SelectedPizza.pizzaParts[partindx].Toppings[ai].quant > 0)
                            theTotalToppingsCount += (1 * SelectedPizza.pizzaParts[partindx].Toppings[ai].quant);
                    }
                    if (SelectedTopping.isSauce == false) 
                        theTotalToppingsCount += 1 * Quantity;

                    if (theTotalToppingsCount > MaxToppings && BAseMaterialsCategoryID == 1) {
                        alert('Μπορείτε να βάλετε μέχρι και ' + MaxToppings + ' Toppings συνολικά');
                        return
                    }
                    setSelectedTopping(el);

                    if (SelectedPizza.pizzaParts[partindx].Toppings == null || SelectedPizza.pizzaParts[partindx].Toppings == 'undefined')
                        SelectedPizza.pizzaParts[partindx].Toppings = new Array();

                    var ToppingIndex = 0;
                    
                    if (SelectedTopping.isSauce == true)
                        ToppingIndex = findToppingByID(SelectedPizza.pizzaParts[partindx].Toppings,SelectedTopping.id);
                    else
                        ToppingIndex = SelectedPizza.pizzaParts[partindx].Toppings.findValue(SelectedTopping);
                        
                    if (ToppingIndex == -1) {
                        SelectedPizza.pizzaParts[partindx].Toppings.push(SelectedTopping);
                    }
                    else {
                        if (SelectedTopping.quant != SelectedPizza.pizzaParts[partindx].Toppings[ToppingIndex].quant)
                            SelectedPizza.pizzaParts[partindx].Toppings[ToppingIndex].quant = SelectedTopping.quant;
                        
                    }
                    parent.children().removeAttr('ON')
                    $(el).attr('ON', 'ON');
                }
            }
            calculate();
        }
        else {
            alert('Το topping δεν βρέθηκε.')
        }
    }
    else {
        alert('Επιλέξτε συνταγή και τύπο ζύμης');
        $(el).removeClass('selected');
        $(el).removeAttr('ON');
    }
}

function findToppingByID(topps, id) 
{
    var resID = null;  //-1;
    for (counter = 0; counter < topps.length; counter++)
    {
        if (topps[counter].id == id)
        {
            resID = topps[counter];  //ooo;
            break;
        }
    } 
    return resID;
}

function setQuantity(el)
{
    if(el)
    {
        var theVal = 1;
        if(el.tagName == 'SELECT')
        {
            theVal = el.value;
            $('.finalOrderItemQuantity').find('DIV').removeClass('selected');
            $('.finalOrderItemQuantity').find('DIV').mouseleave(function(){
                    $(this).removeClass('selected');
            })
        }
        else
        {
            theVal = $(el).text();
            $(el).mouseleave(function () {
                $(this).addClass('selected');
            })
            $('.finalOrderItemQuantity').find('SELECT').val(6);
        }
    }      
    txtQuantity.val(theVal); 
    calculate();     
}

function calculateProduct() {
    var Total = 0;
    if (SelectedProductPrice) {
        var Quantity = parseInt(txtQuantity.val());
        Total = SelectedProductPrice * Quantity;

    }
    $('.finalOrderItemPrice').html(Total.toFixed(2));
}

function calculateCCPizza() {

    if (SelectedPizza && SelectedPizza.pizzaParts) {
        txtBaseMaterialsSizeID.val(SelectedPizza.BasematerialsSizeID);
        var partsCount = SelectedPizza.pizzaParts.length;
        if (partsCount > 0) {
            var txt = SelectedPizza.Basematerial + ' | ' + SelectedPizza.BasematerialSize + '<br> ';
            var ToppingIds = new Array();
            var Menus = new Array();

            for (tpindx = 0; tpindx < partsCount; tpindx++) {
                WriteBaseMaterialsAndMenu(SelectedPizza.pizzaParts[tpindx], Menus)
                WriteToppings(SelectedPizza.pizzaParts[tpindx], ToppingIds);
                if (SelectedPizza.pizzaParts[tpindx].MenuDescr) {
                    var p2m = GetPrice2Menu(SelectedPizza.pizzaParts[tpindx].menuID, SelectedPizza.baseMaterialsID);
                    if (p2m)
                        SelectedPizza.pizzaParts[tpindx].MenuDescr = p2m.codeKitchen;
                    else
                        SelectedPizza.pizzaParts[tpindx].MenuDescr = '';
                    txt = txt + '(' + SelectedPizza.pizzaParts[tpindx].MenuDescr + ')' + SelectedPizza.pizzaParts[tpindx].toppingsText
                    
                    if (partsCount > 1 && tpindx < partsCount - 1)
                        txt += '/';
                }
            }
            calculateParts();
            $('#finalOrderItemText').html(txt);
        }
    }
}

function calculateParts() 
{
    var partsCount = SelectedPizza.pizzaParts.length;
    var isHalfHalf = partsCount > 1
    var partsPrice = 0;

    for (pindx = 0; pindx < SelectedPizza.pizzaParts.length; pindx++) {
        partsPrice += calculatePart(SelectedPizza.pizzaParts[pindx], isHalfHalf);
    }
    var quantity = parseInt(txtQuantity.val());
    partsPrice = partsPrice * quantity;
    $('.finalOrderItemPrice').html(parseFloat(partsPrice).toFixed(2).replace('.',','));
}

function calculatePart(part, isHalfHalf) {
    
    var partPrice = 0;
    if (part.menuID) {
        var p2m = GetPrice2Menu(part.menuID, SelectedPizza.baseMaterialsID);
        if (p2m) {
            
            if (isHalfHalf == true)
                partPrice = p2m.PriceHalf;
            else
                partPrice = p2m.Price;
        }
        
        partPrice += calculatePartToppings(part.Toppings, isHalfHalf, part.menuID);
        
    }
    return partPrice;
}

function calculatePartToppings(topppings, isHalfHalf, menuID) {

    
    var ToppingsFinalPrice = 0;
    if (topppings && topppings.length > 0) {
        var TotalToppingsCount = 0;
        var ToppingsOnly = new Array();
        for (tindx = 0; tindx < topppings.length; tindx++) {
            var topping = topppings[tindx];
            if (topping.price > 0) {
                
                var price = topping.price;
                if (isHalfHalf == true)
                    price = topping.priceHalf;
                if (topping.isSauce == false)
                    ToppingsOnly.push(topping);
                else {
                    if (CanRemoveSauce == false) 
                        ToppingsFinalPrice += price * (topping.quant - topping.defQuant);
                    
                }
            }
        }

        var FreeQuantity = 0;
        var AllowDiscount = true;
        var ToppingsIncreasePrice = new Array();

        for (tindx = 0; tindx < ToppingsOnly.length; tindx++) {

            var t = ToppingsOnly[tindx];
            TotalToppingsCount += parseInt(1 * t.quant);

            if ((menuID == CustomPizzaID && TotalToppingsCount <= NofreeToppings) || menuID != CustomPizzaID) {
                if (t.defQuant == 0 && (t.defQuant != t.quant)) {
                    if (FreeQuantity - t.quant < 0) {
                        var tmpQ = t.quant;
                        //t.quant = Math.abs(FreeQuantity - t.quant);
                        t.quant = Math.abs((FreeQuantity < 0 ? 0 : FreeQuantity) - t.quant);
                        ToppingsIncreasePrice.push(new Topping(t.id, t.price, t.vat, t.quant, t.defQuant, t.isSauce, t.BaseMaterialsSizeId, t.baseMaterialCategory, t.category, t.CodeKitchen, t.priceHalf));
                        t.quant = tmpQ;
                    }
                    //FreeQuantity = 0;
                    FreeQuantity -= t.quant;
                }
                //alert(t.isSauce);
                if (t.defQuant > 0) {
                    if (t.defQuant > t.quant) {
                        if (AllowDiscount) {
                            if (ToppingsIncreasePrice.length > 0) {
                                if (AllowDiscount)
                                    ToppingsIncreasePrice[0].quant = parseInt(ToppingsIncreasePrice[0].quant - Math.abs((t.defQuant - t.quant)));
                            }
                            else {
                                FreeQuantity = parseInt(Math.abs(t.defQuant - t.quant));
                                if (FreeQuantity > 1)// kai allo kolpo grosso
                                    FreeQuantity = FreeQuantity;
                            }
                            AllowDiscount = false;
                        }
                    }
                    else {
                        //if (t.defQuant != t.quant) ToppingsIncreasePrice.push(t);
                        if (t.defQuant != t.quant)
                        {
                            if (FreeQuantity <= 0) ToppingsIncreasePrice.push(t);
                            FreeQuantity += (t.defQuant - t.quant);
                        }
                    }
                }
            }
            else if (menuID == CustomPizzaID && TotalToppingsCount <= NofreeToppings + 1) {
                if (t.quant > 1) {
                    var theDiff = Math.abs(TotalToppingsCount - NofreeToppings);
                    ToppingsFinalPrice += getToppingPrice(t, isHalfHalf, theDiff);
                }
            }
            else {
                //do nothing
            }
        }
    }
    if (ToppingsIncreasePrice) {
        for (i = 0; i < ToppingsIncreasePrice.length; i++) {
            var thePrice = ToppingsIncreasePrice[i].price;
            if (isHalfHalf == true)
                thePrice = ToppingsIncreasePrice[i].priceHalf
            if (ToppingsIncreasePrice[i].isSauce == true) {
                if (ToppingsIncreasePrice[i].quant > ToppingsIncreasePrice[i].defQuant)
                    ToppingsFinalPrice = ToppingsFinalPrice + ((thePrice) * Math.abs(ToppingsIncreasePrice[i].defQuant - ToppingsIncreasePrice[i].quant));
                else
                    ToppingsFinalPrice = ToppingsFinalPrice + ((thePrice) * ToppingsIncreasePrice[i].quant);
            }
            else {
                ToppingsFinalPrice = ToppingsFinalPrice + ((thePrice) * Math.abs(ToppingsIncreasePrice[i].defQuant - ToppingsIncreasePrice[i].quant));
            }
        }
    }

    return ToppingsFinalPrice;
}

function ValidateSubmit(sender, e) 
{
    var partindx = GetPizzaPartIndex(txtHalfPizza.val());
    e.IsValid = false;
    if (SelectedPizza && SelectedPizza.pizzaParts && SelectedPizza.pizzaParts[partindx] && SelectedPizza.pizzaParts[partindx].Toppings) {
        var ToppingsCount = GetToppingsCount(SelectedPizza.pizzaParts[partindx].Toppings);
        if (BAseMaterialsCategoryID == 1) {
            if (ToppingsCount >= minToppings)
                e.IsValid = true;
            else
                alert('Πρέπει να βάλετε τουλάχιστον ' + minToppings  + ' Toppings!')
        }
        else {
            e.IsValid = true;
        }
    }

}
function ValidateSubmitProducts(sender, e) {
    e.IsValid = false;
    if (txtProductID.val() && txtProductID.val().length > 0 && isNaN(txtProductID.val()) == false)
        e.IsValid = true;
    if (e.IsValid == false)
        alert('Επιλέξτε προϊόν');
}
function closeWindow()
{
    $('.divPizzaMain').hide('fast');
    $('.modalBackground').hide('fast');
    $('.panelSuperControls').hide('fast');
    SelectedPizza = null;
}
function closeAndRefresh() {
    closeWindow();
    $('#divMenusHLDTabs div').removeClass('selected');
    if (txtHalfPizza)
        txtHalfPizza.val(0);
}
/*##################   CALL CENTER HELPER METHODS   ##############################*/
function GetToppingsCount(topps) 
{
    var count = 0;
    for (ci = 0 ; ci< topps.length;ci++) {
        if (topps[ci].isSauce == false)
            count += 1 * topps[ci].quant;
    }
    return count
}

function GetToppingByID(id, BaseMaterialsSizeId) 
{
    var resultTopp;
    for (tpindx = 0; tpindx < aTPS.length; tpindx++) 
    {
        if (aTPS[tpindx].id == id && aTPS[tpindx].BaseMaterialsSizeId == BaseMaterialsSizeId) 
        {
            //resultTopp = aTPS[tpindx];
            var sTopping = aTPS[tpindx];            
            //new Topping(t.id, t.price, t.vat, t.quant, t.defQuant, t.isSauce, t.BaseMaterialsSizeId, t.baseMaterialCategory, t.category, t.CodeKitchen, t.priceHalf)
            resultTopp = new Topping(sTopping.id, sTopping.price, sTopping.vat, sTopping.quant, sTopping.defQuant, sTopping.isSauce, sTopping.BaseMaterialsSizeId,
                                        sTopping.baseMaterialCategory, sTopping.category, sTopping.CodeKitchen, sTopping.priceHalf);
            resultTopp.disabled = sTopping.disabled;
            break;
        }
    }
    return resultTopp;
}

function GetMenuSelectedTopping(selectedMenuID, SelectedToppingID) 
{
    var resultTopp;

    for (stpindx = 0; stpindx < arrMenuSelectedToppings.length; stpindx++) 
    {
        if (arrMenuSelectedToppings[stpindx].Menuid == selectedMenuID && arrMenuSelectedToppings[stpindx].toppingID == SelectedToppingID) 
        {
            resultTopp = arrMenuSelectedToppings[stpindx];
            break;
        }
    }
    return resultTopp;
}

function GetPizzaPartIndex(partNumber) 
{

    var PizzaPartIndex = -1;
    if (SelectedPizza) 
    {
        if (SelectedPizza.pizzaParts) 
        {
            for (ppindx = 0; ppindx < SelectedPizza.pizzaParts.length; ppindx++) 
            {
                if (SelectedPizza.pizzaParts[ppindx].partNumber == partNumber) 
                {
                    PizzaPartIndex = ppindx;
                    break
                }
            }
        }
    }
    return PizzaPartIndex;
}

function GetDefaultToppingsByMenuID(menuID, BaseMaterialsSizeID, BaseMaterialsId) {

    var arrdft = new Array();
    //alert('GetDefaultToppingsByMenuID : ' + aMDT.length + ', BaseMaterialsSizeID: ' + BaseMaterialsSizeID + ', BaseMaterialsId: ' + BaseMaterialsId);
    for (dtmindx = 0; dtmindx < aMDT.length; dtmindx++) 
    {
        if (aMDT[dtmindx].Menuid == menuID && aMDT[dtmindx].BaseMaterialsSizeID == BaseMaterialsSizeID && aMDT[dtmindx].BaseMaterialsId == BaseMaterialsId && aMDT[dtmindx].defaultQuantity > 0)
            arrdft.push(aMDT[dtmindx]);
    }
    return arrdft;
}

function GetDefaultQuantity(toppingID, menuID, basematerialsSizeID, BaseMaterialsId) 
{
    var quan = 0;
    for (gdqindx = 0; gdqindx < aMDT.length; gdqindx++) 
    {
        if (aMDT[gdqindx].Menuid == menuID && aMDT[gdqindx].toppingID == toppingID && aMDT[gdqindx].BaseMaterialsId == BaseMaterialsId && aMDT[gdqindx].BaseMaterialsSizeID == basematerialsSizeID) 
        {
            quan = aMDT[gdqindx].defaultQuantity;
            break;
        }
    }
    return quan;
}
function GetPrice2Menu(menuID, BasematerialsID) {
    
    var p2mres;
    for (pmindx = 0; pmindx < aP2M.length; pmindx++)
    {
        if (aP2M[pmindx].MenuId == menuID && aP2M[pmindx].BaseMaterialsId == BasematerialsID)
        {
            p2mres = aP2M[pmindx];
            break;
        }
    }
    return p2mres;
}

function getToppingPrice(topping, isHalfHalf, quantDiff) 
{
    var p = 0;
    if (topping) 
    {
        var q = topping.quant;
        if (quantDiff)
            q = quantDiff;
        if (isHalfHalf == true)
            p = topping.priceHalf * q;
        else
            p = topping.price * q;
    }
    return p;
}

function WriteToppings(selectedPart, ToppingIds) {
    
    if (selectedPart && selectedPart.Toppings && selectedPart.Toppings.length > 0) 
    {
        var ToppingsCodeKitchen = '';
        for (wtindx = 0; wtindx < selectedPart.Toppings.length; wtindx++) {
            var topp = selectedPart.Toppings[wtindx];
            ToppingIds.push(selectedPart.partNumber + '-' + topp.id + '-' + topp.defQuant + '-' + topp.quant);
            
            if (topp.defQuant == 0 && topp.quant > 0 || (topp.defQuant > 0 && topp.quant > topp.defQuant)) {

//            '1 -> 2 X
//            '2 -> 1 ~X (-X)

//            '0 -> 2 2X
//            '2 -> 0 -X (-2X)
            
                if (Math.abs(topp.defQuant - topp.quant) > 1 ) ToppingsCodeKitchen = ToppingsCodeKitchen + topp.quant;                

                ToppingsCodeKitchen = ToppingsCodeKitchen + topp.CodeKitchen;
//                if (topp.quant > 1)                                    
//                    ToppingsCodeKitchen = ToppingsCodeKitchen + topp.quant + topp.CodeKitchen;
//                else
//                    ToppingsCodeKitchen = ToppingsCodeKitchen + topp.CodeKitchen;
            }
            else if (topp.defQuant > 0 && topp.quant < topp.defQuant)
            {
                if (topp.defQuant == 2) {
                    if (topp.quant == 1) {
                        //'2 -> 1 ~X
                        ToppingsCodeKitchen = ToppingsCodeKitchen + '~' + topp.CodeKitchen;
                    }
                    else { //2 -> 0 -X
                        ToppingsCodeKitchen = ToppingsCodeKitchen + '-' + topp.CodeKitchen;
                    }                    
                }
                else {
                    ToppingsCodeKitchen = ToppingsCodeKitchen + '-';                    
                    if (Math.abs(topp.defQuant - topp.quant) > 1) ToppingsCodeKitchen = ToppingsCodeKitchen + Math.abs(topp.defQuant - topp.quant);
                    ToppingsCodeKitchen = ToppingsCodeKitchen + topp.CodeKitchen;
                    //                var df = Math.abs(topp.defQuant - topp.quant)
                    //                for (dfi = 0; dfi < df; dfi++) 
                    //                {
                    //                    ToppingsCodeKitchen = ToppingsCodeKitchen + '-' + topp.CodeKitchen;
                    //                }

                }   
                
            }
        }
        txtSelectedToppings.val(ToppingIds.toString());
        selectedPart.toppingsText = ToppingsCodeKitchen;
        ToppingIds = null;
    }
}

function WriteBaseMaterialsAndMenu(selectedPart, Menus)
{
    if (selectedPart && selectedPart.menuID) 
    {
        Menus.push(selectedPart.partNumber + '-' + selectedPart.menuID)
        txtSelectedMenus.val(Menus.toString());
    }
}

function ShowSelectedToppings(toppingsToshow) 
{        
    $('#divToppingsHLD').find('td').removeClass('selected');
    $('#divToppingsHLD').find('td').removeAttr('ON');    

    if (toppingsToshow) {        
        for (tptsindx = 0; tptsindx < toppingsToshow.length; tptsindx++) {
            var toppingToShow = toppingsToshow[tptsindx];

            if (toppingToShow.disabled == false || toppingToShow.disabled == 'undefined' || toppingToShow.disabled == null) {                
                var ToppingCell = $('#Topping_' + toppingToShow.id);
                if (ToppingCell.get(0)) {
                    var quant = toppingToShow.quant
                    if (quant == 1) {
                        ToppingCell.next().addClass('selected');
                        ToppingCell.next().attr('ON', 'ON');
                    }
                    else {
                        ToppingCell.next().next().addClass('selected')
                        ToppingCell.next().next().attr('ON', 'ON');
                    }
                    //ToppingCell.parent().children("td [id='" + quant + "']").addClass('selected');
                }
            }           
        }
    }    
}

function ShowSelectedMenu(menuID) 
{
    $('#divMenusHLD').find('div').removeClass('selected');
    $('#divMenusHLD').find('div').mouseleave(function () {
        $(this).removeClass('selected');
    })
    if (menuID && menuID > 0) {
        $('#Menu_' + menuID).addClass("selected");
        $('#Menu_' + menuID).mouseleave(function () {
            $(this).addClass('selected');
        })
    }
}



function RemoveSauce(partindx, newSauceID, newQuant) {

    var arrTTT = SelectedPizza.pizzaParts[partindx].Toppings;
    var arr2Remove = new Array();
    var arr2Add = new Array();
    var len = arrTTT.length;

    for (iii = 0; iii < len; iii++) 
    {
        var sauce2Go = arrTTT[iii];
        if (sauce2Go.isSauce == true) {

            if (sauce2Go.id != newSauceID) {
                var qdf = Math.abs(sauce2Go.quant - sauce2Go.defQuant);
                var newSauce = new Topping(sauce2Go.id, sauce2Go.price, sauce2Go.vat, sauce2Go.quant, sauce2Go.defQuant, sauce2Go.isSauce, sauce2Go.BaseMaterialsSizeId, sauce2Go.baseMaterialCategory, sauce2Go.category, sauce2Go.CodeKitchen, sauce2Go.priceHalf);
                newSauce.quant = 0;

                if (sauce2Go.defQuant > 0 && sauce2Go.id != newSauceID && GetDefaultRemovedCount(arrTTT, sauce2Go.id) == 0) {
                    arr2Add.push(newSauce);
                }
                else if (sauce2Go.defQuant > 0 && sauce2Go.id == newSauceID && newQuant != sauce2Go.defQuant && GetDefaultRemovedCount(arrTTT, sauce2Go.id) == 0) {
                    arr2Add.push(newSauce);
                }
                else if (sauce2Go.defQuant > 0 && sauce2Go.id == newSauceID && newQuant != sauce2Go.defQuant && newQuant != sauce2Go.quant && GetDefaultRemovedCount(arrTTT, sauce2Go.id) == 1) {
                    arr2Add.push(newSauce);
                }
                else if (sauce2Go.defQuant > 0 && sauce2Go.id == newSauceID && newQuant == sauce2Go.defQuant && GetDefaultRemovedCount(arrTTT, sauce2Go.id) > 0) {
                    var iidx = GetDefaultRemovedIndex(arrTTT, sauce2Go.id);
                    arr2Remove.push(arrTTT[iidx])
                }
                else if (sauce2Go.defQuant == 0) {
                    arr2Remove.push(sauce2Go);
                }

                if (sauce2Go.defQuant > 0 && sauce2Go.id != newSauceID && sauce2Go.quant > 0) {
                    arr2Remove.push(sauce2Go);
                }
            }
            else
                sauce2Go.Quantity = newQuant;
        }
    }
    
    for (li = 0; li < arr2Remove.length; li++) 
    {
        arrTTT.removeValue(arr2Remove[li]);
    }
    for (li = 0; li < arr2Add.length; li++) {
        arrTTT.push(arr2Add[li]);
    }
}
function GetDefaultRemovedCount(topps, id) {
    var count = 0;
    for (pai = 0; pai < topps.length; pai++) {
        if (topps[pai].isSauce == true && topps[pai].id == id && topps[pai].defQuant > 0 && topps[pai].quant == 0) {
            count++;
        }
    }
    return count;
}
function GetDefaultRemovedIndex(topps,id) {
    var resindx = -1
    for (bai = 0; bai < topps.length; bai++) {
        if (topps[bai].isSauce == true && topps[bai].id == id && topps[bai].defQuant > 0 && topps[bai].quant == 0) {
            resindx = bai;
            break;
        }
    }
    return resindx;
}

function clearAll() {
    SelectedPizza = null;
    $('div,td').removeClass('selected');
    $('.divPizzaMain input:text').val('');
    $('.divPizzaMain textarea').val('');
    $('.finalOrderItemPrice').text('');
    $('.finalOrderItemQuantity').find('SELECT').val(6);
    txtQuantity.val(1);
}

function clearAllProducts() {
    SelectedProductPrice = null;
    $('#<%=txtQuantity.ClientID %>').val(1);
    $('.finalOrderItemPrice').html('');
    $('#<%=txtProductID.ClientID %>').val('');
    $('DIV').removeClass('selected');
}


/*##################  OBJECTS ######################*/
function Topping(id,price,vat,quant,defQuant,isSauce,BaseMaterialsSizeId,baseMaterialCategory,category,CodeKitchen,priceHalf,disabled)
{

    this.id = parseInt(id);
    this.price = parseFloat(price);
    this.priceHalf = parseFloat(priceHalf);
    this.vat = parseFloat(vat);
    this.quant = parseInt(quant);
    this.defQuant = parseInt(defQuant);
    if(isSauce)
        this.isSauce = isSauce;
    else
        this.isSauce = false;
    this.BaseMaterialsSizeId = BaseMaterialsSizeId;
    this.baseMaterialCategory = baseMaterialCategory;
    this.category = category;
    this.CodeKitchen = CodeKitchen;
    this.disabled = disabled;
}   
    //BaseMaterial
	function BM(ID,typeID,sizeID,price)
	{
	    this.ID = ID
	    this.typeID = typeID
	    this.sizeID = sizeID
	    this.price = price
	
	}
	
	function P2M(id,MenuId,BaseMaterialsId,Price,PriceHalf,Points,codeKitchen)
	{
	    this.id = id;
	    this.MenuId = MenuId
	    this.BaseMaterialsId = BaseMaterialsId
	    this.Price = Price
	    this.PriceHalf= PriceHalf
	    this.Points = Points
	    this.codeKitchen = codeKitchen;
	}
	//MenuDefaultTopping
	function MDT(Menuid,toppingID,defaultQuantity,BaseMaterialsId,BaseMaterialsSizeID,disabled)
	{
	    this.Menuid = Menuid;
	    this.toppingID = toppingID;
	    this.defaultQuantity = defaultQuantity;
	    this.BaseMaterialsSizeID = BaseMaterialsSizeID;
	    this.BaseMaterialsId = BaseMaterialsId;
	    this.disabled = disabled;
	    
	}
	
	function Pizza(BasematerialsSizeID,pizzaParts,baseMaterialsID,Basematerial,BasematerialSize)
	{
	    this.pizzaParts = pizzaParts;
	    this.BasematerialsSizeID = BasematerialsSizeID;
	    this.baseMaterialsID = baseMaterialsID;
	    this.Basematerial = Basematerial;
	    this.BasematerialSize = BasematerialSize;
	}

	function PizzaPart(partNumber, menuID, Toppings, toppingsText, MenuDescr, MenuKC)
	{
	    this.partNumber = partNumber;
	    this.menuID = menuID;
	    this.Toppings = Toppings;
	    this.toppingsText = toppingsText;
	    this.MenuDescr = MenuDescr;
        this.MenuKC = MenuKC

	}
	function Product(id, price, quantity, groupID, categoryID) {
	    this.id = id;
	    this.price = parseFloat(price);
	    this.quantity = parseInt(quantity);
	    this.groupID = groupID;
	    this.categoryID = categoryID;
	}




/* ****************************************************************************** */
/* For Lartigiano */

	var MenuTopping = { ToppingId: null, OriginalQuantity: null, SelectedQuantity: null, Price: null, Enabled: null, Fixed: null };
	var MenuToppings = new Array(); /* */
	
