﻿//combo box behavior 

function SelectOption(oOption,oSelectedText,oSelectedValue,oDivOptions)
{
    oSelectedText = document.getElementById(oSelectedText);
    oSelectedValue = document.getElementById(oSelectedValue);
    if (oSelectedText!=null && oSelectedValue!=null)
    {
        oSelectedText.value = oOption.innerHTML;
        oSelectedValue.value = oOption.getAttribute("value");
        
        var oOptions = oDivOptions.getElementsByTagName("div");
        for (iOption=0; iOption<oOptions.length; iOption++)
        {
            if (oOptions[iOption].getAttribute("value")==oOption.getAttribute("value"))
                oOptions[iOption].className = "ComboBoxSelectedItem";
            else
                oOptions[iOption].className = "ComboBoxItem";
        }
        
         fireOnChangeEvent(oSelectedText);
    }
    oDivOptions.style.display = "none";
}


var oLastList = null;
function ToggleList(oDivOptions)
{
    oDivOptions = document.getElementById(oDivOptions);
    if (oDivOptions!=null)
    {
        if (oLastList!=null)
            if (oLastList!=oDivOptions)
                oLastList.style.display = "none";
        
        oLastList = null;
        if (oDivOptions.style.display=="")
            oDivOptions.style.display = "none";
        else
        {
            oDivOptions.style.display = "";
            oLastList = oDivOptions;
        }
    }       
}

function ToggleInfoBox(sInfoBox, oLink) {
    if (document.getElementById(oLink) != null)
        oLink = document.getElementById(oLink);
    var oInfoBox = document.getElementById(sInfoBox);
    
    if (oInfoBox != null) {
        var oImg = oLink.getElementsByTagName("img");
        if (oImg.length > 0)
            oImg = oImg[0];

        if (oInfoBox.className.indexOf("_show") == -1) {
            oInfoBox.className = oInfoBox.className + "_show";
            oImg.src = oImg.src.replace("_down.gif", "_up.gif");

            //height for IE8
            if (sInfoBox == "divInfoBox2") {
                document.getElementById("divContentWidth").style.height = document.getElementById("divE1MainBox").offsetHeight + 250;
            }
        }
        else {
            oInfoBox.className = oInfoBox.className.replace("_show", "");
            oImg.src = oImg.src.replace("_up.gif", "_down.gif");

            if (sInfoBox == "divInfoBox2") {
                document.getElementById("divContentWidth").style.height = "auto";
            }
        }
    }
}

function fireOnChangeEvent(control)
{ 
    if (document.all) 
    { 
        control.fireEvent("onchange"); 
    } 
    else 
    { 
        var clickEvent = document.createEvent('HTMLEvents');
        clickEvent.initEvent("change", true, true); 
        control.dispatchEvent(clickEvent); 
    }
}


function AddToBasketEnter(sTitle, iItemPageId, sItemCode, sItemPrice, sItemURL, event) {
    var kC = (window.event) ?    // MSIE or Firefox?
						 event.keyCode : e.keyCode;
    if (kC == 13) {
        AddToBasket(sTitle, iItemPageId, sItemCode, sItemPrice, sItemURL)
    }
}

function AddToBasket(sTitle, iItemPageId, sItemCode, sItemPrice, sItemURL)
{
    var oValue = ASP.MasterPageStore.AddToBasket(sTitle, iItemPageId, sItemCode,  sItemPrice, sItemURL).value;
    var oDivBasket = document.getElementById("divBasket");
    if (oDivBasket!=null)
    {
        oDivBasket.innerHTML = oValue;
        if (oValue!= "")
        {
            var oBasketBox = document.getElementById("divBasketBox");
            var oBasketNoItems = document.getElementById("divBasketNoItems");
            if (oBasketBox!=null)
                oBasketBox.style.display = "block";
            if (oBasketNoItems!=null)
                oBasketNoItems.style.display = "none";
        }
    }
}

