$(document).ready(function(){						   
   $('#loginForm').ajaxForm({
   beforeSubmit: validate,
   success: hecho
   });    
});

function onlyNumbers(obj){
	reg = /[^0-9]/g;
	obj.value =  obj.value.replace(reg,"");
 }
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function checkEmail(inputvalue){	
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if(pattern.test(inputvalue)){         
		return true;   
    }else{   
		return false; 
    }
}
function validate(formData, jqForm, options) {		
	var form = jqForm[0];
	
	if (!trim(form.usuario.value)){
		$("#mensajeLogin").html("");
		$("#mensajeLogin").append("<strong>E-mail</strong> es un campo requerido.");
		$("#mensajeLogin").slideDown();			
		form.usuario.className += " highlight";
		form.usuario.focus();
		return false;
	}	
	if (!checkEmail(trim(form.usuario.value))){
		$("#mensajeLogin").html("");
		$("#mensajeLogin").append("<strong>E-mail</strong> debe tener un formato correcto.");
		$("#mensajeLogin").slideDown();	
		form.usuario.className += " highlight";
		form.usuario.focus();
		return false;
	}
	if (!trim(form.contrasena.value)){
		$("#mensajeLogin").html("");
		$("#mensajeLogin").append("<strong>Clave</strong> es un campo requerido.");
		$("#mensajeLogin").slideDown();			
		form.contrasena.className += " highlight";
		form.contrasena.focus();
		return false;
	}
}

function hecho(responseText, statusText){			
	if (responseText.indexOf("user_logged-in") > -1){
		document.location.href="index.php";
		/*clearForm(document.getElementById("loginForm"));
		document.getElementById("loginFormDiv").style.display = "none";
		document.getElementById("mensaje").style.display = "none";
		document.getElementById("mensajeNoActivo").style.display = "none";
		document.getElementById("loginInfoDiv").style.display = "block";*/			
	} else if (responseText.indexOf("user_no_activo") > -1){ 	
		document.getElementById("mensajeLogin").style.display = "none";
		document.getElementById("mensajeNoActivo").style.display = "block";
	} else {
		document.getElementById("mensajeNoActivo").style.display = "none";
		$("#mensajeLogin").html("");
		$("#mensajeLogin").append(responseText);
		$("#mensajeLogin").slideDown();
	}
}

function clearForm(form){	
	form.usuario.value = "";	
	form.contrasena.value = "";	
}
