function shade(elemento)   { 
    el = document.getElementById(elemento);
    isVisible = (el.style.display == 'none' || el.style.display == '') ? true : false;
    
    el.style.visibility = isVisible ? "visible" : "hidden";
    el.style.display = isVisible ? "block" : "none";

    if(isVisible) {
		setCookie(elemento, 1, 1);
	}else{
		setCookie(elemento, 0, 1);
	}
	}

function setCookie(sNome, sValore, iGiorni) {
  var dtOggi = new Date()
  var dtExpires = new Date()
  dtExpires.setTime
    (dtOggi.getTime() + 24 * iGiorni * 3600000)
  document.cookie = sNome + "=" + escape(sValore) +
    "; expires=" + dtExpires.toGMTString();
}

// restituisce il valore del cookie sNome
function getCookie(sNome) {
  // genera un array di coppie "Nome = Valore"
  // NOTA: i cookies sono separati da ';'
  var asCookies = document.cookie.split("; ");
  // ciclo su tutti i cookies
  for (var iCnt = 0; iCnt < asCookies.length; iCnt++)
  {
    // leggo singolo cookie "Nome = Valore"
    var asCookie = asCookies[iCnt].split("=");
    if (sNome == asCookie[0]) { 
      return (unescape(asCookie[1]));
    }
  }

  // SE non esiste il cookie richiesto
  return("");
}

// rimuove un cookie
function delCookie(sNome) {
  setCookie(sNome, "");
}


function shadeTabs (menuList, container)
{
m = document.getElementById(menuList);
c = document.getElementById(container);

mElements = m.document.getElementsByTagName("a");
cElements = c.document.getElementsByTagName("div");

  for (var i = 0; i < mElements.length; i++) 
    {
      mElements[i].style.border = "1px solid gray";
      mElements[i].onclick = multiShade(cElements[i].id, menuList, container);
      cElements[i].style.visibility = "hidden";
      cElements[i].style.display = "none";
    }
}


function shadeSwitch(anchor, elem, menu, contain)
{
if (window.location.hash == anchor) multiShade(elem, menu, contain);
}

function showSection(section, elemClass, link, hilite, hiliteClass)
{
  // Get total DIV elements with Class "elemClass"
  if (hilite!=null && !hiliteClass) var hiliteCLass='currentLink';
  if (link) {
            var cont = getParentByTagName(link, "DIV");
            var allLinks = cont.getElementsByTagName("a");
            }
  var elements = getElementsByClassName(elemClass); 
  var target = document.getElementById(section);
  for (x in elements)
          {
            elements[x].style.visibility = 'hidden'; 
            elements[x].style.display = 'none';
          }
        target.style.visibility = 'visible'; 
        target.style.display = 'block';
  if (link) {
        for (var alink=0; alink < allLinks.length; alink++)
        {
          if (hasClass(allLinks[alink], hiliteClass)) removeClass(allLinks[alink], hiliteClass);
        }
        //link.className = 'currentLink';
        if (hilite) addClass(link, hiliteClass);
            }
}


function decorate(elId)
{
  var element = document.getElementById(elId);
  var header = element.firstChild;
  var cont = element.parentNode;  
  var newDiv = document.createElement("DIV");
  element.removeChild(header);
  newDiv.setAttribute("id", element.id + "_decoration");
  newDiv.setAttribute("class", "decoration");
  newDiv.appendChild(header);
  //header.innerHTML += "<a onclick=\"shade('"+ element.id + "');\">Nascondi</a>";
  //header.setAttribute("onclick",  "shade('"+ element.id + "');");
  newDiv.appendChild(element);
  cont.appendChild(newDiv);
  
}

