var errUNAME = false;
var errPASS = false;
var errMAIL = false;
var errMAIL_COMPARE = false;
var errPHONE = false;
var errCEL1 = false;
var errCEL2 = false;
var errCEL = false;
var errCAP = false;

 $(document).ready(function(){

	
	
 	$('#reg_username').val('').blur(function(){
		$.getJSON( '/actions/doUserValidation.php' ,
		{ action: 'usernameString', value: $.trim($('#reg_username').val()) },
		function(data) {
				if (!data.result) {
				
					$('#static_ctrl_user_name').append("<b>username troppo corta! Minimo 4 caratteri</b><br/>");
					$('#static_ctrl_user_name').slideDown('fast');
					
					errUNAME = true;
					
				} else {
				
					errUNAME = false;
					
				}
			});
 	}).blur(function(){
		$.getJSON( '/actions/doUserValidation.php' ,
		{ action: 'username', value: $('#reg_username').val() },
		function(data) {
				if (!data.result) {
					$('#static_ctrl_user_name').append("<b>username gi&agrave; esistente!</b><br/>");
					$('#static_ctrl_user_name').slideDown('fast');
					
					errUNAME = true;
					
				} else {
				
					errUNAME = false;
					
				}
			});
			
 	}).focus(function(){
 		$('#static_ctrl_user_name').slideUp('fast');
 		$('#static_ctrl_user_name').html('');
 	});
 	
 	$('#reg_password1').val('').focus(function(){
 		$('#static_ctrl_password').slideUp('fast');
 		$('#static_ctrl_password').html('');
 	});
 	
 	$('#reg_cpassword').val('').blur(function(){
 		if ($('#reg_cpassword').val() != $('#reg_password1').val())
 		{
 			$('#static_ctrl_password').append("<b>Le password non coincidono</b>!<br/>");
 			$('#static_ctrl_password').slideDown('fast')
 			
 			errPASS = true;
					
		} else {
				
			errPASS = false;
					
		}
	}).focus(function(){
 		$('#static_ctrl_password').slideUp('fast');
 		$('#static_ctrl_password').html('');
 	});
 	
 	
 	$('#reg_data_di_nascita').datepicker({
 		yearRange: '-80:0',
	 	dateFormat: 'yy-mm-dd'
	 });
 	/*
 	*
 	* Inserire i controlli per i campi per il cap
 	*
 	*/
 	$('#reg_cap').focus(function(){
 		$('#static_ctrl_cap').slideUp('fast');
 		$('#static_ctrl_cap').html('');
 	}).blur(function(){
		if ($(this).val()!="CAP*" && $(this).val()!="" && (isNaN($(this).val()) || $(this).val().length!=5 )) {
			$('#static_ctrl_cap').html("<b>Inserire 5 cifre per il CAP</b>");
			$('#static_ctrl_cap').slideDown('fast');
			errCAP = true;
		} else {
			$('#static_ctrl_cap').slideUp('fast');
			$('#static_ctrl_cap').html('');
		}
	});
	
 			
 	
 	/*
 	*
 	* Inserire i controlli per il telefono fisso
 	*
 	*/
 	
 	$('#reg_telefono').val('').blur(function(){
 	if (this.value != "") {
 		if(isMobileNumber($('#reg_telefono').val()) && !isPhone(this.value)) {
 		
 			$('#static_ctrl_telefono').html("<b>Numero di telefono fisso non valido</b>");
	 		$('#static_ctrl_telefono').slideDown('fast');
	 			
	 			errPHONE = true;
		 		return false;
	 		
	 		} else {
	 		
	 			errPHONE = false;
	 		}
 	} else {errPHONE = false; return true;}
 	
 	}).focus(function(){
 	
 		$('#static_ctrl_telefono').slideUp('fast');
 		$('#static_ctrl_telefono').html('');
 	
 	}); 	
 	
 	
 	/*
 	*
 	* Inserire i controlli per i campi per i telefoni celluari
 	*
 	*/
 	$('#reg_cellulare').val('').blur(function(){
 		if (this.value != "") {
			$.getJSON( '/actions/doUserValidation.php' ,
			{ action: 'mobile', value: this.value },
			function(data) {
					if (!data.result) {
						$('#static_ctrl_cellulare').html("<b>Numero di cellulare gi&agrave; esistente!</b>");
					 	$('#static_ctrl_cellulare').slideDown('fast');
					 		
					 	errCEL = true;
					 	return false;
						
					} else {
					
						errCEL = false;
					 	return true;
						
					}
				});
		}	else return true;
 	}).blur(function(){
 	
 		if (this.value != "") {
	 		if (!isMobileNumberLacivita(this.value)) {
				$('#static_ctrl_cellulare').html("<b>Il numero non &egrave; un cellulare!</b>");
				$('#static_ctrl_cellulare').slideDown('fast');
				errCEL = true;
				return false;
		 	}
 		}
 	}).focus(function(){
 	
 		$('#static_ctrl_cellulare').slideUp('fast');
 		$('#static_ctrl_cellulare').html('');
 	
 	}); 	
 		 
	


	
	/*
	*
	* Controllo Email
	*
	*/
	
	$('div [id^=reg_email]').val('').blur(function(){
		$.getJSON( '/actions/doUserValidation.php' ,
		{ action: 'emailString', value: $('#reg_email').val() },
		function(data) {
				if (!data.result) {
					if($('#static_ctrl_email').html()=="")
						$('#static_ctrl_email').append("<b>indirizzo email non valido</b><br/>");
					$('#static_ctrl_email').slideDown('fast');
					
					errMAIL = true;
					
				} else {
				
					errMAIL = false;
					
				}
			});
			
 	}).blur(function(){
		$.getJSON( '/actions/doUserValidation.php' ,
		{ action: 'email', value: $('#reg_email').val() },
		function(data) {
				if (!data.result) {
					if($('#static_ctrl_email').html()=="")
						$('#static_ctrl_email').append("<b>indirizzo email gi&agrave; esistente!</b><br/>");
					$('#static_ctrl_email').slideDown('fast');
					
					errMAIL = true;
					
				} else {
				
					errMAIL = false;
					
				}
			});
	}).blur(function(){
 		if ($('#reg_email_confirm').val() != $('#reg_email').val())
 		{
			if($('#static_ctrl_email').html()=="")
				$('#static_ctrl_email').append("<b>Le email non coincidono!</b><br/>");
 			$('#static_ctrl_email').slideDown('fast')
 			
 			errMAIL_COMPARE = true;
					
		} else {
				
			errMAIL_COMPARE = false;
					
		}
	}).blur(function(){
 		if ($('#reg_email_confirm').val() != $('#reg_email').val())
 		{
 			if($('#static_ctrl_email').html()=="")
				$('#static_ctrl_email').append("<b>Le email non coincidono!</b><br/>");
 			$('#static_ctrl_email').slideDown('fast')
 			
 			errMAIL_COMPARE = true;
					
		} else {
				
			errMAIL_COMPARE = false;
					
		}
		
		
	}).focus(function(){
 	
 		$('#static_ctrl_email').slideUp('fast');
 		$('#static_ctrl_email').html('');
 	
 	});
	
	/*$('#reg_email_confirm').val('').blur(function(){
 		if ($('#reg_email_confirm').val() != $('#reg_email').val())
 		{
 			$('#static_ctrl_email').append("<b>Le email non coincidono!</b><br/>");
 			$('#static_ctrl_email').slideDown('fast')
 			
 			errMAIL_COMPARE = true;
					
		} else {
				
			errMAIL_COMPARE = false;
					
		}
		
		
	}).focus(function(){
 	
 		$('#static_ctrl_email').slideUp('fast');
 		$('#static_ctrl_email').html('');
 	
 	});
 	*/
 	/**
 		Controlli prima del submit.
 	**/
 	
 	
 	
 	$('#static_submit_btn').click(function(){
  		//alert("\nerrMAIL: "+errMAIL+"\nerrMAIL_COMPARE: "+errMAIL_COMPARE+"\nerrCEL: "+errCEL+"\nerrCEL1: "+errCEL1+"\nerrCEL1: "+errCEL1+"\nerrPHONE: "+errPHONE)
		$('#static_ctrl_final').slideUp('fast');
		$('#static_ctrl_final').html('');
		
		if ($.trim($('#reg_username').val()).match(/@/)=="@") {
			$('#static_ctrl_final').html("Attenzione!! Lo Username non pu&ograve; essere un'indirizzo email.");
			$('#static_ctrl_final').slideDown('fast');
			return false;
		}
		if (($.trim($('#reg_username').val()) == "") || ($.trim($('#reg_password1').val()) == "")) {
	 			
	 			$('#static_ctrl_final').html("Username e Password non possono essere vuoti");
	 			$('#static_ctrl_final').slideDown('fast');
	 			return false;
	 	}
	 	
	 	 if ($('#reg_password1').val() !=  $('#reg_cpassword').val()) {
	 			
	 			$('#static_ctrl_final').html("Password e conferma non coincidono!");
	 			$('#static_ctrl_final').slideDown('fast');
	 			return false;
	 	}
	 	
	 	if (($.trim($('#static_reg_name').val()) == "") || ($.trim($('#static_reg_Cognome').val()) == "")) {
	 			
	 			$('#static_ctrl_final').html("Nome e Cognome non possono essere vuoti");
	 			$('#static_ctrl_final').slideDown('fast');
	 			return false;
	 	}
	 	
	 	if ($('#reg_data_di_nascita').val() == ""){
 		
 			$('#static_ctrl_final').html("La data di nascita non pu&ograve; essere vuota!");
 			$('#static_ctrl_final').slideDown('fast');
 			return false;
 		
 		}
		/*
	 	if (((errUNAME) || (errPASS))){
	 		
	 		$('#static_ctrl_final').html("Controlla lo username o la password");
	 		$('#static_ctrl_final').slideDown('fast');
	 		return false;
	 	}
		*/
	 	
	 	if ($('#reg_cap').val() == "" || $('#reg_cap').val() == "CAP*"){
 		
 			$('#static_ctrl_final').html("Il CAP non pu&ograve; essere vuoto!");
 			$('#static_ctrl_final').slideDown('fast');
 			return false;
 		
 		}
		
	 	if ($('#reg_email').val() == ""){
 		
 			$('#static_ctrl_final').html("L'indirizzo email non pu&ograve; essere vuoto");
 			$('#static_ctrl_final').slideDown('fast');
 			return false;
 		
 		}
 		if ($('#reg_cellulare').val() == ""){
 		
 			$('#static_ctrl_final').html("Devi specificare il cellulare");
 			$('#static_ctrl_final').slideDown('fast');
 			return false;
 		
 		}
 		if ($('input[name=reg_conoscenza_sito]:checked').val() == "" || $('input[name=reg_conoscenza_sito]:checked').val() ==undefined){
 		
 			$('#static_ctrl_final').html("Devi rispondere alla domanda \"Come sei venuta a conoscenza del sito?\"");
 			$('#static_ctrl_final').slideDown('fast');
 			return false;
 		
 		}
		
		
		almenoUno = false;
		$("#main_content").find("div[class=controlloDiv]").each(function(){
			if($(this).html()!="")
				almenoUno = true;
		})
		
		if (almenoUno){
 			$('#static_ctrl_final').html("Controlla i dati inseriti, non sono corretti");
 			$('#static_ctrl_final').slideDown('fast');
 			return false;
 		}
 		/*if($('#reg_privacy').attr("checked")==false)
 		{	 
			$('#static_ctrl_final').html("E' obbligatorio acconsentire al trattamento dati.");
			$('#static_ctrl_final').slideDown('fast');
			return false;
	 	}*/
		if ($('#static_reg_form #reg_marketing:checked').val() == null){
			 alert("Per registrarti devi aver letto ed accettato l'informativa sulla privacy e acconsentire a ricevere la newsletter e ad essere contattato per attivita' di marketing da Lines");
			 return false;
		} 
		if ($('#static_reg_form #reg_teco:checked').val() == null){
			 alert("Per registrarti devi aver letto ed accettato i termini e le condizioni d'uso dei servizi di Fater SpA");
			 return false;
		}
		/*
		if (!errMAIL && !errCEL && !errCEL1 && !errCEL2 && !errPHONE && !errMAIL_COMPARE){
 			alert("ok");
 			//postAjaxRequest('static_reg_form','/actions/doStaticRegister.php','','div.inner_page_content_container');
 			
 		} else {
 			
 			$('#static_ctrl_final').html("Controlla i dati inseriti, non sono corretti");
 			$('#static_ctrl_final').slideDown('fast');
 			return false;
 		
 		}*/
		
		almenoUno = false;
		$("#main_content").find("div[class=controlloDiv]").each(function(){
			if($(this).html()!="")
				almenoUno = true;
		})
		
		if (almenoUno){
 			$('#static_ctrl_final').html("Controlla i dati inseriti, non sono corretti");
 			$('#static_ctrl_final').slideDown('fast');
 			return false;
 		
 		}
		else
		{
			//alert("ok");
			$('div#reg_full_form_container').fadeOut('fast', function(){
			
 				postAjaxRequest('static_reg_form','/actions/doStaticRegister.php','','div#reg_form_result');
 			});
		}
 	});

});


	function isMobileNumber(number){	

		prefix = number.substr(0,3);
		
		var numbers = new Array('330', '331', '333', '334', '335', '336', '337', '338', '339', '360', '363', '366', '368','340', '343', '345', '346', '347', '348', '349','320', '323', '327', '328', '329', '380', '383', '388', '389','390', '391', '392', '393','389', '377','366','331');
		
		
		if (number.length == 10 ) {
			 for (i = 0; i < numbers.length; i++){
			 
			 	if (prefix == numbers[i]) {
			 		return true;
			 	}
			 
			 }
		 }
		 return false;
	
	
	}
	
	 function isMobileNumberLacivita(valore){
		 /** 
		 regole:
			prima cifra
				1/9 => max 13, min 9 cifre
				+ => 13 cifre
		 */
		 if(valore.substr(0,1)=="+" && (valore.length==13 || valore.length==12)){
			 return true;
		 }
		 if(valore.substr(0,1)!="+" && valore.substr(0,1)!="0" && (valore.length==10 || valore.length==9)){
			 return true;
		 } 
		 if(valore.substr(0,2)=="00"&& (valore.length==14 || valore.length==13)){
			 return true;
		 } 
		 
		 return false;
	 }

	
	function isPhone(number) {
	
		reg = new RegExp("^\\d{7,12}$");
		//alert(reg);
		//alert(reg);
		if (reg.test(number)){
		
		 	return true;
		 	
		} else {
		
			return false;
		}
	
	}

 		
