// AU3commonfunctions.js
// (c) 2007 ISoftData Systems, Inc.
// Brian Roy
// Last modified: 6/14/07

// This library contains a set of common action Javascript functions
//  for the AU3 project.  This library provides functionality for the 
//	AU3Ajax object to do common functions, like autocomplete, or generic
//	div data.
//////////////////////////////////////////////////////
// Common action functions
/////////////////////////////////////////////////////

// AU3_ajaxLoadIntoObject( dataSource, request, objid[, username, password] )
// Input: a data source url, a request to pass to that url, the id to an 
//			object to write the data response into, and optionally a username 
//			password
// Output: none
// Return: none
// This function writes data from some data source, given a request, into
//	the innerHTML of an object specified by objid.  It assumes the use of
//	POST.
function AU3_ajaxLoadIntoObject( dataSource, request, objid, 
		username, password )
{
	// Validate that we have an XMLHttpRequestObject.  If we don't, make one.
	if (!this.XMLHttpRO)
	{
		this.XMLHttpRO = this.create();
	}

	// get the object by its id
	var obj = document.getElementById( objid );

	// open our XMLHttpRequestObject with the specified params
	this.open( dataSource, "POST", username, password );

	// configure the callback function to handle the server response
	this.onDataReceived = function ( responseText )
	{
		obj.innerHTML = responseText;
	}

	// send the request to the server via our XMLHttpRequestObject
	this.send( request );
}

// AU3_ajaxAutocomplete( table, column, textboxid[, username[, 
//						password]] )
// Input: table and column specify the table and column in the database which
//				this should match against for autocomplete.
//			the id of a textbox to run autocomplete against,
//			and optional values for a username and password to connect with
// Return: none
// This function provides an autocomplete ability for a text box.  It is meant
//	do be called from the onKeyPress of the textbox, and matches for a specific
//	category, in the form of a SQLQuery object which provides the array to
//	autocomplete against.
function AU3_ajaxAutocomplete( table, column, textboxid, username, 
		password )
{

	// Get the textbox and autocomplete div objects
	var textbox = document.getElementById( textboxid );
	var obj = document.getElementById("autocompletediv");
	var text = textbox.value;
	var serverString = null;

	// set up the SQL variables
	var passTable = encodeURIComponent(table);
	var passColumn = encodeURIComponent(column);
	var passTextboxid = encodeURIComponent(textboxid);

	// set up some variables for positioning
	var posx = 0;
	var posy = 0;

	// make the autocomplete div visible
	obj.style.visibility = "visible";
	obj.style.width = "152px";

	// configure the position
	posx = (findPosX( textbox.id ) + 1);
	posy = (findPosY( textbox.id ) + 23);
	obj.style.left = posx + "px";
	obj.style.top = posy + "px";

	// build the server string with the text to match with
	serverString = "match=" + encodeURIComponent( text );

	// Add the table and column to search against to the server string
	serverString += "&table=" + passTable + "&column=" + passColumn;
	serverString += "&textboxid=" + passTextboxid;

	// we can use loadInto to load the autocomplete data into the
	//	autocomplete div
	this.loadInto( "./components/autocomplete.php", serverString, obj.id, 
			username, password );
}

// AU3_ajaxCombobox( table, columnval, columnlabel, where, comboboxid
//						[, username[, password]] )
// Input: table is the table in the database to use to stock the combobox
//		 	columnval is the column to use from the table to stock the
//				value entry of the combobox
//			columnlabel is the column to use from the table to stock the 
//				labels (visible text) of the combobox
//			where is a set of comma separated groups, each group consists
//				of a simple query -- column comparator value
//				like city=Lincoln
//			the id of a combobox to fill with values,
//			and optional values for a username and password to connect with
// Return: none
// This function fills a dropdown combobox with values retrieved from the
//	database.
function AU3_ajaxCombobox( table, columnval, columnlabel, where, comboboxid,
		username, password )
{
	// setup variables
	var serverString = null;
	var combobox = document.getElementById( comboboxid );
	var labelArray = new Array();
	var valueArray = new Array();

	// set up the SQL variables
	var passTable = encodeURIComponent(table);
	var passColumnVal = encodeURIComponent(columnval);
	var passColumnLabel = encodeURIComponent(columnlabel);
	var passWhere = encodeURIComponent(where);

	// Setup the string to send to the server
	serverString = "table=" + passTable + "&columnval=" + passColumnVal
		+ "&columnlabel=" + passColumnLabel + "&where=" + passWhere;

	// Validate that we have an XMLHttpRequestObject.  If we don't, make one.
	if (!this.XMLHttpRO)
	{
		this.XMLHttpRO = this.create();
	}

	// open our XMLHttpRequestObject with the specified params
	this.open( "./components/combobox.php", "POST", username, password );

	// configure the callback function to handle the server response
	this.onDataReceived = function ( responseText )
	{
		// parse the response text to retrieve label/value pairs
		eval( responseText );

		// replace the current contents of the combobox with the 
		//	label/value pairs from the server
		combobox.length = 0;

		for (var i = 0; i < responseArray.length; ++i )
		{
			combobox.options[i] = new Option( responseArray[i][0], 
					responseArray[i][1]  );
		}
	}

	// send the request to the server via our XMLHttpRequestObject
	this.send( serverString );
}

// AU3_ajaxExecuteRequest( url, request[, username, password] )
// Input: a url, a request to pass to that url, 
//			and optionally a username and password
// Output: none
// Return: none
// This function executes a request against a URL.  No processing is
//	done on data returned from that URL, so this function is only useful
//	for AJAX operations which only affect database values, and the visible
//	effects are handled separately.  The request is presented to the URL
//	as a POST.
function AU3_ajaxExecuteRequest( url, request, username, password )
{
	// Validate that we have an XMLHttpRequestObject.  If we don't, make one.
	if (!this.XMLHttpRO)
	{
		this.XMLHttpRO = this.create();
	}

	// open our XMLHttpRequestObject with the specified params
	this.open( url, "POST", username, password );

	// send the request to the server via our XMLHttpRequestObject
	this.send( request );
}

// AU3_ajaxEvaluateRequest( url, request[, username, password] )
// Input: a url, a request to pass to that url,  
//			and optionally a username and password
// Output: none
// Return: none
// This function passes a request to a url and then evaluates the 
//	response as though it is Javascript code.
function AU3_ajaxEvaluateRequest( url, request, username, password )
{
	// Validate that we have an XMLHttpRequestObject.  If we don't, make one.
	if (!this.XMLHttpRO)
	{
		this.XMLHttpRO = this.create();
	}

	// open our XMLHttpRequestObject with the specified params
	this.open( url, "POST", username, password );

	// configure the callback function to handle the server response
	this.onDataReceived = function ( responseText )
	{
		eval(responseText);
	}

	// send the request to the server via our XMLHttpRequestObject
	this.send( request );
}

/////////////////////////////////////////
// Link these functions to the AU3Ajax object
/////////////////////////////////////////
// static methods for the AU3Ajax object which are based on common functions
AU3Ajax.prototype.loadInto = AU3_ajaxLoadIntoObject;
AU3Ajax.prototype.autocomplete = AU3_ajaxAutocomplete;
AU3Ajax.prototype.fillCombobox = AU3_ajaxCombobox;
AU3Ajax.prototype.execute = AU3_ajaxExecuteRequest;
AU3Ajax.prototype.evaluate = AU3_ajaxEvaluateRequest;
