// JavaScript Document
$(document).ready(function() {			 
			var loginActivo = false;
			muestral = function ()
			   {
						$('#caja_login').show();			
						setTimeout(function()
								  {
									  $("#correoe").focus()
								  },50);		
						loginActivo = true;
						$('#contInic').addClass('derActivo');				   
			   }
			escondel = function()
			   {
					$('#caja_login').hide();
					loginActivo = false;
					$('#contInic').removeClass('derActivo');				   
			   }
			$("#inicSess").click(function(event)
				{
					if (!loginActivo)
					  {
						  muestral();
					  }
					else
					  {
						  escondel();
					  }		  				   
					  event.stopPropagation();
					  event.preventDefault();
				});  //#inicSess.click
			$("#login_div").click(function(event)
				{
					event.stopPropagation();
				});
			$(document).keyup(function(event) 
					{	  
					  if (loginActivo) 
						{
						 if (event.keyCode == '27')
							{
		 					 	  escondel();
								  $("#inicSess").html("ENTRAR");
								 event.preventDefault();
							}
						}
					 });//document.keyup
			
			 $(document).click(function() 
				{
					  if (loginActivo) 
						{
							 escondel();
							 $("#inicSess").html("ENTRAR");
						}
				 });//document.click
			procesado = function(data)
			   {
				   var d = (data === "ko");
				   if (d)
				     {
				        location.reload(true);
					 }
					else
					 {
						muestral();
						$("#inicSess").html("ERROR");
						/*$("#correoe").val("").addClass("error");*/
						$("#contra").val("").addClass("error");
						$("#mensaje").html("O e-mail ou senha s&atilde;o incorretos. ");
						$("#mensaje").show();						
					 }
			   }
			procesa = function()
			   {
				    escondel();			   
					var ran_number=Math.floor(Math.random()*50000);	
					var datos = $('#registro_form').serialize();
					$.post('sesion/proceso.php', datos, procesado);
//					$("#hl_div").append('	<div class="cent" id="contInic">Validando Informa&ccedil;&atilde;o</div>');
					$("#inicSess").html("VALIDANDO");
			   }
			   
			$("#login").click(function () 
				{ 
						 if ( $("#registro_form").valid() ) 
							{
								procesa();
							}	
				});	
			$('#contra').keypress(function(e){
				  if(e.which == 13)
				  		{
							 if ( $("#registro_form").valid() ) 
								{
									procesa();
								}				
							  event.stopPropagation();
							  event.preventDefault();														
						}
				  });	
			
			$('#correoe').keypress(function(e){
				  if(e.which == 13)
				  		{
							$("#contra").focus();
						  event.stopPropagation();
						  event.preventDefault();							
						}										
				  });				
	$('#registro_form').validate(
		{
		 rules: 
		   {
			  correoe: 
				{
				   required: true,
				   email: true,
				   minlength:6						   
				},
			   contra:
				{
					required: true,
				    minlength:3,
					maxlength:8
				}
			}, //end rules
		 messages: 
		   {
			  correoe: 
				{
					required: "",
					email: "",	
					minlength: ""
				},
			   contra:
				{
					required:"",
					minlength:"",
					maxlength:""
				}
			},
		errorLabelContainer: "#mensaje",
		showErrors: function(errorMap, errorList) 
		   {

		      $("#mensaje").html("Por favor, escreva seu usu&aacute;rio e senha. N&atilde;o esque&ccedil;a de verificar se escreveu seu e-mail corretamente.");
			  this.defaultShowErrors();
		   }//this.numberOfInvalids()
  }); // end validate	
});//document.ready	 