var BoxFrame = '<div id="SubWindow" style="display: fixed; top: 10px; left: 10px; width: %width%px;">'
+'<table cellspacing="0" cellpadding="2" width="%width%px" style="border: 1px solid black">'
+'  <tr style="border-bottom: 1px solid black; color: #ffffff" height="20">'
+'    <td style="background: #65738E" width="5px">&nbsp;</td>'
+'    <td style="background: #65738E" id="SubWindowTitle" onmousedown="HandleMouseDown(this)" onmouseup="HandleMouseUp(this)"><b>%title%</b></td>'
+'    <td style="background: #65738E" width="5px" align="left">'
+'      <img src="/images/closebutton.gif" onclick="CloseBox()" style="cursor: pointer"/>'
+'    </td>'
+'  </tr>'
+'  <tr height="%height%px" valign="top" style="border: 1px solid black; background-color: white">'
+'    <td>&nbsp;</td>'
+'    <td align="left">'
+'      <div id="SubWindowHTML" onRemoteDone="CenterBox(\'SubWindow\',0)">&nbsp;</div>'
+'    </td>'
+'    <td>&nbsp;</td>'
+'  </tr>'
+'</table>'
+'</div>';

var MouseX = 0;
var MouseY = 0;
var DragId = null;

function SetImageSwap(AImg,OverSrc){
  var OutSrc = AImg.src;
  AImg.onmouseover = function(){
    AImg.src = OverSrc;
  }
  AImg.onmouseout = function(){
    AImg.src = OutSrc;
  }
  AImg.src = OverSrc;
}

function MenuOver(AMenu,AId){
  var o = document.getElementById('SubMenu'+AId);
  o.style.display='';
  o.style.position = 'absolute';
}
function MenuOut(AMenu, AId){
  var o = document.getElementById('SubMenu'+AId);
  o.style.display='none';
}
function SubMenuOver(AItem){
  AItem.style.color = '#ffffff';
  AItem.style.backgroundColor = "#486c00"; //"#728617";
}
function SubMenuOut(AItem){
  AItem.style.color = 'black';
  AItem.style.backgroundColor = "#e3e3e3";  
}

function CenterBox(AId,AInterval){
  var e = document.getElementById(AId);
  if (!e)
    return;
  if (e.style.display == 'none')
    return;
  if (e.id == DragId)
    return;
  var w, h;
  if (window.innerWidth){
    w = window.innerWidth;
    h = window.innerHeight;
  }else{
    if (document.documentElement && document.documentElement.clientWidth){
      w = document.documentElement.clientWidth;
      h = document.documentElement.clientHeight;
    } else {
      w = document.body.offsetWidth;
      h = document.body.offsetHeight;
    }
  }
  var x = w/2 - e.clientWidth/2 + (document.body.scrollLeft + document.documentElement.scrollLeft);
  var y = h/2 - e.clientHeight/2 + (document.body.scrollTop + document.documentElement.scrollTop);
  if (x < 0)
    x = 0;
  if (y < 0)
    y = 0;
  if ((AInterval) && (AInterval > 0) && (AInterval < 1000)){
    var xx = parseInt(e.style.left);
    var yy = parseInt(e.style.top);
    x = (x - xx)/(1000/AInterval) + xx;
    y = (y - yy)/(1000/AInterval) + yy;
  }
  e.style.left = x+'px';
  e.style.top = y+'px';
  if ((AInterval) && (AInterval > 0)){
    setTimeout("CenterBox('"+AId+"',"+AInterval+")",AInterval);
  }
}

function HandleMouseUp(e){
  DragId = null;
}

function HandleMouseDown(e){
  e.onMouseUp =  function(){HandleMouseUp(e)}
  var w = document.getElementById('SubWindow');
  //Get current xy
  var CurX = parseInt(w.offsetLeft);
  var CurY = parseInt(w.offsetTop);
  
  var OfsX = CurX - MouseX;
  var OfsY = CurY - MouseY;
  
  w.setAttribute('OfsX',OfsX);
  w.setAttribute('OfsY',OfsY);
  DragId = w.id;
}

function HandleMouseMove(e){
  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;
    }
  }
  if (DragId){
    var w = document.getElementById(DragId);
    if (w){
      if (w.style.display == 'none'){
        DragId = null
      } else {
        var OfsX = parseInt(w.getAttribute('OfsX'));
        var OfsY = parseInt(w.getAttribute('OfsY'));
        w.style.left = (MouseX + OfsX)+'px';
        w.style.top = (MouseY + OfsY)+'px';
      }
    }
  }
}

function CloseBox(){
  document.onmousemove = null;
  var e = document.getElementById('SubWindow');
  if (!e)
    return;
  e.style.display = 'none';
}

function MakeBox(AWidth,AHeight,ATitle){
  if (!AWidth)
    AWidth = 400;
  if (!AHeight)
    AHeight = 200;
  //Remove The Old Dialog
  var e = document.getElementById('SubWindow');
  if (e)
    e.parentNode.removeChild(e);
  //Create The New Dialog
  var s = BoxFrame;
  s = s.replace('%title%',ATitle);
  s = s.replace('%width%',AWidth);
  s = s.replace('%height%',AHeight);
  var d = document.getElementById('SubWindowTarget');
  if (d)
    d.innerHTML = s;
  else
    document.body.innerHTML += s;
  var e = document.getElementById('SubWindow');
  if (!e){
    alert('Failed To Create Dialog');
    return null;
  }
  //Display
  e.style.display = '';
  e.style.position = 'absolute';
  CenterBox(e.id);
  //CenterBox(e.id,100);
  document.onmousemove = HandleMouseMove;
  return e;
}

function CopyInnerHTML(SourceId, TargetId){
  var eSource = document.getElementById(SourceId);
  var eTarget = document.getElementById(TargetId);
  if ((!eSource) || (!eTarget))
    return;
  eTarget.innerHTML = eSource.innerHTML;
}

//document.onmousemove = HandleMouseMove;
