// JavaScript Document

$(document).ready(function(){

function validateEmail(elementValue){
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(elementValue);
 }
		
	//alert(path);
	$('#lgregister').click(function(e) {
		e.preventDefault();
		window.location.href = path+'register';
	});
	
	$('.lgforgot').click(function(e) {
		e.preventDefault();
		$('.lgforgot2').show();
	});
	
	$('.lgforgot4').click(function(e) {
		e.preventDefault();
		$('.lgforgot2').hide();
	});
	
	$('.lgforgot3').click(function(e) {
		e.preventDefault();
		
		if( validateEmail( $('#ForgotUsername').val() ) ) {
		$(this).attr("disabled", true);
		
		$.ajax({
			url: path+'users/enter',
			data: $('.loginForm').serialize() + '&action=recovery',
			type: 'POST',
			cache: false,
			dataType: 'html',
			success: function (data, textStatus) {
				//$('#testing').html(data);

				if(data == "none" && textStatus == "success") {
					$.growlUI('Denegado', "<div class=\"flash_failure\">No existe el correo en nuestro sistema.</div>", 3000, '', 0); 
					$('#ForgotUsername').val('');
					$('#ForgotUsername').focus();
					$('.lgforgot3').attr("disabled", false);
				} else {
					$.growlUI('Notificación', '<div class=\"flash_success\">Se han enviado los pasos a seguir al correo electrónico solicitado.</div>', 3000, '', 1); 
					$('.lgforgot2').hide();
					$('#ForgotUsername').val('');
					$('.lgforgot3').attr("disabled", false);
				}
			}
			
		});
		//if not email forgotten
		} else {
			$('#ForgotUsername').val('');
			$.growlUI('Denegado', "<div class=\"flash_failure\">Escribe un correo valido</div>", 3000, '', 0); 
		}
	});
	
	
	$('.loginForm').submit(function (e) {
		e.preventDefault();
		$('.lenter').attr("disabled", true);
		if($('#email').val() != '' && $('#password').val() != '') {
			//console.log($('.loginForm').serialize());
			$.ajax({
				url: path+'users/enter',
				data: $('.loginForm').serialize() + '&action=send',
				type: 'POST',
				cache: false,
				dataType: 'html',
				success: function (data) {
					//$('#testing').html(data);
					if(data == "logged") {
						$.growlUI('Notificación', '<div class=\"flash_success\">Bienvenido al Sistema Spider Tech, redireccionando...</div>', 3000, '', 1); 
						$('.login_info').html('<strong>Bienvenido al Sistema Spider Tech, redireccionando...</strong>');
						setTimeout("window.location.reload();", 3000);
	
					}
					if(data == "error") {
						$.growlUI('Denegado', "<div class=\"flash_failure\">Error al validar la cuenta, verifica tu usuario/password</div>", 3000, '', 0); 
						$('.login_info').html('<strong>Error al validar la cuenta, verifica tu usuario/password</strong>');
						$('#name').val('');
						$('#password').val('');
						
						$('.lenter').attr("disabled", false);
						$('#RevealUsername').focus();
					}
				}
				
			});
			
		} else {
			$('.lenter').attr("disabled", false);
			$.growlUI('Aviso', "<div class=\"flash_failure\">Ingresa tu usuario/correo y tu password</div>", 3000, '', 0);
		}
		
	});
	
	
});
