// JavaScript Document

$(document).ready(function()
{

$("#loginForm").submit(function()
	{
	
	var logmail = $("#logmail");
	var password = $("#password");
	var filter_email = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/;

	//var pass2 = $("#pass2");
	
	if ( (logmail.val().length <5)&&(password.val().length <5))
		{
		  return false;
		}
	else if(!logmail.val().toUpperCase().match(filter_email))
	  {
	     return false;
	  }	  	
	
	 else
	 
	
		//remove all the class add the messagebox classes and start fading
		$("#login_msgbox").removeClass().addClass('login_messagebox').text('Validating....').fadeIn(1000);
		//check the username exists or not from ajax
		$.post("includes/check_login.php",{ logmail:$('#logmail').val(), password:$('#password').val(),rand:Math.random() } ,function(data)
        {
		  if(data=='yes') //if correct login detail
		  {
		  	$("#login_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.....').addClass('login_messageboxok').fadeTo(900,1,
              function()
			  { 
			  	 //redirect to secure page
				 document.location='';
			  });
			  
			});
		  }
		  else 
		  {
		  	$("#login_msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Invalid Login Details').addClass('login_messageboxerror').fadeTo(900,1);
			});		
          }
				
        });
 		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from 
	$("").blur(function()
	{
		$("#loginForm").trigger('submit');
	});

var logForm = $("#loginForm");
	
	
	var logmail = $("#logmail");
	var logmailInfo = $("#logmailInfo");
	var password = $("#password");
	var passwordInfo = $("#passwordInfo");
	
	
	
	//On blur
	
	
	logmail.blur(validateLogmail);
	password.blur(validatePassword);

	//On key press
	logmail.keyup(validateLogmail);
	password.keyup(validatePassword);

	//On Submitting
	logForm.submit(function(){
		if(validateLogmail() & validatePassword())
			return true
		else
			return false;
	});
	
	//validation functions
	
	function validateLogmail(){
		var filter_email = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/;


		//it's NOT valid
		if(logmail.val().length <5){
			logmail.addClass("error");
			logmailInfo.text("Please Enter Your Email ID");
			logmailInfo.addClass("error");
			return false;
		}
		else if(!logmail.val().toUpperCase().match(filter_email)){
			logmail.addClass("error");
			logmailInfo.text("Please Enter Valid Email ID.");
			logmailInfo.addClass("error");
			return false;
		}
		//it's valid
		else{			
			logmail.removeClass("error");
			logmailInfo.text("Let's validate Your Email ID");
			logmailInfo.removeClass("error");

			return true;
		}
	}
	
	function validatePassword(){
		
		
		//it's NOT valid
		if(password.val().length <5){
			password.addClass("error");
			passwordInfo.text("Please Enter Your Password");
			passwordInfo.addClass("error");
			return false;
		}
		//it's valid
		else{			
			password.removeClass("error");
			passwordInfo.text("Let's Validate it.");
			passwordInfo.removeClass("error");
			//validatelogmail();
			return true;
		}
	}
	
});
