////// FUNZIONE CONTROLLA USATO - PRIVACY ////////////////////////////////////////
function vhUsato() {
	if (document.preventivo.usatoControl.checked==true) {	
			document.getElementById('usato').style.display="block";
			alert("Verifica di non avere le finestre di popup bloccate!\nUtilizziamo questo sistema per darti importanti informazioni\nriguardo alle regole di questo sito!");
			apriPop("popUsatoWarning.php",325,580,0,0);
	}
	else if (document.preventivo.usatoControl.checked==false) {
		document.getElementById('usato').style.display="none";
	}
}
function vhUsato2() {
	alert("Verifica di non avere le finestre di popup bloccate!\nUtilizziamo questo sistema per darti importanti informazioni\nriguardo alle regole di questo sito!");
	apriPop("popUsatoWarning.php",325,580,0,0);
}
function vhPrivacy() {
	if (document.preventivo.privacy.checked==true) {	
			document.getElementById('testo-autorizzo').style.display="block";
	}
	else if (document.preventivo.privacy.checked==false) {
		document.getElementById('testo-autorizzo').style.display="none";
	}
}
////// FUNZIONE CONTROLLA FORM PREVENTIVO ////////////////////////////////////////
function checkPrev(){

if (document.preventivo.tipo.value=='azienda') {
	if (document.preventivo.ditta.value==''){
    alert('Compila il campo prima di inviare.1');
    document.preventivo.ditta.focus();
    return false;
    }
  if (document.preventivo.nomecognome.value==''){
    alert('Compila il campo prima di inviare.5');
    document.preventivo.nomecognome.focus();
    return false;
  }
	if (document.preventivo.piva.value==''){
    alert('Compila il campo prima di inviare.2');
    document.preventivo.piva.focus();
    return false;
    }
	if (document.preventivo.aperturapiva.value==''){
    alert('Compila il campo prima di inviare.3');
    document.preventivo.aperturapiva.focus();
    return false;
    }
} else if (document.preventivo.tipo.value=='privato') {
  if (document.preventivo.nomecognome.value==''){
    alert('Compila il campo prima di inviare.5');
    document.preventivo.nomecognome.focus();
    return false;
  }
  if (document.preventivo.settore.value==''){
    alert('Compila il campo prima di inviare.16');
    document.preventivo.settore.focus();
    return false;
  }
}
  if (document.preventivo.tel.value==''){
    alert('Compila il campo prima di inviare.6');
    document.preventivo.tel.focus();
    return false;
  }
  if (document.preventivo.citta.value==''){
    alert('Compila il campo prima di inviare.7');
    document.preventivo.citta.focus();
    return false;
  }
  if (document.preventivo.provincia.value==''){
    alert('Compila il campo prima di inviare.8');
    document.preventivo.provincia.focus();
    return false;
  }
  if (document.preventivo.indirizzo.value==''){
    alert('Compila il campo prima di inviare.9');
    document.preventivo.indirizzo.focus();
    return false;
  }
  if (document.preventivo.cap.value==''){
    alert('Compila il campo prima di inviare.10');
    document.preventivo.cap.focus();
    return false;
  }
  if (document.preventivo.email.value.indexOf("@")<0) {
	alert("inserisci una mail valida!");
	return false;
  }
  if (document.preventivo.email.value==''){
    alert('Compila il campo prima di inviare.11');
    document.preventivo.email.focus();
    return false;
  }
  if (document.preventivo.email.value!=document.preventivo.confermaemail.value){
    alert('Le e-mail non coincidono!.12');
    document.preventivo.email.focus();
    return false;
  }
 
  if (document.preventivo.tipo.value=='privato') {
	if (document.preventivo.codicefiscale.value==''){
      alert('Compila il campo prima di inviare.13');
      document.preventivo.codicefiscale.focus();
      return false;
    }
	if (document.preventivo.contratto.value==''){
      alert('Compila il campo prima di inviare.14');
      document.preventivo.contratto.focus();
      return false;
    }
    if (document.preventivo.settore.value==''){
      alert('Compila il campo prima di inviare.15');
      document.preventivo.settore.focus();
      return false;
	}
  }
  if (document.preventivo.marca.value==''){
    alert('Compila il campo prima di inviare.17');
    document.preventivo.marca.focus();
    return false;
  }
  if (document.preventivo.modello.value==''){
    alert('Compila il campo prima di inviare.18');
    document.preventivo.modello.focus();
    return false;
  }
  if (document.preventivo.durata.value==''){
    alert('Compila il campo prima di inviare.19');
    document.preventivo.durata.focus();
    return false;
  }
  if (document.preventivo.km.value==''){
    alert('Compila il campo prima di inviare.20');
    document.preventivo.km.focus();
    return false;
  }
  if (document.preventivo.usatoControl.checked==true){
	    if (document.preventivo.marcaUsato.value==''){
	    alert('Compila il campo prima di inviare.21');
	    document.preventivo.marcaUsato.focus();
	    return false;
	  }
	    if (document.preventivo.modelloUsato.value==''){
	    alert('Compila il campo prima di inviare.22');
	    document.preventivo.modelloUsato.focus();
	    return false;
	  }
	    if (document.preventivo.alim.value==''){
	    alert('Compila il campo prima di inviare.23');
	    document.preventivo.alim.focus();
	    return false;
	  }
	    if (document.preventivo.cilin.value==''){
	    alert('Compila il campo prima di inviare.24');
	    document.preventivo.cilin.focus();
	    return false;
	  }
	    if (document.preventivo.kw.value==''){
	    alert('Compila il campo prima di inviare.25');
	    document.preventivo.kw.focus();
	    return false;
	  }
	    if (document.preventivo.porte.value==''){
	    alert('Compila il campo prima di inviare.26');
	    document.preventivo.porte.focus();
	    return false;
	  }
	    if (document.preventivo.optionals.value==''){
	    alert('Compila il campo prima di inviare.27');
	    document.preventivo.optionals.focus();
	    return false;
	  }
	    if (document.preventivo.kmUsato.value==''){
	    alert('Compila il campo prima di inviare.28');
	    document.preventivo.kmUsato.focus();
	    return false;
	  }
	  	if (document.preventivo.maUsato.value==''){
	    alert('Compila il campo prima di inviare.29');
	    document.preventivo.maUsato.focus();
	    return false;
	  }
		if (document.preventivo.statoUsato.value==''){
	    alert('Compila il campo prima di inviare.30');
	    document.preventivo.statoUsato.focus();
	    return false;
	  }
  		if (document.preventivo.prezzoInd.value==''){
	    alert('Compila il campo prima di inviare.31');
	    document.preventivo.prezzoInd.focus();
	    return false;
	  }

/////// CONTROLLO NOME FILE INSERITO /////////////////////////////
	    if (document.preventivo.file.value!='') {
		  var_text = document.preventivo.file.value;
		  var_lenght = var_text.length;
				if (var_text.indexOf(",") >= 1) {
				alert('Nome file non valido. Virgole non ammesse!')
				document.preventivo.file.focus();
				return false;
				} else {
						if (var_lenght > 0) {
						  var_diff = var_lenght - 3;
						  ext = var_text.substr(var_diff,var_lenght);
						  if ((ext != 'gif') && (ext != 'jpg') && (ext != 'png')) {
							alert('Formato file non valido!');
							document.preventivo.file.focus();
		return false;
	  }
     }
	}
   }
  }
 
 if (document.preventivo.privacy.checked==false){
 alert('Accetta l\'informativa sulla privacy prima di inviare.');
 document.preventivo.privacy.focus();
 return false;
 }
  if (document.preventivo.usatoControl.checked==false){
	    document.preventivo.marcaUsato.value='';
		document.preventivo.modelloUsato.value='';
		document.preventivo.kmUsato.value='';
  }
  return true;
}
////////////////////////////////////////////////////////////////////////////////