function getSearch(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

function popupSection(section, elemClass, link, hilite, hiliteClass)
{
  if (hilite!=null && !hiliteClass) var hiliteCLass='currentLink';
  var cont = getParentByTagName(link, "DIV");
  var allLinks = cont.getElementsByTagName("a");
  var elements = getElementsByClassName(elemClass); 
  var target = document.getElementById(section);
  
  /*var frame = document.createElement("DIV");
  frame.setAttribute("id", target.id + "_container");
  frame.style.border = "thick solid gray";
  frame.style.position = "fixed";
  frame.style.width = screen.availWidth;
  frame.style.height = screen.availHeigth;
  frame.style.backgroundColor = 'gray';
  frame.style.opacity = 0.5;
  frame.appendChild(target);
  */
  target.style.visibility = 'visible';
  target.style.position = 'fixed';
  target.style.left = (cont.style.width - target.offSetWidth) / 2;
  alert((cont.style.width + " " + target.style.width) );
  target.style.marginRight = 'auto';
  target.style.display = 'block';
  target.style.backgroundColor="white";
        for (var alink=0; alink < allLinks.length; alink++)
        {
          if (hasClass(allLinks[alink], hiliteClass)) removeClass(allLinks[alink], hiliteClass);
        }
        //link.className = 'currentLink';
        if (hilite) addClass(link, hiliteClass);


}


function fade(elementId, startTime, duration) {
	if (document.getElementById) {
    var fadeTarget = document.getElementById(elementId);
        if (!fadeTarget) return false;
        if (fadeTarget.style.MozOpacity!=null) {  
			/* Mozilla's pre-CSS3 proprietary rule */
            fadeTarget.style.MozOpacity = 1;
                }
        else if (fadeTarget.style.opacity!=null) {
			/* CSS3 compatible */
            fadeTarget.style.opacity = 1;
                }
        else if (fadeTarget.style.filter!=null) {
			/* IE's proprietary filter */
                fadeTarget.style.filter = "alpha(opacity=100)";
                }
		fadeTarget.style.visibility = 'visible';
		window.setTimeout("fadeOut(100,'" + elementId +"',"+duration+")" , startTime);
	}
}


function fadeOut(opacity, elementId, duration) {
    var fadeTarget = document.getElementById(elementId);
	if (fadeTarget) {
		if (opacity >= 0) {
			if (fadeTarget.style.MozOpacity!=null) {
				/* Mozilla's pre-CSS3 proprietary rule */
				fadeTarget.style.MozOpacity = (opacity/100); //-.001;
				/* the .001 fixes a glitch in the opacity calculation which normally results in a flash when reaching 1 */
            }
        else if (fadeTarget.style.opacity!=null) {
				/* CSS3 compatible */
				fadeTarget.style.opacity = (opacity/100); //-.001;
            }
        else if (fadeTarget.style.filter!=null) {
				/* IE's proprietary filter */
				fadeTarget.style.filter = "alpha(opacity="+opacity+")";
				/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
            }
      
			opacity -= 10;
			window.setTimeout("fadeOut("+opacity+",'"+elementId+"')", duration);
		}
    else
      {
       fadeTarget.style.display = 'none';
       fadeTarget.style.visibility = 'hidden';
       
      }
	}
}


function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   obj['on' + evType] = fn;
   return true; 

 } 
}



function inPlaceEdit(viewId, modId){
  var viewEl = document.getElementById(viewId);
  var modEl = document.getElementById(modId);
  if (modEl.type != 'password') modEl.value = viewEl.textContent; 
  modEl.style.visibility = "hidden";
  modEl.style.display = "none";
  viewEl.style.visibility = "visible";
  viewEl.style.display = "block";
  
  addEvent(viewEl, 'click', function(){
        modEl.style.visibility = "visible";
        modEl.style.display = "block";
        viewEl.style.visibility = "hidden";
        viewEl.style.display = "none";
        modEl.focus();
    } );
  addEvent(modEl, 'blur', function(){
        modEl.style.visibility = "hidden";
        modEl.style.display = "none";
        viewEl.style.visibility = "visible";
        viewEl.style.display = "block";
        if (modEl.type != 'password') viewEl.textContent = modEl.value;
        
        
    }
      );
  
  
}


function makeEditable(elId, elType, elSubType)
{
  if (!document.getElementById(elId)) return false;
  var view = document.getElementById(elId);
  view.style.cursor = "text";
  var mod = document.createElement(elType);
  if (elSubType) mod.type=elSubType;
  if (mod.type != 'password') mod.value = view.textContent;
  mod.name = view.attributes.getNamedItem("id").value;
  mod.id = view.attributes.getNamedItem("id").value + "_mod";
  mod.style.visibility = "hidden";
  mod.style.display = "none";
  view.parentNode.appendChild(mod);
  inPlaceEdit(elId, mod.id);  
  return true;
}


