  function goPage(page, params) {
    var loc = "engine.php?page=" + page;
    if (params)
      loc += "&" + params
    window.location = loc;
  }

  function colorBG(elem, color) {
    if (!color)
      color = '#efefef';
    elem.style['backgroundColor'] = color;
  }

  function unColorBG(elem) {
    elem.style['backgroundColor'] = '';
  }


  function drawTabTitle(title) {
    var html = "";
    html += "<TABLE CellSpacing=0 CellPadding=0 Border=0 Width='100%'> \n";
    html += "  <TR> \n";
//    html += "    <TD Class=TabTitleSpacer><IMG Src='../images/null.gif' Width=4 Height=1></TD> \n";
    html += "    <TD><IMG Src='../images/tab_sel_sx.gif' Border=0 Width=3 Height=21></TD> \n";
    html += "    <TD Class=TabTitleSel>" + title + "</TD> \n";
    html += "    <TD><IMG Src='../images/tab_sel_dx.gif' Border=0 Width=4 Height=21></TD> \n";
    html += "    <TD Class=TabTitleSpacer Width='100%'><IMG Src='../images/null.gif' Width=4 Height=1></TD> \n";
    html += "  </TR> \n";
    html += "</TABLE> \n";
    document.write(html);
  }

  function drawTabItem(link, title, index, prev, next) {
    var html = "";
    if (title) {
      var sel = (req_index == index);
      var style = "TabTitle";
      if (sel)
        style = "TabTitleSel";
      var width = 0;
      var img = "";

      if (sel) {
        if (req_index == 0) {
          width = 3;
          img = "tab_sel_sx";
        }
        else {
          width = 4;
          img = "tab_selx_sx";
        }
      }
      else if (index != req_index+1) {
        width = 3;
        img = "tab_norm_sx";
      }

      if (img != "") {
        if (!sel)
          html += "    <TD Class=TabTitleSpacer><IMG Src='../images/null.gif' Border=0 Width=1 Height=1></TD> \n";
        html += "    <TD><IMG Src='../images/" + img + ".gif' Border=0 Width=" + width + " Height=21></TD> \n";
      }

      if (!sel) {
        if (link.indexOf("javascript:") == 0) {
          link = link.replace("$index", index);
          title = "<a href='" + link + "'>" + title + "</a>";
        }
        else
          title = "<a href='" + link + "&tab_index=" + index + "'>" + title + "</a>";
      }
      html += "    <TD Class=" + style + ">" + title + "</TD> \n";

      img = "";
      if (sel) {
        if (next) {
          width = 4;
          img = "tab_selx_dx";
        }
        else {
          width = 3;
          img = "tab_sel_dx";
        }
      }
      else if (index != req_index-1) {
        width = 3;
        img = "tab_norm_dx";
      }

      if (img != "")
        html += "    <TD><IMG Src='../images/" + img + ".gif' Border=0 Width=" + width + " Height=21></TD> \n";

    }
    return html;
  }

  function drawTabTitles(link, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9) {
    var html = "";
    html += "<TABLE CellSpacing=0 CellPadding=0 Border=0 Width='100%'> \n";
    html += "  <TR> \n";

    html += drawTabItem(link, n0, 0, false, n1);
    html += drawTabItem(link, n1, 1, n0, n2);
    html += drawTabItem(link, n2, 2, n1, n3);
    html += drawTabItem(link, n3, 3, n2, n4);
    html += drawTabItem(link, n4, 4, n3, n5);
    html += drawTabItem(link, n5, 5, n4, n6);
    html += drawTabItem(link, n6, 6, n5, n7);
    html += drawTabItem(link, n7, 7, n6, n8);
    html += drawTabItem(link, n8, 8, n7, n9);
    html += drawTabItem(link, n9, 9, n8, false);
    html += "    <TD Class=TabTitleSpacer Width='100%'><IMG Src='../images/null.gif' Width=4 Height=1></TD> \n";
    html += "  </TR> \n";
    html += "</TABLE> \n";
    document.write(html);
  }

  function createCookie(name, value) {
    var date = new Date();
    date.setTime(date.getTime()+(30*24*60*60*1000));
    var expires = "; expires=" + date.toGMTString();
    var ck = name + "=" + value + expires + "; path=/";
    document.cookie = ck;
  }

  function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i<ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') 
        c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) 
        return c.substring(nameEQ.length,c.length);
    }
    return null;
  }

  function switchSize(aSize) {
    table = document.getElementById("MainTable");
    if (aSize == "big")
      table.style.width = "100%";
    else
      table.style.width = 800;
    createCookie("bt_size", aSize);
  }
  
  function findPos(obj) {
  	var curleft = curtop = 0;
  	if (obj.offsetParent) {
  		curleft = obj.offsetLeft
  		curtop = obj.offsetTop
  		while (obj = obj.offsetParent) {
  			curleft += obj.offsetLeft
  			curtop += obj.offsetTop
  		}
  	}
  	return [curleft,curtop];
  }
  
  function getImgRealSize(imgsrc) {
    var dummy = new Image();
    dummy.src = imgsrc;
  	return [dummy.width, dummy.height];
  }


function ActivateTabSheet(tabSheetTitle) {
  var sel = xGetElementsByClassName("TabSheetTitleSel", tabSheetTitle.parentNode.parentNode);
  if (sel.length > 0)
    sel[0].className = "TabSheetTitle";
  var contents = xGetElementsByClassName("TabSheetContent", tabSheetTitle.parentNode);
  var sheets = xGetElementsByClassName("PageControlContent", tabSheetTitle.parentNode.parentNode);

  if ((contents.length > 0) && (sheets.length > 0)) {  
    sheets[0].innerHTML = contents[0].innerHTML;
    tabSheetTitle.className = "TabSheetTitleSel";
  }
}

function TabSheetClick(event) {
  if (event.srcElement == undefined)
    obj = this;
  else
    obj = event.srcElement;
  ActivateTabSheet(obj);
}
  
function addTabSheetListeners() {
  list = xGetElementsByClassName("TabSheetTitle", document);
  for (var i = 0; i < list.length; ++i)
    xAddEventListener(list[i], 'click', TabSheetClick, false);
  if (list.length > 0)
    ActivateTabSheet(list[0]);
}
xAddEventListener(window, 'load', addTabSheetListeners, false);


function grandpaSwitch() {
  collapsedHeight = "230px";
  containerDIV = document.getElementById("grandpaDiv");
  contentDIV = containerDIV.getElementsByTagName('DIV')[0];
  if ((containerDIV.style.height == "") || (containerDIV.style.height == collapsedHeight)) {
    containerDIV.style.height = contentDIV.offsetHeight;
    document.getElementById("grandpaLink").innerHTML = "Chiudi";
  } 
  else {
    containerDIV.style.height = collapsedHeight;
    document.getElementById("grandpaLink").innerHTML = "Leggi tutto...";
  }
}

function submitenterReport(myfield, e) {
  var keycode;
  if (window.event) 
    keycode = window.event.keyCode;
  else if (e) 
    keycode = e.which;
  else 
    return true;

  if (keycode == 13) {
    if (myfield.value == "")
      goPage('reports');
    else
      goPage('reports', 'year=all&srcText=' + myfield.value);
    return false;
  }
  else
    return true;
}
