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_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_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

function MM_getProp(objName,theProp) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    return(obj.theProp);
  }
}

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_setTextOfLayer(objName,newText) { //v4.01
  if ((obj=MM_findObj(objName))!=null) with (obj)
	innerHTML = unescape(newText);
}

function MM_getTextOfLayer(objName) { //v4.01
  if ((obj=MM_findObj(objName))!=null) with (obj)
	return(innerHTML);
}

function LeggiTestoDiv(nomeDiv)
{
	return(MM_getTextOfLayer(nomeDiv));
}

function SettaTestoDiv(nomeDiv,testo)
{
	return(MM_setTextOfLayer(nomeDiv,testo));
}

function SpostaDiv(strDiv,iX,iY)
{
	obj = document.all[strDiv].style;
	obj.left = iX;
    obj.top = iY;
}

function DimensioneDiv(strDiv,iWidth,iHeight)
{
	obj = document.all[strDiv].style;
	obj.width = iWidth;
    obj.height = iHeight;
}

function BW_reload() {location.reload();}

function BW_centerLayers() { //v4.1.1
	if (document.layers || document.all || document.getElementById){
		var winWidth, winHeight, i, horz, vert, width, height, offsetX, offsetY, negX, negY, group, x, y, args;
		args = BW_centerLayers.arguments;
		
		onresize = BW_reload;

				
		winWidth = (document.all)?document.body.clientWidth:window.innerWidth;
		winHeight = (document.all)?document.body.clientHeight:window.innerHeight;
				
		for (i=0; i<(args.length-9); i+=10) {
			horz    = args[i+1];
			vert    = args[i+2];
			width   = parseInt(args[i+3]);
			height  = parseInt(args[i+4]);
			offsetX = parseInt(args[i+5]);
			offsetY = parseInt(args[i+6]);
			negX    = args[i+7];
			negY    = args[i+8];
		
			x = ((winWidth - width)/2) + offsetX;
			y = ((winHeight - height)/2) + offsetY;
						
			x = (negX=='false' && (x < 0))?0:x;
			y = (negY=='false' && (y < 0))?0:y;
				
			layerObj = (document.getElementById)?document.getElementById(args[i]):MM_findObj(args[i]);
			
			if (layerObj!=null) {
				layerObj = (layerObj.style)?layerObj.style:layerObj;
				layerObj.left = (horz=="true")?x:layerObj.left;
				layerObj.top = (vert=="true")?y:layerObj.top;
			}
		}
	}
}

function CambiaStile(strIdControllo,strClasse)
{
	strComando="document.getElementById('"+strIdControllo+"').className='"+strClasse+"'";
	eval(strComando);
}

function submit_form(strNomeForm)
{
	var obj=MM_findObj(strNomeForm);
	eval("obj.submit()");
}

function SettaControllo(strIdControllo,strValore,strNomeForm)
 {
	if(strNomeForm!=undefined) strComando="document.forms['"+strNomeForm+"']['"+strIdControllo+"'].value='"+strValore+"'";
	else strComando="document.getElementById('"+strIdControllo+"').value='"+strValore+"'";
	eval(strComando);
 }

function ValoreControllo(strIdControllo,strNomeForm)
 {
  	var strValore=new String;
  	if(strNomeForm!=undefined) strComando="strValore=document.forms['"+strNomeForm+"']['"+strIdControllo+"'].value";
	else strComando="strValore=document.getElementById('"+strIdControllo+"').value";
	eval(strComando);
	return(strValore);
 }
 
 function ValoreListBox(strIdControllo,strNomeForm)
 {
    var iSelectedIndex;
	var strValore;
	
	if(strNomeForm!=undefined) 
	{
	 strComando="iSelectedIndex=document.forms['"+strNomeForm+"']['"+strIdControllo+"'].selectedIndex";
	 eval(strComando);
	 strComando="strValore=document.forms['"+strNomeForm+"']['"+strIdControllo+"'].options["+iSelectedIndex+"].text";
	 eval(strComando);
	 }
	 else
	 {
	   strComando="iSelectedIndex=document.getElementById('"+strIdControllo+"').selectedIndex";
	   eval(strComando);
	   strComando="strValore=document.getElementById('"+strIdControllo+"').options["+iSelectedIndex+"].text";
	   eval(strComando);
	 }
	 return(strValore);
 }
 
function AggiornaFormDinamico(strIdControllo)
{
	var strValore=escape(trim(ValoreControllo(strIdControllo)));
	SettaControllo(strIdControllo+"_DINAMICO",strValore,"formdinamico");
}

function ListBoxChanged(strIdListBox,strComando)
{
	var strValore=trim(ValoreControllo(strIdListBox+"_DINAMICO","formdinamico"));
	comando_menu(strComando,strValore);
}

function ltrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

function rtrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}


function trim(str)
{
   return rtrim(ltrim(str));
}
 
  
function CheckCodiceFiscale(strIdControl)
 {
   var cferrato="Il codice fiscale non è esatto";
   var strComando="formstatico."+strIdControl+".value";
	var cfins=trim(eval(strComando));
   var cf = cfins.toUpperCase();
   if(cf=="") return(true);
   var cfReg = /^[A-Z0-9]{16}$/;
   if (!cfReg.test(cf))
      {alert(cferrato); return false;}
   var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
   var s = 0;
   for( i = 1; i <= 13; i += 2 )
      s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
   for( i = 0; i <= 14; i += 2 )
      s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
   if ( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
      {alert(cferrato); return false;}
   return true;
 }

function CheckCodiceFiscaleAziendale(strIdControl)
 {
    var strComando="formstatico."+strIdControl+".value";
	var cfins=trim(eval(strComando));
   var cf = cfins.toUpperCase();
   ControlChanged(strIdControl);
   var cfReg = /^[A-Z0-9]{16}$/;
   if (cfReg.test(cf)) return(CheckCodiceFiscale(strIdControl));
	else return(CheckPartitaIva(strIdControl,true));	
 }
 
 function CheckPartitaIva(strIdControl,bIsCF)
  {
   var cferrato=(bIsCF==true? "Il codice fiscale non è esatto" : "La partita iva non è esatta");
   var strComando="formstatico."+strIdControl+".value";
	var sz_Codice=trim(eval(strComando));
	 if(sz_Codice=="") return(true);
   var n_Val,n_Som1=0,n_Som2=0,lcv;
   if (sz_Codice.length!=11 || isNaN(parseFloat(sz_Codice)) || parseFloat(sz_Codice)<parseFloat(0))
      {alert(cferrato); return false;}
   
   for (lcv=0;lcv<9;lcv+=2)
   {
      n_Val=parseInt(sz_Codice.charAt(lcv));
      n_Som1+=n_Val;
      n_Val=parseInt(sz_Codice.charAt(lcv+1));
      n_Som1+=Math.floor(n_Val/5) + (n_Val<<1) % 10;
   }
   n_Som2 = 10 - (n_Som1 % 10);
   n_Val=parseInt(sz_Codice.charAt(10));
   if (n_Som2==n_Val)
      return true;
   alert(cferrato); return false;
  }

 function CheckEMail(strIdControl)
 {
 	var strErrato="Il campo non sembra contenere un indirizzo e-mail valido";
   var strComando="formstatico."+strIdControl+".value";
	var strEMail=trim(eval(strComando));
	if(strEMail=="") return true;
	 var eReg = /^[A-Z0-9\.\-\(\)_]{1,}@[A-Z0-9\.\-\(\)_]{2,}.[a-z]{2,4}$/i;
   if (!eReg.test(strEMail)) {alert(strErrato); return false;}
   else return true;
 }
 

function CursoreWait()
{
	document.body.style.cursor="wait";
}

function CursoreMano()
{
	document.body.style.cursor="hand";
}

function CursoreNormale()
{
	document.body.style.cursor="auto";
}

function jsbase64_encode(inp)
{
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
"abcdefghijklmnopqrstuvwxyz" + //all lowercase
"0123456789+/="; // all numbers plus +/=

var out = ""; //This is the output
var chr1, chr2, chr3 = ""; //These are the 3 bytes to be encoded
var enc1, enc2, enc3, enc4 = ""; //These are the 4 encoded bytes
var i = 0; //Position counter
do { //Set up the loop here
chr1 = inp.charCodeAt(i++); //Grab the first byte
chr2 = inp.charCodeAt(i++); //Grab the second byte
chr3 = inp.charCodeAt(i++); //Grab the third byte
//Here is the actual base64 encode part.
//There really is only one way to do it.
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;

if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
//Lets spit out the 4 encoded bytes
out = out + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) +
keyStr.charAt(enc4);
// OK, now clean out the variables used.
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < inp.length); //And finish off the loop
//Now return the encoded values.
return out;
}

