///////////////////////////////////////////////////////////
// Usage IEprompt("dialog descriptive text", "default starting value");
// 
// IEprompt will call promptCallback(val)
// Where val is the user's input or null if the dialog was canceled.
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////
// These are global scope variables, they should remain global.
///////////////////////////////////////////////////////////
//var _dialogPromptID=null;
var _blackoutPromptID=null;
var _HTMLDialog = null;
///////////////////////////////////////////////////////////

function promptCallback(postbackurl, val)
{
  __doPostBack(postbackurl, val);
}

function IEprompt(innertxt,def,postbackurl) {
  
    that=this;
    
    //if def wasn't actually passed, initialize it to null
    if (def==undefined) { def=''; }

    this.wrapupPrompt = function (canceled) {      
     
     var val=document.getElementById('iepromptfield').value; 
     
     HideWindow();          
      _blackoutPromptID.style.display='none';
     
     // call the user's function if the not canceled
     if (!canceled) { promptCallback(postbackurl, val); }
     
     return canceled;
    }

    // This is the HTML which makes up the dialog box, it will be inserted into
    // innerHTML later. We insert into a temporary variable because
    // it's very, very slow doing multiple innerHTML injections, it's much
    // more efficient to use a variable and then do one LARGE injection.
    var tmp = '';    
    tmp += '<form action="" onsubmit="return that.wrapupPrompt(false)">';
    tmp += innertxt + '<BR><BR>';
    tmp += '<input id="iepromptfield" name="iepromptdata" type=text size=40 value="'+def+'">';
    tmp += '<br><br><center>';
    tmp += '<input type="submit" value="&nbsp;&nbsp;&nbsp;OK&nbsp;&nbsp;&nbsp;">';
    tmp += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    tmp += '<input type="button" onclick="that.wrapupPrompt(true)" value="&nbsp;Cancel&nbsp;">';
    tmp += '</form>';

    GetPromptBlackout();

    ShowHTMLInWindow(tmp, "Input Required", false, 300);
    
    // Give the dialog box's input field the focus.
    document.getElementById('iepromptfield').focus();
}

function ShowHTMLInOKDialog(html, title, width) {
  
    // This is the HTML which makes up the dialog box, it will be inserted into
    // innerHTML later. We insert into a temporary variable because
    // it's very, very slow doing multiple innerHTML injections, it's much
    // more efficient to use a variable and then do one LARGE injection.
    var tmp = '';
    tmp += '<form style="margin:0px;padding:0px" action="" onsubmit="HideWindow(); return false;">';
    tmp += html;
    tmp += '<div style="padding-top: 5px; margin: 0px"><center>';
    tmp += '<input type="submit" value="&nbsp;&nbsp;&nbsp;OK&nbsp;&nbsp;&nbsp;">';
    tmp += '</center>';
    tmp += '</div>';
    tmp += '</form>';

    ShowHTMLInWindow(tmp, title, false, width);
}

function ShowHTMLInDialog(html, title, width, postbackurl) {
  
    ShowHTMLInDialogEx(html, title, width, true, postbackurl, '');
}

function ShowHTMLInDialogEx(html, title, width, showCancelBtn, postbackurl, postbackdata) {
  
    that=this;

    this.wrapupPrompt = function (canceled) {      
          
     HideWindow();    
     // call the user's function if the not canceled
     if (!canceled) { promptCallback(postbackurl, postbackdata); }
     
     return canceled;
    }

    // This is the HTML which makes up the dialog box, it will be inserted into
    // innerHTML later. We insert into a temporary variable because
    // it's very, very slow doing multiple innerHTML injections, it's much
    // more efficient to use a variable and then do one LARGE injection.
    var tmp = '';
    tmp += '<form style="margin:0px;padding:0px" action="" onsubmit="return that.wrapupPrompt(false)">';
    tmp += html;
    tmp += '<div style="padding-top: 5px; margin: 0px"><center>';
    tmp += '<input type="submit" value="&nbsp;&nbsp;&nbsp;OK&nbsp;&nbsp;&nbsp;">';
    if(showCancelBtn)
    {
      tmp += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
      tmp += '<input type="button" onclick="that.wrapupPrompt(true)" value="&nbsp;Cancel&nbsp;">';
    }
    tmp += '</center>';
    tmp += '</div>';
    tmp += '</form>';

    ShowHTMLInWindow(tmp, title, false, width);
}

