var NN = (navigator.appName.indexOf("Netscape")>-1) ? 1 : 0; 
var NS4 = (document.layers) ? 1 : 0;
var htmlElement = "";
var test = false;
var GoodDB = "";

function chiudi() {
 if (test == false) {
  showhide();
 }
 test = false;
}

function showhide() {
 if (htmlElement != "") {
   if (htmlElement.style == null) {
     if (NS4 || NN) {
       htmlElement = document[htmlElement.spanid]
     } else {
       htmlElement = document.all[htmlElement.spanid]
     }
   }
   if (htmlElement.style.display=='none') {
     htmlElement.style.display='none';
   } else {
     htmlElement.style.display='none';
   }
 }
}

function showPanel(event,span_id) {
 test = true;
 showhide();
 htmlElement = span_id;
 if (span_id.style == null) {
    if (NS4 || NN) {
      span_id = document[span_id.spanid];
    } else {
      span_id = document.all[span_id.spanid];
    }
  }
  if (span_id.style.display=='none') {
    span_id.style.display='';
  } else {
    span_id.style.display='none';
  }
  if (NS4) {
    var x = window.event.pageX;
    var y = window.event.pageY;
    var sy = window.pageYOffset;
    var sx = window.pageXOffset;
    y = y + sy;
    x = x + sx;
    span_id.style.pageY=y + 2;
    span_id.style.pageX=x;
  } else if (NN) {
    var x = event.clientX;
    var y = event.clientY;
    var sx = window.pageXOffset;
    var sy = window.pageYOffset;
    var prova = window.innerWidth;
    y = y + sy;
    x = x + sx;
    span_id.style.top=y + 2;
    span_id.style.left=x;      
  } else {
    var sy = document.body.scrollTop;
    var sx = document.body.scrollLeft;
    var x = window.event.x;
    if (window.event.x < document.body.offsetWidth - span_id.clientWidth) {
      var x = window.event.x;
      x = x + sx;
    } else {
      var x = window.event.x - span_id.clientWidth;
      x = x + sx;
    } 
    if (window.event.y < document.body.offsetHeight - span_id.clientHeight) {
      var y = window.event.y;
      y = y + sy;
    } else {
      var y = window.event.y - span_id.clientHeight;
      y = y + sy;
    } 
    span_id.style.top=y +2;
    span_id.style.left=x;      
  }
  return false;
}

function changeApice(text) {
  testo = text.value;
  testo = replace(testo,unescape('%27'),'’');
  text.value = testo;
  text.focus();
  return text;
}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}