var _dialogPromptID=null;
var _blackoutPromptID=null;

document.onkeydown=function(e){
    !e?e=window.event:null;
    if(e.keyCode==27){
        closePopUp(e.keyCode);
        // IE and compatible
        e.cancelBubble = true;
        e.returnValue = false;
        // FF and compatible
        if(e.stopPropagation){
            e.stopPropagation();
            e.preventDefault();
        }
    }
    if(e.keyCode==121){
        //alert("Trace me");
        dialog('div', false, false, true, "Enable Debug", "Password/Log", false, buttons, 0, enableDebug, true, def);
    }
}

function closePopUp(){
    if(_dialogPromptID && _blackoutPromptID){
        _dialogPromptID.style.display='none';
        _blackoutPromptID.style.display='none';
        pf = document.getElementById('IEPromptField');
        if(pf){
            pf.value = '';
        }
    }
}

function IEprompt(innertxt,def,cb_func,arg1) {
    var buttons = new Array("OK", "Cancel");
    this.wrapCallBack = function(button_id, val){
        //alert("wrap callback button:" + button_id + ", val:"+ val);
        if(button_id == "0" || button_id == 0){
            //alert("button == 0");
            cb_func(arg1, val);
        }
        return true;
    }
    return dialog('div', false, false, true, "Input Required", innertxt, false, buttons, 0, this.wrapCallBack, true, def);
}

/* mode = div or iframe
 * height = height of the dialog
 * width = width of the dialog
 * title = t/f show or not
 * title_text = text in title bar
 * content = url for an iframe or text for a div
 * modal = t/f, allow close by clicking elsewhere on screen or not
 * buttons = only used for div, array of buttons (button index passwd to close_cb)
 * def_button = button to use onsubmit (enter on a form field)
 * close_cb = js to execute on close
 * input = t/f, is this just a simple input
 * def = default - used with input
 */
function dialog(mode, height, width, title, title_text, content, modal, buttons, def_button, close_cb, input, def) {
    that=this;
    this.wrapupPrompt = function(button_id){
        if(input){
            val=document.getElementById('IEPromptField').value;
        }
        if(close_cb){
            if(buttons && buttons.length > 0 && input){
                ret = close_cb(button_id, val);
            }else if(buttons && buttons.length > 0){
                ret = close_cb(button_id);
            }else if(input){
                ret = close_cb(val);
            }else{
                ret = close_cb();
            }
        }
        if(ret){
            this.hideDialog();
        }
        return false;
    }

    this.hideDialog = function(){
        closePopUp();
    }

    //if def wasn't actually passed, initialize it to null
    if(def==undefined){
        def='';
    }
    if(!buttons && input){
        buttons = new Array("OK", "Cancel");
    }
    if (_dialogPromptID==null) {
        var tbody = document.getElementsByTagName("body")[0];
        tnode = document.createElement('div');
        tnode.id='IEPromptBox';
        tbody.appendChild(tnode);
        _dialogPromptID=document.getElementById('IEPromptBox');
        // Create a new division (blackout)
        tnode = document.createElement('div');
        tnode.id='IEPromptBlackout';
        tbody.appendChild(tnode);
        _blackoutPromptID=document.getElementById('IEPromptBlackout');
    }

        _dialogPromptID=document.getElementById('IEPromptBox');
        _blackoutPromptID=document.getElementById('IEPromptBlackout');

        // assign the styles to the blackout division.
        _blackoutPromptID.style.position='absolute';
        _blackoutPromptID.style.top='0px';
        _blackoutPromptID.style.left='0px';
        _blackoutPromptID.style.height=(document.body.offsetHeight<screen.height) ? screen.height+'px' : document.body.offsetHeight+20+'px'; 
        _blackoutPromptID.style.display='block';
        _blackoutPromptID.style.zIndex='50';
        if(!modal){
            _blackoutPromptID.onclick = this.hideDialog;
        }
        // assign the styles to the dialog box
        _dialogPromptID.innerHTML = "";
        _dialogPromptID.style.position='absolute';
        _dialogPromptID.style.overflow='hidden';
        _dialogPromptID.style.padding='0px';
        _dialogPromptID.style.margin='0px';
        if(width != false){
            _dialogPromptID.style.width=width;
        }else{
            _dialogPromptID.style.width='300px';
        }
        if(height != false){
            _dialogPromptID.style.height=height;
        }else{
            _dialogPromptID.style.height="130px";
        }
        _dialogPromptID.style.zIndex='100';

    var tmp = '';
    if(mode == 'div'){
        if(title){
            tmp = '<div class="IEPromptTitle"><div class="IEPromptTitleLeft">'+title_text+'</div><div class="IEPromptTitleRight"><a href="javascript:closePopUp()">Close</a></div></div>';
        }
        tmp += '<div id="IEPromptDivContent">'+content+'<BR><BR>';
        if(def_button.length > 0){
            tmp += '<form action="#" onsubmit="return that.wrapupPrompt('+def_button+')?true:true;">';
        }else{
            tmp += '<form action="#" onsubmit="return false;">';
        }
        tmp += '<input id="IEPromptField" name="iepromptdata" type=text size=10 value="'+def+'" class="valid">';
        tmp += '<br><br><center>';
        for(var i = 0; i < buttons.length; i++){
            tmp += '<input type="button" onclick="that.wrapupPrompt('+i+')" value="&nbsp;'+buttons[i]+'&nbsp;">';
        }
        tmp += '</form></div>';
    }else if(mode == 'iframe'){
        diahgt = parseInt(_dialogPromptID.style.height,10);
        tmp = "";
        if(title){
            diahgt -= 20;
            //tmp += '<div class="IEPromptTitle">'+title_text+'</div>';
            tmp = '<div class="IEPromptTitle"><div class="IEPromptTitleLeft">'+title_text+'</div><div class="IEPromptTitleRight"><a href="javascript:closePopUp()">Close</a></div></div>';
        }
        tmp += '<div style="padding: 10px 10px 10px 10px; height: '+diahgt+'px;">';
        tmp += '<iframe src="'+content+'" width="100%" height="'+(diahgt-20)+'"></iframe>';
        tmp += '</div>';
    }
    
    // Stretch the blackout division to fill the entire document
    // and make it visible.  Because it has a high z-index it should
    // make all other elements on the page unclickable.
    _blackoutPromptID.style.height=(document.body.offsetHeight<screen.height) ? screen.height+'px' : document.body.offsetHeight+20+'px'; 
    _blackoutPromptID.style.width='100%';
    _blackoutPromptID.style.display='block';
    // Insert the tmp HTML string into the dialog box.
    // Then position the dialog box on the screen and make it visible.
    _dialogPromptID.innerHTML=tmp;
    
    if(Boolean(window.chrome)){
        _dialogPromptID.style.top=parseInt(document.body.scrollTop+(screen.height/3))+'px';
    }else{
        _dialogPromptID.style.top=parseInt(document.documentElement.scrollTop+(screen.height/3))+'px';
    }
    
    _dialogPromptID.style.left=parseInt((document.body.offsetWidth-315)/2)+'px';
    _dialogPromptID.style.display='block';
    // Give the dialog box's input field the focus.
    if(input){
        input_field = document.getElementById('IEPromptField');
        if(input_field){
            input_field.focus();
            input_field.select();
        }
    }
}

