﻿if( typeof(isNull) != 'function' || isNull == null )
    var isNull = new function(v) { if( typeof(v) == 'undefined' || v == null ) return true; return false; };

function RemoveAllDropDownOptions(dropDown) {
    if( !isNull(dropDown) )
        while( !isNull(dropDown.options) && dropDown.options.length > 0 )
            dropDown.remove(0);    
}
function CreateOptionElement(text,value) {
    var output = document.createElement('OPTION');
    output.text = text;
    output.value = value;
    return output;
}

function FindOption(dropDown, value) {
    if( !isNull(dropDown) && !isNull(value) ) {
        for( var i = 0; i < dropDown.options.length; i++ ) {
            if( dropDown.options[i].value == value )
                return i;
        }
    }
    return -1;
}

function InitializeCascadingDropDownPair(primaryDropDown, primaryTextBox, secondaryDropDown, secondaryTextBox, srcArray)
{
    if( !isNull(primaryDropDown) && !isNull(secondaryDropDown) && !isNull(secondaryTextBox) && !isNull(srcArray) )
    {
        primaryDropDown.onchange = function() {
            RemoveAllDropDownOptions(secondaryDropDown);
        
            var secondaryItems = null;
            for( var i = 0; i < srcArray.length; i++ ) {
                if( srcArray[i][0] == primaryDropDown.options[primaryDropDown.selectedIndex].value )
                {
                    secondaryItems = srcArray[i][1];
                    break;
                }
            }
            
            if( secondaryItems != null && secondaryItems.length > 0) {
                secondaryDropDown.className = 'shown';
                secondaryDropDown.options[0] = CreateOptionElement('<Please select>','');
                for( var j = 0; j < secondaryItems.length; j++ ) {
                    secondaryDropDown.options[j+1] = CreateOptionElement(secondaryItems[j],secondaryItems[j]);
                }
            } else {
                secondaryDropDown.className = 'hidden';
                secondaryDropDown.options[0] = CreateOptionElement('<No options available>','');
            }
            secondaryDropDown.options[0].selected = true;
            secondaryDropDown.onchange();
            
            primaryTextBox.value = primaryDropDown.options[primaryDropDown.selectedIndex].value;
        };
        secondaryDropDown.onchange = function() {
            secondaryTextBox.value = secondaryDropDown.options[secondaryDropDown.selectedIndex].value;
        };
        
        //Actual initialization
        RemoveAllDropDownOptions(primaryDropDown);
        primaryDropDown.options[0] = CreateOptionElement('<Please select>','');
        for( var i = 0; i < srcArray.length; i++ ) {
            primaryDropDown.options[i+1] = CreateOptionElement(srcArray[i][0],srcArray[i][0]);
        }
        
        var currentOption = FindOption(primaryDropDown, primaryTextBox.value);
        var secondaryOption = secondaryTextBox.value;
        if( currentOption >= 0 ) {
            primaryDropDown.options[currentOption].selected = true;
            primaryDropDown.onchange();
        } else {
            primaryDropDown.options[0].selected = true;
            primaryDropDown.onchange();
        }

        secondaryOption = FindOption(secondaryDropDown,secondaryOption);
        if( secondaryOption >= 0 ) {
            secondaryDropDown.options[secondaryOption].selected = true;
            secondaryDropDown.onchange();
        }
    }
}