function jsbase64_decode(inp)
{
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
"abcdefghijklmnopqrstuvwxyz" + //all lowercase
"0123456789+/="; // all numbers plus +/=

var out = ""; //This is the output
var chr1, chr2, chr3 = ""; //These are the 3 decoded bytes
var enc1, enc2, enc3, enc4 = ""; //These are the 4 bytes to be decoded
var i = 0; //Position counter
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
var base64test = /[^A-Za-z0-9\+\/\=]/g;
if (base64test.exec(inp)) { //Do some error checking
alert("There were invalid base64 characters in the input text.\n" +
"Valid base64 characters are A-Z, a-z, 0-9, ?+?, ?/?, and ?=?\n" +
"Expect errors in decoding.");
}
inp = inp.replace(/[^A-Za-z0-9\+\/\=]/g, "");
do { //Here’s the decode loop.
//Grab 4 bytes of encoded content.
enc1 = keyStr.indexOf(inp.charAt(i++));
enc2 = keyStr.indexOf(inp.charAt(i++));
enc3 = keyStr.indexOf(inp.charAt(i++));
enc4 = keyStr.indexOf(inp.charAt(i++));
//Heres the decode part. There’s really only one way to do it.
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
//Start to output decoded content
out = out + String.fromCharCode(chr1);
if (enc3 != 64) {
out = out + String.fromCharCode(chr2);
}
if (enc4 != 64) {
out = out + String.fromCharCode(chr3);
}
//now clean out the variables used
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < inp.length); //finish off the loop
//Now return the decoded values.
return out;
}

function CreaXMLHttpRequest()
{
 var xmlhttp=null;
 try 
   {
     xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
   } 
   catch (e) 
   {
     alert("Il suo browser non è aggiornato per le funzioni AJAX - Your browser is not up to date for AJAX functions");
   }
   return(xmlhttp);  
}

function GetRequest(strUrl)
{
 var req = CreaXMLHttpRequest();
 req.open('GET',strUrl,false);
 req.send(null);
 if(req.status==200) return(req.responseText);
 else return(parseString(req.status));
}

function PostRequest(strUrl)
{
 var req = CreaXMLHttpRequest(); 
 req.open('POST',strUrl,false);
 req.send(null);
 if(req.status==200) return(req.responseText);
 else return(parseString(req.status));
}

var FAJreq;
var FAJiSem=0;
var strFAJUrl;
var strFAJResult;
var strFAJElemento;
var strFAJProprieta;
var strFAJCodeAfter;

function FAJWait()
{
 zz = document.getElementsByTagName('A');
 for (var i=0;i < zz.length; i++)
 zz[i].style.cursor='wait';

 zz = document.getElementsByTagName('INPUT');
 for (var i=0;i < zz.length; i++)
 zz[i].style.cursor='wait';
 
 zz = document.getElementsByTagName('IMAGE');
 for (var i=0;i < zz.length; i++)
 zz[i].style.cursor='wait';
 
 zz = document.getElementsByTagName('DIV');
 for (var i=0;i < zz.length; i++)
 zz[i].style.cursor='wait';
}

function FAJNoWait()
{
 zz = document.getElementsByTagName('A');
 for (var i=0;i < zz.length; i++)
 zz[i].style.cursor='';

 zz = document.getElementsByTagName('INPUT');
 for (var i=0;i < zz.length; i++)
 zz[i].style.cursor='';
 
 zz = document.getElementsByTagName('IMAGE');
 for (var i=0;i < zz.length; i++)
 zz[i].style.cursor='';
 
 zz = document.getElementsByTagName('DIV');
 for (var i=0;i < zz.length; i++)
 zz[i].style.cursor='';
}

function FAJSend(strUrl,strMetodo,strElemento,strProprieta)
{
	var element;
	
	FAJWait();
	strFAJResult=null;
	strFAJElemento=strElemento;
	strFAJProprieta=strProprieta;
	FAJCodeAfter="";
	FAJRequest(strUrl,strMetodo);
}

function FAJSendCodeAfter(strUrl,strMetodo,strElemento,strProprieta,strCodeAfter)
{
	var element;
	
	FAJWait();
	strFAJResult=null;
	strFAJElemento=strElemento;
	strFAJProprieta=strProprieta;
	strFAJCodeAfter=strCodeAfter;
	FAJRequest(strUrl,strMetodo);
}

function FAJReceiver()
{
  var element;
  var strComando;
  
  if(FAJreq.readyState==4)
  {
	FAJNoWait();
	FAJiSem=0;
	strFAJResult=FAJreq.responseText;
	if(strFAJElemento!="" && strFAJProprieta!="")
	{
	 element=document.getElementById(strFAJElemento);
	 strComando="element."+strFAJProprieta+"=strFAJResult";
	 eval(strComando);
	 if(strFAJCodeAfter!="") eval(strFAJCodeAfter);
	}
  }
}

function FAJRequest(strUrl,strMetodo)
{
	if(FAJiSem==0)
	{
	  FAJiSem=1;
	  strFAJResult=null;
      if(strMetodo!="GET" && strMetodo!="POST") strMetodo="GET";
	  FAJreq = CreaXMLHttpRequest(); 
      FAJreq.open(strMetodo,strUrl,true);
	  FAJreq.onreadystatechange = FAJReceiver;
      FAJreq.send(null);
    }
}

