var xmlQPS = null;

function _hide(id) { document.getElementById(id).style.display = 'none'; }
function _show(id) { document.getElementById(id).style.display = 'block'; }
function IsVisible(id) { return(document.getElementById(id).style.display == 'block'); }

function getQPSProductCount( product_class )
{
    var query = "";
    var dd_value = "";
    var current_select;
    var select_dds = document.getElementsByTagName("select");
    for( var iCnt = 0; iCnt < select_dds.length; iCnt++ )
    {
        current_select = select_dds[iCnt];
        dd_value = current_select.options[current_select.selectedIndex].value;
        // If the select value contains " AND ", then we want to append it to the query string
        // Except we do NOT append commercial locations.  Those are handled separately.
        if( dd_value.match(" AND ") != null  &&  !(product_class == "Commercial" && dd_value.match("location") != null) )
        {
               query += dd_value;
        }
    }
    if( product_class == "Commercial" )
    {
        // For commercial QPS, append appropriate location to the query string
        var sink_loc = document.getElementById("qps_com_sink_install_location");
        var faucet_loc = document.getElementById("qps_com_faucets_install_location");
        var room_type = document.getElementById("qps_com_roomtype");
        if( sink_loc.selectedIndex != 0 )
        {
            query += sink_loc.options[sink_loc.selectedIndex].value;
        }
        else
        {
            if( faucet_loc.selectedIndex != 0 )
            {
                query += faucet_loc.options[faucet_loc.selectedIndex].value;
            }
            else
            {
                query += room_type.options[room_type.selectedIndex].value.replace(/ALL/,"");
            }
        }
    }
    var url = encodeURI('http://' + location.host + '/cps/rde/xchg/' + project + '/hs.xsl/product_count.xml?verity_string=group_page_id!=""' + brand_query + query);
    if (window.XMLHttpRequest){xmlQPS = new XMLHttpRequest();}
    else if (window.ActiveXObject){xmlQPS = new ActiveXObject("Microsoft.XMLHTTP");}
    if( xmlQPS != null )
    {
        xmlQPS.onreadystatechange = xmlQPS_response;
        xmlQPS.open("GET", url, true);
        xmlQPS.send(null);
    }
    return false;
}

function xmlQPS_response()
{
    if( checkReadyState(xmlQPS) )
    {
        var strHTML = xmlQPS.responseText;
        document.getElementById('qps_quantity').innerHTML = strHTML;
    }
}

function res_prod_type_chg()
{
    var prod_type_select = document.getElementById("qps_res_prod_type");
    var prod_type_text   = prod_type_select.options[prod_type_select.selectedIndex].text;
    switch( prod_type_text )
    {
        case "Select Product Type":
            _hide("res_sinks_div");
            _hide("res_faucets_div");
            _hide("res_accessories_div");
            _hide("qps_sort_div");
            _hide("qps_sort_heading");
            _hide("bowl_depth_asc");
            _hide("bowl_depth_desc");
            _hide("gauge_desc");
            _hide("gauge_asc");
            _hide("qps_product_count");
            break;
        case "Sinks":
            _show("res_sinks_div");
            _hide("res_faucets_div");
            _hide("res_accessories_div");
            _show("qps_sort_div");
            _show("qps_sort_heading");
            _show("bowl_depth_asc");
            _show("bowl_depth_desc");
            _show("gauge_desc");
            _show("gauge_asc");
            _show("qps_product_count");
            break;
        case "Faucets":
            _hide("res_sinks_div");
            _show("res_faucets_div");
            _hide("res_accessories_div");
            _show("qps_sort_div");
            _show("qps_sort_heading");
            _hide("bowl_depth_asc");
            _hide("bowl_depth_desc");
            _hide("gauge_desc");
            _hide("gauge_asc");
            _show("qps_product_count");
            break;
        case "Accessories":
            _hide("res_sinks_div");
            _hide("res_faucets_div");
            _show("res_accessories_div");
            _show("qps_sort_div");
            _show("qps_sort_heading");
            _hide("bowl_depth_asc");
            _hide("bowl_depth_desc");
            _hide("gauge_desc");
            _hide("gauge_asc");
            _show("qps_product_count");
            break;
    }
    if( prod_type_text != "Sinks" )
    {
        document.getElementById("qps_res_sink_install").selectedIndex = 0;
        document.getElementById("qps_res_sink_material").selectedIndex = 0;
        document.getElementById("qps_res_sink_num_of_bowls").selectedIndex = 0;
        document.getElementById("qps_res_sink_width").selectedIndex = 0;
        document.getElementById("qps_res_sink_ada_compliant").selectedIndex = 0;
        document.getElementById("qps_res_sink_bowl_depth").selectedIndex = 0;
        document.getElementById("qps_res_sink_gauge").selectedIndex = 0;
        document.getElementById("qps_res_sink_sound_deadening").selectedIndex = 0;
    }
    if( prod_type_text != "Faucets" )
    {
        document.getElementById("qps_res_faucets_install").selectedIndex = 0;
        document.getElementById("qps_res_faucets_num_holes").selectedIndex = 0;
        document.getElementById("qps_res_faucets_finish").selectedIndex = 0;
        document.getElementById("qps_res_faucets_ada_compliant").selectedIndex = 0;
        document.getElementById("qps_res_faucets_flow_rate").selectedIndex = 0;
    }
    if( prod_type_text != "Accessories" )
    {
        document.getElementById("qps_res_accessories").selectedIndex = 0;
    }
    if( prod_type_text != "Select Product Type" )
    {
        getQPSProductCount("Residential");        
    }
    // If selected sort option is no longer available, set selectedIndex to 0
    var sort_select = document.getElementById("qps_sort");
    if( sort_select.options[sort_select.selectedIndex].style.display == 'none' )
    {
        sort_select.selectedIndex = 0;
    }
}

