/*********************************************************************************************
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
FUNZIONI DATAGRID DIV DISLAY INLINE e NONE
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
*********************************************************************************************/
///////////////////////////////////////////////////////////////////
// Funzioni per controllare la visualizzazione delle tabelle o div
// basta passare come valore l'oggetto che si deve visualizzare
// esempio: <a href="javascript:openObject('lista');">test</a>
// lista è ID="lista"
/////////////////////////////////////////////////////////////////// 
function openObject(openObject){
obj=document.getElementById(openObject);
obj.style.display="inline";}
		
function closeObject(closeObject){
obj=document.getElementById(closeObject);
obj.style.display="none";}	


/*********************************************************************************************
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
FUNZIONI CAMPI FORM INPUT TEXT, TEXTAREA, ECC.
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
*********************************************************************************************/	
///////////////////////////////////////////////////////////////////
// Funzione per cambiar colore ai campi input all'interno del form
// basta passare come valore l'oggetto che si deve visualizzare
// esempio: <INPUT name="xxx" class="textinput" 
// onfocus="activatefield(this);allLista();" onblur="deactivatefield(this)" size="11">
///////////////////////////////////////////////////////////////////
dom = (document.getElementById) ? 1 : 0; 
function activatefield(what){
	if (dom){
	what.style.borderColor='#cdc673';
	what.style.backgroundColor='#fff68f';
	what.style.color='#336699';}	
}
function deactivatefield(what){
	if (dom){
		what.style.borderColor='#d0eeac';
	what.style.backgroundColor='';
	what.style.color='';}
	}

///////////////////////////////////////////////////////////////////
// Funzioni per oscurare la pagina sottostante e aprire un iframe
//superiore
//es: <a href="#" onclick="javascript:apriPopIframe('../modali/popUpUploadDocumenti.aspx')" />
/////////////////////////////////////////////////////////////////// 
 function apriPopIframe(pagina)
 {

	openObject('backgroundAbsolute');
	openObject('comuneIframe');
	

	var nomelink = pagina;
	var linkIframe = (nomelink+'');
	frames['comuneIframe'].location.href  = linkIframe;
 }

 function chiudiPopIframe()
 {
	parent.closeObject('backgroundAbsolute');
	parent.closeObject('comuneIframe');
 }
 
/****************************************************************************
*****************************************************************************
CONTROLLO CODICE FISCALE PRIVATO DOPO LA DIGITIZAZZIONE
onblur="codiceFiscalePrivati(this.value,'form','codiceFiscaleRelataNotifica');"
*****************************************************************************
*****************************************************************************/
function codiceFiscalePrivati(valoreCampo){
	
	var valoreCampo;
	var nomeForm;

	
	if	(valoreCampo)
	{
   caratteri= new Array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
   pari= new Array (0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)
   dispari=new Array (1,0,5,7,9,13,15,17,19,21,1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,10,22,25,24,23)
   cod=valoreCampo.toLowerCase();
   check=true;
   if (cod.length!=16){
      check=false
      }
     else
      {
      lettere=cod.substr(0,6)+cod.substr(8,1)+cod.substr(11,1)+cod.substr(15);
      numeri=cod.substr(6,2)+cod.substr(9,2)+cod.substr(12,3);
      for (i=0;i<10;i++){
         if (lettere.charCodeAt(i)<97 || lettere.charCodeAt(i)>122){
            check=false;}
      }
      for (i=0;i<8;i++){
         if (numeri.charCodeAt(i)<48 || numeri.charCodeAt(i)>57){
            check=false;
         }
      }   
     }
   //checksum del codice fiscale
   test=cod.substr(15,1);
   var somma=0
   for (i=0;i<16;i=i+2){ //dispari
       carattere=cod.substr(i,1)
       for (k=0;k<36;k++){
          if (carattere==caratteri[k]){
             somma=somma+dispari[k]
             break
          }
       }
    }
    for (i=1;i<15;i=i+2){ //pari
       carattere=cod.substr(i,1)
       for (k=0;k<36;k++){
          if (carattere==caratteri[k]){
             somma=somma+pari[k]
             break
          }
       }
    }
   resto=somma % 26;
   var lettera=String.fromCharCode(97+resto);            
   if (test != lettera){
      check=false;
      }   
   if (check==false){
      alert("Codice fiscale errato!");
      
      //document.forms[nomeForm].elements[nomeCampo].focus();
	  return false;
      }
	  else
	  {
   	return true;
	  }
	  }
	  	return true;

 }


