// JavaScript Document

function comando_menu(strComando)
{
	SettaControllo("comandomenu",strComando,"formdinamico");
  submit_form("formdinamico");
}
function comando_menubottone(strComando,strValore)
{
		strComando="formdinamico."+strComando+"_DINAMICO"+".value=\""+strValore+"\";";
		eval(strComando);
		var obj=MM_findObj("formdinamico");
		eval("obj.submit()");
}

function ControlloAxl(strEvento,strIdControllo)
{
	 
	if(strEvento=="change")
	{
		AggiornaFormDinamico(strIdControllo);
	}
	
	switch(strIdControllo)
	{
		case "ELENCOTIPOLOGIE": if(strEvento=="change") comando_menu("tipologiaselezionata"); break;
		case "ELENCOARGOMENTI": if(strEvento=="change") comando_menu("argomentoselezionato"); break;
		case "ELENCOAUTO": if(strEvento=="change") comando_menu("autoselezionata"); break;
		case "OGGETTO": if(strEvento=="change") comando_menu("oggettoselezionato"); break;
		case "ELENCOCARATTERISTICHE": if(strEvento=="change") SettaControllo("VALORECARATTERISTICA",ValoreControllo("ELENCOCARATTERISTICHE","formstatico"),"formstatico"); SettaControllo("VALORECARATTERISTICAOLD",ValoreControllo("ELENCOCARATTERISTICHE","formstatico"),"formstatico");break;
		default: break;
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

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 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 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 ListBoxChangednews(strIdListBox,strComando)
{
	var strRigaJS="formstatico."+strIdListBox+".value";
	var strValore=trim(eval(strRigaJS));
	comando_menubottone(strComando,strValore);
}
function MostraNascondiLivello(strIdLivello,bShow)
{
	var strShow=(bShow==true? "show" : "hide");
	var strComando="MM_showHideLayers('"+strIdLivello+"','','"+strShow+"')";
	eval(strComando);
}

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="arrow";
}
function StoriaIndirizzi(strIdDebitore)
{
	strIndirizzo="storicoindirizzi.php?id="+strIdDebitore;
	MM_openBrWindow(strIndirizzo,'StoricoIndirizzi','width=800,height=400')
}
function CambiaStileButton(strFieldId,strClasse)
{
	MM_changeProp(strFieldId,'','className',strClasse,'INPUT/BUTTON')
}