/*
    sample usage

    <a href="#" onclick="dlgMessage('warning', 'CHP intranet', 'Are you sure that you really want to do that?', Array('Yes', 'No'),
                         function(resp){if(resp==0){document.location='www.mypage.com';}});return false;">do that</a>

    <div id="commentValidateDlg" style="display:none">
        <p>Here we add a custom textarea:</p>
        <textarea id="comment" name="comment"></textarea>
    </div>
    <a href="#" onclick="dlgDiv('commentValidateDlg', null, 'CHP intranet', Array('confirm refuse', 'cancel'),
                         function(resp){if(resp==0){alert(document.getElementById('comment'));}});return false;

    types:
    - error
    - warning
    - success
    - prompt
*/

var TIMER = 5;
var SPEED = 20;     // the higher, the faster
var WRAPPER = 'content';

var answerFunction;

function dlgMessage(type, title, message, buttons, answerFunc, autohide) {
    // type: error, warning, success, prompt
    // button: array('button1', 'button2', 'button3')
    // returns 0, 1, 2 (button1, button2, button3)
    var dialogmask;
    answerFunction = answerFunc;
    if (type==null) type='default';
    if (title==null) title='CHP intranet';
    if (autohide==null) autohide=false;
    if (buttons==null) buttons=Array('OK');

    //<input type="button" value="" onclick="answer(true)" />
    if(!document.getElementById('dlgMessage')) {
        dialog = document.createElement('div');
        dialog.id = 'dlgMessage';
        dialogheader = document.createElement('div');
        dialogheader.id = 'dialog-header';
        dialogtitle = document.createElement('div');
        dialogtitle.id = 'dialog-title';
        dialogclose = document.createElement('div');
        dialogclose.id = 'dialog-close'
        dialogcontent = document.createElement('div');
        dialogcontent.id = 'dialog-content';
        dialogmsg = document.createElement('div');
        dialogmsg.id = 'dialog-message';
        dialogbuttons = document.createElement('div');
        dialogbuttons.id = 'dialog-buttons';
        divclear = document.createElement('div');
        divclear.className = 'under-buttons';
        if (buttons[0]!=null) {
            dialogbtn1 = document.createElement('input');
            dialogbtn1.type='button';
            dialogbtn1.value=buttons[0];
            dialogbtn1.setAttribute('onclick', 'answer(0)');
            dialogbtn1.onclick=new Function('answer(0)');       // IE hack
            dialogbtn1.setAttribute('class', 'dialog-button');
            //dialogbtn1.setAttribute('style', 'font-weight:bold');
            dialogbuttons.appendChild(dialogbtn1);
        }
        if (buttons[1]!=null) {
            dialogbtn2 = document.createElement('input');
            dialogbtn2.type='button';
            dialogbtn2.value=buttons[1];
            dialogbtn2.setAttribute('onclick', 'answer(1)');
            dialogbtn2.onclick=new Function('answer(1)');       // IE hack
            dialogbtn2.setAttribute('class', 'dialog-button');
            dialogbuttons.appendChild(dialogbtn2);
        }
        if (buttons[2]!=null) {
            dialogbtn3 = document.createElement('input');
            dialogbtn3.type='button';
            dialogbtn3.value=buttons[2];
            dialogbtn3.setAttribute('onclick', 'answer(2)');
            dialogbtn3.onclick=new Function('answer(2)');       // IE hack
            dialogbtn3.setAttribute('class', 'dialog-button');
            dialogbuttons.appendChild(dialogbtn3);
        }
        dialogcontent.appendChild(dialogmsg);
        dialogcontent.appendChild(dialogbuttons);
        dialogcontent.appendChild(divclear);
        dialogheader.appendChild(dialogtitle);
        dialogheader.appendChild(dialogclose);
        dialog.appendChild(dialogheader);
        dialog.appendChild(dialogcontent);
        dialogclose.onclick = hideDialog;

        dialogmask = document.createElement('div');
        dialogmask.id = 'dialog-mask';

        document.body.appendChild(dialogmask);
        document.body.appendChild(dialog);
    }

    dialog.style.opacity = .00;
    dialog.style.filter = 'alpha(opacity=0)';
    dialog.alpha = 0;
    var width = pageWidth();
    var height = pageHeight();
    var left = leftPosition();
    var top = topPosition();
    var dialogwidth = dialog.offsetWidth;
    var dialogheight = dialog.offsetHeight;
    var topposition = top + (height / 3) - (dialogheight / 2);
    var leftposition = left + (width / 2) - (dialogwidth / 2);
    var buttonswidth = dialogbuttons.offsetWidth;
    dialog.style.top = topposition + "px";
    dialog.style.left = leftposition + "px";
    dialogheader.className = type + "header";
    dialogtitle.innerHTML = title;
    dialogcontent.className = type;
    dialogbuttons.style.margin = '0 0 0 '+(dialogwidth-buttonswidth)/2+'px';
    document.getElementById('dialog-message').innerHTML = message;
    /*var content = document.getElementById(WRAPPER);
    dialogmask.style.height = content.offsetHeight + 'px';*/
    dialog.timer = setInterval("fadeDialog(1)", TIMER);
    if(autohide) {
        //dialogclose.style.visibility = "hidden";
        window.setTimeout("hideDialog()", (autohide * 1000));
    } else {
        dialogclose.style.visibility = "visible";
    }

}

