function removeAdminPort(link) {
    var rootURL = "www.iccsafe.org";

    var correctedURL = link.href.replace(":8888", "");
    correctedURL = correctedURL.replace("icc-web-cch.iccsafe-int.org", rootURL);
    correctedURL = correctedURL.replace("icc-web-cch", rootURL);
    correctedURL = correctedURL.replace("preview.iccsafe.org", rootURL);
    //    correctedURL = correctedURL.replace("65.201.76.66",rootURL);

    return correctedURL;
}

function removeAdminPortImg(img) {
    var rootURL = "www.iccsafe.org";

    var correctedURL = img.src.replace(":8888", "");
    correctedURL = correctedURL.replace("icc-web-cch.iccsafe-int.org", rootURL);
    correctedURL = correctedURL.replace("icc-web-cch", rootURL);
    correctedURL = correctedURL.replace("preview.iccsafe.org", rootURL);
    //    correctedURL = correctedURL.replace("65.201.76.66",rootURL);

    return correctedURL;
}



function generateEmLink(obj) {




    var unm = obj.name;

    var subject = "";
    if (obj.title.length > 0)
        subject = "?subject=" + obj.title;


    switch (obj.id) {
        case "ICC": dmn = "iccsafe.org"; break;
        case "IAS": dmn = "iasonline.org"; break;
        case "ES": dmn = "icc-es.org"; break;

        default: dmn = obj.id;
    }

    var asgn = "@";

    var adres = unm + asgn + dmn;

    return 'ma' + 'ilto:' + adres + subject;

}



