$(document).ready(function() {
    if ($('select[id$=ddlCountry], select[id$=ddlRPSCountry]').length > 0) {
        toggleStateRequired();
        $('select[id$=ddlCountry], select[id$=ddlRPSCountry]').change(toggleStateRequired);
    }
});

var StateValidationControl = [];

function toggleStateRequired() {
    if (57 == $("select[id$='ddlCountry']").val() || 57 == $("select[id$='ddlRPSCountry']").val()) {
        $("tr[id$='trAddressCountyRequired'], tr[id$='trRPSCounty']").css('display', '');
        $("tr[id$='trAddressCountyAdditional']").css('display', 'none');
        try {
            if (StateValidationControl.length > 0) {
                for (control in StateValidationControl) {
                    Page_Validators.push(StateValidationControl[control]);
                }
            }
        } catch (Error) {
        }

    } else {
        $("tr[id$='trAddressCountyRequired'], tr[id$='trRPSCounty']").css('display', 'none');
        $("tr[id$='trAddressCountyAdditional']").css('display', '');
        if (StateValidationControl.length > 0) {
            for (control in StateValidationControl) {
                Page_Validators.push(StateValidationControl[control]);
            }
        }
        StateValidationControl = [];
        try {
            var temp = [];
            for (var i = 0; i < Page_Validators.length; i++) {
                if (!Page_Validators[i].id.match(/txtAddressCountyRequiredValidator/)
                    && !Page_Validators[i].id.match(/requiredRPSCounty/)
                ) {
                    temp.push(Page_Validators[i]);
                } else {
                    StateValidationControl.push(Page_Validators[i]);
                }
            }
            Page_Validators = temp;
        } catch (Error) {
        }
    }
}


// Provides a way of attaching (and detaching) multiple functions to page load.
function addLoadListener(functionName) {
    if (typeof window.addEventListener != 'undefined') {
        window.addEventListener('load', functionName, false);
    }
    else if (typeof document.addEventListener != 'undefined') {
        document.addEventListener('load', functionName, false);
    }
    else if (typeof window.attachEvent != 'undefined') {
        window.attachEvent('onload', functionName);
    }
    else {
        var oldfn = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = functionName;
        }
        else {
            window.onload = function() {
                oldfn();
                functionName();
            };
        }
    }
}

// Written: Aug 2006 (SEL)
function elementShowHide(elementToShowHide) {
    //support legacy browsers
    if (document.all)
        el = document.all[elementToShowHide];
    else
        el = document.getElementById(elementToShowHide);

    //show-hide search area
    if (el.style.display == 'none')
        el.style.display = 'block';

    else if (el.style.display == '')
        el.style.display = 'none';

    else if (el.style.display == 'block')
        el.style.display = 'none';
}

// Change the visibility of two elements (e.g. swap them)
function elementsShowHide(element1, element2) {
    elementShowHide(element1);
    elementShowHide(element2);
}

// HRBS 2007
function hideWarning() {
    //    if(document.getElementById('jsHideMe'))
    //    {document.getElementById('jsHideMe').style.display = 'none';}
    //    else if(document.all && document.all['jsHideMe'])
    //    {document.all['jsHideMe'].style.display = 'none';}
    $('#jsHideMe').hide();
}

function clearSearchText(target) {
    if (!startSelectText && target.value != '') {
        var sst = $('input[id*=topSearchControl_startSearchText]').get(0);
        //document.getElementById('ctl00_topSearchControl_startSearchText');
        startSelectText = sst.value;
    }
    if (target.value == startSelectText) {
        target.value = '';
    }
}

function setSearchText(target) {
    if (target.value == '' && startSelectText) {
        target.value = startSelectText;
    }
    else {
        checkSearchText(target);
    }
}

function checkSearchText(textbox) {
    var terms = textbox.value;
    terms = terms.replace('\?', ' ');
    terms = terms.replace('\%', ' ');
    terms = terms.replace('\/', ' ');
    terms = terms.replace('\\', ' ');
    terms = terms.replace('>', ' ');
    terms = terms.replace('<', ' ');
    terms = terms.replace('\.', ' ');

    if ($('input[id*=_txtTerms]')) {
        $('input[id*=_txtTerms]').val(terms);
    }

    //if (document.getElementById('ctl00_contentMain_lowerSearchControl_txtTerms')) {
    //    document.getElementById('ctl00_contentMain_lowerSearchControl_txtTerms').value = terms;
    //}
}
