var tipo_mod=0;
function initRegistrazione(pTipo)
{
	tipo_mod=pTipo;
	if($("input[@name='tipo_reg']:checked").val()==0){
		$('.t_privato').show();
		$('.t_azienda').show();
	}else{
		$('.t_azienda').hide();
		$('.t_privato').show();
	}


	$("input[name='tipo_reg']:radio").change(function(){
		if($("input[@name='tipo_reg']:checked").val()==0){
			$('.t_privato').show();
			$('.t_azienda').show();
		}else{
			$('.t_azienda').hide();
			$('.t_privato').show();
		}
	});

	$("#registrati").click(function(){
		
		$('#tgt_err_reg_').html("");
		resetObj();
		var err=0;
		$('.ck_reg').css('background','');
		$('.ck_reg:visible').each(function(index){
			if($(this).val()==""){
				$(this).css('background','#ffde00');
				err++;
			}else{
				addObjItem($(this).attr('id'),$(this).val());
			}
		});
		
		if(!validateEmail($('#r_email').val())){
			$('#r_email').css('background','#ffde00');
			err++;
		}
		if($('#r_password1').val()!=$('#r_password2').val()){
			$('#r_password2').css('background','#ffde00');
			err++;
		}
		if($("input[@name='tipo_reg']:checked").val()==0){
			$('#r_rag_soc').css('background','');
			$('#r_p_iva').css('background','');
			if($('#r_rag_soc').val()==""){
				$('#r_rag_soc').css('background','#ffde00');
				err++;
			}
			if($('#r_p_iva').val()==""){
				$('#r_p_iva').css('background','#ffde00');
				err++;
			}
			addObjItem('r_rag_soc',$("#r_rag_soc").val());
			addObjItem('r_p_iva',$("#r_p_iva").val());
			addObjItem('r_cod_fisc',$("#r_cod_fisc").val());
			
		}else{
			$('#r_cod_fisc').css('background','');
			if($('#r_cod_fisc').val()==""){
				$('#r_cod_fisc').css('background','#ffde00');
				err++;
			}
			addObjItem('r_cod_fisc',$("#r_cod_fisc").val());
		}
		console.log("step 0")
		if(err>0){return;}
		
		addObjItem('r_settore',$("#r_settore").val());
		addObjItem('r_cellulare',$("#r_cellulare").val());
		addObjItem('r_newsletter',($("#r_newsletter").attr('checked'))?1:0);
		addObjItem('tipo',$("input[@name='tipo_reg']:checked").val());
		console.log("step 1")
		if(tipo_mod==0){
			console.log("step 2")
			if(!$('#r_trattamento').attr('checked')){
				$('#tgt_err_reg_').html("Devi accettare il trattamento dei dati personali per proseguire!")
				return;
			}
			if(!$('#r_condizioni').attr('checked')){
				$('#tgt_err_reg_').html("Devi accettare le condizioni di vendita per proseguire!")
				return;
			}
		}
		addObjItem('insup',tipo_mod);
		$.post(_lib+'join.php',getObj(),function(data){
			if(data==0){
				$('#tgt_err_reg_').html("Indirizzo e-mail già presente in archivio");
			}else if(data==1){
				location.href=_root+'registrazione-ok.php';
			}else if(data==2){
				alert("profilo modificato");
			}
		});
	});
	initLogin();
}

function initLogin(){
	if($('#log_email').val()!=""){$('div:first',$('#log_email').parent()).hide();};
	
	$('.fl_login').live('mouseover mouseout keypress click',function(event){
		if(event.type=='mouseover'||event.type=='keypress'||event.type=='click'){
			if(event.type=='keypress' && event.keyCode==13){
				gologin();
			}else{
				$('div:first',$(this).parent()).hide();
			}
		}else{
			if($(this).val()==""){$('div:first',$(this).parent()).show();}
		}
	});
	$("#login").click(function(){
		gologin();
	});	
	$("#btrepwd").click(function(){
		if($('#log_repwd').val()!=""){
			$.post(_lib+'repwd.php',{email:$('#log_repwd').val()},function(d){
				if(d==1){
					$('#tgt_err').html("Recupero password eseguito correttamente .. controlla la posta!");
					swlrp('tg_repwd|tg_login');
				}else{
					$('#tgt_err_reg_').html("Nessun utente con questo indirizo email &egrave; presente in archivio.");
				}
			});
		}
	});	
	getitembag();
}

function gologin()
{
	$('#log_email').css('background','');
	$('#log_pwd').css('background','');
	$('#tgt_err').html("");
	var err=0;
	if($('#log_email').val()==""){
		$('#log_email').css('background','#ffde00');
		err++;
	}
	if($('#log_pwd').val()==""){
		$('#log_pwd').css('background','#ffde00');
		err++;
	}
	if(err>0){return;}
	$.post(_lib+'login.php',{user:$('#log_email').val(),pwd:$('#log_pwd').val()},function(data){
		if(data==1){
			location.href=_root+'profilo.php';
			/*if($('#myref').val()=="")
				location.href=_root+'carrello.php';
			else
				location.href=$('#myref').val();*/
		}else if(data==2){
			$('#tgt_err').html("Questo utente non risulta attivo .. controlla la posta!");
		}else{
			$('#tgt_err').html("Utente non trovato in archivio!!");
			$('.fl_login').val('');
			$('div:first',$('.fl_login').parent()).show();
		}
	});	
}

var objreg;
function resetObj(){
	objreg=new Object();
}
function addObjItem(lab,val){
	objreg[lab]=val;
}
function getObj(){
	return objreg;
}
function swlrp(st){
	var e=st.split('|');
	$('#'+e[0]).fadeOut(500,function(){$('#'+e[1]).fadeIn(500);});
}


