﻿/* Utility functions */

/* Browser detection */
var isMac = (navigator.appVersion.indexOf("Mac") != -1) ? true : false;
var NS4 = (document.layers) ? true : false;
var IEmac = ((document.all) && (isMac)) ? true : false;
var IE4plus = (document.all) ? true : false;
var IE4 = ((document.all) && (navigator.appVersion.indexOf("MSIE 4.") != -1)) ? true : false;
var IE5 = ((document.all) && (navigator.appVersion.indexOf("MSIE 5.") != -1)) ? true : false;
var IE6 = ((document.all) && (navigator.appVersion.indexOf("MSIE 6.") != -1)) ? true : false;
var IE7 = ((document.all) && (navigator.appVersion.indexOf("MSIE 7.") != -1)) ? true : false;
var IE8 = ((document.all) && (navigator.appVersion.indexOf("MSIE 8.") != -1)) ? true : false;
var IE9 = ((document.all) && (navigator.appVersion.indexOf("MSIE 9.") != -1)) ? true : false;
var ver4 = (NS4 || IE4plus) ? true : false;
var NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape') != -1) ? true : false;
var IE = (IEmac || IE4plus || IE4 || IE5 || IE6 || IE7 || IE8 || IE9) ? true : false;
/* Browser detection */

/* Body onload utility (supports multiple onload functions) */
var gSafeOnload = new Array();

function safeAddOnLoad(f) {
    if (IEmac && IE4) {
        // IE 4.5 blows out on testing window.onload    
        window.onload = safeOnload;
        gSafeOnload[gSafeOnload.length] = f;
    }

    else if (window.onload) {
        if (window.onload != safeOnload) {
            gSafeOnload[0] = window.onload;
            window.onload = safeOnload;
        }
        gSafeOnload[gSafeOnload.length] = f;
    }

    else {
        window.onload = f;
    }
}

function safeOnload() {
    for (var i = 0; i < gSafeOnload.length; i++) {
        gSafeOnload[i]();
    } 
}
/* Body onload utility (supports multiple onload functions) */

function checkForLinkUpdates() {
    var location = window.location.href.toString().toLowerCase();

    if (!IE) {
        hideDefaultSitefinityLogo();
    }

    if (location.indexOf('login') > -1) {
        updateLoginPageLabels();
    }    
}

// The terms of the Community Edition of Sitefinity requires a "powered
// by Sitefinity" logo to be displayed in the footer of every page. We've
// done this, but in IE7 and below, an *additional* logo is displayed (not
// visible in FF or Safari, nor IE8 when not in compatiblity mode). Hide this
// logo!
function hideDefaultSitefinityLogo() {
    var telerikLogo = document.getElementById('ctl00_sitefinityLogo');

    if (telerikLogo && telerikLogo.style) {
        telerikLogo.style['display'] = 'none !important';
    }
}

// Sets the focus on the User Name input on the login page
function setInitialFocus() {
    var userNameInput = document.getElementById("ctl00_loginPlaceHolder_ctl00_Login1_UserName");

    if (userNameInput) {
        userNameInput.focus();
    }
}

function updateLoginPageLabels() {
    var containedNode = null;
    var containedNodeValue = '';

    var passwordLi = document.getElementById('ctl00_loginPlaceHolder_ctl00_Login1_PasswordLabel');
    if (passwordLi) {
        containedNode = passwordLi.firstChild;

        if (containedNode != null && containedNode.nodeValue != null) {
            containedNode.nodeValue = 'Password';
        }
    }

    var usernameLi = document.getElementById('ctl00_loginPlaceHolder_ctl00_Login1_UserNameLabel');
    if (usernameLi) {
        containedNode = usernameLi.firstChild;

        if (containedNode != null && containedNode.nodeValue != null) {
            containedNode.nodeValue = 'Username';
        }
    }

    if (passwordLi && usernameLi) {
        setInitialFocus();
    }
}

/* Execute on js load: */
safeAddOnLoad(checkForLinkUpdates);
