/***************************/

$(document).ready(function()
{
	//Variabili di validazione globali
	var form = $("#customForm");
	var name = $("#name");
	var nameInfo = $("#nameInfo");
	var surname = $("#surname");
	var surnameInfo = $("#surnameInfo");
	var username = $("#username");
	var usernameInfo = $("#usernameInfo");
	var email = $("#email");
	var emailInfo = $("#emailInfo");
	var pass1 = $("#pass1");
	var pass1Info = $("#pass1Info");
	var pass2 = $("#pass2");
	var pass2Info = $("#pass2Info");
	var message = $("#message");
	var username = $("#username");
	var stato = $("#stato");
	var citta = $("#citta");
    var zona = $("#zona");
	var tel = $("#tel");
    var cgs = $("#cgs");
    var cgsInfo = $("#cgsInfo");
	var cdp = $("#cdp");
    var cdpInfo = $("#cdpInfo");
    
	//Variabili di validazioni dedicate ai testi
	var titolo = $("#titolo");
	var titoloInfo = $("#titoloInfo");
	var autore = $("#autore");
	var autoreInfo = $("#autoreInfo");
	var materia = $("#materia");
	var materiaInfo = $("#materiaInfo");
	var editore = $("#editore");
	var editoreInfo = $("#editoreInfo");
	var descrizione = $("#descrizione");
	var descrizioneInfo = $("#descrizioneInfo");
	var pagine = $("#pagine");
	var pagineInfo = $("#pagineInfo");
	var condizione = $("#condizione");
	var condizioneInfo = $("#condizioneInfo");
	var prezzocop = $("#prezzocop");
	var prezzocopInfo = $("#prezzocopInfo");
	var prezzoven = $("#prezzoven");
	var prezzovenInfo = $("#prezzovenInfo");
	var prezzomax = $("#prezzomax");
	var prezzomaxInfo = $("#prezzomaxInfo");
	
	//On blur
	name.blur(validateName);
	surname.blur(validateSurname);
	username.blur(validateUsername);
	email.blur(validateEmail);
	pass1.blur(validatePass1);
	pass2.blur(validatePass2);
    zona.blur(validateZona);
    cgs.blur(validateCgs);
    cdp.blur(validateCdp);
	///
	titolo.blur(validateTitolo);
	autore.blur(validateAutore);
	materia.blur(validateMateria);
	editore.blur(validateEditore);
	descrizione.blur(validateDescrizione);
	pagine.blur(validatePagine);
	condizione.blur(validateCondizione);
	prezzocop.blur(validatePrezzocop);
	prezzoven.blur(validatePrezzoven);
	prezzomax.blur(validatePrezzomax);
	//On key press
	name.keyup(validateName);
	surname.keyup(validateSurname);
	username.keyup(validateUsername);
	pass1.keyup(validatePass1);
	pass2.keyup(validatePass2);
    zona.keyup(validateZona);
	message.keyup(validateMessage);
    cgs.keyup(validateCgs);
    cdp.keyup(validateCdp);
	///
	titolo.keyup(validateTitolo);
	autore.keyup(validateAutore);
	materia.keyup(validateMateria);
	editore.keyup(validateEditore);
	descrizione.keyup(validateDescrizione);
	pagine.keyup(validatePagine);
	condizione.keyup(validateCondizione);
	prezzocop.keyup(validatePrezzocop);
	prezzoven.keyup(validatePrezzoven);
	prezzomax.keyup(validatePrezzomax);
	//On Submitting
	form.submit(function(){
	  //alert("***");
		if(validateName()  &&
		   validateSurname() &&
		   validateUsername() &&
		   validateEmail() &&
		   validatePass1() &&
		   validatePass2() &&
           validateZona() &&
           validateCgs() &&
           validateCdp() &&
		   validateMessage() &&
		   validateTitolo() &&
		   validateAutore() &&
		   validateMateria() &&
		   validateEditore() &&
		   validateDescrizione() &&
		   validatePagine() &&
		   validateCondizione &&
		   validatePrezzocop() &&
		   validatePrezzoven() &&
		   validatePrezzomax()
		  )
			return true;
		else
		{
		  alert("Non sono stati compilati i campi richiesti!");
			return false;
		}
	});
	
	//validation functions
	function validateEmail(){
		//testing regular expression
		var a = $("#email").val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(filter.test(a)){
			email.removeClass("error");
			emailInfo.text("Per favore, inserisci un'e-mail valida");
			emailInfo.removeClass("error");
			return true;
		}
		//if it's NOT valid
		else{
			email.addClass("error");
			emailInfo.text("Attenzione! Inserisci un indirizzo valido!");
			emailInfo.addClass("error");
			return false;
		}
	}
	function validateName(){
		//if it's NOT valid
		if(name.val().length < 4){
			name.addClass("error");
			nameInfo.text("Devi inserire un nome con almeno 3 lettere!");
			nameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			name.removeClass("error");
			nameInfo.text("Inserire il tuo nome completo, cognome e nome");
			nameInfo.removeClass("error");
			return true;
		}
	}
	
	function validateSurname(){
		//if it's NOT valid
		if(surname.val().length < 4){
			surname.addClass("error");
			surnameInfo.text("Devi inserire un cognome con almeno 3 lettere!");
			surnameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			surname.removeClass("error");
			surnameInfo.text("Inserire il tuo cognome completo");
			surnameInfo.removeClass("error");
			return true;
		}
	}
	
	function validateUsername(){
		//if it's NOT valid
		if(username.val().length < 4){
			username.addClass("error");
			usernameInfo.text("Devi inserire un username almeno 3 lettere!");
			usernameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			username.removeClass("error");
			usernameInfo.text("Inserire un username valido!");
			usernameInfo.removeClass("error");
			return true;
		}
	}
	
	function validatePass1(){
		var a = $("#password1");
		var b = $("#password2");

		//it's NOT valid
		if(pass1.val().length <5){
			pass1.addClass("error");
			pass1Info.text("Attenzione! Usa almeno 5 caratteri: lettere, numeri e '_'");
			pass1Info.addClass("error");
			return false;
		}
		//it's valid
		else{			
			pass1.removeClass("error");
			pass1Info.text("Almeno 5 caratteri: lettere,numeri e '_'");
			pass1Info.removeClass("error");
			validatePass2();
			return true;
		}
	}
	function validatePass2(){
		var a = $("#password1");
		var b = $("#password2");
		//are NOT valid
		if( pass1.val() != pass2.val() ){
			pass2.addClass("error");
			pass2Info.text("Le password non corrispondono!");
			pass2Info.addClass("error");
			return false;
		}
		//are valid
		else{
			pass2.removeClass("error");
			pass2Info.text("Conferma la password");
			pass2Info.removeClass("error");
			return true;
		}
	}
    
    function validateZona(){
		//if it's NOT valid
		if(zona.val().length < 4){
			zona.addClass("error");
			zonaInfo.text("Devi inserire la zona!");
			zonaInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			zona.removeClass("error");
			zonaInfo.text("Inserire una zona valida!");
			zonaInfo.removeClass("error");
			return true;
		}
	}
    
    function validateCgs(){
		//it's NOT valid
		if(cgs.checked == false){
			cgs.addClass("error");
			cgsInfo.text("L'accettazione delle condizioni è obbligatoria.");
			cgs.addClass("error");
			return false;
		}
		//it's valid
		else{			
			cgs.removeClass("error");
			return true;
    	}
	}
	
    function validateCdp(){
		//it's NOT valid
		if(cdp.checked == false){
			cdp.addClass("error");
			cdpInfo.text("L'accettazione delle condizioni è obbligatoria.");
			cdpInfo.addClass("error");
			return false;
		}
		//it's valid
		else{			
			cdp.removeClass("error");
			return true;
    	}
	}
    
	function validateMessage(){
		//it's NOT valid
		if(message.val().length < 10){
			message.addClass("error");
			return false;
		}
		//it's valid
		else{			
			message.removeClass("error");
			return true;
		}
	}
	
	/// <<VALIDAZIONE TESTI>>
	
	function validateTitolo(){
		//if it's NOT valid
		if(titolo.val().length < 1){
			titolo.addClass("error");
			titoloInfo.text("Devi inserire un titolo!");
			titoloInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			titolo.removeClass("error");
			titoloInfo.text("Inserire il titolo del libro");
			titoloInfo.removeClass("error");
			return true;
		}
	}//Fine
	
	function validateAutore(){
		//if it's NOT valid
		if(autore.val().length < 1){
			autore.addClass("error");
			autoreInfo.text("Devi inserire l'autore!");
			autoreInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			autore.removeClass("error");
			autoreInfo.text("Inserire l'autore del libro");
			autoreInfo.removeClass("error");
			return true;
		}
	}//Fine
	
	function validateMateria(){
		//if it's NOT valid
		if(materia.val().length < 1){
			materia.addClass("error");
			materiaInfo.text("Devi inserire la materia!");
			materiaInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			materia.removeClass("error");
			materiaInfo.text("Inserire la materia del libro");
			materiaInfo.removeClass("error");
			return true;
		}
	}//Fine
	
	function validateEditore(){
		//if it's NOT valid
		if(editore.val().length < 1){
			editore.addClass("error");
			editoreInfo.text("Devi inserire l'editore!");
			editoreInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			editore.removeClass("error");
			editoreInfo.text("Inserire l'editore del libro");
			editoreInfo.removeClass("error");
			return true;
		}
	}//Fine
	
	function validateDescrizione(){
		//if it's NOT valid
		if(descrizione.val().length < 1){
			descrizione.addClass("error");
			descrizioneInfo.text("Devi inserire una descrizione!");
			descrizioneInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			descrizione.removeClass("error");
			descrizioneInfo.text("Inserire la descrizione del libro");
			descrizioneInfo.removeClass("error");
			return true;
		}
	}//Fine
	
	function validatePagine(){
		//if it's NOT valid
		if(pagine.val().length < 1){
			pagine.addClass("error");
			pagineInfo.text("Devi inserire il numero di pagine!");
			pagineInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			pagine.removeClass("error");
			pagineInfo.text("Inserire il numero di pagine del libro");
			pagineInfo.removeClass("error");
			return true;
		}
	}//Fine
	
	function validateCondizione(){
		//if it's NOT valid
		if(condizione.val().length < 1){
			condizione.addClass("error");
			condizioneInfo.text("Devi inserire la condizione del testo!");
			condizioneInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			condizione.removeClass("error");
			condizioneInfo.text("Inserire la condizione del testo");
			condizioneInfo.removeClass("error");
			return true;
		}
	}//Fine
	
	function validatePrezzocop(){
		//if it's NOT valid
		if(prezzocop.val().length < 1){
			prezzocop.addClass("error");
			prezzocopInfo.text("Devi inserire il prezzo di copertina!");
			prezzocopInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			prezzocop.removeClass("error");
			prezzocopInfo.text("Inserire il prezzo di copertina");
			prezzocopInfo.removeClass("error");
			return true;
		}
	}//Fine
	
  function validatePrezzoven(){
		//if it's NOT valid
		if(prezzoven.val().length < 1){
			prezzoven.addClass("error");
			prezzovenInfo.text("Devi inserire il prezzo di vendita!");
			prezzovenInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			prezzoven.removeClass("error");
			prezzovenInfo.text("Inserire il prezzo di vendita");
			prezzovenInfo.removeClass("error");
			return true;
		}
	}//Fine

  function validatePrezzomax(){
		//if it's NOT valid
		if(prezzomax.val().length < 1){
			prezzomax.addClass("error");
			prezzomaxInfo.text("Devi inserire il prezzo massimo!");
			prezzomaxInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			prezzomax.removeClass("error");
			prezzomaxInfo.text("Inserire il prezzo di massimo");
			prezzomaxInfo.removeClass("error");
			return true;
		}
	}//Fine	

});