function candyTable(tableId, evenClass, oddClass, number)
{
  var ie6 = (isIe6() == true) ? true:false;
  var table = document.getElementById(tableId);
  if (table.tagName.toLowerCase() == 'table' && !hasClass(table, 'candy_flag'))  {
      var rows = table.getElementsByTagName("tr");
      for (row = 0; row < rows.length; row++)
        {
            var actualRow = rows[row];
            var ths = actualRow.getElementsByTagName("TH");
            if (number != false)
              {
              var counter = document.createElement("TD");
              var text = document.createTextNode(row);
              var firstTd = rows[row].firstChild;
              if (ths.length == 0) counter.appendChild(text);
              counter.setAttribute("width", "20px");
              rows[row].insertBefore(counter, firstTd);
              }
             if (ths.length == 0 && row % 2 == 0 && evenClass != false) addClass (rows[row], evenClass);
             else if (oddClass != false && ths.length == 0) addClass (rows[row], oddClass);
              addClass(table, 'candy_flag');
        } 
              }
  
}


function isIe6(){
  
  if (navigator.userAgent.indexOf("MSIE 6") > 0) return true;
  else return false;
  
}

function d2h(d)
{
  if(d.toString(16) == 0)
  {
    return "00";
  }
  return d.toString(16).toUpperCase();
} 

function addShadow(elId, effect)
{
      var ie6 = (isIe6() == true) ? true:false;
      var element = (document.getElementById) ? document.getElementById(elId) : document.all.item[elId];
      if (element.nodeType == 1){
      element.style.display="block";
      var wt = element.offsetWidth;
      var ht = element.offsetHeight;
      var offTop = element.offsetTop;
      var offLeft = element.offsetLeft;
      var index = element.style.zIndex;
      //DEBUG
      //alert("Element id: " + element.id + "\n element tag: " + element.tagName +  "\noffsetWidth: " + wt + "\noffsetHeight: " + ht + "\noffsetTop: " + offTop + "\noffsetLeft: " + offLeft + "\nzIndex: " + index)
      
      var shadowCont = document.createElement("div");
      shadowCont.setAttribute("id", elId + "_shadowContainer");
      //shadowCont.style.width = wt + 10 + "px";
      //shadowCont.style.height = ht + 10 + "px";
      //shadowCont.style.display="block";
      //shadowCont.style.position='absolute';
      //shadowCont.style.top = offTop - ht + "px";
      //shadowCont.style.left = offLeft +"px";
      shadowCont.innerHTML = "&nbsp;";
      element.parentNode.appendChild(shadowCont);
      
      var cycles = 5;
      for (var x = 0; x < cycles; x++)
      {
      var shadow = document.createElement("div");
      if (effect != 'bottom') shadow.style.width = wt + x*2 + "px";
      else shadow.style.width = wt + "px";
      shadow.style.height = ht + x + "px";
      shadow.style.display="block";
      shadow.innerHTML = "&nbsp;";
      shadow.style.position='absolute';
      shadow.style.overflow = 'hidden';
      shadow.style.top = (offTop +x) + "px";
      if (effect != 'bottom') shadow.style.left = (offLeft - x*.5) +"px";
      else shadow.style.left = offLeft +"px";
      if (shadow.style.opacity != null) shadow.style.opacity= .6 - (x/cycles*.6);
      else if (ie6 == false && shadow.style.filter!=null) shadow.style.filter = "alpha(opacity="+ (.6 - x/cycles)*100  +")";
      if (ie6 ==true) shadow.style.backgroundColor = "#" + d2h(Math.round((x/cycles)*255)) + d2h(Math.round((x/cycles)*255)) + d2h(Math.round((x/cycles)*255));
      else shadow.style.backgroundColor = "#000000";
      shadow.style.zIndex = -1 - x;
      //alert(wt + "x" + ht + " - " + offTop + "+" + offLeft + " index: " + index);
      // element.parentNode.appendChild(shadow);
      shadowCont.appendChild(shadow);
      //document.write(shadow.style.backgroundColor);
      }
      
    }
  
}


function applyByClass(className, funct)
{
  var els = getElementsByClassName(className);
  for (el in els)
  {
    elId=el.id
    
  }
  
  
  
}


