$(document).ready(function()
{
	$("#login_form").submit(function() {
	    //remove all the class add the messagebox classes and start fading
	    $("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
	    //check the username exists or not from ajax
	    $.post("ajax_login.php",{user:$('#user').val(),password:$('#password').val()} ,function(data){
			if(data.substring(0,3)=='yes') {
                $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
                {
                  //add message and change the class of the box and start fading
                  $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
                  function()
                  {
                     //redirect to secure page
                    document.location="display.php?login=yes&client=" + $('#client').val();
                  });
                });
			} else {
                $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
                {
						//add message and change the class of the box and start fading
						if (data.substring(0,2)=='no') {
								$(this).html('Invalid User or Password...').addClass('messageboxerror').fadeTo(900,1);
						} else if (data.substring(0,8)=='inactive') {
								$(this).html('User Not Active...').addClass('messageboxerror').fadeTo(900,1);
						} else if (data.substring(0,7)=='invalid') {
								$(this).html('Please Sign up for Web Recipe Manager for FREE...').addClass('messageboxerror').fadeTo(900,1);
						} else if (data.substring(0,4)=='nodb') {
								$(this).html("Sorry we're having technical issues, please try again soon...").addClass('messageboxerror').fadeTo(900,1);
						} else {
								$(this).html(data).addClass('messageboxerror').fadeTo(900,1);
						}
                });
	        }
	    });
	    return false;//not to post the  form physically
	});
	$("#user").attr({ value: 'User...' }).focus(function(){
        if($(this).val()=="User..."){
            $(this).val("");
        }
	}).blur(function(){
        if($(this).val()==""){
            $(this).val("User...");
		}
	});
	$('#password-clear').show();
	$('#password').hide();
	 
	$('#password-clear').focus(function() {
	    $('#password-clear').hide();
	    $('#password').show();
	    $('#password').focus();
	});
	$('#password').blur(function() {
	    if($('#password').val() == '') {
	        $('#password-clear').show();
	        $('#password').hide();
	    }
	});
	$('#password').keypress(function(e) {
        if(e.which == 13) {
           $('#login_form').submit();
        }
    });

});
