var isFirefox = (navigator.userAgent.indexOf("Firefox")!=-1) ;

var fontSize ;
var abort  = false ;
var regExp = new RegExp (',', 'g') ;

var showTotals = getCookie("ShowTotals");
if (! ((showTotals == "n") || (showTotals == "y")))
{ showTotals = "n" ; }

var twoScales = getCookie("TwoScales");
if (! ((twoScales == "n") || (twoScales == "y")))
{ twoScales = "y" ; }

var plotSize = getCookie("PlotSize");
if (! ((plotSize == 1) || (plotSize == 2) || (plotSize == 3) || (plotSize == 4) || (plotSize == 5) || (plotSize == 6) || (plotSize == 7) || (plotSize == 8)))
{
  plotSize = 2 ;
  setCookie ("PlotSize", plotSize);
}

var imageFormat = getCookie("ImageFormat");
if (! ((imageFormat == "Gif") || (imageFormat == "Png") || (imageFormat == "Svg")))
{
  imageFormat = "Png" ;
  setCookie ("ImageFormat", imageFormat);
}

function ShowCellTopLeft (text, color, size)
{
  text = '<font color=' + color + '>' + text + '</font>';
  if (size == 'small')
  { text = '<small>' + text + '</small>' ; }
  var x=document.getElementById('table1').rows;
  var y=x[0].cells;
  y[0].innerHTML= text ;
}

function getLegend ()
{
  if (twoScales == "n")
  { return ("&nbsp;") ; }
  else
  if (showTotals == "y")
  { return ("|&nbsp;&nbsp <image src='images/violetbar.gif' width=15 height =10> <b>:</b> " +
            "<image src='images/yellowbar.gif' width=15 height =10> <b>:</b> " +
            "<image src='images/greenbar.gif'  width=15 height =10> = 1 <b>:</b> 1 <b>:</b> 10") ; }
  else
  { return ("|&nbsp;&nbsp <image src='images/yellowbar.gif' width=15 height =10> <b>:</b> " +
            "<image src='images/greenbar.gif'  width=15 height =10> = 1 <b>:</b> 10") ; }
}

function switchShowTotals()
{
  if (showTotals=="n")
  { showTotals = "y" ; }
  else
  { showTotals = "n" ; }
  setCookie ("ShowTotals", showTotals);
  window.location.reload();
}

function switchTwoScales()
{
  if (twoScales=="n")
  { twoScales = "y" ; }
  else
  { twoScales = "n" ; }
  setCookie ("TwoScales", twoScales);
  window.location.reload();
}

function switchLanguage()
{
  var language = document.form.language.value;
  window.location = "Statistics__Language_" + language + ".htm" ;
}

function switchPage()
{
  var url = document.form.page.value;
  gotoPage (url) ;
}

function gotoPage(url)
{
  if (url != "")
  { window.location = url ; }
}

function changeCSS(myclass,element,value)
{
  var CSSRules ;
  if (document.all)
  { CSSRules = 'rules';}
  else
    if (document.getElementById)
    { CSSRules = 'cssRules';}
    else
    {return;}
  for (var i = 0; i < document.styleSheets[0][CSSRules].length; i++)
  {
    if (document.styleSheets[0][CSSRules][i].selectorText == myclass)
    { document.styleSheets[0][CSSRules][i].style[element] = value; }
  }
}

function embedSvg(src)
{
  w = 900 ; // zoom factor = 1.1
  if (plotSize == 1) { w = 700 ; }
  if (plotSize == 2) { w = 770 ; }
  if (plotSize == 3) { w = 847 ; }
  if (plotSize == 4) { w = 932 ; }
  if (plotSize == 5) { w = 1025 ; }
  if (plotSize == 6) { w = 1127 ; }
  if (plotSize == 7) { w = 1240 ; }
  if (plotSize == 8) { w = 1500 ; }
  h = Math.round (w * 384/621) ;

  wh = "width='" + w + "' height='" + h + "'" ;
  document.write ("<embed src='" + src + "' name='SVGEmbed' " + wh +
                  " type='image/svg+xml' pluginspage='http://www.adobe.com/svg/viewer/install/'>") ;
}

function switchPlotSize(zoom)
{
  if ((zoom == "-") && (plotSize > 1))
  {
    plotSize-- ;
    setCookie("PlotSize",plotSize);
    window.location.reload();
  }
  if ((zoom == "+") && (plotSize < 8))
  {
    plotSize++ ;
    setCookie("PlotSize",plotSize);
    window.location.reload();
  }
}

function switchFontSize(zoom)
{
  if ((zoom == "-") && (fontSize > 9))
  {
    fontSize-- ;
    setCookie("FontSize",fontSize);
  }
  if ((zoom == "+") && (fontSize < 18))
  {
    fontSize++ ;
    setCookie("FontSize",fontSize);
  }

  tablesToFontSize (fontSize) ;
}