function dlgDiv(id, type, title, buttons, answerFunc) {
    // type: error, warning, success, prompt
    // button: array('button1', 'button2', 'button3')
    // returns 0, 1, 2 (button1, button2, button3)
    var dialogmask;
    answerFunction = answerFunc;
    if (type==null) type='default';
    if (title==null) title='CHP intranet';
    if (buttons==null) buttons=Array('OK');

    if(!document.getElementById(id)) {
        alert('function dlgDiv: div id not found: '+id);
        exit;
    }
    dialogParent = document.getElementById(id);
    dialog=document.createElement('div');
    dialog.id = 'dlgMessage';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogbuttons = document.createElement('div');
    dialogbuttons.id = 'dialog-buttons';
    divclear = document.createElement('div');
    divclear.className = 'under-buttons';
    if (buttons[0]!=null) {
        dialogbtn1 = document.createElement('input');
        dialogbtn1.type='button';
        dialogbtn1.value=buttons[0];
        dialogbtn1.setAttribute('onclick','answer(0)');
        dialogbtn1.onclick=new Function('answer(0)');       // IE hack
        dialogbtn1.setAttribute('class', 'dialog-button');
        dialogbuttons.appendChild(dialogbtn1);
    }
    if (buttons[1]!=null) {
        dialogbtn2 = document.createElement('input');
        dialogbtn2.type='button';
        dialogbtn2.value=buttons[1];
        dialogbtn2.setAttribute('onclick','answer(1)');
        dialogbtn2.onclick=new Function('answer(1)');       // IE hack
        dialogbtn2.setAttribute('class', 'dialog-button');
        dialogbuttons.appendChild(dialogbtn2);
    }
    if (buttons[2]!=null) {
        dialogbtn3 = document.createElement('input');
        dialogbtn3.type='button';
        dialogbtn3.value=buttons[2];
        dialogbtn3.setAttribute('onclick','answer(2)');
        dialogbtn3.onclick=new Function('answer(2)');       // IE hack
        dialogbtn3.setAttribute('class', 'dialog-button');
        dialogbuttons.appendChild(dialogbtn3);
    }
    dialogcontent.innerHTML=dialogParent.innerHTML;
    dialogcontent.appendChild(dialogbuttons);
    dialogcontent.appendChild(divclear);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogheader);
    dialog.appendChild(dialogcontent);
    dialogclose.onclick = hideDialog;

    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';

    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);

    dialog.style.opacity = .00;
    dialog.style.filter = 'alpha(opacity=0)';
    dialog.alpha = 0;
    var width = pageWidth();
    var height = pageHeight();
    var left = leftPosition();
    var top = topPosition();
    var dialogwidth = dialog.offsetWidth;
    var dialogheight = dialog.offsetHeight;
    var topposition = top + (height / 3) - (dialogheight / 2);
    var leftposition = left + (width / 2) - (dialogwidth / 2);
    var buttonswidth = dialogbuttons.offsetWidth;
    dialog.style.top = topposition + "px";
    dialog.style.left = leftposition + "px";
    dialogheader.className = type + "header";
    dialogtitle.innerHTML = title;
    dialogcontent.className = type;
    dialogbuttons.style.margin = '0 0 0 '+(dialogwidth-buttonswidth)/2+'px';
    dialog.timer = setInterval("fadeDialog(1)", TIMER);
    dialogclose.style.visibility = "visible";
    //document.getElementById('auto_focus').focus();

}

function answer(response) {
    if (typeof dialogParent != 'undefined') {
        // copy all values
        arrParentChildren=dialogParent.childNodes;
        arrChildren=dialogcontent.childNodes;
        for(i = 0; i < arrParentChildren.length; i++) {
            arrParentChildren[i].value=arrChildren[i].value;
        }
        dialogParent=undefined;
    }
    hideDialog();
    if (answerFunction) answerFunction(response);
}

function hideDialog() {
  var dialog = document.getElementById('dlgMessage');
  if (dialog != null)
  {
      clearInterval(dialog.timer);
      dialog.timer = setInterval("fadeDialog(0)", 1);   // TIMER
      //if (document.getElementById('dlgMessage')) document.body.removeChild(document.getElementById('dlgMessage'));
      //if (document.getElementById('dialog-mask')) document.body.removeChild(document.getElementById('dialog-mask'));
  }
}

function clearDialog() {
    dialog.style.visibility = "hidden";
    clearInterval(dialog.timer);
    //document.getElementById('dlgMessage').remove();
    //document.getElementById('dialog-mask').remove();
    document.body.removeChild(document.getElementById('dlgMessage'));
    document.body.removeChild(document.getElementById('dialog-mask'));
}

function fadeDialog(flag) {
    if(flag == null) {
        flag = 1;
    }
    dialog = document.getElementById('dlgMessage');
    if (dialog) {
        var value;
        if(flag == 1) {
            value = dialog.alpha + SPEED;
        } else {
            value = dialog.alpha - SPEED;
        }
        dialog.alpha = value;
        dialog.style.opacity = (value / 100);
        dialog.style.filter = 'alpha(opacity=' + value + ')';

        /*document.getElementById('dialog-mask').alpha=value;
        document.getElementById('dialog-mask').style.opacity = (value / 200);
        document.getElementById('dialog-mask').style.filter = 'alpha(opacity=' + value + ')';*/

        if(value >= 99) {
            clearInterval(dialog.timer);
            dialog.timer = null;
        } else if(value <= 1) {
            dialog.style.visibility = "hidden";
            clearInterval(dialog.timer);
            //document.getElementById('dlgMessage').remove();
            //document.getElementById('dialog-mask').remove();
            document.body.removeChild(document.getElementById('dlgMessage'));
            document.body.removeChild(document.getElementById('dialog-mask'));
        }
    }
}

// calculate the current window width //
function pageWidth() {
    return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// 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 typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

// calculate the position starting at the left of the window //
function leftPosition() {
    return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}