function res_roomtype_chg()
{
    var prod_type_select = document.getElementById("qps_res_prod_type");
    if( document.getElementById("qps_res_roomtype").selectedIndex != 0 )
    {
        prod_type_select.disabled = false;
    }
    else
    {
        prod_type_select.selectedIndex = 0;
        prod_type_select.disabled = true;
    }
    res_prod_type_chg();
}

function com_prod_type_chg()
{
    var prod_type_select = document.getElementById("qps_com_prod_type");
    var prod_type_text   = prod_type_select.options[prod_type_select.selectedIndex].text;
    switch( prod_type_text )
    {
        case "Select Product Type":
            _hide("com_sink_div");
            _hide("com_faucets_div");
            _hide("com_coolers_div");
            _hide("com_accessories_div");
            _hide("qps_sort_div");
            _hide("qps_sort_heading");
            _hide("bowl_depth_asc");
            _hide("bowl_depth_desc");
            _hide("gauge_desc");
            _hide("gauge_asc");
            _hide("flow_rate_asc");
            _hide("flow_rate_desc");
            _hide("qps_product_count");
            break;
        case "Sinks":
            _show("com_sink_div");
            _hide("com_faucets_div");
            _hide("com_coolers_div");
            _hide("com_accessories_div");
            _show("qps_sort_div");
            _show("qps_sort_heading");
            _show("bowl_depth_asc");
            _show("bowl_depth_desc");
            _show("gauge_desc");
            _show("gauge_asc");
            _hide("flow_rate_asc");
            _hide("flow_rate_desc");
            _show("qps_product_count");
            break;
        case "Faucets":
            _hide("com_sink_div");
            _show("com_faucets_div");
            _hide("com_coolers_div");
            _hide("com_accessories_div");
            _show("qps_sort_div");
            _show("qps_sort_heading");
            _hide("bowl_depth_asc");
            _hide("bowl_depth_desc");
            _hide("gauge_desc");
            _hide("gauge_asc");
            _hide("flow_rate_asc");
            _hide("flow_rate_desc");
            _show("qps_product_count");
            break;
        case "Coolers":
            _hide("com_sink_div");
            _hide("com_faucets_div");
            _show("com_coolers_div");
            _hide("com_accessories_div");
            _show("qps_sort_div");
            _show("qps_sort_heading");
            _hide("bowl_depth_asc");
            _hide("bowl_depth_desc");
            _hide("gauge_desc");
            _hide("gauge_asc");
            _show("flow_rate_asc");
            _show("flow_rate_desc");
            _show("qps_product_count");
            break;
        case "Accessories":
            _hide("com_sink_div");
            _hide("com_faucets_div");
            _hide("com_coolers_div");
            _show("com_accessories_div");
            _show("qps_sort_div");
            _show("qps_sort_heading");
            _hide("bowl_depth_asc");
            _hide("bowl_depth_desc");
            _hide("gauge_desc");
            _hide("gauge_asc");
            _hide("flow_rate_asc");
            _hide("flow_rate_desc");
            _show("qps_product_count");
            break;
    }
    if( prod_type_text != "Sinks" )
    {
        document.getElementById("qps_com_sink_install_location").selectedIndex = 0;
        document.getElementById("qps_com_sink_install").selectedIndex = 0;
        document.getElementById("qps_com_sink_material").selectedIndex = 0;
        document.getElementById("qps_com_sink_num_of_bowls").selectedIndex = 0;
        document.getElementById("qps_com_sink_width").selectedIndex = 0;
        document.getElementById("qps_com_sink_compliance").selectedIndex = 0;
        document.getElementById("qps_com_sink_bowl_depth").selectedIndex = 0;
        document.getElementById("qps_com_sink_gauge").selectedIndex = 0;
        document.getElementById("qps_com_sink_sound_deadening").selectedIndex = 0;
    }
    if( prod_type_text != "Faucets" )
    {
        document.getElementById("qps_com_faucets_install_location").selectedIndex = 0;
        document.getElementById("qps_com_faucets_mounting").selectedIndex = 0;
        document.getElementById("qps_com_faucets_hole_drilling").selectedIndex = 0;
        document.getElementById("qps_com_faucets_handle_type").selectedIndex = 0;
        document.getElementById("qps_com_faucets_spout_type").selectedIndex = 0;
        document.getElementById("qps_com_faucets_flow_rate").selectedIndex = 0;
    }
    if( prod_type_text != "Coolers" )
    {
        document.getElementById("qps_com_cooler_unit_type").selectedIndex = 0;
        document.getElementById("qps_com_cooler_mount_type").selectedIndex = 0;
        document.getElementById("qps_com_cooler_num_stations").selectedIndex = 0;
        document.getElementById("qps_com_cooler_hands_free").selectedIndex = 0;
        document.getElementById("qps_com_cooler_vandal_resistant").selectedIndex = 0;
        document.getElementById("qps_com_cooler_filtered").selectedIndex = 0;
        document.getElementById("qps_com_cooler_flow_rate").selectedIndex = 0;
        document.getElementById("qps_com_cooler_finish").selectedIndex = 0;
        document.getElementById("qps_com_cooler_compliance").selectedIndex = 0;
    }
    if( prod_type_text != "Accessories" )
    {
        document.getElementById("qps_com_accessories").selectedIndex = 0;
    }
    var room_type_select = document.getElementById("qps_com_roomtype");
    var room_type_text   = room_type_select.options[room_type_select.selectedIndex].text;
    switch( room_type_text )
    {
        case "All":
            _show("classroom_sink");
            _show("classroom_faucet");
            _show("scrub_sink");
            _show("scrub_faucet");
            break;
        case "Education":
            _show("classroom_sink");
            _show("classroom_faucet");
            _hide("scrub_sink");
            _hide("scrub_faucet");
            break;
        case "Medical":
            _hide("classroom_sink");
            _hide("classroom_faucet");
            _show("scrub_sink");
            _show("scrub_faucet");
            break;
        default:
            _hide("classroom_sink");
            _hide("classroom_faucet");
            _hide("scrub_sink");
            _hide("scrub_faucet");
            break;
    }
    // If selected location option is no longer available, set selectedIndex to 0
    var location_select = document.getElementById("qps_com_sink_install_location");
    if( location_select.options[location_select.selectedIndex].style.display == 'none' )
    {
        location_select.selectedIndex = 0;
    }
    if( prod_type_text != "Select Product Type" )
    {
        getQPSProductCount("Commercial");
    }
    // If selected sort option is no longer available, set selectedIndex to 0
    var sort_select = document.getElementById("qps_sort");
    if( sort_select.options[sort_select.selectedIndex].style.display == 'none' )
    {
        sort_select.selectedIndex = 0;
    }
}

function com_roomtype_chg()
{
    var prod_type_select = document.getElementById("qps_com_prod_type");
    if( document.getElementById("qps_com_roomtype").selectedIndex != 0 )
    {
        prod_type_select.disabled = false;
    }
    else
    {
        prod_type_select.selectedIndex = 0;
        prod_type_select.disabled = true;
    }
    com_prod_type_chg();
}