﻿//Based on script by Ioannis Cherouvim
//http://blog.cherouvim.com

//translation strings taken from 'bookformdropdowntranslations' array


//Applies cascading behavior for the specified dropdowns
function initBookformDropdown(genreID, formID)
{
    var ddGenre = document.getElementById(genreID);
    var ddForm = document.getElementById(formID);
    if (!(ddGenre && ddForm))
        return;
        
    //When ASP.NET validator is also attached to the ddGenre dropdown, during its initialization it changes call context
    //of dropdown's onchange handler; this breaks our function as genreID and formID are no longer accessible.
    //We must pass them as values.
    //ddGenre.onchange = function() { sortBookformDropdown(genreID, formID); }
    eval("ddGenre.onchange = function() { sortBookformDropdown('" + genreID + "', '" + formID + "'); }");
    
    sortBookformDropdown(genreID, formID);

}

//Displays a dropdown's options in an applicable order
function sortBookformDropdown(genreID, formID)
{
    var ddGenre = document.getElementById(genreID);
    var ddForm = document.getElementById(formID);
    if (!(ddGenre && ddForm))
        return;

    var selectedGenre = ddGenre.value;

    //alert("sorting, G=" + ddForm + ", V=" + selectedGenre);
    if (!ddForm.backup) 
    {
        ddForm.backup = ddForm.cloneNode(true);
    }

    //store current selection, if any
    var selectedForm = ddForm.value;

    //clear dropdown
    while (ddForm.childNodes.length >= 1)
    {
        ddForm.removeChild(ddForm.firstChild);
    }

    //fill with forms arranged in order proper for selected genre
    if (selectedGenre == "0")
    {
        //show all - just copy backup node
        var options = ddForm.backup.childNodes;
        for (var i = 0; i < options.length; i++)
        {
            ddForm.appendChild(options[i].cloneNode(true));
        }
    }
    else
    {
        //first get matching nodes from backup, then attach other nodes
        var allForms = ddForm.backup.getElementsByTagName("option");

        var matchingForms = document.createElement('optgroup');
        matchingForms.label = bookformdropdowntranslations[1];
        var otherForms = document.createElement('optgroup');
        otherForms.label = bookformdropdowntranslations[2];

        ddForm.appendChild(allForms[0].cloneNode(true));      //"choose one" prompt

        for (var i = 1; i < allForms.length; i++)
        {
            var formNode = allForms[i].cloneNode(true);
            if (formNode.className.split(",").include(selectedGenre))
                matchingForms.appendChild(formNode);
            else
                otherForms.appendChild(formNode);
        }

        if (matchingForms.childNodes.length > 0)
        {
            ddForm.appendChild(matchingForms);
        }
        else
        {
            otherForms.label = bookformdropdowntranslations[0]; //display non-matching as "All forms", not "Other forms"
        }
            
        if (otherForms.childNodes.length > 0)
            ddForm.appendChild(otherForms);
            
    }

    //restore selected item
    ddForm.value = selectedForm;

}


