﻿function ChangeDDL(elem)
{
    // redirect browser to apply content filters either Applicable Law (ddlLaw), or Language (ddlLang)
    if (elem.name == 'ddlLaw' || elem.name == 'ddlLang' || elem.name == 'ddlSuperCat')
    {
        var ddlName = elem.name + "=";
        var url = document.URL;
        var indexStart;
        var indexEnd;
        
        indexStart = url.indexOf(ddlName, 0);
        if (indexStart > -1)
        {            
            // item already on qstring so get the url up to the start of it
            var newUrl = url.substring(0, indexStart - 1);
                        
            // Now get the index of the end of the bit we want to remove
            var indexEnd = url.indexOf('&', indexStart);
            if (indexEnd > -1)
            {
                // not the end of the quesrystring so get the remaining values
                newUrl += url.substring(indexEnd);
            }
            
            url = newUrl;
        }

        var newValue = elem.options[elem.selectedIndex].value;
        if (newValue != "")
        {
            // append value to existing qstring values
            url += "&" + ddlName + newValue;
        }
        
        if (url.indexOf('?', 0) == -1)
        {
            // replace the first instance of "&"
            url = url.replace("&", "?");
        } 
        
        // remove bookmark
        var indexOfBookmark = url.indexOf('#RefineSearch', 0);
        if (indexOfBookmark > -1)
        {
            // remove the bookmark
            var stringToRemove = url.substring(indexOfBookmark, indexOfBookmark + 13);
            var urlSections = url.split(stringToRemove);
            url = urlSections[0] + urlSections[1];
        }
        
        // remove "show all" submit button value
        var indexOfSubmit = url.indexOf('&Search=List+Articles', 0);
        if (indexOfSubmit > -1)
        {
            // remove the submit value
            var stringToRemove = url.substring(indexOfSubmit, indexOfSubmit + 21);
            var urlSections = url.split(stringToRemove);
            url = urlSections[0] + urlSections[1];
        }
            
        // remove page keyvalue pair
        var indexOfPageParam = url.indexOf('page=', 0);
        if (indexOfPageParam > -1)
        {
            // now find the next & so we know how many characters to remove
            var indexNextSeparator = url.indexOf('&', indexOfPageParam + 1);
            if (indexNextSeparator > -1)
            {
                // remove the item, it's value and then next separator from querystring
                var stringToRemove = url.substring(indexOfPageParam, (indexNextSeparator + 1));
                var urlSections = url.split(stringToRemove);
                url = urlSections[0] + urlSections[1];
            }
            else
            {
                // no next spearator found so assume it's the end of the querystring.
                // remove the item and it's value from the end of the querystring
                var stringToRemove = url.substring(indexOfPageParam, url.length);
                var urlSections = url.split(stringToRemove);
                url = urlSections[0] + urlSections[1];
            }
        }
        
        window.location = url;
    }
}

