/******************************************************************************************
* @author		Tony Huynh
* @name			getErrorMessage
* @description	Check elements within a specified ID and if empty
*				append to error message and set border to red
* @param		int bodyID 				:: the ID which contains elements to check for
* @param		String[] ignoreElements	:: elements in bodyID which are not error checked
* @return		String 					:: Returns formatted error message
******************************************************************************************/
function getErrorMessage(bodyID, ignoreElements)
{
	/* Get All Input and Textarea Elements within a specific ID */
	elements = new Array();
	elements.push(document.getElementById(bodyID).getElementsByTagName('INPUT'));
	elements.push(document.getElementById(bodyID).getElementsByTagName('TEXTAREA'));
	elements.push(document.getElementById(bodyID).getElementsByTagName('SELECT'));
	var error = "";
	
	/* Loop through Elements and check for errors */
	for (var i = 0; i < elements.length; i++)
	{
		for (var j = 0; j < elements[i].length; j ++)
		{
			/* Make borders black */
			elements[i][j].style.borderColor = "";

			if (elements[i][j].id != "" && !inArray(ignoreElements, elements[i][j].id) && elements[i][j].value == "")
			{
				/* If field is empty, make border red and add to error message */
				elements[i][j].style.borderColor = '#FF0000';
				error += elements[i][j].id + ", ";
			}
		}
	}
	
	return replaceComma(error);
}

/******************************************************************************************
* @author		Tony Huynh
* @name			inArray
* @description	returns true if element is in array, false otherwise
* @param		String[] array 			:: array of string elements
* @param		String element 			:: string to check for in array
* @return		boolean					:: returns true/false
******************************************************************************************/
function inArray(array, element)
{
	if (typeof (array) != "undefined")
	{
		for (var i = 0; i < array.length; i++)
		if (array[i] == element) return true;
	}
	return false;
}

/******************************************************************************************
* @author		Tony Huynh
* @name			replaceComma
* @description	Replaces the second to last comma with "and" and removes the last comma
* @param		int text 				:: text which will be formatted
* @return		String 					:: returns formatted string
******************************************************************************************/
function replaceComma(text)
{
	text = text.substring(0, text.lastIndexOf(","));
	index = text.lastIndexOf(",")
	if (index != -1) return text.substring(0, index) + " and" + text.substring(index+1, text.length);
	return text;
}