/* Variable globale */
var dialogRefresh = false;

function GetById(id) {
    return document.getElementById(id);
}

String.prototype.startsWith = function(s) {
    return (this.match("^" + s) == s);
};

/* calculate the current window height */
function pageHeight() {
    return window.innerHeight != null ? window.innerHeight : document.documentElement
            && document.documentElement.clientHeight ? document.documentElement.clientHeight
            : document.body != null ? document.body.clientHeight : null;
}

/* calculate the current window vertical offset */
function topPosition() {
    return pageHeight() / 2 - 50;
}

/* build-show the dialog box, populate the data and call the fadeDialog function */
/* var t = title */
/* var m = message */
function Dialog(t, m) {

    /* Icone du message */
    var i = '';

    var dialog = GetById('dialog');
    if (!dialog) {
        dialog = document.createElement('DIV');
        dialog.id = 'dialog';
        dialog.innerHTML = '<TABLE style="height:100px;background:#fff no-repeat bottom right;border:1px solid #4f6d81;border-top:none;" cellpadding=0 cellspacing=0 border=0 width=100% id=dialog-table><TR><TD id=dialog-header><BUTTON id=diag-btclose class=bt onclick="hideDialog()" style="display:\'none\';"><SPAN id="diag-btcloseX">X</SPAN></BUTTON><DIV id=dialog-title>&nbsp;</DIV></TD></TR><TR><TD id=dialog-content>&nbsp;</TD></TR><TR><TD align=center style="height:40px;display:\'none\';" id=diag-btline><BUTTON id=diag-bt class=bt onclick="hideDialog()" style="width:70px;">OK</BUTTON></TD></TR></TABLE>';
        dialog.style.top = topPosition();
        document.getElementsByTagName('body')[0].appendChild(dialog);
        var mask = document.createElement('div');
        mask.id = 'dialog-mask';
        mask.style.height = Math.max(document.body.scrollHeight, document.body.offsetHeight) + 'px';
        document.getElementsByTagName('body')[0].appendChild(mask);
    } else {
        dialog.style.top = topPosition();
        dialog.style.display = '';
        GetById('dialog-mask').style.display = '';
    }

    GetById('dialog-title').innerHTML = t;
    GetById('dialog-content').innerHTML = m;
    GetById('dialog-table').style.backgroundImage = i;

    if (m.startsWith("<IFRAME ")) {
        GetById('diag-btline').style.display = 'none';
        GetById('diag-btclose').style.display = '';
    } else {
        GetById('diag-btline').style.display = '';
        GetById('diag-btclose').style.display = 'none';
        GetById('diag-bt').focus();
    }

    return false;
}

/* hide the dialog box */
function hideDialog() {
    if (dialogRefresh)
        Refresh();
    GetById('dialog').style.display = 'none';
    GetById('dialog-mask').style.display = 'none';
}