﻿(function()
{
    function isIE()
    {
        var isIE = false;

        if (navigator.appName.toLowerCase().indexOf('internet explorer') >= 0)
        {
            isIE = true;
        }

        return isIE;
    }
    
    window.getCountiesByRegion = function(
		loadingMessageClientID,
		countyCodeListClientID)
    {
        var countyCodeList = document.getElementById(
			countyCodeListClientID);

        if ((this != null) &&
			(countyCodeList != null) &&
			(typeof(GetCountiesByRegions) == 'function') &&
			(this.selectedIndex > 0))
        {
            GetCountiesByRegions(
		        loadingMessageClientID,
		        this,
		        countyCodeList,
		        true)
        }
        else if (countyCodeList != null)
        {
            countyCodeList.selectedIndex = 0;
            countyCodeList.disabled = true;
        }
    };

    function initalizeSelectElement(
        selectElement)
    {
        if ((selectElement) &&
            (selectElement.options) &&
            (selectElement.options.length > 0))
        {
            for (var index = 0; index < selectElement.options.length; index++)
            {
                if (selectElement.options[index].disabled)
                {
                    var option = selectElement.options[index];

                    var optGroup = document.createElement(
                        'optgroup');

                    optGroup.label = option.innerHTML.replace(
                        '&amp;',
                        '&');

                    optGroup._option = option;

                    var parent = option.parentNode;

                    if (parent)
                    {
                        parent.replaceChild(
                            optGroup,
                            option);
                    }
                }
            }
        }
    }

    window.initializeSelectElements = function()
    {
        if (isIE())
        {
            var selects = document.getElementsByTagName(
                'select');

            if ((selects) &&
                (selects.length > 0))
            {
                for (var index = 0; index < selects.length; index++)
                {
                    initalizeSelectElement(
                        selects[index]);
                }
            }
        }
    };
})();