var lastResizeTime = 0;
var wform = null;
var wespace = null;
var waccount = null;
var wpassword = null;
var walerts = null;
var wtimes = null;
var wprint = null;
var wpdf = null;
var doc = null;

function setFocus(id)
{
  if (!id) { id="focus" }
  var elem = document.getElementById(id);  
  if (elem) { elem.focus(); }
  else {
    elem = document.getElementById("s-words");  
    if (elem) { elem.focus(); }  
  }
}

function submitEnter(field,event)
{
  var keycode;
  if (window.event) { keycode = window.event.keyCode; }
  else if (event)   { keycode = event.which; }
  else              { return true; }
  if (keycode==13) {
    field.form.submit();
    return false;
  } else {
    return true;
  }
}

function updateScroll(scrollTop)
{
  resizeScroll();
  repositScroll(scrollTop);
}

function resizeScroll()
{
  var date = new Date();
  var delay = date.getTime() - lastResizeTime;
  if (delay>2000) {
    lastResizeTime = date.getTime();
    var table = document.getElementById("content");  
    var div = document.getElementById("scroll");
    if (table&&div) {
      var tabHeight = table.clientHeight;  
      var cliHeight = clientHeight();
      var divHeight = 0;
      if (cliHeight>tabHeight) { divHeight = cliHeight-230; }
      else                     { divHeight = tabHeight-220; }
      if (divHeight<300) { divHeight = 300; }
      div.style.height = (divHeight+'px'); 
    }
  }
}

function resetScroll()
{
  var div = document.getElementById("scroll");  
  if (div) { div.scrollTop = 0; }
}

function repositScroll(scrollTop)
{
  var anchor = "";
  var url = document.location.href; 
  var pos = url.indexOf("#");
  if (pos>=0) { anchor = url.substring(pos+1); }
  if (anchor=="") {
    var div = document.getElementById("scroll");  
    if (div) { div.scrollTop = scrollTop; }
  }
}

function loadPage(app,url,href,menu,id,open,zone,type,bid,file,anchor)
{
  var popen;
  if (open) { popen = "menu="+menu+"&id="+id+"&mode=close"; }
  else      { popen = "menu="+menu+"&id="+id+"&mode=open";  }
  if (file=="") { 
    if (anchor!="") { popen += "#"+anchor; }
    loadURL(app,url+"?"+popen);  
  } else {
    var pfile = "zone="+zone+"&type="+type+"&bid="+bid+"&file="+file;
    if (anchor!="") { pfile += "#"+anchor; }    
    loadURL(app,url+"?"+href+"&"+popen+"&"+pfile);
  }
}

function buildURL(app,url)
{
  var newURL;
  var stop = scrollPosition();    
  if (stop>=0) {    
    var pos = url.indexOf("?");
    if (pos<0) { newURL = url+"?stop="+stop; }
    else { 
      pos = url.indexOf("#");
      if (pos<0) { newURL = url+"&stop="+stop; }
      else {
        var anchor = url.substring(pos);
        newURL = url.substring(0,pos)+"&stop="+stop+anchor; 
      }
    }      
  } else {
    newURL = url;
  }  
  var appURL;  
  var pos = newURL.indexOf("?");
  if (pos<0) { 
    appURL = newURL+"?xapp="+app; 
  } else {
    pos = url.indexOf("#");
    if (pos<0) { appURL = newURL+"&xapp="+app; }
    else {
      var anchor = url.substring(pos);
      appURL = newURL.substring(0,pos)+"&xapp="+app+anchor; 
    }     
  }  
  return appURL;
}

function loadURL(app,url) 
{   
  document.location = buildURL(app,url); 
}

function scrollPosition()
{
  var stop = -1;
  var div = document.getElementById("scroll");
  if (div!=null) { stop = div.scrollTop; }
  return stop;
}

function clientHeight()
{
  var h = 0;
  if (typeof(window.innerWidth)=='number')                                  { h = window.innerHeight; }
  else if (document.documentElement&&document.documentElement.clientHeight) { h = document.documentElement.clientHeight; }
  else if (document.body&&document.body.clientHeight)                       { h = document.body.clientHeight; }
  return h;
}

function clientWidth()
{
  var w = 0;
  if (typeof(window.innerWidth)=='number')                                 { w = window.innerWidth; }
  else if (document.documentElement&&document.documentElement.clientWidth) { w = document.documentElement.clientWidth; }
  else if (document.body&&document.body.clientWidth)                       { w = document.body.clientWidth; }
  return w;
}

function pageHeight()
{
  var h = clientHeight();
  if (window.scrollMaxY) { h += window.scrollMaxY; }
  return h;
}

function hideExternal()
{
  var p = document.getElementById("external");  
  p.style.display = "none";
}

function hidePirate()
{
  var p = document.getElementById("pirate");  
  p.style.display = "none";
  var px = document.getElementById("piratex");  
  px.style.display = "none";   
}

