﻿var channelOptions;

function resetDropDowns(){
    //re-enable the correct drop downs.
    productSelectionChange();
    channelSelectionChange();
}

// Determines the options to display in the channel drop down based on the selected product
function productSelectionChange()
{
    var productDropDown = $('productDropDown');
    var channelDropDown = $('channelDropDown');
    var selectedProduct = productDropDown.selectedIndex;
    var submitButton = $('detailsButton');
    
    //some APcalendars don't have channel drop down.  Default to retail for those cases.
    if(channelDropDown == null){        
        if(productDropDown.selectedIndex != 0){
            submitButton.disabled = false;
        }else{
            submitButton.disabled = true;
        }
        return;
    }
    
    if(channelOptions == null){
        //if the channelOptions backup array has not been populated, do so (should happen on first change of drop down)
        channelOptions = new Array();
        
        //copy all options from the drop down to the backup array (don't need to copy default value)
        for(i = 1; i < channelDropDown.options.length; i++){
            var option = document.createElement('option');
            option.text = channelDropDown.options[i].text;
            option.value = channelDropDown.options[i].value;
            option.selected = channelDropDown.options[i].selected;
            option.id = channelDropDown.options[i].id;
            channelOptions.push(option);
        }
    }
    
    //remove old elements from the drop down (don't need to remove default value)
    for(i = channelDropDown.options.length - 1; i >= 1; i--)
    {
        channelDropDown.options[i] = null;
    }
    
    channelDropDown.setAttribute('disabled', 'disabled');
    
    //copy the values from the backup array back into the drop down.
    for(i = 0; i < channelOptions.length; i++){
        //determine if the option should be displayed
        var features = productDropDown[selectedProduct].value;
        if(features.charAt(i) == '1'){
            var option = document.createElement('option');
            option.text = channelOptions[i].text;
            option.value = channelOptions[i].value;
            option.selected = channelOptions[i].selected;
            option.id = channelOptions[i].id;
            channelDropDown.options[channelDropDown.options.length] = option;
            
            //enable the drop down since at least one option is available
            channelDropDown.removeAttribute('disabled');
        }
    }
    
    if(productDropDown.selectedIndex == 0)
    {
       submitButton.disabled = true; 
    }
    
}

//Enable or disable the submit button
function channelSelectionChange(){
    var channelDropDown = $('channelDropDown');
    var submitButton = $('detailsButton');
    
    if(channelDropDown != null){
        if(channelDropDown.selectedIndex != 0){
            submitButton.disabled = false;
        }else{
            submitButton.disabled = true;
        }
    }
}

//Button click.  Redirect the user to the correct product page
function viewFeatures(cID){
    var productDropDown = $('productDropDown');
    var channelDropDown = $('channelDropDown');
    var selectedProduct = productDropDown.selectedIndex;
    
    //special case for APCalendars that do not have channel drop down.  Default to retail.
    if(channelDropDown == null){
        if(cID == "")
        {
        location.href = "product.aspx?" + AppendQuizParameters("pID=" + productDropDown[selectedProduct].getAttribute("id") + "&method=retail");
        }
        else
        {
        location.href = "product.aspx?" + AppendQuizParameters("pID=" + productDropDown[selectedProduct].getAttribute("id") + "&cID=" + productDropDown[selectedProduct].getAttribute("Cid") + "&method=retail");
        }
        
        return;
    }
    
    var selectedChannel = channelDropDown.selectedIndex;
    
    //if there is a URL override, use it instead of going to the default product page
    if(channelDropDown[selectedChannel].value.length > 0){
        location.href = channelDropDown[selectedChannel].value;
    }
    else
    {
        if(cID != '')
        {
            location.href = "product.aspx?" + AppendQuizParameters("pID=" + productDropDown[selectedProduct].getAttribute("id") + "&cID=" + cID  + "&method=" + channelDropDown[selectedChannel].getAttribute("id"));
        }
        else
        {
            location.href = "product.aspx?" + AppendQuizParameters("pID=" + productDropDown[selectedProduct].getAttribute("id") + "&cID=" + productDropDown[selectedProduct].getAttribute("Cid")  + "&method=" + channelDropDown[selectedChannel].getAttribute("id"));
        }
    }
}
