var ammessi="0123456789";

function openwindow(url,width,height) {
	mywindow = window.open(url, "_0", "width=" + width + ",height=" + height + ", scrollbars=1, menubar=0, toolbar=0, status=0, resizable=1, top=10, left=10");
	//mywindow.focus();
}


function indirizzoInternetValido(indirizzo) {
	validi = "qwertyuiopasdfghjklzxcvbnm_-0123456789://.";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( indirizzo.charAt(i) ) == -1 )
			return false;
	}
	return true;
}



function ControllaCF(cf)
{
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' )  return '';
	cf = cf.toUpperCase();
	if( cf.length != 16 )
		return "La lunghezza del Codice Fiscale deve essere di 16 caratteri alfanumerici.\n";
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
			return "Codice Fiscale contiene un carattere non valido '" +
				cf.charAt(i) +
				"'. Solo caratteri alfanumerici.\n";
	}
	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.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 "Codice Fiscale non corretto:"+
			" il codice di controllo non corrisponde.\n";
	return "";
}

function isDigitsObb(n){
	if( n == '' )  return false;
	validi = "0123456789.";
	for( i = 0; i<=n.length; i++ ){
		if( validi.indexOf( n.charAt(i) ) == -1 ){
			return false;
		}
	}
	return true;
}

function ControllaPIVA(iva)
{
	if( iva == '' )  return '';
	if( iva.length != 11 )
		return "La lunghezza della partita IVA deve essere di 11 cifre\n";
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( iva.charAt(i) ) == -1 )
			return "Partita IVA contiene un carattere non valido: '" +
				iva.charAt(i) + "'. Sono accettate solo cifre.\n";
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += iva.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( iva.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != iva.charCodeAt(10) - '0'.charCodeAt(0) )
		return "Partita IVA non valida: " +
			"il codice di controllo non corrisponde.\n";
	return '';
}

function Verifica_tel(tel)
{
	var c;
	for(i=0; i<tel.length; i++)
		{
		c=tel.charAt(i);
		if(ammessi.indexOf(c)==-1)
		return false;
		}
	return true;
}

function Verifica_cellulare(cellulare)
{
	var c;
	for(i=0; i<cellulare.length; i++)
		{
		c=cellulare.charAt(i);
		if(ammessi.indexOf(c)==-1)
		return false;
		}
	return true;
}

function Verifica_cap(cap)
{
	var c;
	if(cap.length < 5){return false;}
	for(i=0; i<cap.length; i++)
		{
		c=cap.charAt(i);
		if(ammessi.indexOf(c)==-1)
		return false;
		}
	return true;
}

function Verifica_cod_sede(cod_sede)
{
	var c;
	for(i=0; i<cod_sede.length; i++)
		{
			c=cf.charAt(i);
			if(ammessi_cf.indexOf(c)==-1)
			return false;
		}
	return true;
}

function Verifica_numcc(numcc)
{
	var c;
	for(i=0; i<numcc.length; i++)
		{
		c=numcc.charAt(i);
		if(ammessi_cf.indexOf(c)==-1)
		return false;
		}
	return true;
}

function indirizzoEmailValido(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
}


function controlla_data(giorno, mese, anno){
	var giorno_ok = eval(giorno);
	var mese_ok = eval(mese);
	var anno_ok = eval(anno);
	
	if( (giorno_ok>29) && (mese_ok==2) ) return "Il mese di febbraio non può avere più di 29 giorni.\n";
	else if( (giorno_ok>28) && (mese_ok==2) )
	{
		resto=anno_ok%4;
		if(resto!=0) return "L'anno selezionato non è bisestile. 29 febbraio non esiste.\n";
	}
	else if(giorno_ok>30)
	{
		if( (mese_ok==2) || (mese_ok==4) || (mese_ok==6) || (mese_ok==9) || (mese_ok==11) ) return "Non può esistere la data di nascita: 31/"+mese+"/"+anno+"\n";
	}

	return "";
}

/////////////////////////////////////////////////////////////////////////////////////////////7


