
// funzione per la finestra popup centrata rispetto allo schermo
function openWindow(thepage,thewname,wheigth,wweigth,scroll_,menu_,tool_,resize_)
{
   var aw = screen.availWidth;
   var ah = screen.availHeight;
   var film_width = wweigth; 
   var film_heigth = wheigth;

   posW = (screen.availWidth/2)-(film_width/2);
   posH = (screen.availHeight/2)-(film_heigth/2);
  
   if(document.all)
     newWin = window.open(thepage,thewname,"toolbar="+tool_+",scrollbars="+scroll_+",menubar="+menu_+",resizable="+resize_+",height="+film_heigth+",width="+film_width+",left="+posW+",top="+posH+",status=yes");
   else
     newWin = window.open(thepage,thewname,"toolbar="+tool_+",scrollbars="+scroll_+",menubar="+menu_+",resizable="+resize_+",height="+film_heigth+",width="+film_width+",screenX="+posW+",screenY="+posH+",status=yes");
   newWin.focus();
}

//controllo della presenza di spazi bianchi all'interno di un campo
function isblank(stringa)
{
  for(var i=0;i<stringa.length;i++)
	 {
	   var c=stringa.charAt(i);
		 if((c!='') && (c!= '\n') && (c != '\t')) return false;
   }
	 return true;
}	 


//controllo del form di login
function CheckDataAccess(theform)
{
  if ((theform.login.value.length==0) || (theform.passwd.value.length==0))
	{
	 if (isblank(theform.login.value) || isblank(theform.passwd.value))
	 {
	   alert ("Per accedere  necessario inserire dei valori validi!");
		 return false;
	 }	 
	}
	else
	 {
	  alert ("Verifica autorizzazioni utente "+ theform.login.value+"....");
	  return true;
	 }
}

// cancellazione dei campi del form di login
function CancellaDataAccess()
{
   document.user_login.login.value="";
   document.user_login.passwd.value="";
   document.user_login.login.focus();
}


function chkEmail(theform,orig)
  {
   if (orig==0)
      campo=theform.email;
   if (orig==1)
      campo=theform.email_cli;

  if(campo.value.length > 7 )
   {
   var stremail = campo.value;
   var result1 = stremail.indexOf("@");
   var result2 = stremail.indexOf(".");
   if(result1 < 0 || result2 < 0)
    {
     alert("Indirizzo email non valido!");
     campo.focus();
     campo.select();
     return false;
    }
   }
  /*else
   {
     alert("Indirizzo email non valido!");
     campo.focus(); 
     campo.select();
     return false;
   }*/
}
	

//apertura pagina grande
function bigPage(thepage)
{
var aw = screen.availWidth - 40;
var ah = screen.availHeight - 80;
//window.resizeTo(aw,ah);
//window.moveTo(0,0);
posW = (screen.availWidth/2)-(aw/2);
posH = (screen.availHeight/2)-(ah/2);

  if(document.all)
      newWindow = window.open(thepage,'bigwin',"toolbar=yes,scrollbars=yes,menubar=yes,resizable=yes,height="+ah+",width="+aw+",left="+posW+",top="+posH+",status=yes");
  else
      newWindow = window.open(thepage,'bigwin',"toolbar=yes,scrollbars=yes,menubar=yes,resizable=yes,height="+ah+",width="+aw+",screenX="+posW+",screenY="+posH+",status=yes");
 newWindow.focus();
}

//conferma eliminazione elemento
function CheckDelete(tipo,codice)
{
  var risposta=window.confirm("Eliminare definitivamente la voce selezionata?");
  if (risposta)
   {
    //document.location.href='http://leandro/fintel/backoffice/cancella.php?tipo='+tipo+'&code=' + codice;
	document.location.href='http://www.fintel.bz/backoffice/cancella.php?tipo='+tipo+'&code=' + codice;
    return true;
    }
}

//controllo codice fiscale e partita IVA
function chkCFPI(theform,orig)
{
 if (orig==1)
  {
   campo=theform.cf;
   valore=6;
  }

if (orig==2)
  {
   campo=theform.intest_cf;
   valore=3;
  }

if (orig==3)
 {
    campo=theform.cfpiva_sott;
    valore=4;
 }

if (orig==4)
 {
    campo=theform.cfpiva_int;
    valore=5;
 }

if ((campo.value.length != 16) && (campo.value.length != 11))
  {
     alert("Codice fiscale o partita iva errati!");
     campo.focus();
     campo.select();
     return false;
   }
  else
   {
       
       if(campo.value.length==11)
            chkPI(theform,valore);
       if(campo.value.length==16)
            chkCF(theform,valore);
   }
}