////// FUNZIONE CONTROLLA FORM USATO ////////////////////////////////////////
function checkUsato(){

	    if (document.usato.modelloUsato.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.modelloUsato.focus();
	    return false;
	  }
	    if (document.usato.kmUsato.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.kmUsato.focus();
	    return false;
	  }
	  	if (document.usato.maUsato.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.maUsato.focus();
	    return false;
	  }
		if (document.usato.statoUsato.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.statoUsato.focus();
	    return false;
	  }
  		if (document.usato.prezzoInd.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.prezzoInd.focus();
	    return false;
	  }

/////// FUNZIONE CONTROLLA FILE USATO /////////////////////////////
	    if (document.usato.file.value!='') {
		  var_text = document.usato.file.value;
		  var_lenght = var_text.length;
				if (var_text.indexOf(",") >= 1) {
				alert('Nome file non valido. Virgole non ammesse!')
				document.usato.file.focus();
				return false;
				} else {
						if (var_lenght > 0) {
						  var_diff = var_lenght - 3;
						  ext = var_text.substr(var_diff,var_lenght);
						  if ((ext != 'gif') && (ext != 'jpg') && (ext != 'png')) {
							alert('Formato file non valido!');
							document.usato.file.focus();
		return false;
	  }
     }
	}
   }
  return true;
}
////////////////////////////////////////////////////////////////////////////////


