$(document).ready(function(){
	/* handling form validation */
	$("#login-form").validate({
		rules: {
			password: {
				required: true,
			},
			username: {
				required: true,
				
			},
		},
		messages: {
			password:{
			  required: "Please enter your password"
			 },
			username: "Please enter your username",
		},
		submitHandler: submitForm	
	});	

$("#staff-login-form").validate({
		rules: {
			password: {
				required: true,
			},
			username: {
				required: true,
				
			},
		},
		messages: {
			password:{
			  required: "Please enter your Staff password"
			 },
			username: "Please enter your Staff username",
		},
		submitHandler: submitFormStaff	
	});	

	/* Handling login functionality */
	function submitForm() {		
		var data = $("#login-form").serialize();
		$.ajax({				
			type : 'POST',
			url  : 'phpClasses/Admin.php?action=login',
			data : data,
			beforeSend: function(){	
				$("#error").fadeOut();
				$("#login_button").html('<span class="glyphicon glyphicon-transfer"></span> &nbsp; sending ...');
			},
			success : function(response){		
				
				if($.trim(response) === "1"){
                                   // alert('login sucess');
					//console.log('dddd');									
					$("#submit-id-submit").html('Signing In ...');
					setTimeout(' window.location.href = "dashboard.php"',2000);
				} else if($.trim(response) === "Reg"){
					$("#submit-id-submit").html('Redirecting to Register Page...');
					setTimeout(' window.location.href = "buiapp/phpClasses/registerstaff.php"',2000);
				}else {									
					$("#error").fadeIn(1000, function(){						
						$("#error").html(response).show();
					});
				}
			}
		});
		return false;
	}

        /* Handling staff login functionality */
	function submitFormStaff() {		
		var data = $("#staff-login-form").serialize();
		$.ajax({				
			type : 'POST',
			url  : 'buiapp/phpClasses/Staff.php?action=login',
			data : data,
			beforeSend: function(){	
				$("#error").fadeOut();
				$("#login_button").html('<span class="glyphicon glyphicon-transfer"></span> &nbsp; sending ...');
			},
			success : function(response){			
				if($.trim(response) === "1"){
                                   // alert('login sucess');
					//console.log('dddd');									
					$("#submit-id-submit").html('Signing In ...');
					setTimeout(' window.location.href = "dashboard.php"',2000);
				}else if($.trim(response) === "Reg"){
					$("#submit-id-submit").html('Redirecting to Register Page...');
					setTimeout(' window.location.href = "buiapp/phpClasses/registerstaff.php"',2000);
				} else {									
					$("#error").fadeIn(1000, function(){						
						$("#error").html(response).show();
					});
				}
			}
		});
		return false;
	}

	/* Handling login functionality */
	function logout() {
		console.log('fdfdf');
		$.ajax({				
			type : 'POST',
			url  : 'phpClasses/Admin.php?action=logout',
			data : data,
			success : function(response){
				window.location.href = "/index.php";
			}
		});
		return false;
	}   
});