function DetailedBasket_RemoveFromBasket(sItemCode, oProd) {
    if (confirm("האם אתה בטוח שברצונך להסיר את המוצר מעגלת הקניות?")) {
        var oTotal = ASP.MasterPageStore.RemoveFromBasket(sItemCode).value;

        var oBasketTotal = document.getElementById("divTotalCost");
        if (oBasketTotal != null)
            oBasketTotal.innerHTML = oTotal + " ₪";

        var sDisplay = "inline";
        var sNoDisplay = "none";
        if (oTotal == "-1" || oTotal == "0") {
            sDisplay = "none";
            sNoDisplay = "inline";
        }
        
        var oProductRow = oProd.parentNode;
        document.getElementById("divBasketDetails").removeChild(oProductRow);
        document.getElementById("divCheckoutForm").style.display = sDisplay;
        
        if (oHeaderDiv != null) {
            oHeaderDiv = document.getElementById(oHeaderDiv);
            if (oHeaderDiv != null)
                oHeaderDiv.style.display = sDisplay;
        }
        
        if (oEmptyRowDiv != null) {
            oEmptyRowDiv = document.getElementById(oEmptyRowDiv);
            if (oEmptyRowDiv != null)
                oEmptyRowDiv.style.display = sNoDisplay;
        }
        
        if (oBottomDiv != null) {
            oBottomDiv = document.getElementById(oBottomDiv);
            if (oBottomDiv != null)
                oBottomDiv.style.display = sDisplay;
        }
    }
    
}

function DetailedBasket_CalculatePriceTotals(oProdNumber, dPrice, sItemCode) {
    // custom combo option is a P element, containg a
    var iProdNo = oProdNumber.getElementsByTagName("a")[0].innerHTML;
    
    var iItemPrice = parseFloat(dPrice);
    
    //update basket on server
    var oAjaxResult = ASP.MasterPageStore.UpdateBasket(sItemCode, iProdNo).value;
    var oTotal = oAjaxResult.split("#")[1];
    oAjaxResult = oAjaxResult.split("#")[0];

    if (oAjaxResult == "3") {
        //hide all basket details and chekcout form if no item remains
        document.getElementById("divCheckoutForm").style.display = "none";
        if (oHeaderDiv != null) {
            oHeaderDiv = document.getElementById(oHeaderDiv);
            if (oHeaderDiv != null)
                oHeaderDiv.style.display = "none";
        }

        if (oEmptyRowDiv != null) {
            oEmptyRowDiv = document.getElementById(oEmptyRowDiv);
            if (oEmptyRowDiv != null)
                oEmptyRowDiv.style.display = "inline";
        }

        if (oBottomDiv != null) {
            oBottomDiv = document.getElementById(oBottomDiv);
            if (oBottomDiv != null)
                oBottomDiv.style.display = "none";
        }
    }
    
    if (oAjaxResult != "-1") {
        iProdNo = parseInt(iProdNo);

        // get total item price div
        var oItemTotalPrice = oProdNumber.parentNode.parentNode.parentNode.nextSibling;

        // calculate and display current item total price
        var dItemTotalPrice;
        if ((iItemPrice != 0) && !isNaN(iItemPrice)) {
            dItemTotalPrice = iProdNo * dPrice;
            oItemTotalPrice.innerHTML = dItemTotalPrice + " ₪";
        }
        else {
            dItemTotalPrice = 0;
            oItemTotalPrice.innerHTML = dPrice;
        }

        // get all items combo in basket
        var oBasketDetails = document.getElementById("divBasketDetails");
        var oaCombo = oBasketDetails.getElementsByTagName("select");

        //total basket value
        var oBasketTotal = document.getElementById("divTotalCost");
        if (oBasketTotal != null)
            oBasketTotal.innerHTML = oTotal + " ₪";
    }

    //remove items with 0 quantity
    if (oAjaxResult == "4" || oAjaxResult == "3")
        document.getElementById("divBasketDetails").removeChild(oProdNumber.parentNode.parentNode.parentNode.parentNode);
}


