﻿var variationvalues;
var ClickedDropdown;
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');
        }       
    }
           variationvalues="";
        for(var index = 0; index<=ecf_PurchaseModule_DropdownArray.length-1; index++)
         {
            if(variationvalues=="")
            {
                variationvalues=document.getElementById(ecf_PurchaseModule_DropdownArray[index]).value;


            }
            else
            {
                variationvalues  =variationvalues+ "," +document.getElementById(ecf_PurchaseModule_DropdownArray[index]).value;
            }
           
         }    
    try
    {
            Ignify.eCommerce.Web.Store.MasterTemplates.Ignify.Modules.SharedModules.PurchaseModule.UpdatePrice(m_ecf_PurchaseModule_ProductId, variationvalues, m_ecf_PurchaseModule_CurrencyCode, accessories,ClickedDropdown,function(result) {ecf_PurchaseModule_UpdatePricesCallback(result, source);});
    }
    catch(error)
    {
        ecf_PurchaseModule_ToggleAccessoryCheckboxes(false);
    }
}

function ecf_PurchaseModule_UpdatePricesCallback(result, source)
{
    var lblmultipleQty = document.getElementById(m_ecf_PurchaseModule_lblmultiQty); 
    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); 
    try{
    var RetailPrice=document.getElementById(m_ecf_PurchaseModule_RetailPriceControlId); 
    var SavedPrice=document.getElementById(m_ecf_PurchaseModule_SavedPriceControlId); 
    }catch(e){}
    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 SkuQty = document.getElementById(m_ecf_PurchaseModule_SkuQty);
    var Skytype = document.getElementById(m_ecf_PurchaseModule_Skytype);
    //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); 
    try{
        var GeneralPriceRow =  document.getElementById(m_ecf_PurchaseModule_GeneralPriceRowId) ;
        var YouSaveRow = document.getElementById(m_ecf_PurchaseModule_YouSaveRowId) ;
     }catch(e){};
        
    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;
        
        var updatevalues=result.value[16].split('#');
    if(ecf_PurchaseModule_DropdownArray.length > 1)
    {
        for(var index = 0; index<ecf_PurchaseModule_DropdownArray.length; index++)
        {
             for(var dindex=0;dindex<document.getElementById(ecf_PurchaseModule_DropdownArray[index]).length;dindex++)
             {
                if(updatevalues[index].indexOf(document.getElementById(ecf_PurchaseModule_DropdownArray[index]).options[dindex].value)!=-1)
                {
                  document.getElementById(ecf_PurchaseModule_DropdownArray[index]).options[dindex].text=document.getElementById(ecf_PurchaseModule_DropdownArray[index]).options[dindex].text.replace("(N/A)","");
                }
                else
                {
                  document.getElementById(ecf_PurchaseModule_DropdownArray[index]).options[dindex].text=document.getElementById(ecf_PurchaseModule_DropdownArray[index]).options[dindex].text.replace("(N/A)","");
                  document.getElementById(ecf_PurchaseModule_DropdownArray[index]).options[dindex].text=document.getElementById(ecf_PurchaseModule_DropdownArray[index]).options[dindex].text+"(N/A)";
                }
             }
        }
    }
    
  
    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.innerHTML=result.value[6];
    if(result.value.length >= 8)
        ListPrice.innerHTML=result.value[7];
    if(result.value.length >= 9)
        try{RetailPrice.innerHTML=result.value[8];}catch(e){}
    if(result.value.length >= 10)
        try{SavedPrice.innerHTML=result.value[9];}catch(e){}
    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.length >= 15)
        Skytype.value = result.value[14];
    if(result.value.length >= 16)
        SkuQty.value = result.value[15];
   
   
   //Adding the message for Multiple Qty.
   if(result.value[14] == "Multiple" && result.value[15] > 0)
   {
        lblmultipleQty.innerHTML = "(Can be Purchased only in multiples of " + result.value[15] + ")";
   }else
   {
        lblmultipleQty.innerHTML="";
   }
   
   //
   
   
    if(result.value[10]=="" && QtyBasePricingPanel != "undefined" && QtyBasePricingPanel != null)
     {
        QtyBasePricingPanel.style.display = "none";   
       // QtyBasePricingPanel.style.height="0px";
        SinglePricePanel.style.display = "block";
        AsLowAsPrice.style.display = "none";
        if (result.value[9].replace("$", '') > 0) 
        {
         GeneralPriceRow.style.display = "block";
         YouSaveRow.style.display = "block";
        }else
        {
         GeneralPriceRow.style.display = "none";
         YouSaveRow.style.display = "none";
        }
       
     }
     else
     {
        //SinglePricePanel.style.visibility = "hidden";
        SinglePricePanel.style.display = "none";
       // SinglePricePanel.style.height="0px";
        //QtyBasePricingPanel.style.visibility = "visible";   
        //AsLowAsPrice.style.visibility = "visible";
        QtyBasePricingPanel.style.display = "block";   
        AsLowAsPrice.style.display = "block";
  
     }   
     
              
}

function ecf_PurchaseModule_PurchaseOptionChanged(source,id)
{
    document.getElementById("__ECFPURCHASEMODULE_ITEMID").value = source.value;
    m_ecf_PurchaseModule_VariationId = source.value;
     ClickedDropdown = id;
    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();       
    
     for(var index = 0; index<ecf_PurchaseModule_DropdownArray.length; index++)
    {
          if(document.getElementById(ecf_PurchaseModule_DropdownArray[index])!=null)
          {
            //Check for Any (N/A) is present or not according to that display the N/A message
            for(var dindex=0;dindex<document.getElementById(ecf_PurchaseModule_DropdownArray[index]).length;dindex++)
                 {
                      if(document.getElementById(ecf_PurchaseModule_DropdownArray[index]).options[dindex].text.indexOf("(N/A)")!=-1)
                        {
                            document.getElementById("NAdiv").style.visibility = "visible";
                        }
                 }
            document.getElementById(ecf_PurchaseModule_DropdownArray[index]).onchange = new Function("ecf_PurchaseModule_PurchaseOptionChanged(this,"+ index +")");     
          }
          else
          {
               document.getElementById("NAdiv").style.visibility = "hidden";
          }
            
    }
}

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;
    }
}