﻿var object;
var FirstTime=true;
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
    {
             if(Minitemplate!=null && Minitemplate==true) 
            {
                parent.ShowProcessing("Loading Sku, please wait...");
            }
            else
            {
                ShowProcessing("Loading Sku, please wait...");
            }
            
            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);
    }
}

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 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 ProdPrice = document.getElementById(m_ecf_PurchaseModule_ProdPriceControlId);
   
    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 qtyPricingControl = document.getElementById(m_ecf_PurchaseModule_QtyPricingControlId);
    var hdnItemID = document.getElementById(m_ecf_PurchaseModule_ItemCodehdnControlId); 
    var pnlitem = document.getElementById(m_ecf_PurchaseModule_pnlitem);
   //new    
     if(document.getElementById("ctl00_MainContent_ProductInfo1_ctl00_PurchaseCtrl_Putup_OptionValueCtrl")!=null && document.getElementById("ctl00_MainContent_ProductInfo1_ctl00_PurchaseCtrl_Putup_OptionValueCtrl") != "undefined")
    {
        var putup_value = document.getElementById(m_ecf_PurchaseModule_putup_value);
        putup_value.innerHTML =document.getElementById("ctl00_MainContent_ProductInfo1_ctl00_PurchaseCtrl_Putup_OptionValueCtrl").options[document.getElementById("ctl00_MainContent_ProductInfo1_ctl00_PurchaseCtrl_Putup_OptionValueCtrl").selectedIndex].text; 
    }
  //end
    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 (object.code == null || object.code == '')
    pnlitem.style.display = "none"
   else
    pnlitem.style.display = "block"
   
    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(object.attributeDropDownNames != '' && object.attributeDropDownNames == index )
            {
            var updateDDLvalues=updatevalues[0].split('`');
            document.getElementById(ecf_PurchaseModule_DropdownArray[index]).length = 0;
                  for (var tindex = 0 ; tindex < updateDDLvalues.length ; tindex++)
                        {
                            updateDtxtval = updateDDLvalues[tindex].split('^');
                            document.getElementById(ecf_PurchaseModule_DropdownArray[index])
                            optn =document.createElement("OPTION");
                            optn.text = updateDtxtval[1].replace("&apos;","'");
                            optn.value = updateDtxtval[0];
                            document.getElementById(ecf_PurchaseModule_DropdownArray[index]).options.add(optn);
                        }
             }
             if (object.attributeDropDownNames != '' && object.attributeDropDownNames < index)
             document.getElementById(ecf_PurchaseModule_DropdownArray[index]).length = 1;
        }
    }
    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";
            }
            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";
            }
        }
        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";
        }
    }
  
    if(itemCode != "undefined" && itemCode != null)
        itemCode.innerHTML = GetNotAvailable(object.code);
    
    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(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(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";   
     }
     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(DiscountDesc != "undefined" && DiscountDesc != null)
     DiscountDesc.innerHTML = object.discountDisc;
     LoadSkuImage(); 
     //Atul : Swatch Image refresh
        var test = object.componentList.split("^^");

        if (typeof swatchcarousel != "undefined") 
        { 
            if (test[0] != 'na')
            {
                swatchcarousel.clearItems();
                var row=2;
                row=test.length/12;
		        row=Math.ceil(row);
		        swatchcarousel.set("numVisible", [21])
                for (i=0;i< test.length;i++)
                {
                    swatchcarousel.addItem(test[i])
                }
               
		        swatchcarousel.render(); // get ready for rendering the widget
			    swatchcarousel.show();   // display the widget
			    document.getElementById("Message").innerHTML="Select Color";
		        document.getElementById("colorname").innerHTML="";
		    }
		        var ij = 0
		        var imgdisplay  = 0;
		        for (ij = 0; ij < ecf_PurchaseModule_DropdownArray.length; ij++)
		        { 
    		        if (document.getElementById(ecf_PurchaseModule_DropdownArray[ij]).value != "0")
    		            imgdisplay = 1
    		            
		            if (ecf_PurchaseModule_DropdownArray[ij].indexOf("SwatchName")!=-1 &&  imgdisplay == 1)
		            {
		            
		            if (document.getElementById(ecf_PurchaseModule_DropdownArray[ij]).value != "0")
                        SetSwatchPage(document.getElementById(ecf_PurchaseModule_DropdownArray[ij]).value,document.getElementById(ecf_PurchaseModule_DropdownArray[ij]).selectedIndex);    
                    else
                        swatchcarousel.set("selectedItem", -1);

		            }
		        } 
		        if (imgdisplay==0) 
		        {
		          
		            //swatchcarousel.clearItems(); 
		             document.getElementById("Message").innerHTML="";
		            document.getElementById("colorname").innerHTML="";
		         
		        }

        }
        
        // Atul : End - Swatch Image refresh
     if(object.image!="")
     {
        if(typeof(paginator) != "undefined")
            paginator.setPage(1, true);
     }
     
    if(Minitemplate!=null && Minitemplate==true) 
    {
     parent.StopProcessing();     
     }
     else
     {
        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 GetNotAvailable(value)
{
  if(value=="")
  {
    return "Not Available";
  }
  return value;
}
function ecf_PurchaseModule_PurchaseOptionChanged(source,id)
{
    document.getElementById("__ECFPURCHASEMODULE_ITEMID").value = source.value;
    m_ecf_PurchaseModule_VariationId = source.value;
    if (m_ecf_PurchaseModule_VariationId == 0)
    {
        for(var index = id+1; index< ecf_PurchaseModule_DropdownArray.length; index++)
        {
            document.getElementById(ecf_PurchaseModule_DropdownArray[index]).length = 1;
        }
    return false;
    }
    ClickedDropdown = id;
    ecf_PurchaseModule_InitAccessoryCheckboxes();
    if(source!=null)
    {
        if(source.id.indexOf("SwatchName")!=-1)
        {
          SetSwatchPage(source.value,source.selectedIndex); 
        }
    } 
    
}
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(jQuery("#color",obj).val()==clrname)//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 ecf_PurchaseModule_InitOptionLists()
{ 
    if(typeof(ecf_PurchaseModule_DropdownArray) == "undefined")
        return;
    if(ecf_PurchaseModule_DropdownArray.length==1)
    {
      FirstTime=false;
    } 
        
    //initDynamicOptionLists();       
    
    //new
    if(document.getElementById("ctl00_MainContent_ProductInfo1_ctl00_PurchaseCtrl_Putup_OptionValueCtrl")!=null && document.getElementById("ctl00_MainContent_ProductInfo1_ctl00_PurchaseCtrl_Putup_OptionValueCtrl") != "undefined")
    {
        var putup_value = document.getElementById(m_ecf_PurchaseModule_putup_value);
        putup_value.innerHTML =document.getElementById("ctl00_MainContent_ProductInfo1_ctl00_PurchaseCtrl_Putup_OptionValueCtrl").options[document.getElementById("ctl00_MainContent_ProductInfo1_ctl00_PurchaseCtrl_Putup_OptionValueCtrl").selectedIndex].text; 
     }
     //end
    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;
    }
}


   