function ShowHTMLInWindow(html, title, showCloseBtn, width)
{
  // If this is MSIE 7.0 then...
  if (_HTMLDialog==null) {
     _HTMLDialog = CreateDialogPrompt('DescriptionDiv');
  }
  // This is the HTML which makes up the dialog box, it will be inserted into
  // innerHTML later. We insert into a temporary variable because
  // it's very, very slow doing multiple innerHTML injections, it's much
  // more efficient to use a variable and then do one LARGE injection.
  var tmp = '';
  tmp += '<div style="width: 100%;background-color: blue; color: white; font-family: verdana; font-size: 10pt; font-weight: bold; height: 20px">';
  tmp += '<div style="float:left">' + title + '</div>'; 
  if(showCloseBtn) {
    tmp += '<div onclick="HideWindow();" style="height: 16px; width: 16px; float:right; cursor:pointer">X</div>'; 
  }
  tmp += '</div>';
  tmp += '<div style="padding: 5px; padding-left: 10px; margin: 0px;">' + html + '</div>';

  // Insert the tmp HTML string into the dialog box.
  // Then position the dialog box on the screen and make it visible.
  _HTMLDialog.innerHTML=tmp;
  _HTMLDialog.style.width=width + 'px';  
  _HTMLDialog.style.display='block';  
  
  _HTMLDialog.style.top=parseInt( document.documentElement.scrollTop + (document.documentElement.clientHeight-_HTMLDialog.clientHeight)/2)+'px';
  _HTMLDialog.style.left=parseInt((document.body.offsetWidth-_HTMLDialog.scrollWidth)/2)+'px';  
  
  hideSelects();
}

function HideWindow()
{  
  if( _HTMLDialog == null ) return;
  
  Hide(_HTMLDialog.id);  
  unhideSelects();
}

function CreateDialogPrompt(promptID)
{
   // Check to see if we've created the dialog divisions.
   // This block sets up the divisons
   // Get the body tag in the dom
   var tbody = document.getElementsByTagName("body")[0];
   // create a new division
   tnode = document.createElement('div');
   // name it
   tnode.id=promptID;
   // attach the new division to the body tag
   tbody.appendChild(tnode);
   // and save the element reference in a global variable
   var dialogPromptID=document.getElementById(promptID);
   // assign the styles to the dialog box
   dialogPromptID.style.border='2px solid blue';
   dialogPromptID.style.backgroundColor='#DDDDDD';
   dialogPromptID.style.position='absolute';
   dialogPromptID.style.width='300px';
   dialogPromptID.style.zIndex='100';
   dialogPromptID.style.margin='0px';
   dialogPromptID.style.padding='0px';
   
   return dialogPromptID;
}

function GetPromptBlackout()
{
  if (_blackoutPromptID==null) {
    // Check to see if we've created the dialog divisions.
    // This block sets up the divisons
    // Get the body tag in the dom
    var tbody = document.getElementsByTagName("body")[0];
    // Create a new division (blackout)
    tnode = document.createElement('div');
    // name it.
    tnode.id='promptBlackout';
    // attach it to body.
    tbody.appendChild(tnode);
    // And get the element reference
    _blackoutPromptID=document.getElementById('promptBlackout');
    // assign the styles to the blackout division.
    _blackoutPromptID.style.opacity='.9';
    _blackoutPromptID.style.position='absolute';
    _blackoutPromptID.style.top='0px';
    _blackoutPromptID.style.left='0px';
    _blackoutPromptID.style.backgroundColor='#555555';
    _blackoutPromptID.style.filter='alpha(opacity=90)';
    _blackoutPromptID.style.height=(document.body.offsetHeight<screen.height) ? screen.height+'px' : document.body.offsetHeight+20+'px'; 
    _blackoutPromptID.style.display='block';
    _blackoutPromptID.style.zIndex='50';
  }
  
  // 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';
  
  return _blackoutPromptID;
}

/*
document.onmousemove = getMouseXY; // update(event) implied on NS, update(null) implied on IE
var mousex;
var mousey;
function MoveWindow(e)
{
  if( _CtrlForHTMLDialog == null ) return;

  getMouseXY(e);
  _HTMLDialog.style.display='block';
  _HTMLDialog.style.left = mousex + 10;
  _HTMLDialog.style.top = mousey + 10;
}

function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }  
  }
}
*/