/**************************************
 **** CONTENT MANAGEMENT FUNCTIONS ****
 **************************************/
/* location of the editor buttons used */
var cms_imagePath = "../cms/img/";

/**************************************
form_name:	the name of the form to be
			accessed
field_name:	the name of the field to be
			accessed
options:	list of arguments that
			determines what options are
			available and in what order
type:		the type used to filter
			files from the file system
			to be used for images or
			links to files
***************************************/
function cms_writeTools( form_name, field_name, options, type )
{	
	var optionArray = options.split(" ");
	
	document.write('<table cellpadding="0" cellspacing="0" border="0"><tr>');
	
	for ( var a = 0; a < optionArray.length; a++ )
	{
		if ( optionArray[a].toLowerCase() == "bold" )
			document.write('<td><a href="javascript:void(0);" onClick="cms_bold(document.' + form_name + '.' + field_name + ');"><img class="image" src="' + cms_imagePath + 'icon_bold.gif" alt="Bold" width="21" height="20" border="0"></a></td>');
		if ( optionArray[a].toLowerCase() == "italic" )
			document.write('<td><a href="javascript:void(0);" onClick="cms_italic(document.' + form_name + '.' + field_name + ');"><img class="image" src="' + cms_imagePath + 'icon_italic.gif" alt="Italic" width="21" height="20" border="0"></a></td>');
		if ( optionArray[a].toLowerCase() == "underline" )
			document.write('<td><a href="javascript:void(0);" onClick="cms_underline(document.' + form_name + '.' + field_name + ');"><img class="image" src="' + cms_imagePath + 'icon_underline.gif" alt="Underline" width="21" height="20" border="0"></a></td>');
		if ( optionArray[a].toLowerCase() == "header" )
			document.write('<td><a href="javascript:void(0);" onClick="cms_header(document.' + form_name + '.' + field_name + ');"><img src="' + cms_imagePath + 'icon_header.gif" alt="List" width="21" height="20" border="0"></a></td>');
		if ( optionArray[a].toLowerCase() == "subheader" )
			document.write('<td><a href="javascript:void(0);" onClick="cms_subHeader(document.' + form_name + '.' + field_name + ');"><img src="' + cms_imagePath + 'icon_sub_header.gif" alt="List" width="21" height="20" border="0"></a></td>');
		if ( optionArray[a].toLowerCase() == "list" )
			document.write('<td><a href="javascript:void(0);" onClick="cms_list(document.' + form_name + '.' + field_name + ');"><img src="' + cms_imagePath + 'icon_list.gif" alt="List" width="21" height="20" border="0"></a></td>');
		if ( optionArray[a].toLowerCase() == "url" )
			document.write('<td><a href="javascript:void(0);" onClick="cms_url(document.' + form_name + '.' + field_name + ');"><img src="' + cms_imagePath + 'icon_createlink.gif" alt="Insert Hyperlink" width="21" height="20" border="0"></a></td>');
		if ( optionArray[a].toLowerCase() == "img" )
			document.write('<td><a href="javascript:void(0);" onClick="cms_image(document.' + form_name + '.' + field_name + ', \'' + type + '\');"><img src="' + cms_imagePath + 'icon_insertimage.gif" alt="Insert Image" width="21" height="20" border="0"></a></td>');
		if ( optionArray[a].toLowerCase() == "|" )
			document.write('<td><img src="' + cms_imagePath + 'separator.gif" alt="" width="6" height="20" border="0"></td>');
	}

	document.write('</tr></table>');
}

function cms_bold( field )
{
	var sel = cms_getSelection( field );
	var text = null;
	
	if ( sel == "" )
	{
		text = window.prompt("Enter the text to bold.");
		
		if ( text == null || text == "" )
			return;
	}
	
	cms_insertAtCursor( field, "[B]" + (text != null ? text : (sel != "" ? sel : "")) + "[/B]" );
}

function cms_italic( field )
{
	var sel = cms_getSelection( field );
	var text = null;
	
	if ( sel == "" )
	{
		text = window.prompt("Enter the text to italicize.");
		
		if ( text == null || text == "" )
			return;
	}
	
	cms_insertAtCursor( field, "[I]" + (text != null ? text : (sel != "" ? sel : "")) + "[/I]");
}

function cms_underline( field )
{
	var sel = getSelection( field );
	var text = null;
	
	if ( sel == "" )
	{
		text = window.prompt("Enter the text to underline.");
		
		if ( text == null || text == "" )
			return;
	}
	
	cms_insertAtCursor( field, "[U]" + (text != null ? text : (sel != "" ? sel : "")) + "[/U]");
}

function cms_header( field )
{
	var sel = cms_getSelection( field );
	var text = null;
	
	if ( sel == "" )
	{
		text = window.prompt("Enter the text to make a header.");
		
		if ( text == null || text == "" )
			return;
	}
	
	cms_insertAtCursor( field, "[HEAD]" + (text != null ? text : (sel != "" ? sel : "")) + "[/HEAD]" );
}

