var timer = {
   time: 0,
   now: function(){ return (new Date()).getTime(); },
   start: function(){ this.time = this.now(); },
   since: function(){ return this.now()-this.time; }
};

var normaliza = true;
var normaliza2 = true;
var normaliza3 = true;

jQuery(document).ready(function(){

	jQuery('#via_label').hide();
	jQuery('#vias').hide();

	jQuery('#cp').keyup(function(){ 
	 	cp = jQuery(this).val();
	 	if(cp.length == 5){
	 		var loading = 'Cargando...';
	 		jQuery('#localidad').html(loading);
			jQuery.getJSON('datos.php',{'cp':cp},function(datos){
				select = '';
				jQuery.each(datos,function(){
					select = select + '<option value="' + this + '">' + this + '</option>';
	 			})
	 			jQuery('#localidad').html(select);
	 			jQuery('#via_label').show();
 			}); 
		}
		else
		{
			jQuery('#via').val('');
			jQuery('#localidad').html('');
			jQuery('#vias').html('');
			jQuery('#vias').hide();
			jQuery('#via_label').hide();
		}
	});	
	
	jQuery('#via').keyup(function(){
		if(normaliza == true)
		{
	 	cp = jQuery('#cp').val();
		via = jQuery(this).val();
		div = '';
		lastref = jQuery('#via').attr('lastref');
		if(via != '')
		{
			if(via != lastref)
			{
				if (timer.since() < 1000) {
				 	setTimeout(function(){
						jQuery('#via').trigger('keyup');
					},1000);
				 	return;
				}
				jQuery.getJSON('datos.php',{'cp':cp,'via':via},function(datos){
					div = div + '<li class="opcloser" style="cursor:pointer;">No encuentro mi calle</li>';
				   	
					jQuery.each(datos,function(){
						div = div + '<li class="op" style="cursor:pointer;">' + this + '</li>';
		 			})
		 			jQuery('#vias').html(div);
		 			jQuery('#vias').show('fast');
		 			jQuery('.op').click(function(){
						jQuery('#via').val(jQuery(this).html());
						jQuery('#via').attr('lastref', jQuery(this).html());
						jQuery('#vias').hide('fast');
					});
					jQuery('.opcloser').click(function(){
						  jQuery('#vias').hide('fast');
						  normaliza = false;
					  });
				});	
				timer.start();
			}
		}
		}	 
	});


	/* Código del segundo Formulario */

                jQuery('#vias2').hide();

                jQuery('#cp2').keyup(function(){ 
				cp = jQuery(this).val();
				if(cp.length == 5){
					var loading = 'Cargando...';
					jQuery('#localidad2').html(loading);
					jQuery.getJSON('datos.php',{'cp':cp},function(datos){
								   select = '';
								   jQuery.each(datos,function(){
												  select = select + '<option value="' + this + '">' + this + '</option>';
								   })
								   jQuery('#localidad2').html(select);
							   jQuery('#via_label2').show();
				}); 
				}
				else
				{
					jQuery('#via2').val('');
					jQuery('#localidad2').html('');
					jQuery('#vias2').html('');
					jQuery('#vias2').hide();
				}
				});           

				jQuery('#via2').keyup(function(){
				if(normaliza2 == true)
				{
				cp = jQuery('#cp2').val();
				via = jQuery(this).val();
				div = '';
				lastref = jQuery('#via2').attr('lastref');
				if(via != '')
				{
				if(via != lastref)
				{
					if (timer.since() < 1000) {
						   setTimeout(function(){
								jQuery('#via2').trigger('keyup');
						  },1000);
						   return;
				   }
				   jQuery.getJSON('datos.php',{'cp':cp,'via':via},function(datos){
				   	div = div + '<li class="op2closer" style="cursor:pointer;">No encuentro mi calle</li>';
				   	
					  jQuery.each(datos,function(){
						div = div + '<li class="op2" style="cursor:pointer;">' + this + '</li>';
					  })
					  jQuery('#vias2').html(div);
					  jQuery('#vias2').show('fast');
					  jQuery('.op2').click(function(){
						  jQuery('#via2').val(jQuery(this).html());
						  jQuery('#via2').attr('lastref', jQuery(this).html());
						  jQuery('#vias2').hide('fast');
					  });
					  jQuery('.op2closer').click(function(){
						  jQuery('#vias2').hide('fast');
						  normaliza2 = false;
					  });
				});           
			   timer.start();
			}
		}   
		}           
	});


				/* Código del tercer Formulario */

                jQuery('#vias3').hide();

                jQuery('#cp3').keyup(function(){ 
				cp = jQuery(this).val();
				if(cp.length == 5){
					var loading = 'Cargando...';
					jQuery('#localidad3').html(loading);
					jQuery.getJSON('/datos.php',{'cp':cp},function(datos){
								   select = '';
								   jQuery.each(datos,function(){
												  select = select + '<option value="' + this + '">' + this + '</option>';
								   })
								   jQuery('#localidad3').html(select);
							   jQuery('#via_label3').show();
				}); 
				}
				else
				{
					jQuery('#via3').val('');
					jQuery('#localidad3').html('');
					jQuery('#vias3').html('');
					jQuery('#vias3').hide();
				}
				});           

				jQuery('#via3').keyup(function(){
				if(normaliza3 == true)
				{
				cp = jQuery('#cp3').val();
				via = jQuery(this).val();
				div = '';
				lastref = jQuery('#via3').attr('lastref');
				if(via != '')
				{
				if(via != lastref)
				{
					if (timer.since() < 1000) {
						setTimeout(function(){
							jQuery('#via3').trigger('keyup');
						},1000);
					return;
				}
				jQuery.getJSON('/datos.php',{'cp':cp,'via':via},function(datos){
					div = div + '<li class="op3closer" style="cursor:pointer;">No encuentro mi calle</li>';
					jQuery.each(datos,function(){
						div = div + '<li class="op3" style="cursor:pointer;">' + this + '</li>';
					});
				  jQuery('#vias3').html(div);
				  jQuery('#vias3').show('fast');
				  jQuery('.op3').click(function(){
					  jQuery('#via3').val(jQuery(this).html());
					  jQuery('#via3').attr('lastref', jQuery(this).html());
					  jQuery('#vias3').hide('fast');
					});
					jQuery('.op3closer').click(function(){
						  jQuery('#vias3').hide('fast');
						  normaliza3 = false;
					});
				});           
			   timer.start();
			}
		} 
		}             
	});
	

});