// js handling the login procedures

// constants
var NORMAL_STATE = 4;
var LOGIN_PREFIX = 'http://www.blogsoop.com/login.php?';

// variables
var http = getHTTPObject(); // We create the HTTP Object
var hasSeed = false;
var loggedIn = false;
var seed_id = 0;
var seed = 0;
var fullname = '';
var messages = '';


// validateLogin method: validates a login request
function validateLogin()
{
	// get form form elements 'username' and 'password'
	username = document.getElementById('username').value;
	password = document.getElementById('password').value;

	// ignore if either is empty
	if (username != '' && password  != '') {
		
		// open the http connection
		http.open('GET', LOGIN_PREFIX + 'task=checklogin&username='+username+'&password='+password, true);
		
		// where to go
		http.onreadystatechange = handleHttpValidateLogin;
		http.send(null);
	}
}

// handleHttpValidateLogin method: called when the validation results are returned from the server
function handleHttpValidateLogin()
{
	// did the connection work?
	if (http.readyState == NORMAL_STATE) {
		// split by the pipe
		results = http.responseText.split('|');
		if (results[0] == 'true')
		{
			loggedIn = true;
			fullname = results[1];
			messages = '';
		}
		else
		{
			messages = results[1];
		}
		showLogin();
	}
}

// resetLogin method: if logged in, 'logs out' and allows a different user/pass to be entered
function resetLogin()
{
	loggedIn = false;
}