﻿var object;
var FirstTime=true;
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
    {
        if(FirstTime==false)
        {
            ShowProcessing("Loading Sku please wait...");
           
            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);});
        }
        else
        {
            FirstTime=false;
        }
        
    }
    catch(error)
    {
        ecf_PurchaseModule_ToggleAccessoryCheckboxes(false);
    }
}

function PurchaseModule_UpdatePrices(prodid,skuid)
{   

    var skulistid="";
    skulistid=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AlternativePictures_hdnSKUIDSize').value;
  
    var arr;
    var arrskuid;
    arr=skulistid.split(",");
              
  
   for(i=0;i<arr.length;i++)
   {
      arrskuid="";
      arrskuid=arr[i].split("$");
      if(skuid==arrskuid[0])
      {
           
            document.getElementById('SIZE_'+arrskuid[0]).className ="selected";
            document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_PurchaseCtrl_hdnSize').value=arrskuid[1];            
      }
      else
      {
          
            document.getElementById('SIZE_'+arrskuid[0]).className ="";
      }
   }
        
        
    var items = new Array(0);
   /* if(source!=null)
    {
        if(!source.checked)
        {
            accessories[0] = -source.getAttribute('accessoryid');
        }       
    }*/
    
    try
    {
        if(FirstTime==false)
        {
            ShowProcessing("Loading Sku please wait...");
           
            Ignify.eCommerce.Web.Store.MasterTemplates.Ignify.Modules.SharedModules.PurchaseModule.UpdatePrice(prodid, skuid, m_ecf_PurchaseModule_CurrencyCode, items, function(result) {ecf_PurchaseModule_UpdatePricesCallback(result, null);});
        }
        else
        {
            FirstTime=false;
        }
        
    }
    catch(error)
    {
        ecf_PurchaseModule_ToggleAccessoryCheckboxes(false);
    }
   // document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_PurchaseCtrl_hdnSize').value="";
}

function ecf_PurchaseModule_UpdatePricesCallback(result, source)
{  
    object = YAHOO.lang.JSON.parse(result.value); 
    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 addtocartdetails = document.getElementById(m_ecf_PurchaseModule_hdnItemDetails); 
    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;
       
   
    
    itemCode.innerHTML = object.code;
    if(qtyPricingControl != "undefined" && qtyPricingControl != null)
        qtyPricingControl.innerHTML = object.qtyPricing;
    if(StockStatus != "undefined" && StockStatus != null)
        StockStatus.innerHTML=object.stockStatus;
    
    ListPrice.innerHTML=object.listPrice;
    RetailPrice.innerHTML='Sale! '+ object.savedPrice;
   // SavedPrice.innerHTML=object.savedPrice;
    
    if(object.listPrice == object.savedPrice)
    {
        RetailPrice.style.display='none';
    }
    else
    {
        RetailPrice.style.display='block';
    }
    
    if(AsLowAsPrice != "undefined" && AsLowAsPrice != null)
        AsLowAsPrice.innerHTML=object.asLoweAs;
    
    itemName.innerHTML = object.name;
    hdnItemID.value = object.variationId;
    addtocartdetails.value = object.addToCartDetails;
    /*if(object.qtyPricing =="")
     {
     //   QtyBasePricingPanel.style.visibility = "hidden";   
      //  QtyBasePricingPanel.style.height="0px";
       // SinglePricePanel.style.height="60px";
      //  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";
     }   
     DiscountDesc.innerHTML = object.discountDisc;
     //LoadSkuImage(); 
     if(object.image!="")
     {
        paginator.setPage(1, true);
     }*/ 
     StopProcessing();

}

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;
    if(ecf_PurchaseModule_DropdownArray.length==1)
    {
      FirstTime=false;
    } 
        
    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;
    }
}


 