﻿//------------------------------------------------------------------------------------------
function popupShowWait(msg)
{
   var el, s;
   
   el = _showShield('waitBox', 'popInfo', (msg.length > 400 ? '600px' : '300px'), (msg.length > 400 ? '500px' : '100px'), new Array());
   s = '<table cellSpacing="0" cellPadding="2"><tr><td>&nbsp;<img src="./images/wait.png">&nbsp;</td><td>' + msg + '</td></tr></table>';
   el.innerHTML = s;
}
//------------------------------------------------------------------------------------------
function popupHideWait()
{
   popupHide('waitBox');
}
//------------------------------------------------------------------------------------------
function popupShowInfo(msg)
{
   var el, s;
   
   el = _showShield('popInfo', 'popInfo', (msg.length > 400 ? '600px' : '300px'), (msg.length > 400 ? '500px' : '100px'));
   s = '<table cellSpacing="0" cellPadding="2"><tr><td>&nbsp;<img src="./images/info.png">&nbsp;</td><td>' + msg + '</td></tr></table>';
   el.innerHTML = s;
}
//------------------------------------------------------------------------------------------
function popupShowError(msg)
{
   var el, s;
   
   el = _showShield('errorMsg', 'popError', (msg.length > 400 ? '600px' : '300px'), (msg.length > 400 ? '500px' : '100px'));
   s = '<table cellSpacing="0" cellPadding="2"><tr><td>&nbsp;<img src="./images/error.png">&nbsp;</td><td>' + msg + '</td></tr></table>';
   el.innerHTML = s;
}
//------------------------------------------------------------------------------------------
function popupAskQuestion(msg, onYes)
{
   var el;
   
   el = _showShield('questionMsg', 'popQuestion', '300px', '100px', new Array(resource.buttonYes, 'popupHide("questionMsg"); ' + onYes, resource.buttonNo, 'popupHide("questionMsg");'));
   s = '<table cellSpacing="0" cellPadding="2"><tr><td>&nbsp;<img src="./images/question.png">&nbsp;</td><td>' + msg + '</td></tr></table>';
   el.innerHTML = s;
}
//------------------------------------------------------------------------------------------
function popupBox(id, url, prompt, width, height, buttons)
{
   var el, s, pr;
   
   el = _showShield('popup'+id, 'popStd', width, height, buttons);
   

   s = '<iframe style="height: ' + el.style.height + '; width: 100%; display:block; background-color: transparent" name="dialogFrame' + id + '" frameBorder="0"></iframe>';
   el.innerHTML = s;
   
   // Set busy note.
   if(prompt) {
      pr = prompt;
   }
   else {
      pr = resource.promptWorkingPleaseWait;
   }
   _popupSetHtml(id, "<table style='font-family: Arial; font-size: x-small;'><tr><td>&nbsp;</td></tr><tr><td>&nbsp;&nbsp;<img alt='' align='absmiddle' src='./images/working3.gif' />&nbsp;" + pr + "</td></tr></table>");
   
   miscOpenWindow(url, "dialogFrame" + id);
}
//------------------------------------------------------------------------------------------
function popupBoxAjax(id, ajaxReq, prompt, width, height, buttons)
{
   var el, s, pr;
   
   ajaxReq.callback = popupBoxAjaxHandler;
   ajaxReq.context = {contentId:id};
   
   el = _showShield('popup'+id, 'popStd', width, height, buttons);

   s = '<span id="dialogFrame' + id + '"></span>';
   el.innerHTML = s;
   
   // Set busy note.
   if(prompt) {
      pr = prompt;
   }
   else {
      pr = resource.promptWorkingPleaseWait;
   }
   _popupSetDivHtml(id, "<table style='font-family: Arial; font-size: x-small;'><tr><td>&nbsp;&nbsp;</td></tr><tr><td>&nbsp;&nbsp;<img alt='' align='absmiddle' src='./images/working3.gif' />&nbsp;" + pr + "</td></tr></table>");
   
   ajaxReq.execute();
}
function popupBoxAjaxHandler(context, ar)
{
   if(ar.js) {
      ar.evaluateJs();
   }
   if(ar.html && ar.html.length > 0 && context.contentId) {
      _popupSetDivHtml(context.contentId, ar.html);
   }
   else if(ar.response && ar.response.length > 0 && context.contentId) {
      _popupSetDivHtml(context.contentId, ar.response);
   }
}
//------------------------------------------------------------------------------------------
function popupBoxHtml(id, html)
{
   var el, s, pr;
   
   el = _showShield('popup'+id, 'popStd', '50%', '50%');

   s = '<iframe style="width: 100%; height: 100%; background-color: transparent" name="dialogFrame' + id + '" src="about:blank" frameBorder="no"></iframe>';
   el.innerHTML = s;
   
   _popupSetHtml(id, html);
}
//------------------------------------------------------------------------------------------
function popupHide(id)
{
   try {
      var mainFrm = $get('mainForm');
      var div = $get('dialogFrame' + id);
      if(div) {
         div.src = "about:blank";
      }
      var div = $get('popup' + id);
      if(div) {
         mainFrm.removeChild(div);
      }
      div = $get(id);
      if(div) {
         mainFrm.removeChild(div);
      }
   }
   catch(ex) {
   }
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function _showShield(id, clsName, width, height, buttons)
{
   var div, tbl1, tbl, tr, tr2, td, mainFrm, contentCell;
   var h, r2Height, maxHeight, maxWidth;
   var row;
   
   if(!buttons) {
      buttons = new Array();
      buttons.push(resource.buttonClose);
      buttons.push('popupHide("'+id+'")');
   }
   
   maxHeight = miscClientHeight() - 100;
   maxWidth = miscClientWidth() - 100;

   if(!width) {
      width = maxWidth;
   }
   else {
      // Convert to value.
      if(width.endsWith('%')) {
         h = parseInt(width);
         width = maxWidth * (h / 100.0);
      }
   }
   if(!height) {
      height = maxHeight;
   }
   else {
      // Convert to value.
      if(height.endsWith('%')) {
         h = parseInt(height);
         height = maxHeight * (h / 100.0);
      }
   }
   
   // See if window is already open.
   mainFrm = $get('mainForm');
   div = $get(id);
   if(div) {
      popupHide(id);
   }
   if(mainFrm) {
      h = Math.min(parseInt(height), maxHeight);
      r2Height = h + 'px';
      tbl1 = document.createElement('table');
      tbl1.className = 'popTbl';
      tbl1.id = id;
      tbl1.style.left = '0px';
      tbl1.style.top = '0px';
      tbl1.style.zIndex = 10000;
      tbl1.cellSpacing = '0px';
      tbl1.cellPadding = '0px';
      if(mainFrm) {
         mainFrm.appendChild(tbl1);
      }
      
      row = 0;
      
      // Content row
      tr2 = tbl1.insertRow(row++);
      td = tr2.insertCell(0);
      td.className = clsName;
      td.innerHTML = '&nbsp;';
      contentCell = tr2.insertCell(1);
      contentCell.style.width = width;
      contentCell.className = clsName;
      td = tr2.insertCell(2);
      td.className = clsName;
      td.innerHTML = '&nbsp;';
     
      // Content table
      tbl = document.createElement('table');
      tbl.style.width = '100%';
      tbl.style.height = r2Height;
      tbl.style.zIndex = 10000;
      tbl.cellSpacing = '0px';
      tbl.cellPadding = '0px';
      contentCell.appendChild(tbl);

      row = 0;
      tr = tbl.insertRow(row++);
      contentCell = tr .insertCell(0);
      contentCell.innerHTML = "";
      contentCell.className = 'popBox';
      contentCell.style.height = tbl.style.height;

      if(buttons && buttons != "none") {
         tr = tbl.insertRow(row++);
         td = tr.insertCell(0);
         td.style.height = '25px';
         td.className = 'popBoxBut';
         td.innerHTML = _makeButtons(id, buttons);
      }
      
      if(tbl.offsetHeight > tbl1.offsetHeight) {
         tbl.style.height = maxHeight;
      }
      
      contentCell.focus();
      return (contentCell);    
   }
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function _makeButtons(id, buttons)
{
   var s;
   
   s  = '<table width="100%" class="popButRow" cellpadding="0" cellspacing="0"><tr class="hdrBtnRow"><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
   for(var i = 0; i < buttons.length; i += 2) {
      s += '<td class="hdrBtnLt">&nbsp;</td><td width="75px" class="hdrBtnMid" onclick=\'' + (buttons[i+1] == null ? 'popupHide("' + id + '")' : buttons[i+1]) + '\'>' + buttons[i] + '</td><td class="hdrBtnRt"></td>';
      if(i+1 < buttons.length) {
         s += '<td width="10px">&nbsp;</td>';
      }
   }
   s += '<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>';
   
   return (s);
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function _popupSetDivHtml(id, html)
{
   var div;

   div = $get('dialogFrame' + id);
   if(div) {
      div.innerHTML = html;
   }
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function _popupSetHtml(id, html)
{
   var doc, div, s, frm;

   try {
      frm = window.frames['dialogFrame' + id];
      if(frm.document) {
         doc = frm.document;
      }
      else if(frm.contentDocument) {
         doc = frm.contentDocument;
      }
      else {
         return;
      }
      
      if(! doc.body) {
         div = doc.createElement('div');
         div.style.visibility = "visible";
         div.innerHTML = html;
         doc.appendChild(div);
      }
      else if(! doc.body.innerHTML) {
         div = doc.createElement('div');
         div.style.visibility = "visible";
         div.innerHTML = html;
         doc.body.appendChild(div);
      }
      else {
         doc.body.innerHTML = html;
      }
   }
   catch(ex) {alert(ex);}
}
