$(document).ready(function() {
        
		
		//the min chars for username
		var min_chars = 3;
		
		//result texts
		var characters_error = '';
		var checking_html = '<img src="imgs/lb_loading.gif" /> Chequeando...';
		
		//when button is clicked
		$('#chequear').click(function(){
			//run the character number check
			if($('#usuario').val().length < min_chars){
				//if it's bellow the minimum show characters_error text
				$('#disponible').html(characters_error);
			}else{			
				//else show the cheking_text and run the function to check
				$('#disponible').html(checking_html);
				check_availability();
			}
		});
		
		
});

//function to check username availability	
function check_availability(){
		
		//get the username
		var usuario = $('#usuario').val();
		
		//use ajax to run the check
		$.post("check_username.php", { usuario: usuario },
			function(result){
				//if the result is 1
				if(result == 1){
					//show that the username is available
					$('#disponible').html('<span class="celeste"><b>' +usuario + '</b> est&aacute; disponible</span>');
				}else{
					//show that the username is NOT available
					$('#disponible').html('<span class="rojo"><b>' +usuario + '</b> no est&aacute; disponible</span>');
				}
		});

}  