function AlxlAjaxScreenFlush()
{  
 //
}

function AlxlAjaxWait()
{
 FAJWait();
 AlxlAjaxScreenFlush();
}

function AlxlAjaxNoWait()
{
 FAJNoWait();
}

function AlxlAjaxSynSend(strFunzione,strDati,strElemento,strProprieta)
{
 AlxlAjaxWait();
 var strQuery="&funz="+strFunzione+"&"+strDati;
 strTesto=new String(PostRequest("ajaxhelper.php?"+strQuery));
 AlxlAjaxNoWait();
 var elemento=document.getElementById(strElemento);
 var strComando="elemento."+strProprieta+"=strTesto";
 eval(strComando);
}

function AlxlAjaxAsynSend(strFunzione,strDati,strElemento,strProprieta,strCodeAfter)
{
 var strQuery="&funz="+strFunzione+"&"+strDati;
 FAJSendCodeAfter("ajaxhelper.php?"+strQuery,"POST",strElemento,strProprieta,strCodeAfter);
}

function AlxlAjaxAsynSend2(strFunzione,strDati,strElemento,strProprieta,strCodeFirst,strCodeAfter)
{
 var strQuery="&funz="+strFunzione+"&"+strDati;
 if(strCodeFirst!="" ) eval(strCodeFirst);
 var AlxlAjaxAsynSendEval2_req=CreaXMLHttpRequest();
 AlxlAjaxAsynSendEval2_req.onreadystatechange=function()
 {
  if(AlxlAjaxAsynSendEval2_req.readyState==4)
  {
	if(strElemento!="" && strProprieta!="")
	{
	 element=document.getElementById(strElemento);
	 strComando="element."+strProprieta+"=AlxlAjaxAsynSendEval2_req.responseText";
	 eval(strComando);
	}
	if(strCodeAfter!="") eval(strCodeAfter);
  }
 }
 AlxlAjaxAsynSendEval2_req.open("POST","ajaxhelper.php?"+strQuery,true);
 AlxlAjaxAsynSendEval2_req.send(null); 
}

function AlxlAjaxAsynSendEval(strFunzione,strDati,strCodeFirst)
{
	var strQuery="&funz="+strFunzione+"&"+strDati;
	if(strCodeFirst!="" ) eval(strCodeFirst);
	var AlxlAjaxAsynSendEval_req=CreaXMLHttpRequest();
	AlxlAjaxAsynSendEval_req.onreadystatechange=function()
	{
  	 if(AlxlAjaxAsynSendEval_req.readyState==4 )
	 {
		eval(AlxlAjaxAsynSendEval_req.responseText);
	 }
	}
	 AlxlAjaxAsynSendEval_req.open("POST","ajaxhelper.php?"+strQuery,true);
     AlxlAjaxAsynSendEval_req.send(null);
}


var bWhattaDoRun=false;
var iWhattaDoDelay=3000;
function AlxlAjaxWhattaDo()
{
    var AlxlAjaxAsynSendEval_req=CreaXMLHttpRequest();
	AlxlAjaxAsynSendEval_req.onreadystatechange=function()
	{
  	 if(AlxlAjaxAsynSendEval_req.readyState==4)
	 {
		if(AlxlAjaxAsynSendEval_req.responseText=="") bWhattaDoRun=false;
		else eval(AlxlAjaxAsynSendEval_req.responseText);
	 }
	}
	 AlxlAjaxAsynSendEval_req.open("POST","ajaxhelper.php?"+"funz=whattado",true);
     AlxlAjaxAsynSendEval_req.send(null);
	 if(bWhattaDoRun==true) setTimeout("AlxlAjaxWhattaDo",iWhattaDoDelay);
}

function ElementoVisibile(strID,bVisibile) {
 var element = document.getElementById(strID); 
 element.style.display=(bVisibile==true? "block" : "none");
}

function DivMessage(iddiv,height,strMessage)
{
   obj=document.getElementById(iddiv);
   var strTesto="<table width=\"100%\" align=\"center\"><tr><td align=\"center\" height=\""+height+"\">"+strMessage+"</td></tr></table>";
   obj.innerHTML=strTesto;
}

function AbilitaElemento(strElemento,bAbilitato)
{
 bAbilitato=(bAbilitato==true? true : false);
 var element=document.getElementById(strElemento);
 element.disabled=(bAbilitato==true? false : true);
}

function TrasparenzaElemento(strIDElemento,element,iPercentuale)
{
 var element;

 if(element=="") var element=document.getElementById(strIDElemento);
 element.style.opacity=iPercentuale/100;
 element.style.filter="alpha(opacity="+iPercentuale+")";
 element.style.MozOpacity=iPercentuale/100;
 element.style.KHTMLOpacity=iPercentuale/100;
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
