var logPopup = false;
var logFolder = "shared";
//var defaultloginwidth = 385;
//var defaultloginheight = 340;
var defaultloginwidth = 700;
var defaultloginheight = 445;

// show producer document
function showdocument(id, width, height, bypasslogin) {
    if (!width) width = 550;
    if (!height) height = 500;
    bypasslogin = !(!bypasslogin);
    login(defaultSite + "/common/shared/ctl/displaydocument.aspx?id=" + escape(id), width, height, true, true, false, false, bypasslogin);
}

function loguser(url, formname, viewForgetPW, width, height, language) {
    if (location.href.indexOf("login.aspx") > -1) return;

    var top = (screen.availHeight - height) / 2; 	// define the popup's vertical position on screen;
    var left = (screen.availWidth - width) / 2; 	// define the popup's horizontal position on screen;
    if (!width) width = defaultloginwidth; 			// define the popup's width;
    if (!height) height = defaultloginheight; 		// define the popup's height;

    if (language == null)
        url = defaultSite + '/common/' + logFolder + '/navigation/login.aspx?rfr=' + escape(location.href) + '&url=' + escape(url) + '&formname=' + formname;
    else
        url = defaultSite + '/common/' + logFolder + '/navigation/login-' + language + '.aspx?rfr=' + escape(location.href) + '&url=' + escape(url) + '&formname=' + formname;
    if (viewForgetPW) // précise s'il faut afficher ou non le texte "Forgot your ID / password?" => autre texte en haut de popup
        url = url + '&viewForgetPW=' + viewForgetPW

    if (overlayIframe) {
        overlayIframe(url, width, height);
        logPopup = true;
    }
    else {
        logPopup = window.open(url, getLinkID(url), "width=" + width + ",height=" + height + ",titlebar=0,toolbar=0,scrollbars=0,resizable=no,top=" + top + ",left=" + left);
    }
    if (logPopup)
        if (!logPopup.closed)
        logPopup.focus();
}

function login(url, width, height, redirect, resizable, loginwidth, loginheight, bypasslogin) {
    if (location.href.indexOf("login.aspx") > -1) return;

    var left = (screen.availWidth - width) / 2;
    var top = (screen.availHeight - height) / 2;
    if (!loginwidth) loginwidth = defaultloginwidth;
    if (!loginheight) loginheight = defaultloginheight;
    if (!height) height = 400;
    bypasslogin = !(!bypasslogin);
    redirect = (redirect == false) ? "no" : "yes";
    resizable = (resizable == false) ? "no" : "yes";
    if (bypasslogin || GetCookie(".ASPXUSERDEMO") != null) {
        if (width) {
            logPopup = window.open(url, getLinkID(url), 'width=' + width + ',height=' + height + ',titlebar=0,toolbar=0,scrollbars=1,resizable=' + resizable + ',left=' + left + ',top=' + top);
        }
        else {
            location.href = url;
            logPopup = false;
        }
    }
    else {
        url = defaultSite + '/common/' + logFolder + '/navigation/login.aspx?rfr=' + escape(location.href) + '&url=' + escape(url);
        if (width)
            url += "&target=popup&width=" + width + "&height=" + height + "&r=" + redirect + "&s=" + resizable;

        if (overlayIframe) {
            logPopup = overlayIframe(url, loginwidth, loginheight);
            logPopup = true;
            return;
        }
        else {
            logPopup = window.open(url, getLinkID(url), 'width=' + loginwidth + ',height=' + loginheight + ',titlebar=0,toolbar=0,scrollbars=0,resizable=no,top=130,left=250');
        }
    }
    if (logPopup)
        if (!logPopup.closed)
        logPopup.focus();
}

function closePopup() {
    if (logPopup) {
        if (!logPopup.closed) {
            logPopup.close();
            logPopup = false;
        }
    }
}
var linkArray = new Array();

if (!overlayIframe) {
    window.onUnload = closePopup();
}

function getLinkID(url) {
    var id = "";
    for (i = 0; i < url.length; i++) {
        id += url.charCodeAt(i, i + 1);
    }
    return id;
}

function GetCookie(name) {
    var cookie = " " + document.cookie;
    var tmpCookie = cookie.toUpperCase();
    name = name.toUpperCase();

    var offset = 0;
    var end = 0;
    var end2 = 0;
    var search = " " + name + "=";
    offset = tmpCookie.indexOf(search);
    if (offset != -1) {
        offset += search.length;
        end = tmpCookie.indexOf(";", offset)
        if (end == -1) {
            end = tmpCookie.length;
        }
        end2 = tmpCookie.indexOf("&", offset)
        if (end2 == -1) {
            end2 = tmpCookie.length;
        }
        if (end2 < end)
            end = end2;
        return unescape(cookie.substring(offset, end));
    }
    var search = "&" + name + "=";
    offset = tmpCookie.indexOf(search);
    if (offset != -1) {
        offset += search.length;
        end = tmpCookie.indexOf(";", offset)
        if (end == -1) {
            end = tmpCookie.length;
        }
        end2 = tmpCookie.indexOf("&", offset)
        if (end2 == -1) {
            end2 = tmpCookie.length;
        }
        if (end2 < end)
            end = end2;
        tmpCookie = unescape(cookie.substring(offset, end));
        if (tmpCookie != "") {
            return tmpCookie;
        }
    }
    return null;
}

//-------------------------------------------------
// function used in the right hand login box
//-------------------------------------------------
function LoginTick() {
    var form = document.forms["myscform"];
    if (form["rememberme"].value == "1") {
        document.getElementById("tick").src = "/img/navigation/login/tick_off.gif";
        form["rememberme"].value = "0";
    }
    else {
        document.getElementById("tick").src = "/img/navigation/login/tick_on.gif";
        form["rememberme"].value = "1";
    }
}
