﻿// var qtyPricingControl = document.getElementById(m_ecf_PurchaseModule_QtyPricingControlId);
function ecf_PurchaseModule_OnAccessoriesUpdate(source, accessories)
{
    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 listPrice = document.getElementById(m_ecf_PurchaseModule_ListPriceControlId);
    //var finalPrice = document.getElementById(m_ecf_PurchaseModule_FinalPriceControlId);
    //var discountPrice = document.getElementById(m_ecf_PurchaseModule_DiscountPriceControlId);
 
    var itemCode = document.getElementById(m_ecf_PurchaseModule_ItemCodeControlId);    
    //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 lblSelectQty=document.getElementById(m_ecf_PurchaseModule_lblSelectQty);
    var hdnQuantity=document.getElementById(m_ecf_PurchaseModule_hdnQuantity);
    var MfgPartNumber = document.getElementById(m_ecf_PurchaseModule_MfgPartNumberId);   
    var trowMfgPart= document.getElementById(m_ecf_PurchaseModule_trMfgPart); 
    //var MfgPartVal=document.getElementById(m_ecf_PurchaseModule_MfgPartValuefromAdd2cart); 
  //  alert(m_ecf_PurchaseModule_MfgPartValuefromAdd2cart);
    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;
       
    //listPrice.innerHTML = result.value[0];
    //finalPrice.innerHTML = result.value[1];
    //if(discountPrice != null)
    //    discountPrice.innerHTML = result.value[2];
        
    if(result.value.length >= 4)
    {
    if(result.value[3].length>6)
        itemCode.innerHTML = result.value[3].substring(0,6);
        else
        itemCode.innerHTML = result.value[3];
     }   
       // alert(result.value[6]);
       if(result.value[6]!="")
       {
             if(MfgPartNumber !=null )
             {
                MfgPartNumber.innerHTML = result.value[6];
             }
             if(m_ecf_PurchaseModule_MfgPartValuefromAdd2cart!="POPUP")
             {
                 trowMfgPart.style.display="block";
             }
       }
       else
       {
             trowMfgPart.style.display="none";
       }
       // alert(qtyPricingControl);
    //if(result.value.length >= 5)        
    //availability.innerHTML = result.value[4];
    
        if(result.value.length >= 6)     
        {   
            priceListControl.innerHTML = result.value[5];
           // priceListControl.style.display="block";
        }
//    if(result.value.length >= 7 && qtyPricingControl != "undefined" && qtyPricingControl != null)
//        qtyPricingControl.innerHTML = result.value[7];

 // alert(result.value[7]);
    if(result.value.length >= 7 && qtyPricingControl != "undefined" && qtyPricingControl != null)
    {
     //   alert(valueQty[0]);
    qtyPricingControl.options.length=result.value[7].split("~").length-1;
    for(var index=0;index<result.value[7].split("~").length-1;index++)
    {
        var valueQty=result.value[7].split("~");
        var val=valueQty[index].split("@");
       // var optn = document.createElement("OPTION");
        qtyPricingControl.options[index].text=val[0]+" - "+val[1]+"  "+"("+val[2]+")";
        qtyPricingControl.options[index].value=val[0] + "~" + val[1];
         //qtyPricingControl.options.add(optn);
    }   
 
     if(result.value[7]!="")
     {
        //lblSelectQty.visible=true;
        // qtyPricingControl.visible=true;
        qtyPricingControl.options[0].selected=true;
         hdnQuantity.value=qtyPricingControl.options[0].value;
        qtyPricingControl.style.display="block";
        lblSelectQty.style.display="block";

    }
    else
    {
    
        qtyPricingControl.style.display="none";
        lblSelectQty.style.display="none";
//        priceListControl.style.display="block";
    }
//alert(hdnQuantity.value);     
 }  
// else
// {
// lblSelectQty.style.visibility =hidden;
// qtyPricingControlstyle.visibility =hidden;
// } 
//       // alert(result.value[7]);

	if(result.value[8]!="")
     {
        document.getElementById('WarrantyDropDown').innerHTML =result.value[8];

    }
    else
    {
    document.getElementById('WarrantyDropDown').innerHTML =''; 
    }
}


function ecf_PurchaseModule_PurchaseOptionChanged(source)
{
    document.getElementById("__ECFPURCHASEMODULE_ITEMID").value = source.value;
   // alert("in ecf_PurchaseModule_PurchaseOptionChanged"+source.value);
 //alert(qtyPricingControl);
    m_ecf_PurchaseModule_VariationId = source.value;
    ecf_PurchaseModule_InitAccessoryCheckboxes();
}

function ecf_PurchaseModule_Init()
{
//alert("ecf_PurchaseModule_Init");
    window.onload = function(){ecf_PurchaseModule_InitAccessoryCheckboxes2(true);ecf_PurchaseModule_InitOptionLists();};   
    //  var qtyPricingControl2 = document.getElementById(m_ecf_PurchaseModule_QtyPricingControlId);
      //alert(qtyPricingControl2);
    
}

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;
    }
}