////// FUNZIONE CONTROLLA FORM USATO ////////////////////////////////////////
function checkGenerale(){
	  if (document.usato.nome.value=='') {
	    alert('Compila il campo prima di inviare.');
	    document.usato.nome.focus();
	    return false;
	  }
	  if (document.usato.tel.value=='') {
	    alert('Compila il campo prima di inviare.');
	    document.usato.tel.focus();
	    return false;
	  }
	  if (document.usato.email.value=='') {
	    alert('Compila il campo prima di inviare.');
	    document.usato.email.focus();
	    return false;
	  }
	  if (document.usato.email.value != document.usato.confermaemail.value) {
	    alert('I campi E-mail e Conferma E-mail non coincidono.');
	    document.usato.confermaemail.focus();
	    return false;
	  }
	  if (document.usato.email.value.indexOf("@")<0) {
		alert("inserisci una mail valida!");
		return false;
	  }
	  if (document.usato.marcaUsato.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.marcaUsato.focus();
	    return false;
	  }
		if (document.usato.modelloUsato.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.modelloUsato.focus();
	    return false;
	  }
	    if (document.usato.cilin.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.cilin.focus();
	    return false;
	  }
	  	if (document.usato.kw.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.kw.focus();
	    return false;
	  }
		if (document.usato.colore.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.colore.focus();
	    return false;
	  }
		if (document.usato.porte.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.porte.focus();
	    return false;
	  }
		if (document.usato.kmUsato.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.kmUsato.focus();
	    return false;
	  }
		if (document.usato.maUsato.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.maUsato.focus();
	    return false;
	  }
  		if (document.usato.prezzoInd.value==''){
	    alert('Compila il campo prima di inviare.');
	    document.usato.prezzoInd.focus();
	    return false;
	  }

/////// FUNZIONE CONTROLLA FILE USATO /////////////////////////////
	    if (document.usato.file.value!='') {
		  var_text = document.usato.file.value;
		  var_lenght = var_text.length;
				if (var_text.indexOf(",") >= 1) {
				alert('Nome file non valido. Virgole non ammesse!')
				document.usato.file.focus();
				return false;
				} else {
						if (var_lenght > 0) {
						  var_diff = var_lenght - 3;
						  ext = var_text.substr(var_diff,var_lenght);
						  if ((ext != 'gif') && (ext != 'jpg') && (ext != 'png')) {
							alert('Formato file non valido!');
							document.usato.file.focus();
		return false;
	  }
     }
	}
   }
  	  if (document.usato.privacy.checked==false){
	    alert('Accetta le condizioni relative alla privacy prima di inviare.');
	    document.usato.privacy.focus();
	    return false;
	  }
  return true;
}
////////////////////////////////////////////////////////////////////////////////


////// FUNZIONE CONTROLLA FORM INFORMAZIONI ////////////////////////////////////
function checkInfo(){

  if (document.info.nome.value==''){
    alert('Compila il campo prima di inviare.');
    document.info.nome.focus();
    return false;
  }
  if (document.info.email.value==''){
    alert('Compila il campo prima di inviare.');
    document.info.email.focus();
    return false;
  }
  if (document.info.telefono.value==''){
    alert('Compila il campo prima di inviare.');
    document.info.telefono.focus();
    return false;
  }
 if (document.info.privacy.checked==false){
    alert('Autorizza al trattamento dei dati prima di inviare.');
    document.info.privacy.focus();
    return false;
 }
 return true;
}
////////////////////////////////////////////////////////////////////////////////

//// APRI POP-UP ///////////////////////////////////////////////////////////////

function apriPop(url,x,y,t,l){
	win=window.open(url,'p','toolbar=no,top='+t+',left='+l+',location=no,directories=no,menubar=no,scrollbars=yes,resizable=0,width='+x+',height='+y+'');
	win.focus();
}
////////////////////////////////////////////////////////////////////////////////


/* GESTIONE FORM PREVENTIVO A SECONDA DELLA TIPOLOGIA */
function mostra(what) {
	function azienda(action) {
		if (action=="mostra") {
			action="block";
		} else {
			action="none";
		}
			document.getElementById("labeldi3").style.display=action;
			document.getElementById("labeldi").style.display=action;
			document.getElementById("ditta").style.display=action;
			document.getElementById("labelpi").style.display=action;
			document.getElementById("piva").style.display=action;
			document.getElementById("labelpi2").style.display=action;
			document.getElementById("piva2").style.display=action;
			document.getElementById("labelap").style.display=action;
			document.getElementById("aperturapiva").style.display=action;
	}
	
	function privato(action) {
		if (action=="mostra") {
			action="block";
		} else {
			action="none";
		}
			document.getElementById("labeldi2").style.display=action;
			document.getElementById("labelcf").style.display=action;
			document.getElementById("codicefiscale").style.display=action;
			document.getElementById("labelco").style.display=action;
			document.getElementById("contratto").style.display=action;
			document.getElementById("labelse").style.display=action;
			document.getElementById("settore").style.display=action;
	}
	// Written by Gabriel Di Grazia c/o Tesene S.r.l.

	if (what=="privato") {
		azienda("nascondi");
		privato("mostra");
	} else {
		azienda("mostra");
		privato("nascondi");
	}
}

// CONTROLLO ESTENSIONE FILE ///////////////////////////////////////////////////