function chkPI(theform,orig)
{
    if (orig==0)
       piva=theform.piva;
    if (orig==1)
       piva=theform.piva_cli;
    if (orig==2)
       piva=theform.piva_intest;
    if (orig==3)
       piva=theform.intest_cf;
    if (orig==4) 
       piva=theform.cfpiva_sott;
    if (orig==5) 
       piva=theform.cfpiva_int;
    if (orig==6) 
       piva=theform.cf;    

	if( piva.value.length != 11 )
           {
	     alert( "La lunghezza della partita IVA non  \n" +
		"corretta: la partita IVA dovrebbe essere lunga\n" +
		"esattamente 11 caratteri.\n");
                piva.focus();
                piva.select();
                return false;
            }

	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( piva.value.charAt(i) ) == -1 )
                            {
			alert("La partita IVA contiene un carattere non valido " +
				theform.piva.value.charAt(i) + ".\nI caratteri validi sono le cifre.\n");
                                   piva.focus();
                                   piva.select();
                                   return false;
                             }
           }
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += piva.value.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( piva.value.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != piva.value.charCodeAt(10) - '0'.charCodeAt(0) )
           {
	        alert ("La partita IVA non  valida:\n" +
			"il codice di controllo non corrisponde.\n");
                     piva.focus();
                     piva.select();
                     return false;
            }
}


function chkCF(theform,orig)
{
    if ((orig==0) || (orig==6))
        cf=theform.cf;
    if (orig==1)
        cf=theform.cf_cli;
    if (orig==2)
        cf=theform.cf_intest;
    if (orig==3)
       piva=theform.intest_cf;
    if (orig==4) 
       piva=theform.cfpiva_sott;
    if (orig==5) 
       piva=theform.cfpiva_int;
 


	var validi, i, s, set1, set2, setpari, setdisp;
           	
	cf.value = cf.value.toUpperCase();
           if( cf.value.length != 16 )
            {
		alert("La lunghezza del codice fiscale non  \n"
		+"corretta: il codice fiscale dovrebbe essere lungo\n"
		+"esattamente 16 caratteri.\n");
                        cf.focus();
                        cf.select();
                        return false;
   
             }
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.value.charAt(i) ) == -1 )
                        {
			alert ("Il codice fiscale contiene un carattere non valido " +
				cf.value.charAt(i) +
				".\nI caratteri validi sono le lettere e le cifre.\n");
                                   cf.focus();
                                   cf.select();
                                   return false;

                        }
	}
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for( i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.value.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.value.charAt(i) )));
	if( s%26 != cf.value.charCodeAt(15)-'A'.charCodeAt(0) )
           {
		alert("Il codice fiscale non  corretto:\n"+
			"il codice di controllo non corrisponde.\n");
                       cf.focus();
                       cf.select();
                       return false;

            }
}


function Compare(thevalue)
{
   var theform=document.tabella;
   
   if ((theform.urbane.value.length != 0) && (theform.interdistrett.value.length != 0) && (theform.interurbane.value.length != 0) && (theform.cell.value.length != 0) && (theform.internaz.value.length != 0)) 
   {
      for(ctr=1;ctr<listini.length;ctr++)
      {
        if (listini[ctr][0]==(thevalue))
        {
           if (listini[ctr][1]=='y')
            {
               fin_urb=(listini[ctr][3]*5)+listini[ctr][2];
               fin_interurb=(listini[ctr][4]*5)+listini[ctr][2];
               fin_cell=(listini[ctr][5]*5)+listini[ctr][2];
               fin_internaz=(listini[ctr][6]*5)+listini[ctr][2];
               fin_interdistr=(listini[ctr][3]*5)+listini[ctr][2];

               tel_urb=theform.urbane.value*5;
               tel_interurb=theform.interurbane.value*5;
               tel_interdistr=theform.interdistrett.value*5;
               tel_cell=theform.cell.value*5;
               tel_internaz=theform.internaz.value*5;

               m_urb=Math.round(((fin_urb-tel_urb)/tel_urb)*100);
               m_interurb=Math.round(((fin_interurb-tel_interurb)/tel_interurb)*100);
               m_interdistr=Math.round(((fin_interdistr-tel_interdistr)/tel_interdistr)*100);
               m_cell=Math.round(((fin_cell-tel_cell)/tel_cell)*100);
               m_internaz=Math.round(((fin_internaz-tel_internaz)/tel_internaz)*100);
             }
           else
            {
               m_urb=Math.round(((listini[ctr][3]-theform.urbane.value)/theform.urbane.value)*100);
               m_interurb=Math.round(((listini[ctr][4]-theform.interurbane.value)/theform.interurbane.value)*100);
               m_interdistr=Math.round(((listini[ctr][3]-theform.interdistrett.value)/theform.interdistrett.value)*100);
               m_cell=Math.round(((listini[ctr][5]-theform.cell.value)/theform.cell.value)*100);
               m_internaz=Math.round(((listini[ctr][6]-theform.internaz.value)/theform.internaz.value)*100);
             }

            
             theform.m_urbane.value=parseFloat(m_urb)+" %";
             theform.m_idistrett.value=parseFloat(m_interdistr)+" %";
             theform.m_interurb.value=parseFloat(m_interurb)+" %";
             theform.m_cell.value=parseFloat(m_cell)+"%";
             theform.m_internaz.value=parseFloat(m_internaz)+" %";

             if (m_urb > 0)
             {
                 theform.m_urbane.className='positivo';
                 theform.m_urbane.value="+" + theform.m_urbane.value;
             }
             else
                 theform.m_urbane.className='negativo';
            
             if (m_interdistr > 0)
             {
                 theform.m_idistrett.className='positivo';
                 theform.m_idistrett.value="+" + theform.m_idistrett.value;
             }
             else
                 theform.m_idistrett.className='negativo';
           

             if (m_interurb > 0)
             {
                 theform.m_interurb.className='positivo';
                 theform.m_interurb.value="+" + theform.m_interurb.value;
             }
             else
                 theform.m_interurb.className='negativo';
           

             if (m_cell > 0)
             {
                 theform.m_cell.className='positivo';
                 theform.m_cell.value="+" + theform.m_cell.value;
             }
             else
                theform.m_cell.className='negativo';
           

             if (m_internaz > 0)
             {
                 theform.m_internaz.className='positivo';
                 theform.m_internaz.value="+" + theform.m_internaz.value;
             }
             else
                 theform.m_internaz.className='negativo';
                     
         break;    
         } 
      }
 }
}


