if ($.browser.msie && jQuery.browser.version <= 7) { $('head').append('<link type="text/css" rel="stylesheet" media="all" href="/resources/css/blitzen/rrc_ie.js.css" />'); }

$(document).ready(function() {
    var links = $('a.shadowbox-iframe');
    if (links.length > 0) {
        Shadowbox.setup(links, { player: 'iframe', width: '850' });
    }
    
    if ($('select[id$=ddlCountry], select[id$=ddlRPSCountry]').length > 0) {
        toggleStateRequired();
        $('select[id$=ddlCountry], select[id$=ddlRPSCountry]').change(toggleStateRequired);
    }

    $('input[id$=hfHasJavaScript]').val("true");

    $('img[pngSrc*=rss-trans.png][width=0],img[src*=rss-trans.png][width=0]').attr('width', '18').attr('height', '18').css({ width: '18px', height: '18px' });
    $('img[pngSrc*=email_notifications-trans.png],img[src*=email_notifications-trans.png]').attr('width', '40').attr('height', '40').css({ width: '40px', height: '40px' });
});

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();
    //            };
    //        }
    //    }

    //PNW 2010
    $('document').ready(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';


    //PNW 2010
    switch ($('#' + elementToShowHide).is(':visible')) {
        case true:
            $('#' + elementToShowHide).hide();
            break;
        default:
            $('#' + elementToShowHide).show();
            break;
    }
}

// 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';}

    //PNW 2009
    $('#jsHideMe').hide();
}

function clearSearchTextBox(target) {
    if (!startSelectText && target.value != '') {
        var jq = $('input[name$=searchStartingText][type=hidden]');
        if (jq.length > 0) {
            sst = jq.get(0);
            startSelectText = sst.value;
        }
    } else if (startSelectText != sst.value) {
        startSelectText = sst.value;
    }
    if (target.value == startSelectText) {
        target.value = '';
    }
}

function setSearchTextBox(target) {
    if (target.value == '' && startSelectText) {
        target.value = startSelectText;
    }
    else {
        checkSearchTextBox(target);
    }
}

function checkSearchTextBox(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;
    //}
}

/**
* Author:   Peter Wright
* Company:  Renishaw PLC
* Copy:     2010(c) 
* Title:    buttonRender
* Desc:     Dynamically renders remove option buttons from the given html element (cell), the string of values from a hidden input (hfLang)
*           it requires a drop down list of options (ddl) and the method to be called when the button is clicked (onClickMethod)
*           it also takes a string (seperator) for use in rendering the dividers between buttons, and a string (clickToRemoveStr)
*           that is used for generating the anchor title
**/
function buttonRender(
        cell,   //jQuery Object
        hfLang, //jQuery Object
        ddl,    //DOM Object
        onClickMethod,  //string
        seperator,   // string (used to seperate the 'or' between buttons)
        clickToRemoveStr
    ) {
    if (typeof ddl != "undefined" && typeof cell != "undefined" && typeof hfLang != "undefined") {
        cell.html(""); //Clear out the cell's html
        var t = hfLang.val();
        var a = String(t).split(";");
        for (var i = 0; i < a.length; i++) {
            if (typeof ddl != "undefined") {
                for (var j = 0; j < ddl.options.length; j++) {
                    if (ddl.options[j].value == a[i]) {
                        ddl.options[j].className = "hide-option";
                        cell.append("<a class=\"button-remove-language remove-filter\" href=\"#\" onclick=\"javascript: return " + onClickMethod + "(this, '" + ddl.options[j].value + "');\" title=\"" + String(clickToRemoveStr).replace(/\{0\}/g, ddl.options[j].text) + "\"><span>" + ddl.options[j].text + " </span><span class=\"remove-cross\">&times;</span></a>");
                    }
                }
            }
        }
    }
    buttonSeperatorRender(cell, seperator);
    return false;
};

/**
* Author:   Peter Wright
* Company:  Renishaw PLC
* Copy:     2010(c) 
* Title:    buttonSeperatorRender
* Desc:     Dynamically renders the specified seperator string between the dynamically added buttons
**/
function buttonSeperatorRender(cell, seperator) {
    if (typeof cell != "undefined") {
        var re = new RegExp(' ' + seperator + ' (?!\')', 'ig');
        cell.html(String(cell.html()).replace(re, ""));
        $('a:not(:last)', cell).after(' ' + seperator + ' ');
    }
    return false;
};

/**
* Author:   Peter Wright
* Company:  Renishaw PLC
* Copy:     2010(c) 
* Title:    SmoothDocumentSizing
* Desc:     Used in conjunction with a Slideup on a jQuery object to move the position of the scrollbar to hide the ugly background flicker with a firefox bug
**/
SmoothDocumentSizing = {
    version: '1.0',
    timer: '',
    Activate: function() {
        //only activate if we're viewing from Firefox as this is the browser with the flicker bug
        //or the browser is ie6
        if ($.browser.mozilla || ($.browser.msie && $.browser.version < 7)) {
            this.timer = setInterval(SmoothDocumentSizing.Tick, 5);
            if (
                ($(document).height() - 40) < ($(window).height() + $(document).scrollTop())
                && $(document).scrollTop() > 0
            ) {
                $(document).scrollTop($(document).height() - $(window).height() - 40);
            }
        }
    },
    Tick: function() {
        if (
                ($(document).height() - 40) < ($(window).height() + $(document).scrollTop())
                && $(document).scrollTop() > 0
            ) {
            $(document).scrollTop($(document).height() - $(window).height() - 40);
        } else if ($(document).scrollTop() == 0) {
            SmoothDocumentSizing.deActivate();
        }
    },
    deActivate: function() {
        clearInterval(this.timer);
    }
};

function slideOpen(jqObj) {
    if (!jqObj.is(':visible') && jqObj.is(':not(:animated)')) {
        if ($(document).scrollTop() + $(window).height() >= ($(document).height() - 20)) {
            $(document).scrollTop($(document).height() - $(window).height() - 20);
        }
        jqObj.slideDown();
    }
};

function slideClosed(jqObj) {
    if (jqObj.is(':visible') && jqObj.is(':not(:animated)')) {
        SmoothDocumentSizing.Activate();
        jqObj.slideUp("slow", function() { SmoothDocumentSizing.deActivate(); });
    }
};


/**
* Document ready event
**/
$('document').ready(function() {
    $('input[type=text][id*=txtTerms]').focus(function() {
        clearSearchTextBox(this);
    }).blur(function() {
        setSearchTextBox(this);
    }).keyup(function() {
        checkSearchTextBox(this);
    });
});
