function zeri(valore){

	codice_iper=document.autenticazione.cod_iper.value;
	codice_cli=document.autenticazione.cod_cli.value;
	if (valore=='cod_iper'){
	lung=codice_iper.length;
	for (i=0;i<6-lung;i++){
	codice_iper="0"+codice_iper;
	}
	document.autenticazione.cod_iper.value=codice_iper;
	return;}
	if (valore=='cod_cli'){
	lung=codice_cli.length;
	for (i=0;i<6-lung;i++){
	codice_cli="0"+codice_cli;
	}
	document.autenticazione.cod_cli.value=codice_cli;
	return;}
	}
	function invia(){
	inseriti=0;

	v1=document.dati_send.Titolo.value;
	v2=document.dati_send.Autore.value;
	v3=document.dati_send.Editore.value;
	v4=document.dati_send.ISBN.value;
	v5=document.dati_send.EAN.value;

	if(v1.length>0)
		inseriti=inseriti+1;
	if(v2.length>0)
		inseriti=inseriti+1;
	if(v3.length>0)
		inseriti=inseriti+1;
	if(v4.length>0)
		inseriti=inseriti+1;
	if(v5.length>0)
		inseriti=inseriti+1;
	if(inseriti>0)	
		document.dati_send.submit();
	else
		alert("Inserire una chiave per autore o per titolo o per editore oppure il codice ISBN");
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }
/*String.prototype.trim = function() {return this.replace(/^([\s\t\n]|\&nbsp\;)+|([\s\t\n]|\&nbsp\;)+$/g, '');}*/

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 non è\n"
		+"corretta: il codice fiscale dovrebbe essere lungo\n"
		+"esattamente 16 caratteri.\n";
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
			return "Il codice fiscale contiene un carattere non valido `" +
				cf.charAt(i) +
				"'.\nI caratteri validi sono le lettere e le cifre.\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 "Il codice fiscale non è corretto:\n"+
			"il codice di controllo non corrisponde.\n";
	return "";
}


function inviaAnagrafica(a){
	cognome=document.dati_send.cognome.value;
	nome=document.dati_send.nome.value;
	telefono=document.dati_send.telefono.value;
	email=document.dati_send.email.value;
	privacy=document.dati_send.privacy.checked;
	cellulare=document.dati_send.cellulare.value;	
	codice_fiscale =document.dati_send.codice_fiscale.value;

	if( a == 1){
		
		indirizzo=document.dati_send.indirizzo.value;
		comune=document.dati_send.comune.value;
		cap=document.dati_send.cap.value;
		provincia=document.dati_send.provincia.value;
	}

	errori=0;
	messaggio='Attenzione!\n';
	if(!privacy){
		errori++;
		messaggio=messaggio+"Devi consentire l\'utilizzo dei dati sensibili.\n";
	}

	if(cognome.trim().length<2){
		errori++;
		messaggio=messaggio+"Manca il cognome.\n";
	}
	if(nome.trim().length<2){
		errori++;
		messaggio=messaggio+"Manca il nome.\n";
	}
	if(telefono.trim().length<2){
		errori++;
		messaggio=messaggio+"Manca il telefono.\n";
	}
	if(cellulare.trim().length<2){
		errori++;
		messaggio=messaggio+"Manca il cellulare.\n";
	}
	if(email.trim().length<2){
		errori++;
		messaggio=messaggio+"Manca l'email.\n";
	}
	if(!checkEmail(email)){
		errori++;
		messaggio=messaggio+"Scrivi correttamente l'indirizzo email.\n";
	}
	if(codice_fiscale.trim().length<16){
		errori++;
		messaggio=messaggio+"Manca il codice fiscale.\n";
	}
	/*
	if(controllaAlfanumerici(codice_fiscale)){ 
		errori++;
		messaggio=messaggio+"Codice fiscale: Usa solo lettere e numeri.\n";
	}*/

	var controllo_codfis = ControllaCF(codice_fiscale);
	if(controllo_codfis.trim().length>0){
		errori++;
		messaggio=messaggio+" "+controllo_codfis+".\n";	
		
	}
		
	if( a == 1){

			if(indirizzo.trim().length<2){
			errori++;
			messaggio=messaggio+"Manca l'indirizzo.\n";
		}
			if(comune.trim().length<2){
			errori++;
			messaggio=messaggio+"Manca il comune.\n";
		}
			if(cap.trim().length<5){
			errori++;
			messaggio=messaggio+"Manca il cap.\n";
		}
		if(provincia.trim().length<2){
			errori++;
			messaggio=messaggio+"Manca la provincia.\n";
		}
		
		if(controllaAlfanumerici(indirizzo)){
			errori++;
			messaggio=messaggio+"Indirizzo: Usa le lettere a-z e l\'apostrofo per gli accenti.\n";
		}
		if(controllaAlfanumerici(comune)){ 
			errori++;
			messaggio=messaggio+"Comune: Usa le lettere a-z e l\'apostrofo per gli accenti.\n";
		}
		if(controllaAlfanumerici(provincia)){ 
			errori++;
			messaggio=messaggio+"Provincia: Usa le lettere a-z.\n";
		}
		

		
		if(controllaTelefono(cap) ){
			errori++;
			messaggio=messaggio+"Cap: Usa solo numeri senza spazi.\n";
		}

	}
	
	if(controllaAlfanumerici(cognome) ){
		errori++;
		messaggio=messaggio+"Cognome: Usa le lettere a-z e l\'apostrofo per gli accenti.\n";
	}
	
	if(controllaAlfanumerici(nome)){
		errori++;
		messaggio=messaggio+"Nome: Usa le lettere a-z e l\'apostrofo per gli accenti.\n";
	}
	

	if(controllaTelefono(cellulare) ){
		errori++;
		messaggio=messaggio+"Cellulare: Usa solo numeri senza spazi.\n";
	}
	
	if(controllaTelefono(telefono)){
		errori++;
		messaggio=messaggio+"Telefono: Usa solo numeri senza spazi.\n";
	}
	
	if(controllaEmail(email)){
		errori++;
		messaggio=messaggio+"Email: Usa solo i caratteri consentiti.\n";
	}
	
	if(errori>0)
		alert(messaggio);
	else{
		document.dati_send.action='invioOrdine.jsp';	

		document.dati_send.submit();
	}
		
}

