/* Robert Paul Alan Gleeson
 rob@push-labs.org 
 */
 
 
 
 /* This function retrieves the user input, and 
   then post's it to a PHP script for parsing. */
function postlogin ()
{
$.blockUI.defaults.css = {
	padding:        '0', 
        margin:         '0', 
        width:          '20%',  
        top:            '40%',  
        left:           '40%',  
        textAlign:      'center',  
        color:          '#415481;',  
        border:         '3px solid #aaa', 
        backgroundColor:'#fff', 
        cursor:         'wait'  }

// Call the popup box that  displays "Loading.. Please wait.."
$.blockUI ( { message : '<div id="img" style="float : left; margin-right : 5px; padding-left : 25px;"><img src="images/busy.gif"></div><div class="txt" style="float : left; line-height : 35px; font-family : verdana,sans-serif; font-size : 0.75em; color : #415481; ">Loading.. Please wait...</div>' } );


// Read the input.
var username = $(".text").val(); // store username.
var password = $(".password").val(); // store password.
var language = $("#language").val(); // store language

// Post our data to the PHP script.
$.post ( 'inc/checklogin.php' , { username: username,
						     password: password,
						     language: language }, logincallback);

return false;
}

/* This function retrieves the data sent back from
    the called script and takes the apprioate action */

function logincallback ()
{
var response = arguments[0];

	if ( response == 'OK' )
	{ // True if the login was successful.
	
	// Set #usernameerror and #passworderror to blank.
	// This removes 'The following errors were encountered'.
	$("#usernameerror").html("");
	$("#passworderror").html("");	
	
	// Retrieve the hostname of our server.
	var hostname = location.hostname;
	
	// Redirect to index.php
	// window.location = 'http://' + hostname + '/index.php';
	window.location = 'index.php';
	}
	else
	{ // This block of code runs if there was an error during the login process.
	
	// Clear these div's.
	$("#usernameerror").html("");
	$("#passworderror").html("");
	
	// Print this.
	$("#process").html('The following errors were encountered : <br>');
	
	// The error response is fed back like this : username-error|password-error, split this string at | to get each error seperately.
	var splitresponse = response.split("|");
		
		if (splitresponse[0])
		{ // True if splitresponse[0] is defined, in other words, true if there was a username error.
		$("#usernameerror").html('* ' + splitresponse[0]);
		}
		
		if (splitresponse[1])
		{ // True if splitresponse[1] is defined, in other words, true if there was a password error.
		$("#passworderror").html('* ' + splitresponse[1]);
		}

	}

// Kill the popup box.
$.unblockUI();
}

/* This anonymous function is is loaded when the document is ready */
$(document).ready ( function ()
{
// 	$(".submit").bind('click', postlogin); // When .submit is clicked, call 'postlogin'.
	$("#loginform").bind('submit', postlogin);
});

