// JavaScript Document
// jon ward
// phosphor essence 2008
$(document).ready(function () {
    $('#choose').remove();
    $("a.reset").remove();
    
    $('#topID').change(function () {
        var nodeID = $(this).val();
        $('#locID').remove();
        $('#productList li:gt(0)').empty();

        buildDropDown(nodeID, 1);   
    });
    
    function buildDropDown(nodeID, position) {
        $.get('ajaxProductList.aspx', {nodeID : nodeID}, function (response) {
            placeDropDown(position, response);   
            $('#productList select').eq(position).change(function () {
                var value = $(this).val();
                $('#productList li:gt(' + position + ')').empty();

                if (value.match(/^\//)) {
                    window.location = value;
                } else if (value == 0) {
                    $('#productList li:gt(' + position + ')').remove();
                } else {
                    buildDropDown(value, position+1);
                }
            });
        }, 'HTML');
    };

    function placeDropDown(position, content){
        var numberOfElem =  $('#productList li').size();
        if (numberOfElem < position + 1) $('#productList').append('<li></li>');
        $('#productList li').eq(position).empty().append(content);      
    };
});
