/**********************************************************************
 *  Copyright notice 
 * 
 *  (c) 2006 Mikko Mensonen <mikko.mensonen@gmail.com>
 *
 *  This script is part of workbase. work management system.
 *
 *  workbase. is free software; you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published 
 *  by the Free Software Foundation; either version 2 of the License, 
 *  or (at your option) any later version.
 *
 *  See the GNU General Public License for more details. You should 
 *  have received a copy of the GNU General Public License
 *  along with the workbase. package; if not, you can obtain it from
 *  http://www.gnu.org/licenses/gpl.html
 *
 *********************************************************************/
/**
 * Lovely prompt script
 *
 * Use of domdrag is commented out. If you want to have the prompt box
 * movable, add the domdrag script and uncomment the Drag.init line
 * 
 * @author Mikko Mensonen <mikko.mensonen@gmail.com>
 */

var msg_ok = 'OK';
var msg_cancel = 'Cancel';

function hidePrompt() {
    
    if (document.getElementById('promptcontainer')) {
        document.body.removeChild(document.getElementById('promptcontainer'));
    }
    
    if (document.getElementById('main')) {
        document.getElementById('main').style.opacity = '1';
        document.getElementById('main').style.filter = 'alpha(opacity = 100)';
    }
}


/**
 * Pops up a css prompt
 *
 * @param string type   One of the following: success, info, error or prompt
 * @param string title  The prompt title
 * @param string text   Prompt text
 * @param string hook   String javaScript function or a valid javascript code that will be
 *                      executed when button OK is clicked
 * @param string hook2  String javaScript function or a valid javascript code that will be
 *                      executed when button Cancel is clicked
 */
function promptIt(type, title, text, hook, hook2) {
    
    divPrompt = document.createElement('div');
    h1 = document.createElement('h1');
    
    lines = text.split('\n');

    txt = document.createElement('p');
    for (i=0; i<lines.length; i++) {
        txt.appendChild(document.createTextNode(lines[i]));
        txt.appendChild(document.createElement('br'));
    }
    
    
    titleTxt  = document.createTextNode(title);
    
    h1.appendChild(titleTxt);
    
    switch (type) {
        case 'success':
            
            h1.id = 'success';
            btn = document.createElement('input');
            btn.type = 'button';
            btn.value = msg_ok;
            btn.id = 'focusbtn';
            btn.onclick = function() {
                hidePrompt();
                if (hook != undefined) {
                    eval(hook);
                }
            }
            divPrompt.appendChild(h1);
            divPrompt.appendChild(txt);
            divPrompt.appendChild(document.createElement('br'));
            divPrompt.appendChild(document.createElement('br'));
            divPrompt.appendChild(btn);
            
            setTimeout('hidePrompt()', 4000);
        
        break;
        
        case 'error':
            
            h1.id = 'error';
            btn = document.createElement('input');
            btn.type = 'button';
            btn.value = msg_ok;
            btn.id = 'focusbtn';
            btn.onclick = function() {
                hidePrompt();
                if (hook != undefined) {
                    eval(hook);
                }
            }
            divPrompt.appendChild(h1);
            divPrompt.appendChild(txt);
            divPrompt.appendChild(document.createElement('br'));
            divPrompt.appendChild(document.createElement('br'));
            divPrompt.appendChild(btn);
        
        break;
        
        case 'prompt':
            
            h1.id = 'info';
            okbtn = document.createElement('input');
            okbtn.type = 'button';
            okbtn.value = msg_ok;
            okbtn.tabIndex = 1;
            okbtn.onclick = function() {
                hidePrompt();
                if (hook != undefined) {
                    eval(hook);
                }
            }
            cancelbtn = document.createElement('input');
            cancelbtn.type = 'button';
            cancelbtn.value = msg_cancel;
            cancelbtn.id = 'focusbtn';
            cancelbtn.onclick = function() {
                hidePrompt();
                if (hook2 != undefined) {
                    eval(hook2);
                }
            }
            divPrompt.appendChild(h1);
            divPrompt.appendChild(txt);
            divPrompt.appendChild(document.createElement('br'));
            divPrompt.appendChild(document.createElement('br'));
            divPrompt.appendChild(okbtn);
            divPrompt.appendChild(cancelbtn);
        
        break;
        
        default:
            
            h1.id = 'info';
            btn = document.createElement('input');
            btn.type = 'button';
            btn.value = msg_ok;
            btn.id = 'focusbtn';
            btn.onclick = function() {
                hidePrompt();
                if (hook != undefined) {
                    eval(hook);
                }
            }
            divPrompt.appendChild(h1);
            divPrompt.appendChild(txt);
            divPrompt.appendChild(document.createElement('br'));
            divPrompt.appendChild(document.createElement('br'));
            divPrompt.appendChild(btn);
            
            setTimeout('hidePrompt()', 3000);
            
         break;
        
    }
    
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        winWidth = window.innerWidth;
        winHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        winWidth = document.documentElement.clientWidth;
        winHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        winWidth = document.body.clientWidth;
        winHeight = document.body.clientHeight;
    }
    
    divPrompt.id = 'divprompt';
    divPrompt.style.width = '300px';

    promptContainer = document.createElement('div');
    promptContainer.id = 'promptcontainer';
    promptContainer.className = 'dragme';
    promptContainer.style.position = 'absolute';
    promptContainer.style.top = winHeight / 2 + 'px';
    promptContainer.style.left = winWidth / 2 + 'px';
    
    promptContainer.style.width = '325px';
    promptContainer.style.marginLeft = '-150px';
    promptContainer.appendChild(divPrompt);
    
    document.body.appendChild(promptContainer);
    
    getPrompt = document.getElementById('promptcontainer');
    getPrompt.style.marginTop = '-' + getPrompt.offsetHeight + 'px';
    getPrompt.style.height = getPrompt.offsetHeight + 10 + 'px';
    document.getElementById('main').style.opacity = '.30';
    document.getElementById('main').style.filter = 'alpha(opacity = 30)';

    if (document.getElementById('focusbtn').nodeName == 'input') { document.getElementById('focusbtn').focus(); }

}