function CheckValue(theform,orig)
{
 switch (orig)
 {
    case 0:
    campo=theform.urbane;
    break;
  
    case 1:
    campo=theform.interdistrett;
    break;

    case 2:
    campo=theform.interurbane;
    break;

    case 3:
    campo=theform.cell;
    break;

    case 4:
    campo=theform.internaz;
    break;

 }

var stringa=campo.value;
re=",";
r = stringa.replace(re, ".");   
campo.value=r;

validi ="0123456789.";
for( i = 0; i < campo.value.length; i++ )
{
 if( validi.indexOf( campo.value.charAt(i) ) == -1 )
    {
        alert ("Inserire un valore numerico!");
        campo.focus();
        campo.select();
        return false;
    }
 }
}


function printit()
{  

var NS = (navigator.appName == "Netscape");
var VERSION = parseInt(navigator.appVersion);

if (NS) {
    window.print() ;  
} else {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, 2);
  WebBrowser1.outerHTML = "";  
}
}


function ControllaData()
{
 var SlashCount=0;
 var append="";
 var addedError1=false;
 var addedError2=false;
 var errors="";
 var campo=document.nuovo.data_contratto;
 
 for(var j=0;j< campo.value.length;j++)
 {
  var c=campo.value.charAt(j);

  if ((c=='/')) SlashCount++;

  if (c != '/' && (c < '0' || c > '9') && addedError1==false)
    {
      addedError1=true;
      append+="\n - deve contenere solo numeri e /";
    }
  }

 if (j!=10 || SlashCount!=2) 
      append+="\n - deve avere il formato gg/mm/aaaa";

 if (SlashCount !=2) 
      append+="\n - deve contenere il carattere /";

 if ( append || (campo.value.length==0) || (campo.value=='gg/mm/aaaa'))
  { 
   errors+="Il campo data contratto " + append;
   alert(errors);
   //campo.focus();
   //campo.select();
   return false;
  }
 else
   return true;
}

function ControllaDataL(campo)
{
 var SlashCount=0;
 var append="";
 var addedError1=false;
 var addedError2=false;
 var errors="";
 
 for(var j=0;j< campo.value.length;j++)
 {
  var c=campo.value.charAt(j);

  if ((c=='/')) SlashCount++;

  if (c != '/' && (c < '0' || c > '9') && addedError1==false)
    {
      addedError1=true;
      append+="\n - deve contenere solo numeri e /";
    }
  }

 if (j!=10 || SlashCount!=2) 
      append+="\n - deve avere il formato gg/mm/aaaa";

 if (SlashCount !=2) 
      append+="\n - deve contenere il carattere /";

 if ( append || (campo.value.length==0) || (campo.value=='gg/mm/aaaa'))
  { 
   errors+="Il campo data contratto " + append;
   alert(errors);
   //campo.focus();
   //campo.select();
   return false;
  }
 else
   return true;
}

//conferma delle modifiche apportate
function ConfirmModify()
{
  var risposta=confirm("Confermare le modifiche apportate?");
	if (risposta)
	 return true;
	else
	 return false;
	  
}

//conferma dell'inserimento di un elemento
function ConfirmInsert()
{
  var risposta=confirm("Confermare l'inserimento?");
	if (risposta)
	 return true;
	else
	 return false;
	  
}

//************************************funzione per la verifica dell'esistenza dello username considerato
function InviaForm(){
 var theform = document.nuovo;
 theform.submit();
}

//funzione per la verifica dell'esistenza dello username considerato
function ChkLogin(newvalue,oldvalue,tabella,campo){
 if (newvalue != oldvalue){
   openWindow('chklogin.php?username='+newvalue+'&tabella='+tabella+'&campo='+campo,'WinLog',200,300);
 }else{
   //InviaForm();   
   return true;
}
}
