function setDisabled(frmName, fldName)
{
  document.forms[frmName].elements[fldName].value = "";
  document.forms[frmName].elements[fldName].disabled=!document.forms[frmName].elements[fldName].disabled;
  
}

function setDisabledEx(frmName, fldName, bValue )
{
  document.forms[frmName].elements[fldName].value = "";
  document.forms[frmName].elements[fldName].disabled=bValue;
  
}

function setPresenza(frmName, fldName, bValue )
{
  if (document.forms[frmName].elements[fldName].value == "0")
       document.forms[frmName].elements[fldName].checked = bValue;	
  
}

function setValue(frmName, fldName, bValue)
{
	document.forms[frmName].elements[fldName].value = bValue;	
	return 1;
}

function locate(url) 
{
 window.open(url,"my_new_window","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=500,height=350")
 //location=url; 
}

function popupEx(url, sizex, sizey)
{
 if (popupwin != 0)
 {
  popupwin.close();
 }
 popupwin = window.open(url,"my_new_window","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+sizex+",height="+sizey)
 
}

function popup(url, sizex, sizey)
{
  window.open(url,"my_new_window","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width="+sizex+",height="+sizey)
}

function popupFull(url)
{
 window.open(url, "my_new_window", "toolbar=yes,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no");
}

function open_for_print(url)
{
 window.open(url,"my_new_window","toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes, resizable=no,copyhistory=no,width=1024,height=600")
}

function opentab(url)
{
 location=url;
}

function ask_for_confirm(url)
{	
 input_box=confirm("Sicuro di voler cancellare?");
 if (input_box==true)
 {
   location=url;
 }
}

function ask_for_confirmSubmit()
{	
 input_box=confirm("Sicuro di voler cancellare?");
 if (input_box==true)
   return true;
 else
   return false;
}


function ask_conf_ex(which)
{
 alert(buttonPressed);	
}

function ask_for_confirm_form()
{	
  if (confirmNeeded)
   {
     confirmNeeded=confirm("Sicuro di voler cancellare?");
     return confirmNeeded;
   }
 else
   return 1;
}

function ask_for_confirm_form_ex(txt)
{	
  if (confirmNeeded)
   {
     confirmNeeded=confirm(txt);
     return confirmNeeded;
   }
 else
   return 1;
}

function warn_mesg(mesg)
{	
 confirm(mesg);
}

var confirmNeeded = 0;
var popupwin = 0;

function conf_needed(value)
{	
  confirmNeeded = value;
}


function handleBookmark(url, titolo_default)
{
  titolo = prompt("Titolo", titolo_default)
  
  if (titolo != null)
    {
       location = url + "&titolo="+titolo
    }
    
}

function closePopupWin()
{
 if (popupwin != 0)
 {
    popupwin.close()
 }
}


function codiceFISCALE(cfins)
{ 
	   var cf = cfins.toUpperCase();
	   //Controllo formale di validita
	   var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
	   if (!cfReg.test(cf))
	     return false;
	   //Controllo del carattere di controllo  
	   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) )
             return false;
	   return true;
}

function semifullcheck_codiceFISCALE(cfins,nome,cognome,sesso,day,month,year)
{
	   var cf = cfins.toUpperCase();
	   //Controllo formale di validita
	   var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
	   if (!cfReg.test(cf))
	     return false;
	   //Controllo del carattere di controllo  
	   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) )
             return false;
	   //Controllo delle prime 11 lettere del codice fiscale, nome cognome sesso data nascita
	   nome = nome.toUpperCase();
	   cognome=cognome.toUpperCase();
	   var n=0;
	   n = sesso.length;
	   var sex="";
           for(i=0;i<n;i++) {
             if(sesso[i].checked==true)
	       sex=sesso[i].defaultValue;
	   }
	   if(sex=="")
	     return false;
	   sex=sex.toUpperCase();
	   
	   var calc_cod_fisc="";

	   calc_cod_fisc=CalcolaCognome(cognome);
	   calc_cod_fisc=calc_cod_fisc+CalcolaNome(nome);
	   calc_cod_fisc=calc_cod_fisc+CalcolaNascita(day, month, year, sex);
	   cf=cf.substring(0,11);
	   if(cf==calc_cod_fisc) 
	     return true;
	   else
	{
	alert ("cf calcolato:" + calc_cod_fisc);
	    return false;
	}	
}

function GetConsonanti(Stringa)
{
   var consonanti =  "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
   var cns = "";
   for (i = 0; i < Stringa.length; i++)
      if (consonanti.indexOf(Stringa.substring(i, i + 1)) != -1)
         cns += Stringa.substring(i, i + 1);
   return cns.toUpperCase();
}

function GetVocali(Stringa)
{
   var consonanti =  "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
   var voc = "";
   for (i = 0; i < Stringa.length; i++)
      if (consonanti.indexOf(Stringa.substring(i, i + 1)) == -1 && Stringa.substring(i, i + 1) != " ")
         voc += Stringa.substring(i, i + 1);
   return voc.toUpperCase();
}

function CalcolaCognome(Cognome)
{

   Cognome=Cognome.replace(/'/,"");
   var code = "";
   code = GetConsonanti(Cognome);
   if (code.length >= 3)
      code = code.substring(0, 3);
   else
   {
      code += GetVocali(Cognome).substring(0, 3 - code.length)
      if (code.length < 3)
         for (i = code.length; i < 3; i++)
            code += "X";
   }
   return code;
}

function CalcolaNome(Nome)
{
   Nome=Nome.replace(/'/,"");
   var code = "";
   cons = GetConsonanti(Nome);
   if (cons.length > 3)
      code = cons.substring(0, 1) + cons.substring(2, 3) + cons.substring(3, 4);
   else if (cons.length == 3)
      code = cons;
   else
   {
      code = cons + GetVocali(Nome).substring(0, 3 - cons.length);
      if (code.length < 3)
         for (i = code.length; i < 3; i++)
            code += "X";
   }
   return code;
}

function CalcolaNascita(Giorno, Mese, Anno, Sesso)
{
   var mesi = "ABCDEHLMPRST";
   var code = "";
   code += Anno.substring(2, 4) + mesi.charAt(Mese-1);
   if (Sesso == "M")
       {
	   if (Giorno<10 && Giorno.length<2)
	       code += "0";
	   code += Giorno;
       }	
   else
      code += parseInt(40, 10) + parseInt(Giorno, 10);
   return code;
}

function partitaIVA(sz_Codice)
{	
   var n_Val,n_Som1=0,n_Som2=0,lcv;
   if (sz_Codice.length!=11 || isNaN(parseFloat(sz_Codice)) || parseFloat(sz_Codice)<parseFloat(0))
      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;
   }

	if ((n_Som1 % 10) == 0)
		n_Som2 = 0;
	else
	   n_Som2 = 10 - (n_Som1 % 10);
   
	n_Val=parseInt(sz_Codice.charAt(10));
   if (n_Som2==n_Val)
      return true;
   return false;
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ) //in ore
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

