// JavaScript Document

function Trim(s){
	// implemento la funzione Trim in javascript
	s = s.replace(/^\s*/,'').replace(/\s*$/, '');
	return s;
}

function displayOnOff(idElem){
	current=(document.getElementById(idElem).style.display == 'none') ? 'block' : 'none';
	document.getElementById(idElem).style.display = current;
}

function apriPopup(imgPath, testo) {
	/*  apre una popup e le passa 2 parametri
	*/
	var urlPopup = "popup.asp?imgPath=" + imgPath + "&testo=" + testo;
	window.open(urlPopup,'popup','toolbar=yes,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,width=200,height=200');
}

function displayHideLayers() { //v6.0
  var i,p,v,obj,args=displayHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'block':(v=='hide')?'none':v; }
    obj.display=v; }
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' deve contenere un indirizzo e-mail.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- il campo "'+nm+'" deve contenere un numero.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' dave contenere un numero tra '+min+' e '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- il campo "'+nm+'" è richiesto.\n'; }
  } if (errors) alert('ATTENZIONE\n\n'+'per salvare la pagina:\n\n'+errors);
  document.MM_returnValue = (errors == '');
}


function addEvent(obj, evt, func) { 
	// AGGIUNGE una funzione all'event-handler di un oggetto (es. aggiunge funzione a window.onload)
	// anzichè sovrascriverlo (e permette l'uso di "this" nella funzione)
	//es. addEvent(window,'onload', inizializzaCSS);
	  if(obj[evt]) { 
		obj[evt]=function(f,g){ 
		   return function(){ 
			 f.apply(this,arguments); 
			 return g.apply(this,arguments); 
		   }; 
		}(func, obj[evt]); 
	  } else obj[evt]=func; 
} 


function writeCookie(name, value, hours) {

	// writeCookie("myCookie", "my name", 24);
	// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.

  var expire = "";
  if(hours != null) {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}


function readCookie(name) {
	
	// alert( readCookie("myCookie") );

  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0) { 
    offset = document.cookie.indexOf(search);
    if (offset != -1){ 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