function iccFinalizePage(url) {
    if (!url || "" == url) {
        url = document.URL;
    }


    if (url.indexOf(":8888") == -1) {
        $("a[href*=':8888'],a[href*='icc-web-cch']").each(function() {
            $(this).attr("href", removeAdminPort(this))
        });


        // SSO Sign Out
        $("a:contains('Sign Out')[href*='$LoginView$']").click(function() {
            var href = $(this).attr("href");
            $(document.body).append('<IFRAME id="eWebLogout" width="1" height="1"></IFRAME>');
            var iframe = $('iframe#eWebLogout');
            iframe.load(function() {
                window.location = href;
            });
            iframe.attr('src', 'https://av.iccsafe.org/eweb/Logout.aspx?Site=icc');
            return false;
        });

    }


    if (url.indexOf("NewForm.aspx") >= 0) {
        //Override the behavior of the Cancel button to go "back" when clicked

        $(function() { $("input[value='Cancel']").live("click", function() { history.go(-1); return false }) });
    }


    // generate email addresses
    $(".emLink").click(function() {
        $(this).attr("href", generateEmLink(this))
    });


    // fix the ad rotator on the home page
    $("#ctl00_m_g_f78e82cd_52d4_47c9_8399_d0e1f42392b7").css("padding-left", "28px");


    // two fixes to allow an anonymous user to get to an Event Calendar entry - our custom form
    if (url.indexOf("/Pages/events.aspx") >= 0) {
        $("#MontlyViewDefault_CalendarView a,td[href]").each(function() {
            $(this).attr("href", this.href.replace("/Lists/EventCalendar/DispForm.aspx?", "/Pages/event.aspx?event"))
        });

    }

    if (url.indexOf("/Pages/events-list.aspx") >= 0) {
        $("a").each(function() {
            $(this).attr("href", this.href.replace("/Lists/EventCalendar/DispForm.aspx?", "/Pages/event.aspx?event"))
        });

    }



    // fix to allow an anonymous user to access Course Catalog detail page
    if (url.indexOf("/Education/Courses/Pages/catalog.aspx") >= 0) {
        $("a").each(function() {
            $(this).attr("href", this.href.replace("/Lists/Catalog/DispForm.aspx?", "/Pages/course.aspx?course"))
        });

    }


    // fix to remove "Other" sites from breadcrumb
    if (url.indexOf("/content/Pages/") >= 0) {
        $(".ms-sitemapdirectional").each(function() {
            if ($(this).attr("href").indexOf("/content/Pages") >= 0) {
                $(this).css("display", "none");
                $(this).parent().next().css("display", "none");
            }
        });

    }


    //fix to remove "No Product ID for URL" message from Education live schedule detail page
    if (url.indexOf("/Education/Courses/Lists/Schedule/DetailForm.aspx") >= 0) {
        $("#ctl00_m_g_025de323_ecd7_4ec3_b00c_e04dfd046da2").css("display", "none");
    }


    if (url.indexOf("/Store/Pages/FreeCodes.aspx") >= 0 && $(".loginBoxFields").length) {
        $("table.ms-listviewtable td.ms-vb2 a[target='_blank']").live("click", function() {
            $(this).attr("href", "/Store/Pages/eCodesOptionalSurvey.aspx?Source=%2FStore%2FPages%2FeCodesLauncher.aspx%3FSource%3D" + encodeURIComponent(encodeURIComponent(this.href)));
            $(this).attr("target", "");
        });
    }


    if (url.indexOf("/Store/Pages/eCodesLauncher.aspx") >= 0) {
        $("a#redirect").attr("href", decodeURIComponent(location.search.substring(8)));
        $("a#redirect2").attr("href", decodeURIComponent(location.search.substring(8)));
    }

    //fix to remove link to AllItems.aspx on new member app form
    if (url.indexOf("/Membership/Lists/ICCSafeMember/NewForm.aspx") >= 0) {
        $("h2.ms-pagetitle").html("New Member Application Form");
    }


    //fix to remove link on chapterEvents.aspx
    if (url.indexOf("/Education/Courses/Pages/chapterevents.aspx") >= 0) {
        $("a").each(function() {
            if ($(this).attr("href").indexOf("/gr/Lists/") >= 0) {
                $(this).attr("href", "#");
            }
        });
    }

    //fix to continue button on register.aspx
    if (url.indexOf("/Pages/register.aspx") >= 0) {
        var continueButton = $(":submit[id$='_ContinueButtonButton']");
        var returnUrl = GetUrlKeyValue("ReturnUrl", false);
        if (null != continueButton && "" != returnUrl) {
            continueButton.click(function() {
                window.location.href = returnUrl;
                return false;
            });
        }
    }



    // temporary fix promo URLs
    if (url.indexOf("/NotFound.aspx") >= 0) {
        var decodeurl = decodeURIComponent(url).replace("&#061 ", "=");
        var searchvalue = "&k=promo.html?promoid";
        var index = decodeurl.indexOf(searchvalue);
        if (index != -1) {
            var code = jQuery.trim(decodeurl.substring(index + searchvalue.length + 1));
            var href = "/Store/Pages/AddToCartRedirect.aspx?Coupon=@&RedirectUrl=/Store/Pages/@.aspx".replace(/@/g, code);
            $("div[class='smart404'] h2").text("The page you requested has been moved");
            $("div[class='smart404Notice']").html("The page you requested has been moved, <a href='" + href + "'>access the new page here</a>.");
            $("div[class='smart404Return']").hide();
            window.location.replace(href);
        }
    }


    //Hide Links from MySite Left Nav: Lists, Discussions, Surveys and Sites
    if (url.indexOf("/ICCPersonal/") >= 0 || url.indexOf("/MySite/") >= 0) {
        $("#zz2_QuickLaunchMenun10").css("display", "none");
        $("#zz2_QuickLaunchMenun11").css("display", "none");
        $("#zz2_QuickLaunchMenun12").css("display", "none");
        $("#zz2_QuickLaunchMenun13").css("display", "none");
    }

    //fix to continue button on register.aspx
    if (url.indexOf("/Store/Pages/QuickOrder.aspx") >= 0) {
        $("#ctl00_PlaceHolderBannerContent_ctl01_LoginView_login_FailureText").removeClass("ms-error");
        $("#ctl00_PlaceHolderBannerContent_ctl01_LoginView_login_FailureText").addClass("ms-error2");
    }

}
_spBodyOnLoadFunctionNames.push("iccFinalizePage");
