var g_ResultDivId = null;					// the div in which the result will be displayed
var g_CompletionFunction = null;	// the function to execute on completion of php script

/**
*	Executes specified php script and returns results in specified div
*
*	@param String scriptURL The URL of the php script
*	@param String[] parameterList The list of parameters to pass to the script
*	@param String paramMethod The method used
*	@param String resultDiv The div in which to display the result
*/
function executePHP(scriptURL, parameterList, paramMethod, resultDivId, completeFunction)
{
	g_ResultDivId = resultDivId;
	g_CompletionFunction = completeFunction;
	params = mergeParameters(parameterList);
	var ajax = new Ajax.Request(
		scriptURL, 
		{
			method: paramMethod, 
			parameters: params,
			onComplete: runOnComplete
		}
	);
}

/**
*	@private
*
*	Displays the result of executePHP in the specified div.
*	Executes specified global function after displaying
*/
function runOnComplete(originalRequest)
{
	if (g_ResultDivId)
	{
		$(g_ResultDivId).innerHTML = originalRequest.responseText;
	}
	if (g_CompletionFunction)
	{
		// if the function does not have parameter braces, add them
		if (g_CompletionFunction.indexOf('(') == -1)
		{
			g_CompletionFunction += '()';
		}
		compFunction = new Function(g_CompletionFunction);
		compFunction();
	}
}

/**
* @private
*
*	Grabs the elements in the array parameter
* and concatonates value=key pairs
*
*	@param String[] paramArray The list of ids from which to grab values
*	@return String The concatonated string
*/
function mergeParameters(paramArray)
{
	mergedString = '';
	numElems = paramArray.length;
	for (i = 0; i < numElems; i++)
	{
		// if this is not the first element append a '&'  
		if (i > 0)
		{
			mergedString += '&';
		}
		domElem = document.getElementById(paramArray[i]);
		if (domElem == null)
		{				
			alert('Can not merge parameter: Element ' + paramArray[i] + ' not found.');
		}
		mergedString += domElem.getAttribute('name') + '=' + $F(paramArray[i]);
	}
	return mergedString;
}

