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

/******************************************************************************
doServerRequest - Send an action request to the server.
	requestData - Object containg the request data
	respFunction - Function to process the response.
	localData - Auxilary information to be returned to the response function.
******************************************************************************/

function doServerRequest (requestData, respFunction, localData)
{
	if (!localData)
		localData = {};

	reqText = $.JSON.encode (requestData);

	setWaitCursor (true);

	ajaxReq = {type:			"POST",
				url:			"serverreq.py",
				data:			reqText,
				datatype:		"json",
				contentType:	"application/json; charset=utf-8", 
				success:		procServerResp,
				error:			procRespError,
				userSuccess:	respFunction,
				requestData:	requestData,
				localData:		localData};
	$.ajax (ajaxReq);
}

function procRespError (jqXHR, textStatus, errorThrown)
{
	alert (textStatus);
	if (errorThrown)
		alert (errorThrown);
	setWaitCursor (false);
}

/******************************************************************************
procServerResp - Process an AJAX response.
					Check for time out
					Invoke user function.
******************************************************************************/

function procServerResp (response)
{
	setWaitCursor (false);

	// Get the result code.

	var	bOK = false;

	bOK = (response ["res"] == "ack");

	if (!bOK) {

		if (response ["res"] == "redirect") {

			var	newLocation = response ["location"];
			if (!newLocation)
				newLocation = "/index.html";
			window.location = newLocation;
			return false;

		} else {
			var	reqType = this.requestData ["reqType"];

			if ("errmsg" in response)
				response ["errmsg"] = "(" + reqType + ") " + response ["errmsg"];
			else
				response ["errmsg"] = "(" + reqType + ") ";
		}
	}

	if ("resultTag" in response) {

		// Get the result tag(s).

		var	resultTags = makeArray ("resultTag", response);

		// Return query results as, possibly empty, array(s).

		for (var i = 0; i < resultTags.length; i++) {

			var	resultTag = resultTags [i];

			if (resultTag)
				response [resultTag] = makeArray (resultTag, response);
		}

	}

	// Invoke the user function.

	if (this.userSuccess) {

		// Invoke the response handler.

		response.localData = this.localData;

		var	userClass = this.localData ["self"];

		if (userClass)
			this.userSuccess.call (userClass, bOK, response);
		else
			this.userSuccess (bOK, response);
	}
}

/******************************************************************************
logObject - Helper to display an object contents in the console.log
******************************************************************************/

function logObject (respObj, level)
{
	if (!level)
		level = "";

	for (var item in respObj) {
		console.log (level + item + " = '" + respObj [item] + "'");
		if (typeof (respObj [item]) == "object") {
			logObject (respObj [item], level + "  ");
		}
	}
}

/******************************************************************************
setWaitCursor - Show or hide the "wait" cursor.
******************************************************************************/

function setWaitCursor (bOn)
{
	document.body.style.cursor = bOn ? "wait" : "auto";
}