function fix(obj) {
	
	for (var i = 0; i < 20; i++) {
		if (i != obj.substring(3)) {
			document.getElementById("reg" + i).style.display='none';
			document.getElementById("reg" + i).className='notfixme'; 
		}	
	}	
	
	document.getElementById(obj).className='fixme'; 
	document.getElementById(obj).style.display='block';
}

function roundTo(value, decimalpositions)
{
    var i = value * Math.pow(10,decimalpositions);
    i = Math.round(i);	
	i = i / Math.pow(10,decimalpositions);
	return(a2cifre(i));
} 

function a2cifre(x) {
	var c = String(Math.round(x*100));
	while (c.length < 3) c = '0' + c;
	return c.replace(/([0-9][0-9])$/,".$1");
}

function correggiApici(stringa){
	var stringa1= stringa.replace(/'/g,"\\'");
	stringa1= stringa1.replace(/"/g,"");
	return 	stringa1;
}


function checkEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		var StopPos = str.lastIndexOf(dot);
		
		
		if (StopPos < lat) {
 			return false;
		}
		if ((lstr-StopPos)<2 ) {
			return false;
		}
	/*	if(ldot.length<2){
			return false
		}*/
		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 abilita(a,b){
	if(a==0){
		document.getElementById(b).style.display='none';
	}else{
		document.getElementById(b).style.display='block';
	}
}

function controllaAlfanumerici(valore)
{
    var stringa = valore;
    var myregexp = /^[' a-zA-Z0-9\.\-\/]+$/; 
    if (stringa.length>0 && myregexp.test(stringa) == false)
    {
        return true;
    }
    else
    {
         return false;
    }
}


function controllaEmail(valore)
{
    var stringa = valore;
    var myregexp = /^[' a-zA-Z0-9\.\-\/\@\_]+$/; 
    if (stringa.length>0 && myregexp.test(stringa) == false)
    {
        return true;
    }
    else
    {
         return false;
    }
}

function controllaTelefono(valore)
{
    var stringa = valore;
    var myregexp = /^[0-9\+]+$/; 
    if (stringa.length>0 && myregexp.test(stringa) == false)
    {
        return true;
    }
    else
    {
         return false;
    }
}


/*
function checkSpecial(id, strValue) {
	objRegExp = /’/g;
	if(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, '\'');
		special = 1;
	}
	objRegExp = /’ /g;
	if(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, '\'');
		special = 1;
	}	
	var objRegExp = /è/g;
	var special = 0;
	if(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, 'e\'');
		special = 1;
	}
	objRegExp = /é/g;
	if(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, 'e\'');
		special = 1;
	}
	objRegExp = /ò/g;
	if(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, 'o\'');
		special = 1;
	}
	objRegExp = /ù/g;
	if(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, 'u\'');
		special = 1;
	}
	objRegExp = /ì/g;
	if(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, 'i\'');
		special = 1;
	}
	objRegExp = /à/g;
	if(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, 'a\'');
		special = 1;
	}	
		objRegExp = /£/g;
	if(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, ' ');
		special = 1;
	}		
}*/
