/*

  digsys-adv.js           April 8, 2008

  Copyright (c) 2003-2008 Scandinavian Digital Systems AB

*/

function HRefMailto(idx)
// Hide: <A HREF="mailto:me@somewhere.com">e-mail me</A>
{
  var s;

  // <A HREF="mailto:
  s=String.fromCharCode(60,65,32,72,82,69,70,61,34,109,97,105,108,116,111,58);
  // Last char ':'
  switch (idx)
  {
    // me@somewhere.com">e-mail me
    default:
    case 0: // digsys
      s+=String.fromCharCode(100,105,103,115,121,115,64,100,105,103,115,121,115,46,
        115,101,34,62,100,105,103,115,121,115,64,100,105,103,115,121,115,46,115,101);
      break;
    case 1: // adsoft
      s+=String.fromCharCode(97,100,115,111,102,116,64,100,105,103,115,121,115,46,
        115,101,34,62,97,100,115,111,102,116,64,100,105,103,115,121,115,46,115,101);
      break;
  }
  // </A>
  s+=String.fromCharCode(60,47,65,62);
  document.write(s);
}

var bDOM1=false; // W3C DOM Level 1
var bBorderBox=false; // If width/height includes borders and paddings
var bPixelPos=false; // If pixelLeft/pixelTop is used (IE4 & IE5)
var bOpera=false; // Opera
var bMSIEWin=false; // Internet Explorer for Windows
var bEnableResizeReload=true; // Enable reload of current page on resize

/*
  Usage for FuncGetElement...:

  var x = new FuncGetElementById('name');

  x.obj givs access to the actual HTML element.
  x.style givs access to the styles of the HTML element.
*/

function FuncGetElementById(name)
{
  if (document.getElementById)
  {
    this.obj = document.getElementById(name);
    this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
    this.obj = document.all[name];
    this.style = document.all[name].style;
  }
  else if (document.layers)
  {
    this.obj = document.layers[name];
    this.style = document.layers[name];
  }
  else
  {
    this.obj = null;
    this.style = null;
  }
}

/*
  Cookie Data
*/

var nFontSize=10;

function SetDataCookie()
{
  var date = new Date();
  var data = "Data=0x"+nFontSize.toString(10);
  var a;
  var s;
  var n;

  date.setMonth(date.getMonth()+6);
  document.cookie=data+";expires="+date.toGMTString()+";path=/";
  // Use date "Thu, 01-Jan-70 00:00:01 GMT" to delete cookie
  a=document.cookie.split(';');
  for (n=0; n<a.length; n++)
  {
    s=a[n];
    while (s.length>1 && s.charAt(0)==" ") { s=s.substr(1); }
    if (s.indexOf(data)==0)
    {
      return true;
    }
  }
  return false;
}

function GetDataCookie()
{
  var a = document.cookie.split(';');
  var s;
  var n;

  for (n=0; n<a.length; n++)
  {
    s=a[n];
    while (s.length>1 && s.charAt(0)==" ") { s=s.substr(1); }
    if (s.indexOf("Data=")==0)
    {
      s=s.substr(5);
      a = s.split('x');
      if (a.length>=2 && parseInt(a[0])==0)
      {
        nFontSize=parseInt(a[1]);
        return true;
      }
      break;
    }
  }
  return false;
}

function MakePX(value)
{
  return value.toString(10)+"px";
}

