/*

  vardbolaget.js          January 30, 2007

  Copyright (c) 2003-2007 Scandinavian Digital Systems AB

  Developed by Anders Danielsson, http://www.digsys.se/

*/

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 divhead = new FuncGetElementById('divhead');
  var divleft = new FuncGetElementById('divleft');
  var divright = new FuncGetElementById('divright');
  var divbot = new FuncGetElementById('divbot');
  var divfoot = new FuncGetElementById('divfoot');
  var divmain = new FuncGetElementById('divmain'); // Uses overflow: auto
  var nFontWidth;
  var nFontHeight;
  var nPageWidth;
  var nPageHeight;
  var nHeadSize;
  var nLeftSize;
  var nRightSize;
  var nFootSize;
  // var nMainSize;
  var nPos;

  bEnableResizeReload=false;
  if (bDOM1)
  {
    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;
  }
  // For removal of scrollbar, widht needs to be processed twiced
  // Width, first time process, excluding position
  if (typeof(window.innerWidth)=='number')
  {
    // Non-IE
    nPageWidth = window.innerWidth;
  }
  else if (document.documentElement
    && document.documentElement.clientWidth && document.documentElement.clientHeight)
  {
    // IE 6+ in 'standards compliant mode'
    nPageWidth = document.documentElement.clientWidth;
  }
  else if (document.body
    && document.body.clientWidth && document.body.clientHeight)
  {
    // IE 4 compatible
    nPageWidth = document.body.clientWidth;
  }
  else
  {
    nPageWidth = 0;
  }
  if (bDOM1)
  {
    nLeftSize=divleft.obj.offsetWidth;
    if (nLeftSize<divbot.obj.offsetWidth)
      nLeftSize=divbot.obj.offsetWidth;
    nRightSize=divright.obj.offsetWidth;
  }
  else
  {
    nLeftSize=Math.round(nFontWidth*12+16); // Number of characters, plus padding 8+8 and border 0+0
    nRightSize=Math.round(nFontWidth*3+16); // Number of characters, plus padding 8+8 and border 0+0
  }
  nHeadSize=Math.round(nFontWidth*35+16+2); // Min number of characters, plus padding 8+8 and border 1+1
  if (nHeadSize<216+16+2) nHeadSize=216+16+2; // Min size due to logo.gif image width, plus padding 8+8 and border 1+1
  if (nPageWidth-nLeftSize-nRightSize>nHeadSize+32) // Adding 32px empty/guard space
  {
    nHeadSize=Math.round(nFontWidth*55+16+2); // Max number of characters
    if (nHeadSize<216+16+2) nHeadSize=216+16+2; // Min size due to logo.gif image width, plus padding 8+8 and border 1+1
    if (nPageWidth-nLeftSize-nRightSize<nHeadSize+32)
      nHeadSize=nPageWidth-nLeftSize-nRightSize-32;
  }
  if (bBorderBox)
  {
    divbot.style.width=nLeftSize;
    divleft.style.width=nLeftSize;
    divright.style.width=nRightSize;
    divhead.style.width=nLeftSize+nHeadSize+nRightSize;
    divfoot.style.width=nHeadSize+nRightSize;
    divmain.style.width=nHeadSize;
  }
  else
  {
    divbot.style.width=MakePX(nLeftSize-16); // Assuming padding 8+8 and border 0+0
    divleft.style.width=MakePX(nLeftSize-16); // Assuming padding 8+8 and border 0+0
    divright.style.width=MakePX(nRightSize-16); // Assuming padding 8+8 and border 0+0
    divhead.style.width=MakePX(nLeftSize+nHeadSize+nRightSize-16); // Assuming padding 8+8 and border 0+0
    divfoot.style.width=MakePX(nHeadSize+nRightSize-16); // Assuming padding 8+8 and border 0+0
    divmain.style.width=MakePX(nHeadSize-16-2); // Assuming padding 8+8 and border 1+1
  }
  // Height (process before final width for removal of scrollbar)
  if (typeof(window.innerHeight)=='number')
  {
    // Non-IE
    nPageHeight = window.innerHeight;
  }
  else if (document.documentElement
    && document.documentElement.clientWidth && document.documentElement.clientHeight)
  {
    // IE 6+ in 'standards compliant mode'
    nPageHeight = document.documentElement.clientHeight;
  }
  else if (document.body
    && document.body.clientWidth && document.body.clientHeight)
  {
    // IE 4 compatible
    nPageHeight = document.body.clientHeight;
  }
  else
  {
    nPageHeight = 0;
  }
  if (bDOM1)
  {
    nHeadSize=divhead.obj.offsetHeight;
    if (nHeadSize<136+16) nHeadSize=136+16; // Min size due to logo.gif image height, plus padding 8+8 and border 0+0
    nFootSize=divfoot.obj.offsetHeight;
    if (nFootSize<divbot.obj.offsetHeight)
      nFootSize=divbot.obj.offsetHeight;
  }
  else
  {
    nHeadSize=136+16; // logo.gif image height, plus padding 8+8 and border 0+0
    nFootSize=Math.round(nFontHeight*2+16); // Two rows, plus padding 8+8 and border 0+0
  }
  nLeftSize=Math.round(nFontHeight*15+16+2); // Min number of rows, assuming padding 8+8 and border 0+0
  if (nPageHeight>nHeadSize+nFootSize+nLeftSize+32) // Adding 32px empty/guard space
  {
    nLeftSize=Math.round(nFontHeight*35+16+2); // Max number of rows ...
    if (nPageHeight<nHeadSize+nFootSize+nLeftSize+32)
      nLeftSize=nPageHeight-nHeadSize-nFootSize-32;
  }
  if (bBorderBox)
  {
    divhead.style.height=nHeadSize;
    divfoot.style.height=nFootSize;
    divbot.style.height=nFootSize;
    divleft.style.height=nLeftSize;
    divright.style.height=nLeftSize;
    divmain.style.height=nLeftSize;
  }
  else
  {
    divhead.style.height=MakePX(nHeadSize-16); // Assuming padding 8+8 and border 0+0
    divfoot.style.height=MakePX(nFootSize-16); // Assuming padding 8+8 and border 0+0
    divbot.style.height=MakePX(nFootSize-16); // Assuming padding 8+8 and border 0+0
    divleft.style.height=MakePX(nLeftSize-16); // Assuming padding 8+8 and border 0+0
    divright.style.height=MakePX(nLeftSize-16); // Assuming padding 8+8 and border 0+0
    divmain.style.height=MakePX(nLeftSize-16-2); // Assuming padding 8+8 and border 1+1
  }
  nPos=Math.floor((nPageHeight-nHeadSize-nFootSize-nLeftSize-1)/2); // Round down
  if (nPos<0) nPos=0;
  if (bPixelPos)
  {
    divhead.style.pixelTop=nPos;
    divleft.style.pixelTop=nPos+nHeadSize;
    divright.style.pixelTop=nPos+nHeadSize;
    divmain.style.pixelTop=nPos+nHeadSize;
    divfoot.style.pixelTop=nPos+nHeadSize+nLeftSize;
    divbot.style.pixelTop=nPos+nHeadSize+nLeftSize;
  }
  else
  {
    divhead.style.top=MakePX(nPos);
    divleft.style.top=MakePX(nPos+nHeadSize);
    divright.style.top=MakePX(nPos+nHeadSize);
    divmain.style.top=MakePX(nPos+nHeadSize);
    divfoot.style.top=MakePX(nPos+nHeadSize+nLeftSize);
    divbot.style.top=MakePX(nPos+nHeadSize+nLeftSize);
  }
  // Width, second time process, including position
  if (typeof(window.innerWidth)=='number')
  {
    // Non-IE
    nPageWidth = window.innerWidth;
  }
  else if (document.documentElement
    && document.documentElement.clientWidth && document.documentElement.clientHeight)
  {
    // IE 6+ in 'standards compliant mode'
    nPageWidth = document.documentElement.clientWidth;
  }
  else if (document.body
    && document.body.clientWidth && document.body.clientHeight)
  {
    // IE 4 compatible
    nPageWidth = document.body.clientWidth;
  }
  else
  {
    nPageWidth = 0;
  }
  if (bDOM1)
  {
    nLeftSize=divleft.obj.offsetWidth;
    if (nLeftSize<divbot.obj.offsetWidth)
      nLeftSize=divbot.obj.offsetWidth;
    nRightSize=divright.obj.offsetWidth;
  }
  else
  {
    nLeftSize=Math.round(nFontWidth*12+16); // Number of characters, plus padding 8+8 and border 0+0
    nRightSize=Math.round(nFontWidth*3+16); // Number of characters, plus padding 8+8 and border 0+0
  }
  nHeadSize=Math.round(nFontWidth*35+16+2); // Min number of characters, plus padding 8+8 and border 1+1
  if (nHeadSize<216+16+2) nHeadSize=216+16+2; // Min size due to logo.gif image width, plus padding 8+8 and border 1+1
  if (nPageWidth-nLeftSize-nRightSize>nHeadSize+32) // Adding 32px empty/guard space
  {
    nHeadSize=Math.round(nFontWidth*55+16+2); // Max number of characters
    if (nHeadSize<216+16+2) nHeadSize=216+16+2; // Min size due to logo.gif image width, plus padding 8+8 and border 1+1
    if (nPageWidth-nLeftSize-nRightSize<nHeadSize+32)
      nHeadSize=nPageWidth-nLeftSize-nRightSize-32;
  }
  if (bBorderBox)
  {
    divbot.style.width=nLeftSize;
    divleft.style.width=nLeftSize;
    divright.style.width=nRightSize;
    divhead.style.width=nLeftSize+nHeadSize+nRightSize;
    divfoot.style.width=nHeadSize+nRightSize;
    divmain.style.width=nHeadSize;
  }
  else
  {
    divbot.style.width=MakePX(nLeftSize-16); // Assuming padding 8+8 and border 0+0
    divleft.style.width=MakePX(nLeftSize-16); // Assuming padding 8+8 and border 0+0
    divright.style.width=MakePX(nRightSize-16); // Assuming padding 8+8 and border 0+0
    divhead.style.width=MakePX(nLeftSize+nHeadSize+nRightSize-16); // Assuming padding 8+8 and border 0+0
    divfoot.style.width=MakePX(nHeadSize+nRightSize-16); // Assuming padding 8+8 and border 0+0
    divmain.style.width=MakePX(nHeadSize-16-2); // Assuming padding 8+8 and border 1+1
  }
  nPos=Math.floor((nPageWidth-nLeftSize-nRightSize-nHeadSize-1)/2); // Round down
  if (nPos<0) nPos=0;
  if (bPixelPos)
  {
    divbot.style.pixelLeft=nPos;
    divleft.style.pixelLeft=nPos;
    divhead.style.pixelLeft=nPos;
    divfoot.style.pixelLeft=nPos+nLeftSize;
    divmain.style.pixelLeft=nPos+nLeftSize;
    divright.style.pixelLeft=nPos+nLeftSize+nHeadSize;
  }
  else
  {
    divbot.style.left=MakePX(nPos);
    divleft.style.left=MakePX(nPos);
    divhead.style.left=MakePX(nPos);
    divfoot.style.left=MakePX(nPos+nLeftSize);
    divmain.style.left=MakePX(nPos+nLeftSize);
    divright.style.left=MakePX(nPos+nLeftSize+nHeadSize);
  }
  // Visibility
  divhead.style.visibility='visible';
  divleft.style.visibility='visible';
  divright.style.visibility='visible';
  divbot.style.visibility='visible';
  divfoot.style.visibility='visible';
  divmain.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("Teckenstorleken kan inte ändras, eftersom cookies inte accepteras av din webbläsare (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>Denna webbplats stöder inte din webbläsare!<BR>Var god uppgradera/ändra till en mer modern webbläsare.<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">');
  if (bMSIEWin)
    document.write('BODY { overflow: auto; }');
  document.write('TABLE, TR, TD { font-size: 1em; }');
  document.write('#divhead TABLE, #divleft TABLE, #divright TABLE, #divbot TABLE, #divfoot TABLE { color: #00BA00; }');
  document.write('</STYLE>');
}
else if (bMSIEWin)
{
  document.write('<STYLE TYPE="text/css">');
  document.write('HTML { overflow: auto; }');
  document.write('</STYLE>');
}
GetDataCookie();