function KeyPressBasket(evt, oObject) {
    
    var oId = oObject.id;
    oId = oId.substring(12);
    var iId = parseInt(oId);
    var oDdl = document.getElementById("SELECT" + iId);
    var iNextCount = 0;

    if (evt != null && evt.keyCode != null) {

        if (evt.keyCode == 38) {
            var oMainRow = document.getElementById("divDetailes" + iId);
            var oItemPrice = oMainRow.ItemPrice;
            if (oItemPrice == null)
                oItemPrice = oMainRow.getAttribute("ItemPrice");
            var oItemCode = oMainRow.ItemCode;
            if (oItemCode == null)
                oItemCode = oMainRow.getAttribute("ItemCode");

            //up key pressed

            if (oDdl.selectedIndex > 1) {
                oDdl.selectedIndex = oDdl.selectedIndex - 1;
                iNextCount = oDdl.selectedIndex;

                var oTestObj = document.getElementById("testId_" + iNextCount + "_" + iId);
                selectMe('SELECT' + iId, iNextCount, iId); hideOptionsDiv(0); DetailedBasket_CalculatePriceTotals(oTestObj, oItemPrice, oItemCode);
            }


            return false;
        }

        if (evt.keyCode == 40) {
            var oMainRow = document.getElementById("divDetailes" + iId);
            var oItemPrice = oMainRow.ItemPrice;
            if (oItemPrice == null)
                oItemPrice = oMainRow.getAttribute("ItemPrice");
            var oItemCode = oMainRow.ItemCode;
            if (oItemCode == null)
                oItemCode = oMainRow.getAttribute("ItemCode");

            //up key pressed

            if (oDdl.selectedIndex < oDdl.length - 1) {
                oDdl.selectedIndex = oDdl.selectedIndex + 1;
                iNextCount = oDdl.selectedIndex;

                var oTestObj = document.getElementById("testId_" + iNextCount + "_" + iId);
                selectMe('SELECT' + iId, iNextCount, iId); hideOptionsDiv(0); DetailedBasket_CalculatePriceTotals(oTestObj, oItemPrice, oItemCode);
            }

            return false;
        }
    
        if (evt.keyCode == 37) {
            //debugger;

            var oMainRow = document.getElementById("divDetailes" + iId);
            var oItemPrice = oMainRow.ItemPrice;
            if (oItemPrice == null)
                oItemPrice = oMainRow.getAttribute("ItemPrice");
            var oItemCode = oMainRow.ItemCode;
            if (oItemCode == null)
                oItemCode = oMainRow.getAttribute("ItemCode");

            //up key pressed

            if (oDdl.selectedIndex > 1) {
                oDdl.selectedIndex = oDdl.selectedIndex - 1;
                iNextCount = oDdl.selectedIndex;

                var oTestObj = document.getElementById("testId_" + iNextCount + "_" + iId);
                selectMe('SELECT' + iId, iNextCount, iId); hideOptionsDiv(0); DetailedBasket_CalculatePriceTotals(oTestObj, oItemPrice, oItemCode);
            }
           

            return false;
        }
        if (evt.keyCode == 39) {
            //debugger;

            var oMainRow = document.getElementById("divDetailes" + iId);
            var oItemPrice = oMainRow.ItemPrice;
            if (oItemPrice == null)
                oItemPrice = oMainRow.getAttribute("ItemPrice");
            var oItemCode = oMainRow.ItemCode;
            if (oItemCode == null)
                oItemCode = oMainRow.getAttribute("ItemCode");

            //up key pressed

            if (oDdl.selectedIndex < oDdl.length - 1) {
                oDdl.selectedIndex = oDdl.selectedIndex + 1;
                iNextCount = oDdl.selectedIndex;

                var oTestObj = document.getElementById("testId_" + iNextCount + "_" + iId);
                selectMe('SELECT' + iId, iNextCount, iId); hideOptionsDiv(0); DetailedBasket_CalculatePriceTotals(oTestObj, oItemPrice, oItemCode);
            }
           
            return false;
        }
    }

    return true;
}