function verifica_dati_autocensimento_fisica()
{
	
	var nome=document.autocensimento_f.nome.value;
	var cognome=document.autocensimento_f.cognome.value;
	var sesso=document.autocensimento_f.sesso.value;
	var giorno=document.autocensimento_f.giorno_nascita.value;
	var mese=document.autocensimento_f.mese_nascita.value;
	var anno=document.autocensimento_f.anno_nascita.value;
	var luogo=document.autocensimento_f.luogo.value;
	var via=document.autocensimento_f.via.value;
	var cap=document.autocensimento_f.cap.value;
	var paese=document.autocensimento_f.paese.value;
	var cf=document.autocensimento_f.cf.value;
	
	var tel=document.autocensimento_f.telefono.value;
	var cellulare=document.autocensimento_f.cellulare.value;
	var mail=document.autocensimento_f.e_mail.value;

	var cf=document.autocensimento_f.cf.value;
//	var cod_sede=document.autocensimento_f.codice_sede.value;
	var msg="";
	
	if(nome==""){
		msg=msg+"- Nome.\n";
	}
	if(cognome==""){
		msg=msg+"- Cognome.\n";
	}
	if(sesso==""){
		msg=msg+"- Sesso.\n";
	}
	
	
	if(anno=="0000" || giorno=="00" || mese=="00"){
		msg=msg+"- Data di nascita.\n";
	}else{
		
		if(controlla_data(giorno, mese, anno)!=""){
			msg=msg+"- "+controlla_data(giorno, mese, anno);
		}
	}

	if(luogo==""){
		msg=msg+"- Luogo di nascita.\n";
	}
	if(via==""){
		msg=msg+"- Via.\n";
	}
	if(!Verifica_cap(cap)){
		msg=msg+"- Cap.\n";
	}
	if(paese==""){
		msg=msg+"- Paese\n";
	}
	if(cf==""){
		msg=msg+"- Codice fiscale.\n";
	}

	if(ControllaCF(cf)!=""){
		msg=msg+'- '+ControllaCF(cf);
	}
	
	//TELEFONO E CELL CONCATENATI
	if(tel=="" || !Verifica_tel(tel)){
		msT="";
	}else{
		msT="A";
	}
	if(cellulare=="" || !Verifica_cellulare(cellulare)){
		msC="";
	}else{
		msC="B";
	}
	if(msT=="" && msC==""){
		msg=msg+"- Inserire almeno un recapito telefonico, Telefono o Cellulare.\n";
	}
	//
	if(mail!=""){
		if(!indirizzoEmailValido(mail)){
			msg=msg+"- E-mail non valida.\n";
		}
	}
	

	//CODICE SEDE ATTUALMENTE NON OBBLIGATORIO
	/*if(cod_sede==""){
		msg=msg+"- Codice Sede\n";
	}*/
	
	//MESSAGGIO ERRORE
 	if(msg!=""){
		alert('CONTROLLARE I SEGUENTI CAMPI OBBLIGATORI:\n\n'+msg);
		return false;
	}else{
		document.autocensimento_f.submit();
		return true;
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function verifica_dati_autocensimento_giuridica()
{	
	var rsociale=document.autocensimento_g.rsociale.value;
	var iva=document.autocensimento_g.iva.value;
	var cf=document.autocensimento_g.cf.value;
	var cittacc=document.autocensimento_g.cittacc.value;
	var numcc=document.autocensimento_g.numcc.value;
	var via=document.autocensimento_g.via.value;
	var paese=document.autocensimento_g.paese.value;
	var cap=document.autocensimento_g.cap.value;
	var tel=document.autocensimento_g.telefono.value;
	var mail=document.autocensimento_g.e_mail.value;
	var nome=document.autocensimento_g.nome_r.value;
	var cognome=document.autocensimento_g.cognome_r.value;
	var via_r=document.autocensimento_g.via_r.value;
	var paese_r=document.autocensimento_g.paese_r.value;
	var cap_r=document.autocensimento_g.cap_r.value;
	var cf_r=document.autocensimento_g.cf_r.value;
	var tel_r=document.autocensimento_g.telefono_r.value;
	var mail_r=document.autocensimento_g.e_mail_r.value;
	//var cod_sede=document.autocensimento_g.codice_sede.value;
	var msg="";
	


	if(rsociale==""){
		msg=msg+"- Ragione Sociale.\n";
	}

	if(iva==""){
		msg=msg+"- Partita IVA.\n";
	}

	if(ControllaPIVA(iva)!=""){
		msg=msg+'- '+ControllaPIVA(iva);
	}

	if(cf==""){
		msg=msg+"- Codice fiscale.\n";
	}

	if(ControllaCF(cf)!=""){
		msg=msg+'- '+ControllaCF(cf);
	}
	
	if(cittacc==""){
		msg=msg+"- Città iscrizione CC.\n";
	}

	if(numcc==""){
		msg=msg+"- Numero iscrizione CC.\n";
	}

	if(via==""){
		msg=msg+"- Via.\n";
	}
	if(!Verifica_cap(cap)){
		msg=msg+"- Cap.\n";
	}
	if(paese==""){
		msg=msg+"- Paese.\n";
	}
	
	if(tel=="" || !Verifica_tel(tel)){
		msg=msg+"- Recapito telefonico.\n";
	}

	if(mail!=""){
		if(!indirizzoEmailValido(mail)){
			msg=msg+"- E-mail.\n";
		}
	}

	if(nome==""){
		msg=msg+"- Nome Legale Rappresentante.\n";
	}

	if(cognome==""){
		msg=msg+"- Cognome Legale Rappresentante.\n";
	}
	
	if(via_r==""){
		msg=msg+"- Via (Legale Rappresentante).\n";
	}
	if(!Verifica_cap(cap_r)){
		msg=msg+"- Cap (Legale Rappresentante).\n";
	}
	if(paese_r==""){
		msg=msg+"- Paese (Legale Rappresentante).\n";
	}

	if(cf_r){
		if(ControllaCF(cf_r)!=""){
			msg=msg+'- '+ControllaCF(cf_r);
		}
	}

	if(tel_r=="" || !Verifica_tel(tel_r)){
		msg=msg+"- Recapito telefonico (Legale Rappresentante).\n";
	}

	if(mail!=""){
		if(!indirizzoEmailValido(mail_r)){
			msg=msg+"- E-mail (Legale Rappresentante).\n";
		}
	}

	//CODICE SEDE ATTUALMENTE NON OBBLIGATORIO
	/*if(cod_sede==""){
		msg=msg+"- Codice Sede\n";
	}*/

	//MESSAGGIO ERRORE
 	if(msg!=""){
		
		alert('CONTROLLARE I SEGUENTI CAMPI OBBLIGATORI:\n\n'+msg);
		return false;
	}else{
		document.autocensimento_g.submit();
		return true;
	}
}

function uscita()
{
	//nome=document.prova_pagina.Uscita.premimi;
	alert(" saluti!");
	window.close();
}

function date()
{
var oggi= new Date();
var giorno= oggi.getDate();
return giorno;
}

