/******************************************************************************
login.js
	Copyright (C) 2010 Atlantic Database Systems, Inc. All rights reserved.
	Define global items.
******************************************************************************/

/******************************************************************************
Wait for jQuery to get ready.
******************************************************************************/

$(document).ready (function ()
{
	// Our use of AJAX returns JSON with a one minute time out.

	$.ajaxSetup ({"dataType": "json", 
					"contentType": "application/json; charset=utf-8", 
					"timeout": 60000});

	onLoginLoad ();
});

/******************************************************************************
DoLoad - Display error message if invalid login
******************************************************************************/

function onLoginLoad ()
{
	var	arg = location.search.substr (1);

	if (arg != "") {
		var	loginerr = document.getElementById ('loginerr');
		loginerr.style.display = "block";
	}

	requestData = {"reqType": "siteConfig"};
	doServerRequest (requestData, havePreloginResp);
}

/******************************************************************************
haveLoginResp (bOK, response) - We have the response
******************************************************************************/

function havePreloginResp (bOK, response)
{
	if (bOK) {
		var	siteTitle = response ["siteTitle"];
		$("#welcomeTitle").text ("Welcome to " + siteTitle);
	} else
		alert ("Unable to access database - " + response.errmsg);

	var	el = document.getElementById ('loginName');
	if (el)
		el.focus ();
}

/******************************************************************************
OnLoginClick - Check that all data have been entered.
	Disable log in button and show message.
	Have server validate credentials.
******************************************************************************/

function OnLoginClick ()
{
	// Get the data entered.

	var	loginName;
	var	password;

	if (document.getElementById ('loginName') && document.getElementById ('password')) {
		loginName = $.trim (document.getElementById ('loginName').value);
		password = $.trim (document.getElementById ('password').value);
	}

	if (!loginName || !password) {
		alert ("Please enter your loginName and password.");
		document.getElementById ('loginName').focus ();
		return false;
	}
		
//	Disable log in button and show message.

	$("#loginsubmit").hide ();
	$("#loginmsg").show ();

//	Have server validate credentials.

	requestData = {"reqType": "login", "loginName": loginName, 
					"password": password};

	doServerRequest (requestData, haveLoginResp);

	return false;
}

/******************************************************************************
haveLoginResp (bOK, response) - We have the response
******************************************************************************/

function haveLoginResp (bOK, response)
{
	if (bOK) {

		var	operatorID = response ["operatorID"];

		if (operatorID)
			window.location = "mainpage.htm";
		else
			$("#loginerr").show ();

	} else {

		if (response.errmsg)
			alert ("Unable to log in - " + response.errmsg);
		else
			document.getElementById ('loginerr').style.display = "block";
	}

	$("#loginsubmit").show ();
	$("#loginmsg").hide ();
}