function cms_subHeader( field )
{
	var sel = cms_getSelection( field );
	var text = null;
	
	if ( sel == "" )
	{
		text = window.prompt("Enter the text to make a sub-header.");
		
		if ( text == null || text == "" )
			return;
	}
	
	cms_insertAtCursor( field, "[SUB]" + (text != null ? text : (sel != "" ? sel : "")) + "[/SUB]" );
}

function cms_list( field )
{
	var sel = cms_getSelection( field );
	var text = null;
	
	if ( sel == "" )
	{
		while ( true )
		{
			text = window.prompt("Enter the text to be listed. To cancel adding list items click the cancel button.");
			
			if ( text == "" || text == null )
				return;
			
			cms_insertAtCursor( field, "[LIST]" + text + "[/LIST]\r");
		}
	}
	
	cms_insertAtCursor( field, "[LIST]" + (sel != "" ? sel : "") + "[/LIST]");
}

function cms_leftAlign( field )
{
	var sel = cms_getSelection( field );
	var text = null;
	
	if ( sel == "" )
	{
		text = window.prompt("Enter the text to left align.");
		
		if ( text == null || text == "" )
			return;
	}
	
	cms_insertAtCursor( field, "[LEFT]" + (text != null ? text : (sel != "" ? sel : "")) + "[/LEFT]");
}

function cms_centerAlign( field )
{
	var sel = cms_getSelection( field );
	var text = null;
	
	if ( sel == "" )
	{
		text = window.prompt("Enter the text to center align.");
		
		if ( text == null || text == "" )
			return;
	}
	
	cms_insertAtCursor( field, "[CENTER]" + (text != null ? text : (sel != "" ? sel : "")) + "[/CENTER]");
}

function cms_rightAlign( field )
{
	var sel = cms_getSelection( field );
	var text = null;
	
	if ( sel == "" )
	{
		text = window.prompt("Enter the text to right align.");
		
		if ( text == null || text == "" )
			return;
	}
	
	cms_insertAtCursor( field, "[RIGHT]" + (text != null ? text : (sel != "" ? sel : "")) + "[/RIGHT]");
}

function cms_image( field, type )
{
	var form_name = field.form.name;
	var field_name = field.name;
	
	window.open("../cms/browse.php?form_name=" + form_name + "&field_name=" + field_name + "&type=" + type, "browse", "width=600, height=400, scrollbars=1, resizable=1");
}

function cms_selectImage( field, file_id )
{
	var align = null;
	
	if ( window.confirm("Would you like to align this image? (Cancel means no)") == true )
		align = window.prompt("Specify the alignment: left, right or center");
	
	cms_insertAtCursor( field, "[IMG" + (align != null ? " ALIGN=" + align : "") + " ID=" + file_id + "]");
}

function cms_url( field )
{
	var display = null;
	var sel = cms_getSelection( field );
	var text = null;
	
	if ( sel == "" )
	{
		display = window.prompt("Enter the text to be displayed for the link (optional):");
		text = window.prompt("Please enter the URL of your link.", "http://");
		
		if ( text == null || text == "" || text == "http://" )
			return;
	}
	else
		cms_insertAtCursor( field, "[URL]" + sel + "[/URL]");
	
	if ( display != null && display != "" )
		cms_insertAtCursor( field, "[URL=" + text + "]" + display + "[/URL]");
	else if ( text != null )
		cms_insertAtCursor( field, "[URL]" + text + "[/URL]");
}

function cms_email( field )
{
	var display = null;
	var sel = cms_getSelection( field );
	var text = null;
	
	
	if ( sel == "" )
	{
		display = window.prompt("Enter the text to be displayed for the link (optional):");
		text = window.prompt("Please enter the email address for the link.");
		
		if ( text == null || text == "" )
			return;
	}
	else
		cms_insertAtCursor( field, "[EMAIL]" + sel + "[/EMAIL]");
		

	if ( display != null && display != "" )
		cms_insertAtCursor( field, "[EMAIL=" + text + "]" + display + "[/EMAIL]");
	else if ( text != null )
		cms_insertAtCursor( field, "[EMAIL]" + text + "[/EMAIL]");
}

function cms_insertAtCursor( field, value )
{
	// internet explorer
	if ( document.selection )
	{
		var node = field;
		
		// find the document node for the field's document, not the current page's document
		while ( true )
		{
			if ( node.nodeName == '#document' )
			{
				var doc = node;
				break;
			}
			
			node = node.parentNode;
		}

		field.focus();
		sel = doc.selection.createRange();
		sel.text = value;
	}
	// firefox and safari
	else if ( field.selectionStart || field.selectionStart == '0' )
	{
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		field.value = field.value.substring(0, startPos) + value + field.value.substring(endPos, field.value.length);
	}
	else
		field.value += value;
}

function cms_getSelection( field )
{
	var sel;
	
	// internet explorer
	if ( document.selection )
	{
		field.focus();
		object = document.selection.createRange();
		sel = object.text;
	}
	// firefox and safari
	else if ( field.selectionStart || field.selectionStart == '0' )
	{
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		sel = field.value.substring(startPos, endPos);
	}
	
	return sel;
}