/****************************************************************************
*****************************************************************************
FUNZIONE CONFERMA
onclick="return functionConferma();" 
*****************************************************************************
*****************************************************************************/
function functionConferma()
{
	action = confirm('Confermi l\'operazione selezionata? L\'operazione non è annullabile!')
	if (action == false){alert('Operazione annullata');return false;}
  	else{return true;}
}


/****************************************************************************
*****************************************************************************
FUNZIONE TUTTO MAIUSCOLO
onkeypress="uppercase()" 
*****************************************************************************
*****************************************************************************/	
function uppercase()
{
  key = window.event.keyCode;
  if ((key > 0x60) && (key < 0x7B))
  window.event.keyCode = key-0x20;
}

/*********************************************************
.validateNumber controllo validazione solo numerico
onkeyup="validateNumber(this);"
*********************************************************/
/* Pour faire une vérification sans autoriser le point ("."), suivez les instructions qui sont écrites en commentaire */

function validateNumber(champ)
{
var chiffres = new RegExp("[0-9\. ]"); /* Modifier pour : var chiffres = new RegExp("[0-9]"); */
var verif;
var points = 0; /* Supprimer cette ligne */

for(x = 0; x < champ.value.length; x++)
{
verif = chiffres.test(champ.value.charAt(x));
if(champ.value.charAt(x) == "."){points++;} /* Supprimer cette ligne */
if(points > 1){verif = false; points = 1;} /* Supprimer cette ligne */
if(verif == false){champ.value = champ.value.substr(0,x) + champ.value.substr(x+1,champ.value.length-x+1); x--;}
}

}

/*********************************************************
.validateCharacterValid controllo validazione solo numerico
onkeyup="validateNumber(this);"
*********************************************************/
/* Pour faire une vérification sans autoriser le point ("."), suivez les instructions qui sont écrites en commentaire */

function validateCharacterValid(champ)
{
var chiffres = new RegExp("[a-zA-Z 0-9 / & , . à  è é ò ì ù ' ° “ ” ’ ]"); /* Modifier pour : var chiffres = new RegExp("[0-9]"); */
var verif;
var points = 0; /* Supprimer cette ligne */

for(x = 0; x < champ.value.length; x++)
{
verif = chiffres.test(champ.value.charAt(x));
if(champ.value.charAt(x) == "."){points++;} /* Supprimer cette ligne */
if(points > 1){verif = false; points = 1;} /* Supprimer cette ligne */
if(verif == false){champ.value = champ.value.substr(0,x) + champ.value.substr(x+1,champ.value.length-x+1); x--;}
}

}

/*********************************************************
.validateNumberTel Telefono
*********************************************************/

function validateNumberTel(champ)
{
var chiffres = new RegExp("[0-9\. +]"); /* Modifier pour : var chiffres = new RegExp("[0-9]"); */
var verif;
var points = 0; /* Supprimer cette ligne */

for(x = 0; x < champ.value.length; x++)
{
verif = chiffres.test(champ.value.charAt(x));
if(champ.value.charAt(x) == "."){points++;} /* Supprimer cette ligne */
if(points > 1){verif = false; points = 1;} /* Supprimer cette ligne */
if(verif == false){champ.value = champ.value.substr(0,x) + champ.value.substr(x+1,champ.value.length-x+1); x--;}
}

}


/*********************************************************
.validateText controllo validazione solo testo
onkeyup="validateText(this);"
*********************************************************/
/* Pour faire une vérification sans autoriser le point ("."), suivez les instructions qui sont écrites en commentaire */

function validateText(champ)
{
var chiffres = new RegExp("[a-zA-Z ]"); /* Modifier pour : var chiffres = new RegExp("[0-9]"); */
var verif;
var points = 0; /* Supprimer cette ligne */

for(x = 0; x < champ.value.length; x++)
{
verif = chiffres.test(champ.value.charAt(x));
if(champ.value.charAt(x) == "."){points++;} /* Supprimer cette ligne */
if(points > 1){verif = false; points = 1;} /* Supprimer cette ligne */
if(verif == false){champ.value = champ.value.substr(0,x) + champ.value.substr(x+1,champ.value.length-x+1); x--;}
}

}



/*********************************************************
.validateAnno
onkeyup="validateNumber(this);"
*********************************************************/
/* Pour faire une vérification sans autoriser le point ("."), suivez les instructions qui sont écrites en commentaire */

function validateAnno(champ,nomeCampo)
{
var nomeCampo;
var champ;
var chiffres = new RegExp("[0-9\.]"); /* Modifier pour : var chiffres = new RegExp("[0-9]"); */
var verif;

for(x = 0; x < champ.value.length; x++)
{
		verif = chiffres.test(champ.value.charAt(x));
		if(verif == false){champ.value = champ.value.substr(0,x) + champ.value.substr(x+1,champ.value.length-x+1); x--;}
}

}



