﻿function ecf_PurchaseModule_OnAccessoriesUpdate(source, accessories)
{
    ecf_PurchaseModule_InitAccessoryCheckboxes();
}

//gets sku details based on the current attribute selection.
//no need to select all dropdowns of the attributes.
function ecf_PurchaseModule_PurchaseOptionChanged1(dropids)
{
        document.getElementById("__ECFPURCHASEMODULE_ITEMID").value = dropids.options[0].value;
        m_ecf_PurchaseModule_VariationId =dropids.options[0].value;
        ecf_PurchaseModule_InitAccessoryCheckboxes();
}

function ecf_PurchaseModule_UpdatePrices(source, accessories)
{
    if(source!=null)
    {
        if(!source.checked)
        {
            accessories[0] = -source.getAttribute('accessoryid');
        }       
    }
    
    try
    {
     
        Ignify.eCommerce.Web.Store.MasterTemplates.Ignify.Modules.SharedModules.PurchaseModule.UpdatePrice(m_ecf_PurchaseModule_ProductId, m_ecf_PurchaseModule_VariationId, m_ecf_PurchaseModule_CurrencyCode, accessories, function(result) {ecf_PurchaseModule_UpdatePricesCallback(result, source);});
    }
    catch(error)
    {
        ecf_PurchaseModule_ToggleAccessoryCheckboxes(false);
    }
}

function ecf_PurchaseModule_UpdatePricesCallback(result, source)
{
    var itemName = document.getElementById(m_ecf_PurchaseModule_NameControlId); 
    var itemCode = document.getElementById(m_ecf_PurchaseModule_ItemCodeControlId); 
    var StockStatus=document.getElementById(m_ecf_PurchaseModule_StockStatusControlId); 
    
    var ListPrice=document.getElementById(m_ecf_PurchaseModule_ListPriceControlId); 
    var RetailPrice=document.getElementById(m_ecf_PurchaseModule_RetailPriceControlId); 
    var SavedPrice=document.getElementById(m_ecf_PurchaseModule_SavedPriceControlId); 
    var AsLowAsPrice=document.getElementById(m_ecf_PurchaseModule_AsLowAsPriceControlId); 
    var SinglePricePanel=document.getElementById(m_ecf_PurchaseModule_SinglePricePanelControlId); 
    var QtyBasePricingPanel=document.getElementById(m_ecf_PurchaseModule_QtyBasePricingPanelControlId); 
    var DiscountDesc=document.getElementById(m_ecf_PurchaseModule_DiscountDescriptionControlId); 
    //var availability = document.getElementById(m_ecf_PurchaseModule_AvailabilityControlId);
    //var priceListControl = document.getElementById(m_ignify_PurchaseModule_PriceListControlId);
    var qtyPricingControl = document.getElementById(m_ecf_PurchaseModule_QtyPricingControlId);
     var hdnItemID = document.getElementById(m_ecf_PurchaseModule_ItemCodehdnControlId); 
        
    if(result.error!=null)
    {
        //alert("AJAX: Error occured while processing the request, refresh the page. Message: " + result.error.Message + ", code: " + result.error.Code);
        ecf_PurchaseModule_ToggleAccessoryCheckboxes(false);
        return;
    }
    
    if(result.value == null || result.value.length < 3)
        return;
       
   
    if(result.value.length >= 4)
        itemCode.innerHTML = result.value[3];
    if(result.value.length >= 6 && qtyPricingControl != "undefined" && qtyPricingControl != null)
        qtyPricingControl.innerHTML = result.value[5];
    if(result.value.length >= 7 && StockStatus != "undefined" && StockStatus != null)
        StockStatus.innerHTML=result.value[6];
    if(result.value.length >= 8)
        ListPrice.innerHTML=result.value[7];
    if(result.value.length >= 9)
        RetailPrice.innerHTML=result.value[8];
    if(result.value.length >= 10)
        SavedPrice.innerHTML=result.value[9];
    if(result.value.length >= 11 && AsLowAsPrice != "undefined" && AsLowAsPrice != null)
        AsLowAsPrice.innerHTML=result.value[10];
    if(result.value.length >= 12)
        itemName.innerHTML = result.value[11];
    if(result.value.length >= 13)
        hdnItemID.value = result.value[12];
   
    if(result.value[10]=="" && QtyBasePricingPanel != "undefined" && QtyBasePricingPanel != null)
     {
        QtyBasePricingPanel.style.visibility = "hidden";   
        QtyBasePricingPanel.style.height="0px";
        //SinglePricePanel.style.height="50px";
        SinglePricePanel.style.visibility = "visible";
        QtyBasePricingPanel.style.overflow="hidden";
        AsLowAsPrice.style.visibility = "hidden";   
     }
     else
     {
        SinglePricePanel.style.visibility = "hidden";
        SinglePricePanel.style.height="0px";
        LABEL=document.getElementById(m_ecf_PurchaseModule_LblQtybaseprice);
        QtyBasePricingPanel.style.height=LABEL.style.height;
        QtyBasePricingPanel.style.visibility = "visible";
        SinglePricePanel.style.overflow="hidden";   
        AsLowAsPrice.style.visibility = "visible";
     }   
      
      if(result.value.length >= 15)
      {
      
       if(result.value[14]=="False")
       {
          document.getElementById(m_ecf_PurchaseModule_AddtoCartControlID).style.display = "none";
          document.getElementById(m_ecf_PurchaseModule_QtyPanelControlID).style.display = "none";
       }
       else
       {
          document.getElementById(m_ecf_PurchaseModule_AddtoCartControlID).style.display = "block";
          document.getElementById(m_ecf_PurchaseModule_QtyPanelControlID).style.display = "block";
       }
       
      }
      
      if(result.value.length >= 14)
        DiscountDesc.innerHTML = result.value[13];
              
}

