// Preload Images
img1 = new Image(16, 16);  
img1.src = "./images/spinner2.gif";

img2 = new Image(16, 16);  
img2.src = "./images/wait2.gif";

// When DOM is ready
$(document).ready(function(){

	// Hide status to start with
	$('#panel_status').hide();

	// When the form is submitted
	$("#login > form").submit(function(){  
		var load_message = "<span class='notice'>Processing&hellip;</span><img src='./images/wait2.gif'>";
		var done_message = "<span class='success'>Now logged in</span><img src='./images/spinner2.gif'>";
		var bye_message = "<span class='success'>Now logged out</span><img src='./images/spinner2.gif'>";

		$('#submit').hide();		// Hide 'Submit' Button

		// Show user that we're busy doing something
		$('#panel_status').html(load_message);
		$('#panel_status').show();

		// 'this' refers to the current submitted form  
		var str = $(this).serialize();  

		// -- Start AJAX Call --
		$.ajax({  
			type: "POST",
			url: "/login/do-login.php",	// Send the login info to this page
			data: str,  
			success: function(msg) {  
				$("#dologin").ajaxComplete(function(event, request, settings) {  

					if (msg == 'OK') {
						// LOGIN OK - tell user
						$('#panel_status').html(done_message);
						go_to_private_page();
					}  
					else if (msg == 'Logout') {
						// LOGOUT OK - tell user
						$('#panel_status').html(bye_message);
						go_to_home_page();
					}  
					else {
						// ERROR - tell user :-
						var login_response = msg;
						$('#panel_status').html(login_response);

						// Show 'Submit' Button to let them try again
						$('#submit').show();

					}  
				});
			}  
		});	// -- End AJAX Call --

		return false;
	});						// End submit event

});		// End $(document).ready

function go_to_private_page()
{
	window.location = "/admin/animal_select.php";
}

function go_to_home_page()
{
	window.location = "/index.php";
}