function initTableSize()
{
  fontSize = getCookie("FontSize");
  if (fontSize == "")
  { fontSize = 11 ; }

  tablesToFontSize (fontSize) ;
}

function tablesToFontSize(fontSize)
{
  if (isFirefox)
  {
    document.getElementById('table1').style.fontsize = fontSize ;  	
    document.getElementById('table2').style.fontsize = fontSize ;  	
    document.getElementById('table3').style.fontsize = fontSize ;  	
    document.getElementById('table4').style.fontsize = fontSize ;  	
    document.getElementById('table5').style.fontsize = fontSize ;  	
    document.getElementById('table6').style.fontsize = fontSize ;  	
  }
  else // MSIE this seems necessary to keep table resize functional does not work in Firefox anyway (?)
 	{
    table1.style.fontSize = fontSize;
    table2.style.fontSize = fontSize;
    table3.style.fontSize = fontSize;
    table4.style.fontSize = fontSize;
    table5.style.fontSize = fontSize;
    table6.style.fontSize = fontSize;
  }  
}

function setCookie(name, value, expires, path, domain, secure)
{
var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
document.cookie = curCookie;
}

function getCookie(name)
{
var prefix = name + "="; var nullstring = ""; var cookieStartIndex = document.cookie.indexOf(prefix);
if (cookieStartIndex == -1) {return nullstring;}
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
if (cookieEndIndex == -1) {cookieEndIndex = document.cookie.length;}
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function y_axis(max1,max2,txtmonth,mode,sep1,sep2)
{
  sep_1000 = sep1 ;
  sep_dec  = sep2 ;

  if ((max2 != -1) && (showTotals == "n"))
  { max = max2 ; }
  else
  { max = max1 ; }

  maxheight = 100 ;
  maxvalue  = max ;
  maxvalue3 = max ;
  if (max == 0)
  { maxvalue = 1 ; }

  if (mode == 1)
  { limit = 1000 ; }
  else
  { limit = 100000 ; }

  yscale    = 1 ;
  exponent  = 0 ;
  while (max / yscale > limit)
  {
    yscale = yscale * 10 ;
    exponent ++ ;
  }
  if (yscale == 1)
  { prefix = '&nbsp;' ; }
  else
  {
    if (exponent > 4)
    { prefix = "x 10<sup>" +  exponent +"</sup>" ; }
    else
    { prefix = "x " +  yscale ; }
  }

  maxvalue2 = " " ;
  if (maxvalue > 1000000)
  {
     maxvalue  = Math.round (maxvalue / 100000) / 10 ;
     maxvalue2 = " M" ;
  }

  maxvalue1 = maxvalue ;
  if (maxvalue > 999)
  {
    maxvalue  = '?' + maxvalue ;
    if (maxvalue2 == " M")
    { maxvalue1 = maxvalue.substr (1,maxvalue.length-6).toString() + sep_1000 + maxvalue.substr (maxvalue.length-5) ; }
   else
    { maxvalue1 = maxvalue.substr (1,maxvalue.length-4).toString() + sep_1000 + maxvalue.substr (maxvalue.length-3) ; }
  }

  width  = 40 ;
  if (mode == 1)
  {
    h = "<tr><td class=chart_scale bgcolor=#ffffff valign=bottom width=" + width + ">" +
        "<img height=2 src='images/blanco.gif' width=" + width + "><br>" +
        maxvalue1.toString() + maxvalue2 + "&nbsp;<br>" +
        "<img height=1 src='images/black.gif'  width=" + width + "><br>" +
        "<img height=" + maxheight + " src='images/blanco.gif' width=" + width + "><br>" +
        "<img height=1 src='images/black.gif'  width=" + width + "><br>" +
        prefix + "&nbsp;<br>" +
        "<img height=1 src='images/blanco.gif'  width=" + width + "><br>" +
        txtmonth + "&nbsp;<br></td>" ;
  }
  else
  {
    h = "<tr><td class=chart_scale>" +
          "<table width='100%' cellSpacing=0 cellPadding=0 border=0><tbody><tr>" +
          "<td class=chart_scale bgcolor=#ffffff valign=bottom>" +
        "<img height=1 src='images/black.gif'  width=" + 10 + "><br>" +
        "<img height=" + 10 + " src='images/blanco.gif' width=1>" +
        maxvalue1.toString() + maxvalue2 + "&nbsp;<br>" +
        "<img height=" + ((maxheight / 2)-11) + " src='images/blanco.gif' width=1>" +
        txtmonth + "&nbsp;<br>" +
        "<img height=" + ((maxheight / 2)-2) + " src='images/blanco.gif' width=1><br>" +
        "<img height=1 src='images/black.gif'  width=" + 10 + "><br>" +
        "</td></tr><tr><td class=chart_scale bgcolor=#ffffff>" + prefix + "&nbsp;</td></tr>" +
        "</tbody></table></td>" ;
  }
  document.write (h);
  maxvalue  = max / yscale ;
  values    = "<td class=chart>" + prefix + "&nbsp;</td>" ;
  odd       = true;
  columns   = 1 ;
}

function bar(color, value, value2, month)
{
  img  = 'blanco' ;
  if (value == "-")
  { value = 0 ; }
  if (value2 == "-")
  { value2 = 0 ; }
  value  *= 1 ;
  value2 *= 1 ;

  if (color == 'Y') { img = 'yellowbar' ; }
  if (color == 'B') { img = 'bluebar' ; }
  if (color == 'R') { img = 'redbar' ; }
  if (color == 'G') { img = 'greenbar' ; }
  if (color == 'V') { img = 'violetbar' ; }
  img2 = 'greybar' ;

  h  = "<td class=chart valign=bottom width=18>" ;

  height = 0 ;
  if ((value != 0) && (maxvalue != 0))
  { height = Math.round (maxheight * ((value/yscale) / maxvalue)) ; }
  height2 = 0 ;
  if ((value2 != 0) && (maxvalue != 0))
  { height2 = Math.round (maxheight * ((value2/yscale) / maxvalue)) - 1 ; }


  if (height2 > 1)
  {
    h += "<img height=1 src='images/black.gif' width=15><br>" +
         "<img height=" + (height2 - 1) + " src='images/" + img2 + ".gif' width=15><br>" ;
  }

  if (height > 0)
  {
    h += "<img height=1 src='images/black.gif' width=15><br>" +
         "<img height=" + height + " src='images/" + img + ".gif' width=15><br>" +
         "<img height=1 src='images/black.gif' width=18><br>" ;
  }
  else
  { h += "<img height=1 src='images/black.gif' width=18><br>" ; }


  value3 = (((value+value2)/yscale)+".").split('.');
  value4 = value3 [0] ;
  if (value4 == 0)
  { value4 = "" ; }

  if (value4 >= 0)
  {
    if (odd)
    { h += "<font color='#000088'>" + value4 + "</font>" ; }
    else
    { h += "<font color='#000000'>" + value4 + "</font>" ; }
    odd = ! odd ;

  }
  h += "<br><img height=1 src='images/grey.gif' width=18>" ;
  h += "<br>" + month + "</td>" ;
  document.write (h);
}

function bar2(color, value)
{
  img  = 'blanco' ;
  if (value == "-") { value = 0 ; }

  if (color == 'Y') { img = 'yellowbar' ; }
  if (color == 'B') { img = 'bluebar' ; }
  if (color == 'R') { img = 'redbar' ; }
  if (color == 'G') { img = 'greenbar' ; }

  columns++ ;
  if (columns == 2)
  {
    if (showTotals == "y")
    { img = 'violetbar' ; }
    else
    { img = 'blanco' ; }
  }

  h = "<td class=chart valign=bottom>" +
      "<table width='100%' cellSpacing=0 background='images/background2.gif' cellPadding=0 border=0><tbody><tr>" ;

  height = 0 ;
  if ((value != 0) && (maxvalue != 0))
  { height = maxheight * ((value/yscale) / maxvalue) ; }

  if ((twoScales == "y")&& (value < (maxvalue3 / 10)))
  {
    height *= 10 ;
    img = 'greenbar2' ;
  }

  height = Math.round (height) ;

  h += "<td class=chart valign=bottom height='102'><center>" ;
  if ((columns == 2) && (showTotals == "n"))
  {
    h += "&nbsp;" ;
  }
  else
  {
    if (height > 0)
    {
      h += "<img height=1 src='images/black.gif' width=15><br>" ;
      h += "<img height=" + height + " src='images/" + img + ".gif' width=15><br>" ;
    }
    h += "<img height=1 src='images/black.gif' width=15></center>" ;
  }

  value2 = ((value/yscale)+".").split('.');
  value = value2 [0]+'.'+(value2[1]+'0000').substr(0,1);
  if ((value2 [0] > 10) || ((color == 'Y') && (yscale == 1)))
  { value = value2 [0] ; }

  if (value > 0)
  {
     if (value > 999)
     { value = value.substr (0,value.length-3) + "," + value.substr (value.length-3) ; }
     value = value.replace(regExp, sep_1000) ;

     h += "</td></tr><tr><td class=chart><font color='#000000'>" + value + "</font></td></tr>" ;
  }
  else
  { h += "</td></tr><tr><td class=chart>&nbsp;</td></tr>" ; }

  h += "</tbody></table></td>" ;
  document.write (h);
}


