﻿var object;
var FirstTime=true;
var variationvalues;
var ClickedDropdown;
var NOT_APPLICABLE ="Not Applicable"
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)
{
 //alert('Hi ecf_PurchaseModule_UpdatePricesCallback 2') ;
    if(source!=null)
    {
        if(!source.checked)
        {
            accessories[0] = -source.getAttribute('accessoryid');
           alert('accessories[0]' + accessories[0]) ;
        }   
    }
    
        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
    {
             //if(Minitemplate!=null && Minitemplate==true) 
            //{
               // parent.ShowProcessing(SKU_LOADING_WAIT_MSG);
            //}
            //else
            //{
              //  ShowProcessing(SKU_LOADING_WAIT_MSG);
            //}
            
            Ignify.eCommerce.Web.Store.MasterTemplates.Ignify.Modules.SharedModules.PurchaseModule.UpdatePrice(m_ecf_PurchaseModule_ProductId, variationvalues, m_ecf_PurchaseModule_CurrencyCode, accessories,ClickedDropdown,m_ecf_PurchaseModule_optionsfields,function(result) {ecf_PurchaseModule_UpdatePricesCallback(result, source);});
    }
    catch(error)
    {
        ecf_PurchaseModule_ToggleAccessoryCheckboxes(false);
        //alert('Error Occurred' + error) 
    }
}
function ecf_PurchaseModule_UpdatePricesCallback(result, source)
{
   // alert('Hi ecf_PurchaseModule_UpdatePricesCallback 1') ;
    object = JSON.parse(result.value);
    //alert(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 ProdPrice = document.getElementById(m_ecf_PurchaseModule_ProdPriceControlId);
   // alert('itemCode' + itemCode);
//alert('itemName' + itemName);  
    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 MultipleRetailPrice = document.getElementById(m_ecf_PurchaseModule_NewRequirementControlId);
    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 qtyPricingControl = document.getElementById(m_ecf_PurchaseModule_QtyPricingControlId);
    var hdnItemID = document.getElementById(m_ecf_PurchaseModule_ItemCodehdnControlId); 
    var productPlanDesc = document.getElementById(m_ecf_PurchaseModule_ProductPlanDescription);
    var smallSkuImage = document.getElementById(m_ecf_PurchaseModule_SkuSmallImage);
    //alert(smallSkuImage);
   // alert('itemCode' + itemCode);
   if(result.error!=null)
    {
   alert(result.error.Message);
        //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(document.getElementById(m_ecf_PurchaseModule_hdnStockQtyControlId) != null)
        document.getElementById(m_ecf_PurchaseModule_hdnStockQtyControlId).value = object.stockQuantity;
    
    if(document.getElementById(m_ecf_PurchaseModule_lblAddToCartMsg) != null)
        document.getElementById(m_ecf_PurchaseModule_lblAddToCartMsg).innerHTML = "";
       
       
    var updatevalues=object.dropDownupdate.split('#');
    if(ecf_PurchaseModule_DropdownArray.length > 1)
    {
        for(var index = 0; index<ecf_PurchaseModule_DropdownArray.length; index++)
        {
            if(ClickedDropdown!=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(document.getElementById("NAdiv") != "undefined" && document.getElementById("NAdiv") != null)
                       document.getElementById("NAdiv").style.visibility = "visible";
                    }
                 }
             }
        }
    }
    //Added for recurring Shipment
    if(typeof(object.allowedSubscriptionFreqs) != "undefined" && object.allowedSubscriptionFreqs != '')
    {
        var subscriptionvals = object.allowedSubscriptionFreqs.split('#');        
        document.getElementById(m_ecf_PurchaseModule_shipmentFreq).disabled = false;
        //document.getElementById(m_ecf_PurchaseModule_txtShipmentCount).disabled = false;  
        //document.getElementById(m_ecf_PurchaseModule_txtShipmentCount).value='';      
        var theDropDown = document.getElementById(m_ecf_PurchaseModule_shipmentFreq);
        var numberOfOptions = theDropDown.options.length;
        
        var selectedValue,selectedText; 
        var selectedIndex = (document.getElementById(m_ecf_PurchaseModule_shipmentFreq).selectedIndex);
        var selectedValue = document.getElementById(m_ecf_PurchaseModule_shipmentFreq).value;
        var selectedText = document.getElementById(m_ecf_PurchaseModule_shipmentFreq).options[selectedIndex].text;
        
        if(numberOfOptions > 1)
        {
            for (index=0; index<numberOfOptions; index++) 
            {
                //Note: Always remove(0) and NOT remove(i)   
                theDropDown.remove(0)   
            }           
        }
        for(var count = 0; count < subscriptionvals.length-1; count++)
        {
            var newvalues = subscriptionvals[count].split(';');
            addOption(theDropDown, newvalues[1], newvalues[0]);
            document.getElementById(m_ecf_PurchaseModule_trDiscount).style.visibility = 'hidden';
        }
    }
    else
    {        
        if(document.getElementById(m_ecf_PurchaseModule_shipmentFreq) != null )
        {
            //document.getElementById(m_ecf_PurchaseModule_txtShipmentCount).value = '';
            document.getElementById(m_ecf_PurchaseModule_shipmentFreq).value = "0";
            document.getElementById(m_ecf_PurchaseModule_shipmentFreq).disabled = true;
            //document.getElementById(m_ecf_PurchaseModule_txtShipmentCount).disabled = true;            
            document.getElementById(m_ecf_PurchaseModule_trDiscount).style.visibility = 'hidden';
        }
    }
    //Added for rental
    if(typeof(object.allowedRentalFreqs) != "undefined" && object.allowedRentalFreqs != '')
    {
        var subscriptionvals = object.allowedRentalFreqs.split('#');        
        document.getElementById(m_ecf_PurchaseModule_rentalFreq).disabled = false;
        var theDropDown = document.getElementById(m_ecf_PurchaseModule_rentalFreq);
        var numberOfOptions = theDropDown.options.length;
        
        var selectedValue,selectedText; 
        var selectedIndex = (document.getElementById(m_ecf_PurchaseModule_rentalFreq).selectedIndex);
        var selectedValue = document.getElementById(m_ecf_PurchaseModule_rentalFreq).value;
        var selectedText = document.getElementById(m_ecf_PurchaseModule_rentalFreq).options[selectedIndex].text;
        
        if(numberOfOptions > 1)
        {
            for (index=0; index<numberOfOptions; index++) 
            {
                //Note: Always remove(0) and NOT remove(i)   
                theDropDown.remove(0)   
            }           
        }
        for(var count = 0; count < subscriptionvals.length-1; count++)
        {
            var newvalues = subscriptionvals[count].split(';');
            addOption(theDropDown, newvalues[1], newvalues[0]);
        }
    }
    else
    {        
        if(document.getElementById(m_ecf_PurchaseModule_rentalFreq) != null )
        {
            document.getElementById(m_ecf_PurchaseModule_rentalFreq).value = "0";
            document.getElementById(m_ecf_PurchaseModule_rentalFreq).disabled = true;
        }
    }
    
    if(document.getElementById(m_ecf_PurchaseModule_btnAddToCartId) != null)
    {
        if(object.stockQuantity <=0)
        {
            if(object.allowBackOrder == "0" && !object.alternateProductsExists)
            {
                
                //disable quantity and addtocart button
//                document.getElementById(m_ecf_PurchaseModule_btnAddToCartId).disabled = true;
//                document.getElementById(m_ecf_PurchaseModule_txtQtyId).disabled = true;
//                document.getElementById(m_ecf_PurchaseModule_txtQtyId).className = "disabledColor layadd2cartext";
//                document.getElementById(m_ecf_PurchaseModule_btnAddToCartId).className = "add2cartdisable";
                  //Fix for Issue 519
                  //Enable it
                document.getElementById(m_ecf_PurchaseModule_btnAddToCartId).disabled = false;
                document.getElementById(m_ecf_PurchaseModule_txtQtyId).disabled = true;
                document.getElementById(m_ecf_PurchaseModule_txtQtyId).className = "layadd2cartext";
                //document.getElementById(m_ecf_PurchaseModule_btnAddToCartId).className = "add2cart layadd2cart";
                document.getElementById(m_ecf_PurchaseModule_btnAddToCartId).className = "add2cart_intab";
            }
            else
            {
                //Enable it
                document.getElementById(m_ecf_PurchaseModule_btnAddToCartId).disabled = false;
                document.getElementById(m_ecf_PurchaseModule_txtQtyId).disabled = false;
                document.getElementById(m_ecf_PurchaseModule_txtQtyId).className = "layadd2cartext";
                //document.getElementById(m_ecf_PurchaseModule_btnAddToCartId).className = "add2cart layadd2cart";
                document.getElementById(m_ecf_PurchaseModule_btnAddToCartId).className = "add2cart_intab";
            }
        }
        else
        {
            document.getElementById(m_ecf_PurchaseModule_btnAddToCartId).disabled = false;
            document.getElementById(m_ecf_PurchaseModule_txtQtyId).disabled = false;
            document.getElementById(m_ecf_PurchaseModule_txtQtyId).className = "layadd2cartext";
            //document.getElementById(m_ecf_PurchaseModule_btnAddToCartId).className = "add2cart layadd2cart";
            document.getElementById(m_ecf_PurchaseModule_btnAddToCartId).className = "add2cart_intab";
        }
    }

    if(itemCode != "undefined" && itemCode != null)
        itemCode.innerHTML = GetNotAvailable(object.code);

       
    if(productPlanDesc != "undefined" && productPlanDesc != null)
    {
        if(object.allowedPrePayFreq != null && object.allowedPrePayFreq != "undefined")
          productPlanDesc.innerHTML = GetNotAvailable(object.allowedPrePayFreq);
         else
          productPlanDesc.innerHTML = NOT_APPLICABLE;     
    }
    
    m_ecf_PurchaseModule_Variationcode=object.code;
    
    if(qtyPricingControl != "undefined" && qtyPricingControl != null)
        qtyPricingControl.innerHTML = GetNotAvailable(object.qtyPricing);
    if(StockStatus != "undefined" && StockStatus != null)
    {
        StockStatus.innerHTML= GetNotAvailable(object.stockStatus);
        StockStatus.className = object.stockClass;
    }
    if(ListPrice != "undefined" && ListPrice != null)
    ListPrice.innerHTML=GetNotAvailable(object.listPrice);
    if(RetailPrice != "undefined" && RetailPrice != null)
    RetailPrice.innerHTML=GetNotAvailable(object.retailPrice);
    if(SavedPrice != "undefined" && SavedPrice != null)
    SavedPrice.innerHTML=GetNotAvailable(object.savedPrice);
    
    if(smallSkuImage != "undefined" && smallSkuImage != null)
    {
        if(GetNotAvailable(object.smallImage) != NOT_APPLICABLE)
        {
            smallSkuImage.src = GetNotAvailable(object.smallImage);
            if(smallSkuImage.style.visibility != 'visible')
            {
                smallSkuImage.style.visibility='visible';
		smallSkuImage.style.maxWidth ='54px';
                smallSkuImage.style.maxHeight ='50px';
            }
        }
        else
        smallSkuImage.style.visibility='hidden';
    }
    
    if(AsLowAsPrice != "undefined" && AsLowAsPrice != null && Minitemplate==false)
    {
        AsLowAsPrice.innerHTML=GetNotAvailable(object.asLoweAs);
        
       if(ProdPrice != "undefined" && ProdPrice != null)
       {
         ProdPrice.innerHTML=GetNotAvailable(object.asLoweAs);
       }
    }
    else
    {
        
     
        if(ProdPrice != "undefined" && ProdPrice != null)
       {
       
        ProdPrice.innerHTML=GetNotAvailable(object.retailPrice);
        }
    }
    if(MultipleRetailPrice != "undefined" && MultipleRetailPrice != null && Minitemplate==false)
    {
        MultipleRetailPrice.innerHTML=GetNotAvailable(object.retailPrice);   
    }
    
    
     
    if(itemName != "undefined" && itemName != null)
    itemName.innerHTML = GetNotAvailable(object.name);
    
    if(hdnItemID != "undefined" && hdnItemID != null)
    hdnItemID.value = object.variationId;
      
    if(object.qtyPricing =="")
     {
         if(QtyBasePricingPanel != "undefined" && QtyBasePricingPanel != null)
        {
        QtyBasePricingPanel.style.visibility = "hidden";   
        QtyBasePricingPanel.style.height="0px";
        QtyBasePricingPanel.style.overflow="hidden";
        }
        
       if(SinglePricePanel != "undefined" && SinglePricePanel != null)
      {
         if(Minitemplate==true) 
            {
                SinglePricePanel.style.height="20px";
            }   
         else
            {
                SinglePricePanel.style.height="60px";
            }   
                        
        SinglePricePanel.style.visibility = "visible";
        }
        
        if(AsLowAsPrice != "undefined" && AsLowAsPrice != null)
        AsLowAsPrice.style.visibility = "hidden"; 
          
        if(MultipleRetailPrice != "undefined" && MultipleRetailPrice != null)
        {
            MultipleRetailPrice.style.visibility = "hidden";
        }
     }
     else
     {
      if(SinglePricePanel != "undefined" && SinglePricePanel != null)
      {
        SinglePricePanel.style.visibility = "hidden";
        SinglePricePanel.style.height="0px";
        SinglePricePanel.style.overflow="hidden";   
       }
        
        LABEL=document.getElementById(m_ecf_PurchaseModule_LblQtybaseprice);
        if(QtyBasePricingPanel != "undefined" && QtyBasePricingPanel != null)
        {
            QtyBasePricingPanel.style.height=LABEL.style.height;
            QtyBasePricingPanel.style.visibility = "visible";
         }
         if(AsLowAsPrice != "undefined" && AsLowAsPrice != null)
            {
             AsLowAsPrice.innerHTML=GetNotAvailable(object.asLoweAs);
             AsLowAsPrice.style.visibility = "visible";
            }
        if(MultipleRetailPrice != "undefined" && MultipleRetailPrice != null)
        {
            MultipleRetailPrice.innerHTML = GetNotAvailable(object.retailPrice);
            MultipleRetailPrice.style.visibility = "visible";
        }
     }
      
     if(DiscountDesc != "undefined" && DiscountDesc != null)
     DiscountDesc.innerHTML = object.discountDisc;
     LoadSkuImage(); 
     if(object.image!="")
     {
        if(typeof(paginator) != "undefined")
            paginator.setPage(1, true);
     }
     
    //if(Minitemplate!=null && Minitemplate==true) 
  //  {
  //   parent.StopProcessing();     
     //}
    // else
     //{
        //alert('StopProcessing'); 
        //StopProcessing();     
     //}
    if(Minitemplate==true)
    {   
      var hs = parent.window.hs;
      var exp = hs.getExpander(parent.window.anchorid);
      if(exp!=null)
      {
      exp.iframe.style.height = exp.getIframePageHeight();
      exp.reflow();
      }
     } 
}
function ecf_PurchaseModule_PurchaseOptionChanged(source,id)
{
    document.getElementById("__ECFPURCHASEMODULE_ITEMID").value = source.value;
    m_ecf_PurchaseModule_VariationId = source.value;
    ClickedDropdown = id;
    ecf_PurchaseModule_InitAccessoryCheckboxes();
    if(source!=null)
    {
        if(source.id.indexOf("SwatchName")!=-1)
        {
          SetSwatchPage(source.value,source.selectedIndex); 
        }   
    }
}
function ShowSku(skuid,ColorName)
{
 
  for(var index=0;index<ecf_PurchaseModule_DropdownArray.length;index++)
      {
            if(ecf_PurchaseModule_DropdownArray[index].indexOf("Swatch")>0)
            {
              if(document.getElementById(ecf_PurchaseModule_DropdownArray[index]).value !=ColorName)
              {
                  for(var dindex=0;dindex<document.getElementById(ecf_PurchaseModule_DropdownArray[index]).length;dindex++)
                     {
                         drpcolorname=document.getElementById(ecf_PurchaseModule_DropdownArray[index]).options[dindex].text.replace("(N/A)","");
                         if(drpcolorname==ColorName)
                         {
                               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].selected=true;   
                               ecf_PurchaseModule_PurchaseOptionChanged(document.getElementById(ecf_PurchaseModule_DropdownArray[index]),index);      
                         }
                     }
              }
            }
      }
      
      
}

