/***************************************************
//                                                           //
//  Modal CSS Dialog Javascript Code                         //
//  Copyright© BrandsPatch LLC                               //
//  http://www.explainth.at                                  //
//                                                           //
//  All Rights Reserved                                      //
//                                                           //
//  Permission is granted to use, modify and redistribute    //
//  this code on the condition that this notice is retained  //
//  unchanged.                                               //
//                                                           //
//  BrandsPatch  declines all responsibility for any losses, //
//  direct or indirect, that may arise  as a result of using //
//  this code.                                               //
**************************************************************/
var CRLF = String.fromCharCode(13) + String.fromCharCode(10);

function CloseDialog() {
    var main = document.getElementById('main');
    var e = document.getElementById('overlay');
    if (e) {
        main.removeChild(e);
    }
    e = document.getElementById('diadiv');
    if (e) {
        main.removeChild(e);
    }
}

function BuildImageButton(ACaption, AClick, AId, ASrc)
{
 var buf = new StringBuffer();
 buf.xAppend(['<input type="image" src="', ASrc, '" id="', AId, '" alt="',
              ACaption, '" onclick="', AClick, ';return false;" ',
              'onmouseover="buttondown(\'', AId, '\')" onmouseout="buttonup(\'',
              AId, '\')"/>']);
 return buf.toString();
}

function BuildButton(ACaption, AClick)
{
 var buf = new StringBuffer();
 buf.xAppend(['<button ','onclick="javascript:',AClick,'">',ACaption,'</button>']);
 return buf.toString();
}

function BuildHRef(ARef)
{
 var buf = new StringBuffer();
 buf.xAppend(['href="javascript:',ARef,'"']);
 return buf.toString();
}

function BuildLink(AId,AClass,ACapt,ARef)
{
 var buf = new StringBuffer();
 buf.xAppend(['<a ',BuildHRef(ARef),' id="',AId,'">',ACapt,'</a>']);
 return buf.toString();
}

function BuildSpan(AId,AClass,AText)
{
 var buf = new StringBuffer();
 buf.xAppend(['<span class="',AClass,'" id="',AId,'">',AText,'</span>']);
 return buf.toString();
}

function BuildDiv(AId,AClass,AContents)
{
 var buf = new StringBuffer();
 buf.xAppend(['<div ','class="',AClass,'" id="',AId,'">',CRLF,AContents,CRLF,'</div>']);
 return buf.toString();
}

function BuildPara(AId,AClass,AContents)
{
 var buf = new StringBuffer();
 buf.xAppend(['<p ','class="',AClass,'" id="',AId,'">',CRLF,AContents,CRLF,'</p>']);
 return buf.toString();
}

function ShowDialog(pText, goToHome)
{
 var newdiv = document.createElement('div');
 var main = document.getElementById('main');
 newdiv.className = 'overlay';
 newdiv.id = 'overlay';
 var h = document.body.clientHeight + 'px';
 newdiv.style.height = h;
 var w = document.body.clientWidth - 40 + 'px';
 newdiv.style.width = w;
 main.appendChild(newdiv);
 
 newdiv = document.createElement('div');
 newdiv.className = 'diadiv';
 newdiv.id = 'diadiv';
 newdiv.style.height = h;
 newdiv.style.width = w;
 newdiv.innerHTML = MakeDialog(pText, goToHome);
 main.appendChild(newdiv); 
 AdjustSizes();
}

function AdjustSizes()
{
 var d = document.getElementById('dlg');
 if (d == null) return;
 var b = document.getElementById('dbody');
 var c = document.getElementById('capt');
 var t = document.getElementById('dtxt');
 var emPerpxX = 25/d.offsetWidth;
 var emPerpxY = 13/d.offsetHeight;
 
 b.style.height = (d.offsetHeight - c.offsetHeight)*emPerpxY + 'em';
 var w = d.offsetWidth*emPerpxX + 'em';
 b.style.width = w;
 c.style.width = w;
 t.style.height = b.offsetHeight*emPerpxY - 2 + 'em';
 t.style.width = (b.offsetWidth - 101)*emPerpxX + 'em';
}

function MakeDialog(pText, goToHome)
{
 var buf = new StringBuffer();
 buf.xAppend([BuildLink('close','diaqbut','x','CloseDialog()'),
              BuildLink('help','diaqbut','?',"alert('You need help?')"),
              BuildSpan('caption','capt','Session Timeout')]);
 var s = buf.toString();
 buf.cleanUp();
 var capt = BuildDiv('capt','diacapt',s);
 
 var okFunction;
 if (goToHome) {
     okFunction = "window.location.href='/merlot/index.htm'";
 } else {
     okFunction = "CloseDialog()";
 }
 s  = BuildPara('para', 'diapara', pText) + 
      BuildDiv('btns', 'diaclose', 
               BuildImageButton('OK', okFunction, 
                                'sessionokbutton',
                                '/merlot/images/buttons/btn_ok.gif'));
 s =    BuildDiv('dtxt','diatxt',s) + 
 BuildDiv('dimg','diaimg',
          '<img width="66" height="66" src="/merlot/images/stopwatch.jpg"');
 s = BuildDiv('dbody','diabody',s);
 return BuildDiv('dlg','diaframe',capt + s);
}
/**********************************************************************
/
/
/    StringBuffer is a simple utility object which delivers
/     faster string concatenation than the Javascript + operator
/
***********************************************************************/
function StringBuffer() {this.buffer = []} 
StringBuffer.prototype.append = function append(string) {this.buffer.push(string)} 
StringBuffer.prototype.xAppend = 
function xAppend(strings)
{
 var i,ALen;
 ALen = strings.length;
 for (i=0;i<ALen;i++) this.buffer.push(strings[i]);
}
StringBuffer.prototype.cleanUp = function cleanUp(){this.buffer = []}
StringBuffer.prototype.toString = function toString(){return this.buffer.join("")}
  

