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;

switchSliderPM = function(idEl){
	$(".regStepC").hide();
	$("#ctrl_step1_end,#ctrl_step2_end,#ctrl_step3").html("");
	$("#"+idEl).show();
}

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;
	}

}


 $(document).ready(function(){

	$('input').focus(function(){
		$('#ctrl_step1').slideUp('fast');
 		$('#ctrl_step1').html('');
		$('#ctrl_step2').slideUp('fast');
 		$('#ctrl_step2').html('');
		$('#ctrl_step3').slideUp('fast');
 		$('#ctrl_step3').html('');
	});
	
	
 	$('#reg_user_name').val('').blur(function(){
		$.getJSON( '/actions/doUserValidation.php' ,
		{ action: 'usernameString', value: $.trim($('#reg_user_name').val()) },
		function(data) {
				if (!data.result) {
				
					$('#ctrl_user_name').append("username troppo corta! Minimo 4 caratteri<br/>");
					$('#ctrl_user_name').slideDown('fast');
					
					errUNAME = true;
					
				} else {
				
					errUNAME = false;
					
				}
			});
 	}).blur(function(){
		$.getJSON( '/actions/doUserValidation.php' ,
		{ action: 'username', value: $('#reg_user_name').val() },
		function(data) {
				if (!data.result) {
					$('#ctrl_user_name').append("username gi&agrave; esistente!<br/>");
					$('#ctrl_user_name').slideDown('fast');
					
					errUNAME = true;
					
				} else {
				
					errUNAME = false;
					
				}
			});
			
 	}).focus(function(){
 		$('#ctrl_user_name').slideUp('fast');
 		$('#ctrl_user_name').html('');
 	});
 	
 	$('#reg_password').val('').focus(function(){
 		$('#ctrl_password').slideUp('fast');
 		$('#ctrl_password').html('');
 	});
 	
 	$('#reg_confirm_password').val('').blur(function(){
 		if ($('#reg_confirm_password').val() != $('#reg_password').val())
 		{
 			$('#ctrl_password').append("Le password non coincidono!<br/>");
 			$('#ctrl_password').slideDown('fast')
 			
 			errPASS = true;
					
		} else {
				
			errPASS = false;
					
		}
	}).focus(function(){
 		$('#ctrl_password').slideUp('fast');
 		$('#ctrl_password').html('');
 	});
 	
 	
 	
 	$('#reg_datadinascita').datepicker({
 		yearRange: '-80:0',
	 	dateFormat: 'yy-mm-dd'
	 });
 		
	$('#step1_btn').click(function(){
		switchSliderPM('reg_slider_step2');
	});
	
	$('#step2_btn').click( function (){
		switchSliderPM('reg_slider_step3');
	});
 	
	
	step1Ckeck = function(){
			
		htmlMessaggio = "";
 		if ($.trim($('#reg_user_name').val()).match(/@/)=="@") {
			htmlMessaggio += "<br>- Lo Username non pu&ograve; essere un'indirizzo email";
		}
		if (($.trim($('#reg_user_name').val()) == "") || ($.trim($('#reg_password').val()) == "")) {	 			
			htmlMessaggio += "<br>- Username e Password non possono essere vuoti";
	 	}
	 	
	 	if ($('#reg_password').val() !=  $('#reg_confirm_password').val()) {
			htmlMessaggio += "<br>- Password e conferma non coincidono!";
	 	}
	 	
	 	if (($.trim($('#reg_name').val()) == "") || ($.trim($('#reg_cogname').val()) == "")) {
 			htmlMessaggio += "<br>- Nome e Cognome non possono essere vuoti";
	 	}
	 	
	 	if (($('#reg_datadinascita').val() == "")) {
 			htmlMessaggio += "<br>- La data di nascita non pu&ograve; essere vuota!";
	 	}
	 	
		if (errUNAME || errPASS){
 			htmlMessaggio += "<br>- Controlla lo username o la password";
		}
		
		if(htmlMessaggio!=""){
	 		$('#ctrl_step1_end').html("<b>STEP 1:</b>"+htmlMessaggio+"<br><p onclick=\"switchSliderPM('reg_slider');\" style='cursor:pointer;'><small>&lt;</small><u>Torna al primo step</u></p>");
	 		$('#ctrl_step1_end').slideDown('fast');
			return false;
	 	} else {
			return true;
	 	}
	 }
	 
	step2Ckeck = function(){
		htmlMessaggio = "";
 		if ($('#reg_step2_cap').val() == "" || $('#reg_step2_cap').val() == "CAP*"){
 			htmlMessaggio += "<br>- Il CAP non pu&ograve; essere vuoto";
 		}

		if ($('#reg_step2_cellulare').val() == ""){
 			htmlMessaggio += "<br>- Devi specificare il cellulare";
 		}
		if ($('#reg_step2_cellulare').val()!="" && !isMobileNumberLacivita($('#reg_step2_cellulare').val())){
 			htmlMessaggio += "<br>- Il numero non &egrave; un cellulare!";
 		}

		if ($('#reg_step2_email').val() == ""){
 			htmlMessaggio += "<br>- L'indirizzo email non pu&ograve; essere vuoto";
 		}
	 	
 		if ($('#reg_step2_email').val()!="" && $('#reg_step2_confermaemail').val()!=$('#reg_step2_email').val()){	 			
 			htmlMessaggio += "<br>- Indirizzo email e conferma non coincidono!";
	 	}
 		
		//alert("\nerrMAIL: "+errMAIL+"\nerrCEL: "+errCEL+"\nerrCEL1: "+errCEL1+"\nerrCEL1: "+errCEL1+"\nerrPHONE: "+errPHONE)
 		
		//if (((errMAIL) || (errCEL) || (errCEL1) || (errCEL2) || (errPHONE))){
 			//$('#ctrl_step2').html("Controlla i dati inseriti, non sono corretti");
 			//$('#ctrl_step2').slideDown('fast');
 		//} else {
 			//alert(errCEL);
			//switchSlider('reg_slider_step2', 'reg_slider_step3');
 		//}
		almenoUno = false;
		$("#reg_slider_step2").find("div[class=controlloDiv]").each(function(){
			if($(this).html()!="")
				almenoUno = true;
		})
		
		if (almenoUno){
 			htmlMessaggio += "<br>- Controlla i dati inseriti nello STEP 2, non sono corretti";
 		} 
		
		if(htmlMessaggio!=""){
	 		$('#ctrl_step2_end').html("<b>STEP 2:</b>"+htmlMessaggio+"<br><p onclick=\"switchSliderPM('reg_slider_step2');\" style='cursor:pointer;'><small>&lt;</small><u>Torna al secondo step</u></p>");
	 		$('#ctrl_step1_end').slideDown('fast');
			return false;
	 	} else {
			return true;
	 	}

 	};
 	
  	/*
 	*
 	* Inserire i controlli per i campi per il cap
 	*
 	*/
 	$('#reg_step2_cap').blur(function(){
     	if ($(this).val()!="CAP*" && $(this).val()!="" && (isNaN($(this).val()) || $(this).val().length!=5 )) {
		//if ($(this).val()!="CAP" && $(this).val()!="" && isNaN($(this).val())) {
			$('#ctrl_cap').html("Inserire solo cifre per il CAP");
			$('#ctrl_cap').slideDown('fast');
			errCAP = true;
		} else {
			$('#ctrl_cap').slideUp('fast');
			$('#ctrl_cap').html('');
		}
	});
	
	
 	/*
 	*
 	* Inserire i controlli per il telefono fisso
 	*
 	*/
 	
 	$('#reg_step2_telefono').val('').blur(function(){
 		if (this.value != "") {
 		if(isMobileNumber($('#reg_step2_telefono').val()) || !isPhone($('#reg_step2_telefono').val())) {
 		
 			$('#ctrl_telefono').html("Numero di telefono fisso non valido");
	 		$('#ctrl_telefono').slideDown('fast');
	 			errPHONE = true;
	 		
	 		} else {
	 		
	 			errPHONE = false;
	 		}
 		} else { errPHONE = false; return true;}
 		
 	}).focus(function(){
 	
 		$('#ctrl_telefono').slideUp('fast');
 		$('#ctrl_telefono').html('');
 	
 	}); 	
 	
 	
 	/*
 	*
 	* Inserire i controlli per i campi per i telefoni celluari
 	*
 	*/
 	$('#reg_step2_cellulare').val('').blur(function(){
 	
 	if (this.value != "") {
		$.getJSON( '/actions/doUserValidation.php' ,
		{ action: 'mobile', value: this.value },
		function(data) {
				if (!data.result) {
					$('#ctrl_cellulare').html("Numero di cellulare gi&agrave; esistente!");
				 	$('#ctrl_cellulare').slideDown('fast');
				 		
				 	errCEL = true;
					
				} else {
				
					errCEL = false;
					
				}
			});
		} else return true;	
 	}).blur(function(){
 	
 	if (this.value != "") {
			if (!isMobileNumberLacivita(this.value)) {
				$('#ctrl_cellulare').html("Il numero non &egrave; un cellulare!");
				$('#ctrl_cellulare').slideDown('fast');
				errCEL = true;
			}
		}
 	}).focus(function(){
 	
 		$('#ctrl_cellulare').slideUp('fast');
 		$('#ctrl_cellulare').html('');
 	
 	}); 	
 	 
 		 

	
	/*
	*
	* Controllo Email
	*
	*/
	/*
	$('#reg_step2_email').val('').blur(function(){
		$.getJSON( '/actions/doUserValidation.php' ,
		{ action: 'emailString', value: $('#reg_step2_email').val() },
		function(data) {
				if (!data.result) {
				
					$('#ctrl_email').append("indirizzo email non valido<br/>");
					$('#ctrl_email').slideDown('fast');
					
					errMAIL = true;
					
				} else {
				
					errMAIL = false;
					
				}
			});
			
 	}).blur(function(){
		$.getJSON( '/actions/doUserValidation.php' ,
		{ action: 'email', value: $('#reg_step2_email').val() },
		function(data) {
				if (!data.result) {
					$('#ctrl_email').append("indirizzo email gi&agrave; esistente!<br/>");
					$('#ctrl_email').slideDown('fast');
					
					errMAIL = true;
					
				} else {
				
					errMAIL = false;
					
				}
			});
		});
	
	$('#reg_step2_confermaemail').val('').blur(function(){
 		if ($('#reg_step2_confermaemail').val() != $('#reg_step2_email').val())
 		{
 			$('#ctrl_email').append("Le email non coincidono!<br/>");
 			$('#ctrl_email').slideDown('fast')
 			
 			errMAIL = true;
					
		} else {
				
			errMAIL = false;
					
		}
	}).focus(function(){
 		$('#ctrl_email').slideUp('fast');
 		$('#ctrl_email').html('');
 	});
	*/
	
	$('div [cosa=email]').val('').blur(function(){
		$.getJSON( '/actions/doUserValidation.php' ,
		{ action: 'emailString', value: $('#reg_step2_email').val() },
		function(data) {
				if (!data.result) {
					if($('#ctrl_email').html()=="")
						$('#ctrl_email').append("indirizzo email non valido<br/>");
					$('#ctrl_email').slideDown('fast');
					
					errMAIL = true;
					
				} else {
				
					errMAIL = false;
					
				}
			});
			
 	}).blur(function(){
		$.getJSON( '/actions/doUserValidation.php' ,
		{ action: 'email', value: $('#reg_step2_email').val() },
		function(data) {
				if (!data.result) {
					if($('#ctrl_email').html()=="")
						$('#ctrl_email').append("indirizzo email gi&agrave; esistente!<br/>");
					$('#ctrl_email').slideDown('fast');
					
					errMAIL = true;
					
				} else {
				
					errMAIL = false;
					
				}
			});
	}).blur(function(){
 		if ($('#reg_step2_confermaemail').val() != $('#reg_step2_email').val())
 		{
			if($('#ctrl_email').html()=="")
				$('#ctrl_email').append("Le email non coincidono!<br/>");
 			$('#ctrl_email').slideDown('fast')
 			
 			errMAIL_COMPARE = true;
					
		} else {
				
			errMAIL_COMPARE = false;
					
		}
	}).blur(function(){
 		if ($('#reg_step2_confermaemail').val() != $('#reg_step2_email').val())
 		{
 			if($('#ctrl_email').html()=="")
				$('#ctrl_email').append("Le email non coincidono!<br/>");
 			$('#ctrl_email').slideDown('fast')
 			
 			errMAIL_COMPARE = true;
					
		} else {
				
			errMAIL_COMPARE = false;
					
		}
	}).focus(function(){
 	
 		$('#ctrl_email').slideUp('fast');
 		$('#ctrl_email').html('');
 	
 	});
	
	
	/*$('#step2_btn').click( function (){
 		if ($('#reg_step2_cellulare').val() == ""){
 		
 			$('#ctrl_step2').html("Devi specificare almeno un cellulare");
 			$('#ctrl_step2').slideDown('fast');
 			return false;
 		
 		}

		if ($('#reg_step2_email').val() == ""){
 		
 			$('#ctrl_step2').html("L'indirizzo email non pu&ograve; essere vuoto");
 			$('#ctrl_step2').slideDown('fast');
 			return false;
 		
 		}
	 	
 		//if ($('#reg_step2_confermaemail').val() != $('#reg_step2_email').val()){	 			
			//$('#ctrl_step2').html("Indirizzo email e conferma non coincidono!");
			//$('#ctrl_step2').slideDown('fast');
			/return false;
	 	//}
 		
		//alert("\nerrMAIL: "+errMAIL+"\nerrCEL: "+errCEL+"\nerrCEL1: "+errCEL1+"\nerrCEL1: "+errCEL1+"\nerrPHONE: "+errPHONE)
 		
		//if (((errMAIL) || (errCEL) || (errCEL1) || (errCEL2) || (errPHONE))){
 			//$('#ctrl_step2').html("Controlla i dati inseriti, non sono corretti");
 			//$('#ctrl_step2').slideDown('fast');
 		//} else {
 			//alert(errCEL);
			//switchSlider('reg_slider_step2', 'reg_slider_step3');
 		//}
		almenoUno = false;
		$("#reg_slider_step2").find("div[class=controlloDiv]").each(function(){
			if($(this).html()!="")
				almenoUno = true;
		})
		
		if (almenoUno){
 			$('#ctrl_step2').html("Controlla i dati inseriti, non sono corretti");
 			$('#ctrl_step2').slideDown('fast');
 			return false;
 		
 		}
		else
		{
			switchSlider('reg_slider_step2', 'reg_slider_step3');
		}
 	
 	});
	*/
 	
 	
 	$('#submit_btn').click(function(){
		resStep1 = step1Ckeck();
		resStep2 = step2Ckeck();
		
		if(!resStep1 || !resStep2){
			return false;
		}
		
 		if ($('input[name=reg_step3_conoscenza_sito]:checked').val() == "" || $('input[name=reg_step3_conoscenza_sito]:checked').val() ==undefined){
 			$('#ctrl_step3').html("Devi rispondere alla domanda \"Come sei venuta a conoscenza del sito?\"");
 			$('#ctrl_step3').slideDown('fast');
 			return false;
 		}
		
 		if ($('#reg_form #reg_marketing:checked').val() == null){
			$('#ctrl_step3').html("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");
			$('#ctrl_step3').slideDown('fast');
			return false;
	 	} 
		
 		if ($('#reg_form #reg_teco:checked').val() == null){
			$('#ctrl_step3').html("Per registrarti devi aver letto ed accettato i termini e le condizioni d'uso dei servizi di Fater SpA");
			$('#ctrl_step3').slideDown('fast');
			return false;
	 	} 
		
		postAjaxRequest('reg_form','/actions/doRegister.php','','div#reg_slider_step3');
 	
 	});

});

 		