function addOption(selectbox,text,value )
{
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}


function GetNotAvailable(value)
{
  if(value=="")
  {
    return NOT_APPLICABLE;
  }
  return value;
}

function SetSwatchPage(clrname,index)
			{
	         if(typeof(swatchcarousel)!= "undefined" && typeof(swatchcarousel) != null)			    
			    {
				    for(i=0;i<swatchcarousel.getItems().length;i++)
				    {
				      var obj=swatchcarousel.getElementForItem(i);
				      if(obj.innerHTML.indexOf(clrname)!=-1)
				      {
				        swatchcarousel.set("selectedItem", i);
				        document.getElementById("colorname").innerHTML=clrname;
				        
//				           document.getElementById("selswatchimage").src=getImage(obj);
//                           document.getElementById("selswtchimg").style.width="30px";
//                           document.getElementById("selswatchimage").style.width="30px";
//                           document.getElementById("selswatchimage").style.height="30px";
//                           document.getElementById("selswatchimage").style.visibility="visible";
				        
				        break;
				      }
				    }				
			    }
			}
 function getImage(parent) {
            var el = parent.firstChild;
                    
            while (el) { // walk through till as long as there's an element
                if (el.nodeName.toUpperCase() == "IMG") { // found an image
                    // flickr uses "_s" suffix for small, and "_m" for big
                    // images respectively
                    
                    return el.src;
                }
                el = el.nextSibling;
            }
            
            return "";
        }

