var AgntUsr = navigator.userAgent.toLowerCase();
var DomYes = document.getElementById ? 1 : 0;
var NavYes = AgntUsr.indexOf('mozilla') != -1 && AgntUsr.indexOf('compatible') == -1 ? 1 : 0;
var ExpYes = AgntUsr.indexOf('msie') != -1 ? 1 : 0;
var Exp4 = ExpYes && !DomYes && document.all ? 1 : 0;
var OprYes = AgntUsr.indexOf('opera') != -1 ? 1 : 0;
var Linux = AgntUsr.indexOf('linux') != -1 || AgntUsr.indexOf('x11') != -1 ? 1 : 0;
var popUpCookieName = "";

/**
 * Displays the  given element on the page by setting the "display"
 * style attribute to 'block'. This method also calls hideComboboxes()
 * to avoid combobox overlap over inline popup in IE.
 *
 * @param popupName id of the element
 */
function showMe(popupName) {
    if (DomYes) {
        document.getElementById(popupName).style.display = 'block';
    } else if (Exp4) {
        document.all[popupName].style.display = 'block';
    }

    hideComboboxes();
}

/**
 * Hides the given element on the page by setting the "display"
 * style attribute to 'none'.
 *
 * @param id of the element
 */
function hideMe(popupName) {
    if (DomYes) {
        document.getElementById(popupName).style.display = 'none';
    } else if (Exp4) {
        document.all[popupName].style.display = 'none';
    }

    showComboboxes();
}

/**
 * Hides all elements related to inline popup message. This
 * is just a shortcut method.
 */
function hideAll() {
    hideMe('popup');
    hideMe('dimmed');
}

/**
 * Tries to parse value for the given cookieName.
 *
 * @param cookieName - name of the cookie
 * @returns value of the cookie or NULL if it is not found
 */
function getCookieValue(cookieName) {
    var cookieValue = document.cookie;
    var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");

    if (cookieStartsAt == -1) {
      cookieStartsAt = cookieValue.indexOf(cookieName + "=");
    }

    if (cookieStartsAt == -1) {
        cookieValue = null;
    } else {
        cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
        var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
        if (cookieEndsAt == -1) {
            cookieEndsAt = cookieValue.length;
        }
        cookieValue = 
          unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
    }

    return cookieValue;
}

/**
 * Sets cookie with the given cookieName.
 *
 * @param cookieName - name of the cookie
 * @param cookieValue - value of the cookie
 * @param cookiePath - validity path
 * @param daysToExpire - set to zero for session lasting
 */
function setCookie(cookieName, cookieValue, cookiePath, daysToExpire) {
    cookieValue = escape(cookieValue);
    cookieExpires = "";
    
    if (daysToExpire > 0) {
        nowDate = new Date();
        nowDate.setDate(nowDate.getDate()+daysToExpire);
        cookieExpires = ";expires="+nowDate.toGMTString();
    }

    if (cookiePath != "") {
        cookiePath = ";Path=" + cookiePath;
    }

    document.cookie = 
        cookieName 
        + "=" 
        + cookieValue 
        + cookieExpires
        + cookiePath;
}

function setDaysToExpire(daysToExpire) {

    // re-set the cookie
    setCookie(popUpCookieName,'served','/',daysToExpire);
}

function initPopup(showTime, hideTime, cookieName, daysToExpire, isDimmed, isAlwaysShown) {
    if (getCookieValue(cookieName) == null || isAlwaysShown) { 
        setTimeout("showMe('popup')", showTime);
        
        if (isDimmed) {
            setTimeout("showMe('dimmed')", showTime);
            setTimeout("adjustHeight()", showTime);
        }
               
        popUpCookieName = cookieName;
        setCookie(cookieName,'served','/',daysToExpire);
        if (hideTime > 0) {
            setTimeout("hideMe('popup')", showTime+hideTime);
            if (isDimmed) {
                setTimeout("hideMe('dimmed')", showTime+hideTime);
            }
        }
    }
}

function adjustHeight() {
    nh  = document.getElementById('contentwrapper').offsetHeight  ;
    document.getElementById('dimmed').style.height = nh+"px";
}

function openWin(target, name, width, height, center) {
    if (NavYes && !Linux) {
        height += 27;
    }

    ypos = (screen.height - height) / 2;
    xpos = (screen.width - width) / 2;

    if (OprYes || (center && screen.height <= 600)) {
        ypos = 0;
        xpos = 0;
    }

    features = 'width='+width+',height='+ height+',top='+ypos+',left='+xpos+
                   ',scrollbars=yes,status=no,resizable=yes,outerWidth='+width+',outerHeight='+height+',screenY='+ypos+',screenX='+ xpos;
    win = window.open(target, name, features);
}

function hideComboboxes(){
    if (!DomYes || !ExpYes) {
        return;
    }

    var sel = document.getElementsByTagName('select');
    for (i = 0; i < sel.length; i++) {
        sel[i].style.visibility = "hidden";
    }
}

function showComboboxes(){
    if (!DomYes || !ExpYes) {
        return;
    }

    var sel = document.getElementsByTagName('select');
    for (i = 0; i < sel.length;i++) {
        sel[i].style.visibility = "visible";
    }
}