function onload_main()
{
  var divinternal = new FuncGetElementById('divinternal');
  var divtop  = new FuncGetElementById('divtop');
  var divhead = new FuncGetElementById('divhead');
  var divleft = new FuncGetElementById('divleft');
  var divmain = new FuncGetElementById('divmain');
  var divbot  = new FuncGetElementById('divbot');
  var divfoot = new FuncGetElementById('divfoot');
  var nFontWidth;
  var nFontHeight;
  var nPageWidth;
  var nHeadSize;
  var nLeftSize;
  var nFootSize;
  var nPos;

  bEnableResizeReload=false;
  if (bDOM1)
  {
    GetDataCookie();
    if (nFontSize>24)
      nFontSize=24;
    else if (nFontSize<4)
      nFontSize=4;
    // Font
    while (true)
    {
      divinternal.style.fontSize=nFontSize+"pt";
      divinternal.obj.innerHTML="<P STYLE=\"margin:0;font-size:4em;line-height:100%\">M</P>";
      nFontWidth=divinternal.obj.offsetWidth/4;
      nFontHeight=divinternal.obj.offsetHeight/4;
      if (nFontWidth<4)
        nFontSize++;
      else if (nFontWidth>64)
        nFontSize--;
      else
        break;
    }
    document.getElementsByTagName('body').item(0).style.fontSize=divinternal.style.fontSize;
    divinternal.obj.innerHTML="";
  }
  else
  {
    nFontWidth=14;
    nFontHeight=16;
  }
  // Debugging
/*
  var szDebug2="Begin: Top="+divtop.obj.offsetWidth+"x"+divtop.obj.offsetHeight+
              " Head="+divhead.obj.offsetWidth+"x"+divhead.obj.offsetHeight+
              " Left="+divleft.obj.offsetWidth+"x"+divleft.obj.offsetHeight+
              " Main="+divmain.obj.offsetWidth+"x"+divmain.obj.offsetHeight+
              " Bot="+divbot.obj.offsetWidth+"x"+divbot.obj.offsetHeight+
              " Foot="+divfoot.obj.offsetWidth+"x"+divfoot.obj.offsetHeight;
*/
  // Page Width
  if (typeof(window.innerWidth)=='number')
  {
    // Non-IE
    nPageWidth = window.innerWidth;
  }
  else if (document.documentElement
    && document.documentElement.clientWidth)
  {
    // IE 6+ in 'standards compliant mode'
    nPageWidth = document.documentElement.clientWidth;
  }
  else if (document.body
    && document.body.clientWidth)
  {
    // IE 4 compatible
    nPageWidth = document.body.clientWidth;
  }
  else
  {
    nPageWidth = 0;
  }
  // Width
  if (bDOM1)
  {
    nLeftSize=divtop.obj.offsetWidth+Math.round(nFontWidth*4);
    if (nLeftSize<divleft.obj.offsetWidth)
      nLeftSize=divleft.obj.offsetWidth;
    if (nLeftSize<divbot.obj.offsetWidth)
      nLeftSize=divbot.obj.offsetWidth;
  }
  else
  {
    nLeftSize=Math.round(nFontWidth*10+16); // Number of characters, plus padding 8+8 and border 0+0
  }
  nHeadSize=Math.round(nFontWidth*35+16*5+16); // Min number of characters
  if (nPageWidth-nLeftSize>nHeadSize+32) // Adding 32px empty/guard space
  {
    nHeadSize=Math.round(nFontWidth*50+16*5+16); // Max number of characters
    if (nPageWidth-nLeftSize<nHeadSize+32)
      nHeadSize=nPageWidth-nLeftSize-32;
  }
  nPos=Math.floor((nPageWidth-nLeftSize-nHeadSize-33)/2); // Round down
  if (nPos<0) nPos=0;
  if (bBorderBox)
  {
    divtop.style.width=nLeftSize;
    divhead.style.width=nHeadSize;
    divleft.style.width=nLeftSize;
    divmain.style.width=nHeadSize;
    divtop.style.width=nLeftSize;
    divfoot.style.width=nHeadSize;
  }
  else
  {
    divtop.style.width=MakePX(nLeftSize-16); // Assuming padding 8+8 and border 0+0
    divhead.style.width=MakePX(nHeadSize-16); // Assuming padding 8+8 and border 0+0
    divleft.style.width=MakePX(nLeftSize-16); // Assuming padding 8+8 and border 0+0
    divmain.style.width=MakePX(nHeadSize-18); // Assuming padding 8+8 and border 1+1
    divbot.style.width=MakePX(nLeftSize-16); // Assuming padding 8+8 and border 0+0
    divfoot.style.width=MakePX(nHeadSize-16); // Assuming padding 8+8 and border 0+0
  }
  if (bPixelPos)
  {
    divtop.style.pixelLeft=nPos;
    divhead.style.pixelLeft=nPos+nLeftSize;
    divleft.style.pixelLeft=nPos;
    divmain.style.pixelLeft=nPos+nLeftSize;
    divbot.style.pixelLeft=nPos;
    divfoot.style.pixelLeft=nPos+nLeftSize;
  }
  else
  {
    divtop.style.left=MakePX(nPos);
    divhead.style.left=MakePX(nPos+nLeftSize);
    divleft.style.left=MakePX(nPos);
    divmain.style.left=MakePX(nPos+nLeftSize);
    divbot.style.left=MakePX(nPos);
    divfoot.style.left=MakePX(nPos+nLeftSize);
  }
  // Height
  if (bDOM1)
  {
    nHeadSize=divhead.obj.offsetHeight;
    if (nHeadSize<divtop.obj.offsetHeight)
      nHeadSize=divtop.obj.offsetHeight;
    nFootSize=divfoot.obj.offsetHeight;
    if (nFootSize<divbot.obj.offsetHeight)
      nFootSize=divbot.obj.offsetHeight;
    nLeftSize=divleft.obj.offsetHeight;
    if (nLeftSize<divmain.obj.offsetHeight)
      nLeftSize=divmain.obj.offsetHeight;
  }
  else
  {
    nHeadSize=Math.round(nFontHeight+10+16); // One rows using total 10px of menu button paddings and borders, plus padding 8+8 and border 0+0
    nFootSize=Math.round(nFontHeight+16); // One row, plus padding 8+8 and border 0+0;
    nLeftSize=Math.round(nFontHeight*30); // Unable to calculate, make DIVs visible
  }
  if (bBorderBox)
  {
    divtop.style.height=nHeadSize;
    divhead.style.height=nHeadSize;
    divleft.style.height=nLeftSize;
    divmain.style.height=nLeftSize;
    divbot.style.height=nFootSize;
    divfoot.style.height=nFootSize;
  }
  else
  {
    divtop.style.height=MakePX(nHeadSize-16); // Assuming padding 8+8 and border 0+0
    divhead.style.height=MakePX(nHeadSize-16); // Assuming padding 8+8 and border 0+0
    divleft.style.height=MakePX(nLeftSize-16); // Assuming padding 8+8 and border 0+0
    divmain.style.height=MakePX(nLeftSize-18); // Assuming padding 8+8 and border 1+1
    divbot.style.height=MakePX(nFootSize-16); // Assuming padding 8+8 and border 0+0
    divfoot.style.height=MakePX(nFootSize-16); // Assuming padding 8+8 and border 0+0
  }
  if (bPixelPos)
  {
    divtop.style.pixelTop=0;
    divhead.style.pixelTop=0;
    divleft.style.pixelTop=nHeadSize;
    divmain.style.pixelTop=nHeadSize;
    divbot.style.pixelTop=nHeadSize+nLeftSize;
    divfoot.style.pixelTop=nHeadSize+nLeftSize;
  }
  else
  {
    divtop.style.top=MakePX(0);
    divhead.style.top=MakePX(0);
    divleft.style.top=MakePX(nHeadSize);
    divmain.style.top=MakePX(nHeadSize);
    divbot.style.top=MakePX(nHeadSize+nLeftSize);
    divfoot.style.top=MakePX(nHeadSize+nLeftSize);
  }
  // Visibility
  divtop.style.visibility='visible';
  divhead.style.visibility='visible';
  divleft.style.visibility='visible';
  divmain.style.visibility='visible';
  divbot.style.visibility='visible';
  divfoot.style.visibility='visible';
  // Debugging
/*
  var szDebug1="Page "+nPageWidth+" Font "+divinternal.style.fontSize+" "+nFontWidth+"x"+nFontHeight+" Left "+parseInt(divhead.style.left)+" Top "+parseInt(divhead.style.top);
  var szDebug3="End: Top="+divtop.obj.offsetWidth+"x"+divtop.obj.offsetHeight+
              " Head="+divhead.obj.offsetWidth+"x"+divhead.obj.offsetHeight+
              " Left="+divleft.obj.offsetWidth+"x"+divleft.obj.offsetHeight+
              " Main="+divmain.obj.offsetWidth+"x"+divmain.obj.offsetHeight+
              " Bot="+divbot.obj.offsetWidth+"x"+divbot.obj.offsetHeight+
              " Foot="+divfoot.obj.offsetWidth+"x"+divfoot.obj.offsetHeight;
  divinternal.style.width='auto';
  divinternal.style.height='auto';
  divinternal.style.padding='4 px';
  divinternal.obj.innerHTML="<P STYLE=\"margin:0;font-size:9pt\">"+szDebug1+"<BR>"+szDebug2+"<BR>"+szDebug3+"</P>";
  divinternal.style.left=MakePX(parseInt(divmain.style.left)+8);
  divinternal.style.top=MakePX(parseInt(divmain.style.top)+8);
  divinternal.style.visibility='visible';
*/
  bEnableResizeReload=true;
}

