/**
 * Verifies at least one checkbox in a set is checked
 */
function verifyCheckboxChecked(checkboxes)
{
	alert(checkboxes);
}

/**
 * Checks or clears a set of checkboxes
 */
function setAllCheckBoxes(checkboxes, flag)
{
	if (checkboxes != undefined)
	{
		for (var i=0; i < checkboxes.length; i++)
		{
			checkboxes[i].checked = flag;
		}
	}
}

/**
 * Selects all checkboxes in a set
 */
function selectAllCheckboxes(checkboxes)
{
	setAllCheckBoxes(checkboxes, true);
}

/**
 * Clears all checkboxes in a set
 */
function clearAllCheckboxes(checkboxes)
{
	setAllCheckBoxes(checkboxes, false);
}

/**
 * Determines if any text is currently highlighted and does the following
 * 1) If text is highlighed, wraps the text with the appropriate html tags
 * 2) If text is NOT highlighted, prompts for text and wraps in appropriate html tags
*/
function wrapText(button, text)
{
	var input = null;
	var htmlText = null;
	
	if (button.name.indexOf('linebreak') != -1)
	{
		htmlText = "<br />";
	}
	else if (button.name.indexOf('bold') != -1)
	{
		if (text == '')
			text = prompt("Enter text to bold", "");
		else if (text != null && text != '')
			htmlText = "<strong>" + text + "</strong>";
	}
	else if (button.name.indexOf('italics') != -1)
	{
		if (text == '')
			text = prompt("Enter text to italicize", "");
		else if (text != null && text != '')
			htmlText = "<em>" + text + "</em>";
	}
	// Return an array for hyperlinks containing text and url
	else if (button.name.indexOf('link') != -1 ||
			 button.name.indexOf('email') != -1)
	{
		if (button.name.indexOf('link') != -1)
		{
			var prompt1 = "Enter web address";
			var prompt2 = "http://";
			var hrefPrefix = "";
		}
		else if (button.name.indexOf('email') != -1)
		{
			var prompt1 = "Enter E-mail address"			
			var prompt2 = "";
			var hrefPrefix = "mailto:";
		}

		var url = prompt(prompt1, prompt2);

		if (text == '')
		{
			var text = prompt("Enter text to display", "");
		}
		if (text != null && text != 'http://')
		{
			htmlText = "<a href=\"" + hrefPrefix + url + "\" target=\"_blank\">" + text + "</a>";
		}
		/*if (button.name.indexOf('link') != -1)
		{
			if (text == '')
			{
				text = prompt("Enter web address", "http://");
				hlink = prompt("Enter text to display", "");
			}
			else if (text != null && text != 'http://')
			{
				htmlText = "<a href=\"" + text + "\" target=\"_blank\">" + text + "</a>";
			}
		}
		else if (button.name.indexOf('email') != -1)
		{
			if (text == '')
				text = prompt("Enter E-mail address", "");
			else if (text != null && text != '')
				htmlText = "<a href=\"mailto:" + text + "\" target=\"_blank\">" + text + "</a>";
		}*/
	}
	
	return htmlText;
}

/**
 * Creates HTML tag + text to be wrapped and updates textarea form field
 */
function createHTML(field, button)
{
	var val = "";
	var sel = null;
	//IE support
	if (document.selection)
	{
		field.focus();
		sel = document.selection.createRange();
		// If no text is highlighted, assume they want to enter it
		val = wrapText(button, sel.text);			
		if (val != null)
			sel.text = val;
	}
	//MOZILLA/NETSCAPE support
	else if (field.selectionStart || field.selectionStart == '0')
	{
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var sel = field.value.substring(startPos, endPos);
		val = wrapText(button, sel);			
		if (val != null)
		{
			field.value = field.value.substring(0, startPos)
			+ val
			+ field.value.substring(endPos, field.value.length);
		}
	}
}
