$(document).ready(function() {
	$('#loopedSlider').loopedSlider();	
	$("a[rel^='prettyPhoto']").prettyPhoto({
	});
	$("#basic-accordian").accordion({
		active: false ,
		collapsible: true,
		autoHeight: false
	});
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		//$(id).css('top',  '800px');
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 
		
		$('#msg').hide();
	
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});		
	
	//Click al login
	$('#login_click').click( function(){
		var userName = $("#username"); //name field  
		var password = $("#password"); //email field  
		if(validateForm(userName,password)){  
		$.ajax({
			type: "POST",
			url: "area-riservata/utenti.class.php?op=login",
			data: "user="+userName.val()+"&password="+password.val(), 
			beforeSend: function(msg){
				$('#msg').show().css('color','green').text('Controllo dati accesso in corso.');
			},
			success: function(msg){
				if(msg=='OK'){
					location.href="area-riservata/home.php";
				
				}else{
					$('#msg').show().text('Errore nell\'autenticazione delle credenziali.');
				
				}
			  
			},
			error:function(msg){
				$('#msg').show().text('Errore nell\'autenticazione delle credenziali.');}
		});
	
		
		}
 
		return false;
	
	});
	
	
 
	
 
	// Click di registrazione
	$('#register_click').click( function(){

		if(!validateFormR($("#socio"),'Codice socio assente!'))
			return false;  
		if(!validateFormR($("#piva"),'Partita Iva assente!'))
			return false; 
		if(!validateFormR($("#iban"),'Iban assente!'))
			return false;  
		if(!validateFormR($("#indirizzo"),'Indirizzo assente!'))
			return false;  
		if(!validateFormR($("#cap"),'Cap assente!'))
			return false;  
		if(!validateFormR($("#localita"),'Localita assente!'))
			return false;  
		if(!validateFormR($("#email"),'Email assente!'))
			return false;  
		if(!validateFormR($("#telefono"),'Telefono assente!'))
			return false;  
		if(!$('input[name=privacy]').is(':checked')){
			$('input[name=privacy]').addClass('invalid');
			$('#msgR').show().text('Devi accettare le l\'informativa sulla Privacy');
			return false;  
		}

			
		$.ajax({
			type: "POST",
			url: "area-riservata/utenti.class.php?op=register",
			data: $('#registrazione').serialize(),
			beforeSend: function(msg){
			 $('#msgR').show().css('color','green').text('Controllo dati accesso in corso.');
			},
			success: function(msg){

				if(msg=='OK'){
					var Str='<span style="text-align:center;">Registrazione effettuata</br>';
						Str=Str	+ 'Vi &egrave; stata inviata una e-mail con tutti i dati di accesso.</span>';
					$('#msgR').show().html(Str).css('color','green');
					
				
				}else{
					$('#msgR').show().text(msg).css('color','red');
				
				}
			  
			},
			error:function(msg){
				$('#msgR').show().text('Errore');}
		});

	});
	
	// Click di registrazione
	$('#reset_submit').click( function(){
		var cod_socio = $("#socio_reset"); //name field  
		var email = $("#email_reset"); //email field  
		if(!validateFormReset($("#socio_reset"),'Codice socio assente!'))
			return false;  
		if(!validateFormReset($("#email_reset"),'Email assente!'))
			return false;  
		
		$.ajax({
			type: "POST",
			url: "area-riservata/utenti.class.php?op=reset",
			data: "cod_socio="+cod_socio.val()+"&email="+email.val(), 
			beforeSend: function(msg){
			 $('#msgReset').show().text('Controllo dati accesso in corso.');
			},
			success: function(msg){
				if(msg=='OK'){
					var Str='<span style="text-align:center;">Operazione effettuata</br>';
						Str=Str	+ 'Vi &egrave; stata inviata una e-mail con i nuovi dati di accesso</br>';
						Str=Str	+ 'da utilizzare per accedere all\'area riservata.</span>';
					$('#msgReset').show().html(Str).css('color','green');
				
				}else{
					$('#msgReset').show().text(msg).css('color','red');
				
				}
			  
			},
			error:function(msg){
				$('#msgReset').show().text('Errore generale.');}
		});
 
		return false;
	});
	
	$('a[name=register]').click( function(){
		$('#login').hide();
		var winH = $(window).height();
		var winW = $(window).width();
		$('#register').css('top',winH/2-$('#register').height()/2);
		$('#register').css('left', winW/2-$('#register').width()/2);
	
		//transition effect
		$('#register').fadeIn(1000);
		$('.msg').hide().html();
 
		return false;
		
		
	});
	
	$('a[name=reset_click]').click( function(){
		$('#login').hide();
		var winH = $(window).height();
		var winW = $(window).width();
		$('#reset').css('top',winH/2-$('#reset').height()/2);
		$('#reset').css('left', winW/2-$('#reset').width()/2);
	
		//transition effect
		$('#reset').fadeIn(1000); 
		$('.msg').hide().html();
		return false;
		
		
	});
	
	$('a[name=indietro]').click( function(){
		$('#register').hide();
		$('#reset').hide();
		var winH = $(window).height();
		var winW = $(window).width();
		$('#login').css('top',winH/2-$('#login').height()/2);
		$('#login').css('left', winW/2-$('#login').width()/2);
	
		//transition effect
		$('.msg').hide().html();
		$('#login').fadeIn(1000); 
 
		return false;
		
		
	});
	
	
	$('input').click(function(){
		//$(this).val('');
		$(this).removeClass('invalid');
	});
	
	$('#piva, #socio').focusout(function(){
		if($('#socio').val()!='' && $('#piva').val()!=''){
			$.getJSON('area-riservata/utenti.class.php?op=userData&cod_socio='+$('#socio').val()+'&piva='+$('#piva').val(), function(obj) {
				if($('#indirizzo').val()==''){
				   $('#indirizzo').val(obj.indirizzo);
				}
				if($('#localita').val()==''){
				   $('#localita').val(obj.localita)
				}
				if($('#cap').val()==''){
				   $('#cap').val(obj.cap)
				}
			});
		}
	});
	checkCookie();
	
});

function validateForm(user,pass){  
	if(!user.val()){
		user.addClass('invalid');
		$('#msg').show().text('Inserisci il codice socio!');
		return false;  
	}	
	if(!pass.val()){
		pass.addClass('invalid');
		$('#msg').show().text('Inserisci la password!');
		return false;  
	}		
	return true;
}  

function validateFormR(ele,msg){  
	if(!ele.val()){
		ele.addClass('invalid');
		$('#msgR').show().text(msg);
		return false;  
	}	
	return true;
}  
function validateFormReset(ele,msg){  
	if(!ele.val()){
		ele.addClass('invalid');
		$('#msgReset').show().text(msg);
		return false;  
	}	
	return true;
}  