/*********************************************************
.validateEmail controllo validazione email
*********************************************************/
function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) 
   {
      alert("Verifica mail");
        element.style.borderColor = '#ff3030';
       this.focus();
      return false;
    }
    else
    {
        element.style.borderColor = '#ACB7EE';
        return true; 
    }
    
    
}



/*********************************************************
.checkTasto se il carattere è alfabetico minuscolo lo rende maiuscolo
*********************************************************/
function checkTasto()
{
	var Kcode;
	var str;
	var isNotNumeric;
	var isNotAlfa;
	
	isSpecial=false;
	
	/*if (event.keyCode > 96 && event.keyCode < 123)
		event.keyCode = event.keyCode - 32;*/
		
	Kcode=event.keyCode;		
	str = "@àèìòùé\' /,.-";
	isNotNumeric=((Kcode <40 ) || (Kcode >62));
	isNotAlfa=((Kcode <65 ) || (Kcode >90)) && ((Kcode <97 ) || (Kcode >122));
	
	for (j=0;j<str.length;j++)
	{
	if (Kcode==str.charCodeAt(j))
		{
			isSpecial=true;
			break;
		}
	}
	
	if (Kcode==13) 
		isSpecial=true;
	
	

	if ((isNotNumeric==true && isNotAlfa==true && isSpecial==false))
		event.returnValue = false;	
}

/****************************************************************************
*****************************************************************************
CONTROLLO PARTITA IVA DOPO LA DIGITIZAZZIONE
onblur="codicePartitaIva(this.value,'form','codiceFiscaleRelataNotifica');"
*****************************************************************************
*****************************************************************************/


function codicePartitaIva(valoreCampo)
{
	
	
	var valoreCampo;
	if	(valoreCampo)
	{
   
			var codFisc=valoreCampo.toLowerCase();
			var codFiscNum = codFisc.length;
			
			
			if (codFiscNum==11)
			{
				//alert('controllo solo numerici 11');
				var checkOK = "0123456789";
				var checkStr = valoreCampo;
				  var allValid = true;
				  for (i = 0; i < checkStr.length; i++)
				  {
				    ch = checkStr.charAt(i);
				    for (j = 0; j < checkOK.length; j++)
				      if (ch == checkOK.charAt(j))
				        break;
				    if (j == checkOK.length)
				    {
				      allValid = false;
				      break;
				    }
				  }
				  if (!allValid)
				 {
				    alert("La partita IVA è formata da 11 caratteri numerici");
				     element.style.borderColor = '#ff3030';
                      this.focus();
				    return (false);
				  } 
				  else
				  { element.style.borderColor = '#ACB7EE';
                    return true;
                  }
				
			return true;	
			}
			else
			 {
				    alert("La partita IVA è formata da 11 caratteri numerici");
				    element.style.borderColor = '#ff3030';
                      this.focus();
				    return (false);
				  } 

	
                   
	 
	 }
	  	element.style.borderColor = '#ACB7EE';
	  	return true;

 }
 
 /****************************************************************************
*****************************************************************************
Questo è utile sia per eliminare eventuali spazi inseriti per sbaglio a inizio o fine stringa

*****************************************************************************
*****************************************************************************/
 function trim(stringa)
 {
  
    while (stringa.substring(0,1) == ' ')
    {
        stringa = stringa.substring(1, stringa.length);
    }
  
    while (stringa.substring(stringa.length-1, stringa.length) == ' ')
    {
        stringa = stringa.substring(0,stringa.length-1);
    }
     element.value = stringa;
    return stringa;
 }
 
 
 
  function chiudiPopIframe()
 {
	parent.closeObject('backgroundAbsolute');
	parent.closeObject('comuneIframe');
 }
 
 
  /****************************************************************************
*****************************************************************************
Stringa. Prima lettera maiuscola e restante stringa minuscola

*****************************************************************************
*****************************************************************************/
function stringMaiuscola(myString)
{
    myString = myString.toLowerCase();
    this.value =''+myString.charAt(0).toUpperCase()+myString.substr(1);
}


function print_zone(obj) 

{
	var content = document.getElementById('printable_zone').innerHTML;
	var print_window = window.open("", "ZoneImpr", "height=400, width=600, toolbar=0, menubar=0, scrollbars=1, resizable=1, status=0, location=0, left=10, top=10");
	
	print_window.document.body.style.color = '#000000';
	print_window.document.body.style.backgroundColor = '#FFFFFF';
	print_window.document.body.style.padding = "10px";
	print_window.document.title = 'TRACE';
	print_window.document.body.innerHTML += " " + content + " ";
	print_window.window.print();
	print_window.window.close();
	return true;
} 