function ecf_PurchaseModule_PurchaseOptionChanged(source)
{
    document.getElementById("__ECFPURCHASEMODULE_ITEMID").value = source.value;
    m_ecf_PurchaseModule_VariationId = source.value;
    ecf_PurchaseModule_InitAccessoryCheckboxes();
}

function ecf_PurchaseModule_Init()
{
    window.onload = function(){ecf_PurchaseModule_InitAccessoryCheckboxes2(true);ecf_PurchaseModule_InitOptionLists();};    
}

function ecf_PurchaseModule_InitOptionLists()
{ 
    if(typeof(ecf_PurchaseModule_DropdownArray) == "undefined")
        return;
        
    initDynamicOptionLists();       
    
    document.getElementById(ecf_PurchaseModule_DropdownArray[ecf_PurchaseModule_DropdownArray.length - 1]).onchange = new Function("ecf_PurchaseModule_PurchaseOptionChanged(this)");
    for(var index = 0; index<ecf_PurchaseModule_DropdownArray.length; index++)
    {
        var dropdown = document.getElementById(ecf_PurchaseModule_DropdownArray[index]);
        if(dropdown != null && dropdown.options.Length > 0)
            dropdown.options[0].selected = true;
    }
}

function ecf_PurchaseModule_InitAccessoryCheckboxes()
{
    ecf_PurchaseModule_InitAccessoryCheckboxes2(false);
}

function ecf_PurchaseModule_InitAccessoryCheckboxes2(onload)
{
    // get array length
    var arrayLength = 0;
    var hasCheckedControls = false;
    for(var index = 0; index<ecf_PurchaseModule_CheckboxesArray.length; index++)
    {
        var checked = document.getElementById(ecf_PurchaseModule_CheckboxesArray[index]);      
        if(checked.checked)
            arrayLength++;
    }

    // populate array
    var items = new Array(arrayLength);
    for(var index = 0; index<ecf_PurchaseModule_CheckboxesArray.length; index++)
    {
        var checked = document.getElementById(ecf_PurchaseModule_CheckboxesArray[index]);
        var id = document.getElementById(ecf_PurchaseModule_CheckboxesArray[index]).getAttribute('accessoryid');
       
        if(checked.checked)
        {
            hasCheckedControls = true;
            items[index] = id;
        }
    }

    // don't update on first visit if checkbox-es are not checked, they will be checked only if back button is clicked    
    if((onload && hasCheckedControls) || !onload)
        ecf_PurchaseModule_UpdatePrices(null, items);
}

function ecf_PurchaseModule_ToggleAccessoryCheckboxes(check)
{
    for(var index = 0; index<ecf_PurchaseModule_CheckboxesArray.length; index++)
    {
        var checked = document.getElementById(ecf_PurchaseModule_CheckboxesArray[index]);      
        checked.checked = check;
    }
}