/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Bobby van der Sluis | http://www.bobbyvandersluis.com/ */

// Details can be found at:
// http://www.bobbyvandersluis.com/articles/unobtrusivedynamicselect.php
// DL from 
// http://javascript.internet.com/forms/unobtrusive-dynamic-select-boxes.html

// 2007-07-17: Andrew Main Carpenter
//    * Added support for <select multiple>
//    * Prefilled values aren't lost (so can be used on edit)
//    * Tidier regex generation using word boundrary (\b)
// 2007-07-18: Venisa Sin
//    * check if sel2 exist
// 2007-09-28: Andrew Main Carpenter
//    * Converted to use AND logic when multiple are selected, rather than OR


function dynamicSelect(id1, id2) {
    // Feature test to see if there is enough W3C DOM support
    if (document.getElementById && document.getElementsByTagName) {
        // Obtain references to both select boxes
        var sel1 = document.getElementById(id1);
        var sel2 = document.getElementById(id2);
        if (sel2) {
            // Clone the dynamic select box
            var clone = sel2.cloneNode(true);
            // Obtain references to all cloned options
            var cloned_options = clone.getElementsByTagName("option");
            // Onload init: call a generic function to display the related 
            // options in the dynamic select box
            refreshDynamicSelectOptions(sel1, sel2, cloned_options);
            // Onchange of the main select box: call a generic function 
            // to display the related options in the dynamic select box
            sel1.onchange = function() {
                refreshDynamicSelectOptions(sel1, sel2, cloned_options);
            };
        }
    }
}

function refreshDynamicSelectOptions(sel1, sel2, cloned_options) {
    previously_selected = new Array();
    for (var i = 0; i < sel2.options.length; i++) {
        if(sel2.options[i].selected) {
            previously_selected[sel2.options[i].value] = 1;
        }
    }
    
    // Delete all options of the dynamic select box
    while (sel2.options.length) {
        sel2.remove(0);
    }
    var selected_options = new Array();
    for (var i = 0; i < sel1.options.length; i++) {
        if(sel1.options[i].selected) {
            selected_options.push(sel1.options[i].value);
        }
    }
    // Iterate through all cloned options
    for (var i = 0; i < cloned_options.length; i++) {
        
        var include_this_option = 0;
        
        // include if none are selected
        if (selected_options.length == 0 ){
            include_this_option = 1;
        }
        // include if there is only one selected and it is blank
        else if (selected_options.length == 1 && selected_options[0] == 0) {
            include_this_option = 1;
        }
        // include if cloned option class includes "select"
        else if(cloned_options[i].className.match(
            new RegExp("( |^)(select)( |$)"))) {
            include_this_option = 1;
        }
        // include if has all the classes of the selected value(s)
        else {
            var num_matches = 0;
            for (var j = 0; j < selected_options.length; j++) {
                var regexp = new RegExp("( |^)(" + selected_options[j] + ")( |$)");
            
                if (cloned_options[i].className.match(regexp)) {
                    num_matches++;
                }
            }
            // OR could be supported if we just checked for num_matches > 0
            if(num_matches == (selected_options.length)) {
                include_this_option = 1;
            }
        }
        
        if( include_this_option ){
            var option = cloned_options[i].cloneNode(true);
            
            if(previously_selected[option.value]) {
                option.selected = true;
            }
            // Append it to the end of the depenendant select box
            sel2.appendChild(option);
        }
    }
}
