﻿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
    {
        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 stockqty = document.getElementById(m_ecf_PurchaseModule_ItemStockhdnControlId);
    var itemID = 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;
       
    //listPrice.innerHTML = result.value[0];
    //finalPrice.innerHTML = result.value[1];
    //if(discountPrice != null)
    //    discountPrice.innerHTML = result.value[2];
        
    if(result.value.length >= 4)
        itemCode.innerHTML = result.value[3];
        
    //if(result.value.length >= 5)        
    //availability.innerHTML = result.value[4];
      
    if(result.value.length >= 6)        
        priceListControl.innerHTML = result.value[5];
    if(result.value.length >= 9 && qtyPricingControl != "undefined" && qtyPricingControl != null)
        qtyPricingControl.innerHTML = result.value[8];
    if(result.value.length >= 9 && stockqty != "undefined" && stockqty != null)
        stockqty.value=result.value[8];
    if(result.value.length >= 10 && itemID != "undefined" && itemID != null)
        itemID.value=result.value[9];
       
    //Set Technical Specification according to SKUs
    if(result.value.length >= 8)
        {
        document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_lblTechnicalSpecification').innerHTML=result.value[7].split("~")[0];
        document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_Velocity').innerHTML=result.value[7].split("~")[1];
        document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_Features').innerHTML=result.value[7].split("~")[2];;  
        
        }
//Set SKU's Image according to Sku
   if(result.value.length>=7)
    {
    var lenght=result.value[6].split("~").length;
     var SkuName=result.value[6].split("~")[0];
     var noImage=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_noImage').src;
     if(lenght>2)
     {
            document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL').src=noImage;
            document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR').src=noImage;
            document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA').src=noImage;
            document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM').src=noImage;
            
            document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_DivImage').style.visibility = "visible"; 
            var ImageZoom=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_aLarge'); 
             if(lenght-1 > 1)
             {
             var L1=result.value[6].split("~")[1].split("_")[1];
             var SkuImgUrl_L0=result.value[6].split("~")[1];
             var SkuImgUrl_L1=result.value[6].split("~")[2];
             var SkuImgUrl_L2=result.value[6].split("~")[3];
                    if(L1=="LeftSide.jpeg")
                    {
                     //    document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL').src="";
                        document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL').src = SkuImgUrl_L0;
                        var ImageL=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL');
                        ImageL.onclick= function() {ChangeMediumImage(SkuImgUrl_L1,SkuImgUrl_L2,SkuName);} 
                        document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_MediumImage').src = SkuImgUrl_L1;
                        ImageZoom.onclick=function() {ZoomImage(SkuImgUrl_L2,SkuName);} 
                    }
                    else if(L1=="RightSide.jpeg")
                    {
                    // document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR').src="";
                        document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR').src = SkuImgUrl_L0;
                        var ImageR=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR');
                        ImageR.onclick= function() {ChangeMediumImage(SkuImgUrl_L1,SkuImgUrl_L2,SkuName);} 
                        document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_MediumImage').src = SkuImgUrl_L1;
                        ImageZoom.onclick=function() {ZoomImage(SkuImgUrl_L2,SkuName);} 
                    }
                    else if(L1=="Angle.jpeg")
                    {
                        // document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA').src="";
                        document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA').src = SkuImgUrl_L0;
                        var ImageA=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA');
                        ImageL.onclick= function() {ChangeMediumImage(SkuImgUrl_L1,SkuImgUrl_L2,SkuName);} 
                        document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_MediumImage').src = SkuImgUrl_L1;
                        ImageZoom.onclick=function() {ZoomImage(SkuImgUrl_L2,SkuName);} 
                    }
                    else if(L1=="Miscellaneous.jpeg")
                    {
                        //document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM').src="";
                       document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM').src = SkuImgUrl_L0;
                       var ImageM=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM');
                       ImageL.onclick= function() {ChangeMediumImage(SkuImgUrl_L1,SkuImgUrl_L2,SkuName);} 
                       document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_MediumImage').src = SkuImgUrl_L1;
                       ImageZoom.onclick=function() {ZoomImage(SkuImgUrl_L2,SkuName);} 
                   }
             }
             else
             {
            // if(document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL'))
            // document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL').src=noImage;
             
             }
             if(lenght-1 > 4)
             {
                       
             var L2=result.value[6].split("~")[4].split("_")[1];
             var SkuImgUrl_R0=result.value[6].split("~")[4];
             var SkuImgUrl_R1=result.value[6].split("~")[5];
             var SkuImgUrl_R2=result.value[6].split("~")[6];
                     if(L2=="RightSide.jpeg")
                     {
                       //    document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR').src="";
                         document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR').src = SkuImgUrl_R0;
                         var ImageR=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR');
                         ImageR.onclick= function() {ChangeMediumImage(SkuImgUrl_R1,SkuImgUrl_R2,SkuName);} 
                     }
                     else if(L2=="LeftSide.jpeg")
                     {
                     //  document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL').src="";
                        document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL').src = SkuImgUrl_R0;
                        var ImageL=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL');
                        ImageL.onclick= function() {ChangeMediumImage(SkuImgUrl_R1,SkuImgUrl_R2,SkuName);} 
                     }
                     else if(L2=="Angle.jpeg")
                     {
                     //      document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA').src="";
                         document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA').src = SkuImgUrl_R0;
                         var ImageA=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA');
                         ImageA.onclick= function() {ChangeMediumImage(SkuImgUrl_R1,SkuImgUrl_R2,SkuName);} 
                     }
                     else if(L2=="Miscellaneous.jpeg")
                     {
                   //    document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM').src="";
                         document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM').src = SkuImgUrl_R0;
                         var ImageM=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM');
                         ImageM.onclick= function() {ChangeMediumImage(SkuImgUrl_R1,SkuImgUrl_R2,SkuName);} 
                     }
           }
             else
             {
            //  if(document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR'))
           //  document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR').src=noImage;
             
             }
             if(lenght-1 > 7)
             {
             
             var L3=result.value[6].split("~")[7].split("_")[1];
             var SkuImgUrl_A0=result.value[6].split("~")[7];
             var SkuImgUrl_A1=result.value[6].split("~")[8];
             var SkuImgUrl_A2=result.value[6].split("~")[9];
                     if(L3=="Angle.jpeg")
                     {
                   //  document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA').src="";
                         document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA').src = SkuImgUrl_A0;
                         var ImageA=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA');
                         ImageA.onclick= function() {ChangeMediumImage(SkuImgUrl_A1,SkuImgUrl_A2,SkuName);} 
                     }
                     else if(L3=="LeftSide.jpeg")
                     {
                    // document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL').src="";
                           document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL').src = SkuImgUrl_A0;
                           var ImageL=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL');
                           ImageL.onclick= function() {ChangeMediumImage(SkuImgUrl_A1,SkuImgUrl_A2,SkuName);} 
                     }
                     else if(L3=="RightSide.jpeg")
                     {
                   //  document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR').src="";
                         document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR').src = SkuImgUrl_A0;
                         var ImageR=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR');
                         ImageR.onclick= function() {ChangeMediumImage(SkuImgUrl_A1,SkuImgUrl_A2,SkuName);} 
                     }
                     else if(L3=="Miscellaneous.jpeg")
                     {
                    // document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM').src="";
                         document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM').src = SkuImgUrl_A0;
                         var ImageM=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM');
                         ImageM.onclick= function() {ChangeMediumImage(SkuImgUrl_A1,SkuImgUrl_A2,SkuName);} 
                     }
              
             }
             else
             {
            // if(document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA'))
            // document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA').src=noImage;
            
             }
             if(lenght-1 > 10)
             {
            
             var L4=result.value[6].split("~")[10].split("_")[1];
             var SkuImgUrl_M0=result.value[6].split("~")[10];
             var SkuImgUrl_M1=result.value[6].split("~")[11];
             var SkuImgUrl_M2=result.value[6].split("~")[12];
                    if(L4=="Miscellaneous.jpeg")
                    {
                    // document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM').src="";
                     document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM').src = SkuImgUrl_M0;
                     var ImageM=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM');
                     ImageM.onclick= function() {ChangeMediumImage(SkuImgUrl_M1,SkuImgUrl_M2,SkuName);} 
                    }
                    else if(L4=="Miscellaneous.jpeg")
                    {
                   //  document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL').src="";
                     document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL').src = SkuImgUrl_M0;
                     var ImageL=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageL');
                     ImageL.onclick= function() {ChangeMediumImage(SkuImgUrl_M1,SkuImgUrl_M2,SkuName);} 
                    }
                    else if(L4=="RightSide.jpeg")
                    {
                   //  document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR').src="";
                     document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR').src = SkuImgUrl_M0;
                     var ImageR=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageR');
                     ImageR.onclick= function() {ChangeMediumImage(SkuImgUrl_M1,SkuImgUrl_M2,SkuName);} 
                    }
                    else if(L4=="Angle.jpeg")
                    {
                  //   document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA').src="";
                     document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA').src = SkuImgUrl_M0;
                     var ImageA=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageA');
                     ImageA.onclick= function() {ChangeMediumImage(SkuImgUrl_M1,SkuImgUrl_M2,SkuName);} 
                    }
                 
             }
             else
             {
            // if(document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM'))
            // document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_AdditionalImageM').src=noImage;
             }
     }
    // else
    // {
    // document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_DivImage').style.visibility = "hidden"; 
    // }
   }

}