function getMouseXY(e) {
var IE = document.all?true:false;
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft;
tempY = event.clientY + document.body.scrollTop;
}
else {  // grab the x-y pos.s if browser is NS
tempX = e.pageX;
tempY = e.pageY;
}  
if (tempX < 0){tempX = 0;}
if (tempY < 0){tempY = 0;}  
document.Show.MouseX.value = tempX;
document.Show.MouseY.value = tempY;
return true;
}



function getParentByTagName(element, parent){
if(!element){return null;};

var elements=[];
parent=parent.toUpperCase();
while(element.parentNode)
  {
    element=element.parentNode;
    //elements.unshift(element);
		if(element.nodeName && element.nodeName.toUpperCase()==parent){return element;};
  };
}

//function openPopup(file, name)

function sectionOnLoad(section, elemClass, link)
{
var hash = window.location.hash;
var search = window.location.search;

if (hash || search) return;
else showSection(section, elemClass, link);
}

function sectionOnHash(anchor, section, elemClass, link)
{
if (window.location.hash.substring(1) == anchor) showSection(section, elemClass, link);

}

function sectionOnSearch(question, section, elemClass, link)
{
if (window.location.search.substring(1).split("=")[0] == question)  showSection(section, elemClass, link);
}


function hilightLink(hilightedClass, containerId){
  var container = document.getElementById(containerId);
  if (!container) container = document;
  var anchors = container.getElementsByTagName("A");
  for (var x = 0; x < anchors.length; x++){
    if (window.location.href == anchors[x].href) anchors[x].className +=  hilightedClass;
  }
}


function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls; 
}
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}



function addOtherToSelect(srcSelect, destSelect){
                    var source = document.getElementById(srcSelect);
                    var dest = document.getElementById(destSelect);
                    dest.options.length = 0;
                    dest.disabled = false;
                    for (var x = 0; x < source.options.length; x++) {
                      if (source.options[x].selected == false)
                      {
                      var optn = new Option(source.options[x].text, source.options[x].value);
                      dest.options.add(optn);
                      }
                    }
}


function getElementsByClassName(theClass) {
var nodes = [];
var myclass = new RegExp('\\b'+theClass+'\\b');
var elem = document.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) 
  {
    var classes = elem[i].className;
    if (myclass.test(classes)) nodes.push(elem[i]);
  }
return nodes;
} 

function addLoadEvent(funct) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = funct;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      funct();
    }
  }
}


function showMessage(el){
  var elem = document.getElementById(el);
  elem.style.position = 'fixed';
  elem.style.left = (window.screen.availWidth - elem.style.offsetWidth)/2;
  elem.style.top = (window.screen.availHeight - elem.style.offsetHeight)/2;
  elem.style.zIndex = '99';
  elem.style.display = 'block';
  elem.style.visibility = 'visible';
}

function addWait(el){
var forms = document.getElementsByTagName('FORM');
var elem = document.getElementById(el);
for(var x = 0; x < forms.length; x++ )
  {
    // addEvent2(forms[x], "submit", showMessage('wait'));
    forms[x].onsubmit = function(){showMessage('waitMessage')};
  }
  
  
}


function preloadImg(imagePath, name){
  
          var immagine=new Image();
          immagine.src=imagePath;
          document.getElementById(name).src = immagine.src;
}



/*function validateForm(pwlength, elements) {
var input = [];
var errors = [];
if (!is_array(elements)) return false;
if (!is_int(pwlength.length)) return false; 
// var theForm = document.getElementById(formId);
for (var i = 0; i < elements.length; i++)
  {
  var target = document.getElementById(elements[i]);
  switch (target.type){
    case "text":
        if (target.value.length() == 0) { errors[] = "L'elemento " + target.id + " non deve essere vuoto."; }
        break;
    case "password": 
        if (target.value.length() == 0) { errors[] = "L'elemento " + target.type + " '" + target.id + "' non deve essere vuoto.";}
        if (target.value.length() < pwlength) { errors[] = "L'elemento " + target.type + " '" +  target.id + "' risulta troppo corto.";}
        break;
    case else:
        break;        
    }
  }
  if (errors.length > 0){return errors;}
  else return true;

}

*/

function getCredits()
{
alert(' ZENITH Framework Engine by Guido Cauli <guido.cauli@gmail.com>\n\ Version 1.1.0 (30 Oct 2008).\n Not for Redistribution.\n This software cannot be copied, user and/or modified in any way without explicit permission of the Author. ');
}
