﻿// Copyright 2005 Mediachase LLC. All rights reserved.
// ===================================================================
// Author: Alexandre Siniouguine <say@mediachase.com>
// WWW: http://www.mediachase.com/
// ===================================================================

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
    {
        Mediachase.eCF.PublicStore.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);
    
    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];
}

function ecf_PurchaseModule_PurchaseOptionChanged(source)
{
    /*if(source.value == 0)
        return;*/
        
    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(); };
       
     // the following line DOESN'T WORK IN FIREFOX 
     //window.onload = function(){ecf_PurchaseModule_InitAccessoryCheckboxes2(true);ecf_PurchaseModule_InitOptionLists();};  
}

function ecf_PurchaseModule_PurchaseOptionChanged2(source)
{
    document.getElementById("__ECFPURCHASEMODULE_ITEMID").value = 0;
    m_ecf_PurchaseModule_VariationId = 0;
    ecf_PurchaseModule_InitAccessoryCheckboxes();
    // need to call change for DynamicOptionList explicitly to trigger the population of the child list (according to DynamicOptionList documentation)
    multiOption.change(source);
}

function ecf_PurchaseModule_InitOptionLists()
{ 
    if(typeof(ecf_PurchaseModule_DropdownArray) == "undefined")
        return;
    
    initDynamicOptionLists(); 
    
    // attach onchange handler that will reset prices selected item is changed in one of dropdowns
    if (ecf_PurchaseModule_DropdownArray.length > 1)
        for(var index = 0; index<ecf_PurchaseModule_DropdownArray.length-1; index++)
            document.getElementById(ecf_PurchaseModule_DropdownArray[index]).onchange = new Function("ecf_PurchaseModule_PurchaseOptionChanged2(this)");
    
    // attach onchange handler that will update prices when selected item is changed in the last dropdown
    document.getElementById(ecf_PurchaseModule_DropdownArray[ecf_PurchaseModule_DropdownArray.length - 1]).onchange = new Function("ecf_PurchaseModule_PurchaseOptionChanged(this)");
    
    // select the first element of each dropdown
    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;
    }
}