var p_mtop = 100;
var p_mleft = 200;
var p_mbottom = 200;
var p_mright = 200;
var p_bstyle = 'solid';
var p_bcolor = '#4C84B0';
var p_bwidth = 3;
var p_padding = 20;

function initMessage(mtop,mleft,mbottom,mright,bstyle,bcolor,bwidth,padding) 
{ 
  var p = document.getElementById("pirate");       
  if (p) {              
    p_mtop = mtop;
    p_mleft = mleft;
    p_mbottom = mbottom;
    p_mright = mright;
    p_bstyle = bstyle;
    p_bcolor = bcolor;     
    p_bwidth = bwidth;
    p_padding = padding;     
    resizePirate(true); 
  }
}

function resizeMessage() { resizePirate(false); }

function resizePirate(init)
{   
  var resize = false;  
  var p = document.getElementById("pirate");       
  if (p) {
    if (init) { resize = true; }
    else {
      if (p.style.display=="inline") {
        resize = true; 
      }
    }
  }
  if (resize) {      
    var width = clientWidth()-p_mleft-p_mright;
    var height = clientHeight()-p_mtop-p_mbottom;    
    p.style.top = p_mtop+'px';  
    p.style.left = p_mleft+'px';  
    p.style.width = width+'px';
    p.style.height = height+'px';           
    p.style.padding = p_padding+'px';           
    p.style.borderStyle = p_bstyle;
    p.style.borderColor = p_bcolor;
    p.style.borderWidth = p_bwidth+'px';         
    p.style.display = "inline";    
    var px = document.getElementById("piratex");    
    px.style.top = p_mtop+'px';  
    px.style.left = (p_mleft+width+(p_padding*2)+(p_bwidth*2)-20)+'px';
    px.style.display = "inline";
  }
}

function openPopup(url,name,top,left,width,height)
{
  if (top<0) { top = (screen.height-height)/2; }
  if (left<0) { left = (screen.width-width)/2; }
  var popup = window.open(url,name,'menubar=no,toolbar=no,location=no,status=no,scrollbars=no,resizable=no,top='+top+',left='+left+',width='+width+',height='+height);
  popup.opener = window;
  popup.focus();
  return popup;
}

function openPopup(url,name,top,left,width,height) { return openWindow(url,name,top,left,width,height,true,false); }

function openWindow(url,name,top,left,width,height,scroll,resize)
{
  if (top<0) { top = (screen.height-height)/2; }
  if (left<0) { left = (screen.width-width)/2; }
  var options = "menubar=no,toolbar=no,location=no,status=no,";
  if (scroll) { options += "scrollbars=yes,"; }
  else        { options += "scrollbars=no,"; }
  if (resize) { options += "resizable=yes,"; }
  else        { options += "resizable=no,"; }    
  options += "top="+top+",left="+left+",width="+width+",height="+height;
  var popup = window.open(url,name,options);
  popup.opener = window;
  popup.focus();
  return popup;
}

function closePopup(refresh,href)
{  
  if (window.opener) {
    if (refresh) {
      if (href) { window.opener.location = href; } 
      else      { window.opener.location.reload(); }
    }
    window.opener.focus(); 
  }
  window.close();
}

function closedPopup(refresh) 
{
  if (window.opener) {  
    if (refresh) { window.opener.location.reload(); }
    window.opener.focus();    
  }
}

function closeChildPopup(popup) 
{
  if (popup) {  
    if (!popup.closed) {   popup.close(); }
  }
}

function editForm(app)     { wform = openPopup(buildURL(app,"form?mode=LDFOR"),"FORM",-1,-1,500,600); }
function showEspace(app)   { wespace = openPopup(buildURL(app,"account"),"ESPACE",-1,-1,600,600); }
function editAccount(app)  { waccount = openPopup(buildURL(app,"account?mode=LDACC"),"ACCOUNT",-1,-1,700,600); }
function editPassword(app) { wpassword = openPopup(buildURL(app,"account?mode=LDPWD"),"PASSWORD",-1,-1,400,350); }
function editAlerts(app)   { walerts = openPopup(buildURL(app,"account?mode=LDALT"),"ALERTS",-1,-1,640,480); }
function showTimes(app)    { wtimes = openPopup(buildURL(app,"account?mode=LDTIM"),"TIMES",-1,-1,400,500); }
function printHTML(app)    { wprint = openPopup(buildURL(app,"print?mode=html"),"HTML",-1,-1,800,600); }
function printPDF(app)     { wpdf = openPopup(buildURL(app,"print?mode=pdf"),"PDF",-1,-1,800,600); }

function closedEspace() 
{  
  closeChildPopup(waccount);
  closeChildPopup(wpassword);
  closeChildPopup(walerts);
  closeChildPopup(wtimes);   
  closedPopup(false); 
}

function disconnect() 
{   
  //closeChildPopup(wespace); 
  //closeChildPopup(wprint);   
  //closeChildPopup(wpdf);   
}





