$(document).ready(function(){
	function slideout(){
		setTimeout(function(){
		$("#error").slideUp("slow", function () {});}, 5000);
	}
	
	$("#error").hide();
	$("#loginsubmit").click(function(){ validlogin();} );
	$('input[type="text"]').each(function(){
		$(this).keypress(function(e){
			if (e.which == 13) { validlogin(); }
		});
	});
	$('input[type="password"]').each(function(){
		$(this).keypress(function(e){
			if (e.which == 13) { validlogin(); }
		});
	});
	function validlogin(){
		$('#loading_img').html('<img src="../images/loading.gif" style="position:relative;top:6px;"/>');
		var name = $("#name").val();
		var pass = $("#pass").val();
		var err = '';
		if(name==''){
			err+='<li>Name is required!.</li>';
		}
		if(pass==''){
			err+='<li>Password is required!.</li>';
		}
		if(err){
			var msg ='<div class="error flash"><ul>'+err+'</ul></div>';
			$("#error").html(msg);
			$("#error").slideDown('slow');
			$('#loading_img').html('');
			//slideout();
		} else {
			//document.loginform.submit();
			var loginval = 'uname='+name+'&auth='+pass; 
			$.post("/user/loginsubmit.php", loginval, function(theResponse){
				if(theResponse == 1){
					var url = '/user/myaccount.php';    
					$(location).attr('href',url);
				}else{
					$("#error").html('<div class="error flash"><ul><li>Sorry, unrecognized username or password. <a href="/user/forgot.php">Have you forgotten your password?</a></li></ul></div>');
					$("#error").slideDown('slow');
					$('#loading_img').html('');
					//slideout();
				}
				
			}); 	
		}
		
	}

});	