function ChangeMediumImage(imgurl_M,imgurl_Z,SkuName)  
{
    
    var ImageZoom=document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_aLarge');
    
    if(imgurl_M.split("_")[1]=="LeftSide")
    {
    document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_MediumImage').src = imgurl_M;
    ImageZoom.onclick=function() {ZoomImage(imgurl_Z,SkuName);} 
    }
    else if(imgurl_M.split("_")[1]=="RightSide")
    {
    document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_MediumImage').src = imgurl_M;
    ImageZoom.onclick=function() {ZoomImage(imgurl_Z,SkuName);} 
    }
    else if(imgurl_M.split("_")[1]=="Angle")
    {
    document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_MediumImage').src = imgurl_M;
    ImageZoom.onclick=function() {ZoomImage(imgurl_Z,SkuName);} 
    }
    else if(imgurl_M.split("_")[1]=="Miscellaneous")
    {
    document.getElementById('ctl00_MainContent_ProductInfo1_ctl00_MediumImage').src = imgurl_M;
    ImageZoom.onclick=function() {ZoomImage(imgurl_Z,SkuName);} 
    }
}
function ZoomImage(ImgURL,Title)
{
 window.open("../Catalog/DisplayImage.aspx?ImgURL="+ImgURL+"&Title="+Title, "DisplayImage", "scrollbars=1,width=800, height=600");
}
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;
        
    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;
    }
}