//
// Name					: getEbId()
// Description	: Shorthand for document.getElementById())
// Author				: Lawrance Shepstone (lawrance@digitalvoice.co.za)
// Created			: 06 February 2009
// Modified			: 06 February 2009
// Arguments		: 0 = STRING elementId: id of the element to reference
// Dependencies	: None
//

function getEbId()
{
	var a = arguments;

	return document.getElementById( a[0] );
}

//
// Name					: getAjaxObject()
// Description	: Create a new Ajax object (cross browser)
// Author				: Lawrance Shepstone (lawrance@digitalvoice.co.za)
// Created			: 09 February 2009
// Modified			: 09 February 2009
// Arguments		: None
// Dependencies	: None
//

function getAjaxObject()
{
	var ajaxObject = null;
	
	if ( window.XMLHttpRequest )
	{
		ajaxObject = new XMLHttpRequest();
	}
	else
	{
		if ( window.ActiveXObject )
		{
			ajaxObject = new ActiveXObject("Microsoft.xmlHttp");
		}
	}
	
	return ajaxObject;
}


//
// Name					: updateTableRecordField()
// Description	: Updates the given field of the specified record
//                in the specified table via AJAX
// Author				: Lawrance Shepstone (lawrance@digitalvoice.co.za)
// Created			: 06 February 2009
// Modified			: 09 February 2009
// Arguments		: 0 = STRING tableName: name of the table to update
//              : 1 = INTEGER recordId: id of the record to update
//              : 2 = STRING fieldName: name of the field to update
//							: 3 = ANY cellValue: value for the cell to update
//              : 4 = STRING dataType: data type required by the database field (integer, boolean or string)
//              : 5 = INTEGER dataLength: length of value if dataType=string
// Dependencies	: getEbId(), getAjaxObject()
//

function updateTableRecordField()
{
	var tableName = arguments[0];
	
	var recordId = arguments[1];
	
	var fieldName = arguments[2];
	
	var cellValue = arguments[3];
	
	var dataType = arguments[4];
	
	var dataLength = arguments[5];

	switch( dataType )
	{
		case 'integer':

			if( isNaN( cellValue ) )
			{
				alert("Sorry, '" + cellValue + "' is not a number");

				return false;
			}

			break;
			
		case 'boolean':

			cellValue = cellValue.toString();

			cellValue = cellValue.toLowerCase();

			if( cellValue == 'on' || cellValue == 'yes' || cellValue == 'true' )
			{
				cellValue = 1;
			}
			else
			{
				if( cellValue == 'off' || cellValue == 'no' || cellValue == 'false' )
				{
					cellValue = 0;
				}
			}

			break;
		
		case 'string':
		
			if( cellValue.length > dataLength )
			{
				alert('Value length is too long');
			}
		
			break;
		
		case 'null':
		
			cellValue = 'NULL';
		
			break;
	}

	var ajaxObject = getAjaxObject();

	if ( ajaxObject != null )
	{		
		var postData = 'tableName=' + tableName + '&recordId=' + recordId + '&fieldName=' + fieldName + '&cellValue=' + escape( cellValue ) + '&dataType=' + dataType + '&dataLength=' + dataLength;

		ajaxObject.open('POST', '/extranet/_shared/actions/save.php', true);

		ajaxObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		
		ajaxObject.setRequestHeader("Content-length", postData.length);
		
		ajaxObject.setRequestHeader("Connection", "close");
		
		ajaxObject.onreadystatechange = function _handler()
		{
			if( ajaxObject.readyState == 4 )
			{
				if( ajaxObject.status == 200 )
				{
				}
			}
		};

		ajaxObject.send( postData );
	}
	
	return false;
}

//
// Name					: openWindow()
// Description	: Opens a pop-up windows with no toolbars or *statusbar*
// Author				: Lawrance Shepstone (lawrance@digitalvoice.co.za)
// Created			: 09 March 2009
// Modified			: 09 March 2009
// Arguments		: 0 = STRING url: url of page to load in the window
//							: 1 = INTEGER width: width of the window
//							: 2 = INTEGER height: height of the window
//							: 3 = STRING: name: name of the window
// Returns			: Reference to window object
// Dependencies	: None
//

function openWindow()
{
	var w = window.open( arguments[0], arguments[3], 'status=0,toolbar=0,location=0,width=' + arguments[1] + ',height=' + arguments[2] + ',screenX=' + (window.screen.width - arguments[1]) / 2 + ',screenY=' + (window.screen.height - arguments[2]) / 2 + ',scrollbars=1');
	
	w.focus();
	
	return w;
}