function ecf_PurchaseModule_Init()
{
    window.onload = function(){ecf_PurchaseModule_InitAccessoryCheckboxes2(true);ecf_PurchaseModule_InitOptionLists();};    
}
function populateNextAttribute(currentsource) {
    //debugger;
    var i, x, y;
    var nextattribute;
    var arrListAttribute;
    var listAttributeNames = "";
    var listAttributeValues;
    var nextAttributeName;
    for (i = 0; i <= ecf_PurchaseModule_DropdownArray.length - 1; i++) {
        attribute = document.getElementById(ecf_PurchaseModule_DropdownArray[i]);
        arrListAttribute = attribute.value.split("~~");

        if (listAttributeNames.length == 0) {
            listAttributeNames = arrListAttribute[0];
            listAttributeValues = arrListAttribute[1];
        }
        else {
            listAttributeNames = listAttributeNames + "~~" + arrListAttribute[0];
            listAttributeValues = listAttributeValues + "~~" + arrListAttribute[1];
        }
        if (attribute.id == currentsource.id) {
            nextattribute = document.getElementById(ecf_PurchaseModule_DropdownArray[i + 1]);
            for (x = i + 1; x < ecf_PurchaseModule_DropdownArray.length; x++) {
                removeattribute = document.getElementById(ecf_PurchaseModule_DropdownArray[x]);
                removeattribute.length = 1;
                //for(y = 1; y < removeattribute.options.length; y++)
                //{
                //    removeattribute.options[y] = null;   
                //}
            }
            break;
        }
    }
    nextAttributeName = nextattribute.value.split("~~");

    //    alert("m_ecf_PurchaseModule_ProductId=" + m_ecf_PurchaseModule_ProductId);
    //    alert("listAttributeNames=" + listAttributeNames);
    //    alert("listAttributeValues=" + listAttributeValues);
    //    alert("nextAttributeName=" + nextAttributeName[0]);    
    Ignify.eCommerce.Web.Store.MasterTemplates.Ignify.Modules.SharedModules.PurchaseModule.PopulateNextAttribute(m_ecf_PurchaseModule_ProductId, listAttributeNames, listAttributeValues, nextAttributeName[0], function(result) { ecf_PurchaseModule_PopulateNextAttribute(result, nextattribute, nextAttributeName[0]); });
}
function ecf_PurchaseModule_PopulateNextAttribute(result, nextattribute, nextAttributeName) {
    if (result.error != null) {
        //alert("AJAX: Error occured while populating Attribute. Message: " + result.error.Message + ", code: " + result.error.Code);
        return;
    }

    //    if(result.value == null || result.value.length == 0)
    //    {
    //        alert("AJAX: No Attribute Value found.");
    //        return;
    //    }

    if (result.value != null) {
        for (var i = 0; i < result.value.length; i++) {
            var nextAttributeValue = result.value[i];
            nextattribute.options[nextattribute.length] = new Option(nextAttributeValue, nextAttributeName + "~~" + result.value[i]);
        }
    }
}

function ecf_PurchaseModule_InitOptionLists()
{ 
    if(typeof(ecf_PurchaseModule_DropdownArray) == "undefined")
        return;
    if(ecf_PurchaseModule_DropdownArray.length==1)
    {
      FirstTime=false;
    } 
        
    //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)
                        {
                            if(document.getElementById("NAdiv") != null)
                                document.getElementById("NAdiv").style.visibility = "visible";
                        }
                 }
            document.getElementById(ecf_PurchaseModule_DropdownArray[index]).onchange = new Function("ecf_PurchaseModule_PurchaseOptionChanged(this,"+ index +")");     
          }
          else
          {
                if(document.getElementById("NAdiv") != null)
                    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;
    }
}


   

