// JavaScript Document

$(document).ready(function() {
	
	$('#login_alert_wrong').hide();
	$('#login_alert_error').hide();
	
	//if submit button is clicked
	$('#login_btn').click(function () {		
		//Get the data from all the fields
		var uname = $('input[name=login_name]');
		var upass = $('input[name=user_pass]');
		var usec = $('select[name=logsec]');

		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if (uname.val()=='') {
			uname.addClass('hightlight');
			return false;
		} else uname.removeClass('hightlight');
		
		if (upass.val()=='') {
			upass.addClass('hightlight');
			return false;
		} else upass.removeClass('hightlight');
		
		//organize the data properly
		var data = 'uname=' + uname.val() + '&upass=' + upass.val() + '&usec=' + usec.val();
		
		$('#login_form').hide();
		
		//show the loading sign
		$('#logDetail .loading').show();
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "login-ajaxform.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//Timeout in miliseconds
			timeout: 20000,
			
			//success
			success: function (html) {				
				//if login-ajaxform.php returned 1/true
				if (html=='0') {					
					$('#logDetail .loading').hide();
					//enabled all the fields	
					$('#login_form').show();
					
					//hide the prev message
					$('#login_alert_error').hide();
					//show the success message
					$('#login_alert_wrong').fadeIn('slow');
					
				//if login-ajaxform.php returned 0/false (send mail failed)
				} 
				else if (html=='1') {					
					//hide the form
					$('#login_form').fadeOut('slow');					
					
					//show the success message
					$('#dialog_log').dialog('close');
					//window.location='./';
					$("#logDetail").load(location.href+" #logDetail>*","");

					
				//if login-ajaxfrom.php returned 0/false (send mail failed)
				}
				else
				{		
					$('#logDetail .loading').hide();
					$('#login_form').show();
					
					$("#login_alert_error_html").html(html);
					
					//hide the prev message
					$('#login_alert_wrong').hide();
					//show the success message
					$('#login_alert_error').fadeIn('slow');
				}
			}		
		});
		//cancel the submit button default behaviours
		return false;
	});	
	
	$('#logout_link').click(function () {	
		//start the ajax
		$.ajax({
			//this is the php file that processes the data
			url: "logout.php",
			//Do not cache the page
			cache: false,
			//success
			success: function (html) {
				$("#toppanel").load(location.href+" #toppanel>*","");
			}
		});
		//cancel the submit button default behaviours
		return false;
	});	
	
	function logHandler()
	{
		$('.login_alert_wrong').hide();
		$('.login_alert_error').hide();
		$('#dialog_log').dialog('open');
	}
			
});	
