var funcoes = function() 
{
    return {
		
		/*
		 * incializa todas as funções
		 */		
        incializar: function() 
		{
			funcoes.IN();
       	},   
			
        IN: function() 
		{
			jQuery(".bLogar").click(function () 
			{
			
				if(document.location.hostname == '192.168.0.13') 
				{ 		
					var dir = document.location.pathname.split('/');
					var host_path = 'http://'+document.location.hostname+'/'+dir[1]+'/';
				} else
					var host_path = 'http://'+document.location.hostname+'/';
	
				var x = 0;
				var cmail = jQuery('input[name="lemail"]');
				var caces = jQuery('input[name="lsenha"]');
				jQuery('#lemail').remove(); jQuery('#lsenha').remove();
				
				//Validação para o campo e-mail;
				var erEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;						
				if (!erEmail.test(cmail.val())) 
				{
					x = 1;
					cmail.focus();
					if(jQuery('#'+cmail.attr('name')).attr('id') == null)
						jQuery('<span id="'+cmail.attr('name')+'" class="msgErro">Por favor confira seu e-mail.</span>').insertAfter(cmail);
				} 		
				
				//Verifica o campo do password estar vazio;
				if (!caces.val()) 
				{
					x = 1;
					caces.focus();
					if(jQuery('#'+caces.attr('name')).attr('id') == null)
						jQuery('<span id="'+caces.attr('name')+'" class="msgErro">'+caces.attr('title')+'</span>').insertAfter(caces);
				} 
				
				//Remove os titles de aviso dos campos;
				inputs = new Array(caces, cmail);
				for (var i = 0 ; i < inputs.length; i++) {
					inputs[i].click(function (e) { if(jQuery('#'+jQuery(this).attr('name'))) jQuery('#'+jQuery(this).attr('name')).remove(); });  
					inputs[i].keypress(function (e) { if(jQuery('#'+jQuery(this).attr('name'))) jQuery('#'+jQuery(this).attr('name')).remove(); });  
				}

				if(x == 0)
				{		
					if(jQuery('#lodar').attr('id') == null)
						jQuery('<span class="loginload"><img src="_includes/images/loader.gif" border="0" id="lodar"></span>').insertAfter('.lemail');					
					

						jQuery.ajax({
						type: "POST",
						url: host_path+"auxiliar.php",
						data: "lemail="+jQuery('input[name="lemail"]').val()+"&lsenha="+jQuery('input[name="lsenha"]').val(),
						success: function(retorno) 
						{
							jQuery('#lodar').remove();
							separa = retorno.split("|");
							switch(separa[0])
							{
								case '1':
									cmail.focus();
									if(jQuery('#'+cmail.attr('name')).attr('id') == null)
										jQuery('<span id="'+cmail.attr('name')+'" class="msgErro">Usuário não existe.</span>').insertAfter(cmail);
								break;								
								case '2':
									caces.focus();
									if(jQuery('#'+caces.attr('name')).attr('id') == null)
										jQuery('<span id="'+caces.attr('name')+'" class="msgErro">Por favor confira sua senha</span>').insertAfter(caces);
								break;								
								case '99':
									window.location.href = window.location.href;
								break;
							}
						}
					})
				}
				
				return false;	
			});	
        }	
    }
}();

$(document).ready(funcoes.incializar);
