﻿/// <reference path="jquery/jquery-1.3.2.min.js" />
/// <reference name="MicrosoftAjax.js" />
var CustomValidatorErrors = new Array();

jQuery(document).ready(function() {
    RunStartupScript();

    // For async requests from UpdatePanels, this is fired instead of $(document).ready()
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);
});

function endRequest(sender, args) {
    RunStartupScript();
}

function RunStartupScript() {
    SetCellPaddings();
    SetLinkButtons();
    SetRoundedCorners(".contentBlock", 10);
    SetRoundedCorners(".sbSubContent", 6);
    FixesForIE6();
}

function SetRoundedCorners(jquerySelector, radiusInPx) {
    try {
        if (!($.browser.msie)) {
            $.jcorners(jquerySelector, { radius: radiusInPx });
        }
    }
    catch (e) { /*alert("SetRoundedCorners: " + e.message);*/ }
}

function GetRadioButtonValue(id) {
    var radio = $('#' + id + ' input');
    for (var j = 0; j < radio.length; j++) {
        if (radio[j].checked)
            return 0;
    }
    return null;
}

function FixesForIE6() {
    try {
        if ($.browser.msie && $.browser.version == "6.0") {
            //Remove borders around checkboxes and radio buttons
            $('input[type="checkbox"]').addClass("noBorder");
            $('input[type="radio"]').addClass("noBorder");

            //Add buttonHover class to buttons since :hover is not supported
            $("input.button").hover(
            function() {
                $(this).addClass("buttonHover");
            },
            function() {
                $(this).removeClass("buttonHover");
            });
        }
    }
    catch (e) { /*alert("FixesForIE6: " + e.message);*/ }
}

function SetLinkButtons() {
    try {
        $("a.button").each(function() {
            if ($(this).find("span").size() == 0) {
                $(this).wrapInner('<span></span>');
            }

            //Fix asp.net firefox bug - register click event for <asp:LinkButton/>
            //controls so that they can be used as DefaultButtons on Panels
            if (typeof (this.click) == 'undefined') {
                this.click = function() {
                    var result = true;
                    if (this.onclick) result = this.onclick();
                    if (typeof (result) == 'undefined' || result) {
                        eval(this.getAttribute('href'));
                    }
                }
            }
        });
    }
    catch (e) { /*alert("SetLinkButtons: " + e.message);*/ }
}

function SetCellPaddings() {
    try {
        $(".contentBlock td, .contentBlock th").each(function() {
            if ($(this).find("div.cell").size() == 0 && this.innerHTML) {
                $(this).wrapInner('<div class="cell"></div>');
            }
        });
        $('.dualList td[valign="middle"]').each(function() {
            $(this).addClass("middle");
        });
    }
    catch (e) { /*alert("SetCellPaddings: " + e.message);*/ }
}

function AddValidationSummaryMessageFunction() {
    if (typeof (Page_ClientValidate) == "function") {
        var pageClientValidateBody = Page_ClientValidate.toString();

        var openCurlyBraceIndex = pageClientValidateBody.indexOf("{");
        var closeCurlyBraceIndex = pageClientValidateBody.lastIndexOf("}");
        var returnIndex = pageClientValidateBody.lastIndexOf("return");
        var partBody1 = pageClientValidateBody.substring(openCurlyBraceIndex + 1, returnIndex);
        var partBody2 = pageClientValidateBody.substring(returnIndex, closeCurlyBraceIndex);

        var pageClientValidateBodyNew = partBody1 + "\r\n AddValidationSummaryMessage();\r\n" + partBody2;

        Page_ClientValidate = new Function("validationGroup", pageClientValidateBodyNew);
    }
}

function AddValidationSummaryMessage() {
    var valsJQ = $("*[id$='valsJQ']")[0];

    if (valsJQ != null && CustomValidatorErrors != null && CustomValidatorErrors.length > 0) {
        var ul = valsJQ.getElementsByTagName("ul")[0];

        if (ul == null) {
            ul = document.createElement("ul");
            valsJQ.appendChild(ul);
        }

        var i = 0;
        for (i = 0; i < CustomValidatorErrors.length; i++) {
            var li = document.createElement("li");

            if ($.browser.msie) {
                li.innerText = CustomValidatorErrors[i];
            }
            else {
                li.textContent = CustomValidatorErrors[i];
            }

            ul.appendChild(li);
        }

        CustomValidatorErrors = new Array();
    }
}

function AddErrorMessage(message) {
    var index = CustomValidatorErrors.length > 0 ? CustomValidatorErrors.length : 0;

    if (!CustomValidatorErrors.contains(message)) {
        CustomValidatorErrors[index] = message;
    }
}

function ClearErrorMessages() {
    if (CustomValidatorErrors.length > 0) {
        CustomValidatorErrors = new Array();
    }
}


Array.prototype.contains = function(obj) { var i = this.length; while (i--) { if (this[i] === obj) { return true; } } return false; }

/** Shows "Please Wait" message in the modal div */
function ShowPwMsg(validationGroup, messageSelector, title, dialogClass) {
    //make sure that all page validators, if present, have been validated before continuing
    if (typeof (Page_ClientValidate) != 'function' || Page_ClientValidate(!validationGroup ? '' : validationGroup)) {
        if (messageSelector == null) {
            messageSelector = 'div#pleaseWait';
        }
        if (title == null) {
            title = 'Please wait';
        }
        if (dialogClass = null) {
            dialogClass = 'modalDialog';
        }
        $(messageSelector).dialog({
            modal: true,
            title: title,
            bgiframe: true,
            width: 350,
            closeOnEscape: false,
            resizable: false,
            draggable: false,
            dialogClass: dialogClass
        });
    }
}
function HidePwMsg(messageSelector) {
    if (messageSelector == null) {
        messageSelector = 'div#pleaseWait';
    }
    $(messageSelector).dialog('destroy');
}

function ShowHelp(rootPath, helpId) {
    try {
        var win = window.open(rootPath + "Help/ContentHelp.aspx?id=" + helpId, 'winHelpItem', 'width=780,height=520,location=0,menubar=0,scrollbars=1,status=0,toolbar=0,resizable=1,left=50,top=50');
        win.focus();
    }
    catch (e) { }
    return false;
}

//called from applicant home page to open region popup window
var str1;
function OpenRegionPopup(IsButtonClick) {
    var val = $("select[id$='ddlEventCategory'] :selected").val();
    if (val == '1') {
        var popupWidth = 800;
        var popupHeight = 580;
        if ($.browser.msie) {
            popupWidth = 800;
            popupHeight = 620;
        }
        $("#ctl00_ctl00_bodySidebarBase_bodySidebar_ddlCareerCenter").val("-1");
        var regionWin = window.open('RegionPopup.aspx', 'regionWindow', 'left=100,top=50,width=' + popupWidth + '",height=' + popupHeight + ',toolbar=0,resizable=1,scrollbars=1');
        if (regionWin != null)
            regionWin.focus();
    }
    else if (IsButtonClick) {
        ShowPwMsg();
    }
}

//called from region popup window to redirect the applicant home page to 'EventResults.aspx' page.
function UpdateParentHomePage() {

    var pleaseWaitWin = window.opener.$("#pleaseWait");
    pleaseWaitWin.dialog({
        modal: true,
        title: 'Please wait',
        bgiframe: true,
        width: 350,
        closeOnEscape: true,
        resizable: false,
        draggable: false,
        dialogClass: 'modalDialog'
    });

    window.opener.location.href = "EventResults.aspx";
    window.close();
}