function onresize_main()
{
  if (bEnableResizeReload)
  {
    history.go(0);
    // Alternative: window.location.reload();
  }
}

function ChangeFontSize(nValue)
{
  var nNewSize;

  if (bDOM1)
  {
    if (nValue==0)
    {
      nNewSize=10;
    }
    else
    {
      nNewSize=nFontSize+nValue;
      if (nNewSize>24)
        nNewSize=24;
      else if (nNewSize<4)
        nNewSize=4;
    }
    if (nNewSize!=nFontSize)
    {
      nFontSize=nNewSize;
      if (!SetDataCookie())
        alert("The font size can not be changed because cookies are not accepted by your browser!");
      history.go(0);
    }
  }
}

function onload_focus(field)
{
  onload_main();
  field.focus();
}

function DetectBrowser()
{
  if (navigator.userAgent.indexOf("Opera")>=0) // Opera uses the "MSIE" string
    bOpera=true;
  else if (navigator.userAgent.indexOf("MSIE")>=0 && navigator.userAgent.indexOf("Windows")>=0)
    bMSIEWin=true;
}

function CheckBrowser()
{
  if (!bDOM1)
  {
    document.write("<H3>This web site does not support your web browser!<BR>Please update/change to a modern browser.<BR>&nbsp;</H3>");
  }
}

/*
  Execute in-line
  Make dynamic CSS
  Fix compability problems on old browsers
*/

DetectBrowser();
if (document.getElementById && document.getElementsByTagName)
  bDOM1=true;
else if (document.all)
  bPixelPos=true;
if (document.compatMode==null || document.compatMode=="BackCompat" || document.compatMode=="QuirksMode")
{
  bBorderBox=true;
  document.write('<STYLE TYPE="text/css">');
  document.write('TABLE, TR, TD { font-size: 1em; }');
  document.write('#divmain TABLE { color: #000000; }');
  document.write('</STYLE>');
}
