
$(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_post.php",{ posting:$('#posting').val(), joke_id:$('#joke_id').val(), joke_rate:$('#joke_rate').val(), joke_rating:$('#joke_rating').val(), user_name:$('#username').val(), password:$('#password').val(), rand:Math.random() } ,function(data)
        {
        if(data=='yes') //if correct login detail
		  {
		  	$("#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()
			  {
                 if($('#posting').val()=='1')
                 {
                  document.viewjokes.submit();
                  location.href='post-jokes.php';
                 }else
                 {
                   document.login_form.submit();
                 }
              });

			});
		  }
		  else
		  {          // all field validation
            if($('#username').val()=="" || data=='txt1')
            {
            $("#msgbox").fadeTo(200,0.1,function()
			{
			 $(this).html('Enter User Name').addClass('messageboxerror').fadeTo(900,1);
			});
            }
            else if($('#password').val()=="" || data=='txt2')
            {
              $("#msgbox").fadeTo(200,0.1,function()
			{
			  $(this).html('Enter Password').addClass('messageboxerror').fadeTo(900,1);
			});
            }
            if(data=='no')
            {
              $("#msgbox").fadeTo(200,0.1,function()
			{
			  $(this).html('Sorry login failed...').addClass('messageboxerror').fadeTo(900,1);
			});
            }
          }

        });
 		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from
	$("#password").blur(function()
	{
		$("#login_form").trigger('submit');
	});
});