/**************************************
 **** 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
***************************************/
function cms_writeTools( form_name, field_name )
{
	document.write('<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><tr><td>');
	document.write('<table cellpadding="3" cellspacing="0" border="0"><tr valign="bottom">');
	document.write('<td><a href="javascript:void(0);" onClick="cms_bold(document.' + form_name + '.' + field_name + '); document.' + form_name + '.' + field_name + '.focus();"><b>B</b></a></td>');
	document.write('<td><a href="javascript:void(0);" onClick="cms_italic(document.' + form_name + '.' + field_name + '); document.' + form_name + '.' + field_name + '.focus();"><i>I</i></a></td>');
	document.write('<td><a href="javascript:void(0);" onClick="cms_underline(document.' + form_name + '.' + field_name + '); document.' + form_name + '.' + field_name + '.focus();"><u>U</u></a></td>');
	document.write('<td><img src="' + cms_imagePath + 'separator.gif" alt="" width="6" height="20" border="0"></td>');
	document.write('<td><a href="javascript:void(0);" onClick="cms_header(document.' + form_name + '.' + field_name + '); document.' + form_name + '.' + field_name + '.focus();"><span class="cms_header">Abc</span></a></td>');
	document.write('<td><a href="javascript:void(0);" onClick="cms_subHeader(document.' + form_name + '.' + field_name + '); document.' + form_name + '.' + field_name + '.focus();"><span class="cms_sub_header">Abc</span></a></td>');
	document.write('<td><a href="javascript:void(0);" onClick="cms_small(document.' + form_name + '.' + field_name + '); document.' + form_name + '.' + field_name + '.focus();"><span class="cms_small">Abc</span></a></td>');
	document.write('<td><img src="' + cms_imagePath + 'separator.gif" alt="" width="6" height="20" border="0"></td>');
	document.write('<td><a href="javascript:void(0);" onClick="cms_bullet(document.' + form_name + '.' + field_name + '); document.' + form_name + '.' + field_name + '.focus();">bullet</a></td>');
	document.write('<td><a href="javascript:void(0);" onClick="cms_indent(document.' + form_name + '.' + field_name + '); document.' + form_name + '.' + field_name + '.focus();">indent</a></td>');
	document.write('<td><img src="' + cms_imagePath + 'separator.gif" alt="" width="6" height="20" border="0"></td>');
	document.write('<td><a href="javascript:void(0);" onClick="cms_url(document.' + form_name + '.' + field_name + '); document.' + form_name + '.' + field_name + '.focus();">insert URL</a></td>');
	document.write('<td>&nbsp;<a href="javascript:void(0);" onClick="cms_file(\'' + form_name + '\', \'' + field_name + '\'); document.' + form_name + '.' + field_name + '.focus();");">link to file</a>&nbsp;</td>');
	document.write('<td><a href="javascript:void(0);" onClick="cms_image(\'' + form_name + '\', \'' + field_name + '\'); document.' + form_name + '.' + field_name + '.focus();">insert image</a></td>');
	document.write('</tr></table>');
	document.write('<td align="right"><a href="javascript:void(0);" onClick="cms_preview(document.' + form_name + ', \'' + field_name + '\');">preview</a></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 = cms_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_small( field )
{
	var sel = cms_getSelection( field );
	var text = null;
	
	if ( sel == "" )
	{
		text = window.prompt("Enter the text to make small.");
		
		if ( text == null || text == "" )
			return;
	}
	
	cms_insertAtCursor( field, "[SMALL]" + (text != null ? text : (sel != "" ? sel : "")) + "[/SMALL]" );
}

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

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


function cms_indent( field )
{
	var sel = cms_getSelection( field );
	var text = null;
	
	cms_insertAtCursor( field, "[INDENT]" + (sel != "" ? sel + "[/INDENT]" : "") );
}

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( form_name, field_name )
{
	window.open("../cms/browse.php?form_name=" + form_name + "&field_name=" + field_name + "&type=image", "browse", "width=600, height=400, scrollbars=1, resizable=1");
}

function cms_selectImage( form_name, field_name, file_id )
{
	var align = null;
	var field = window.opener.document.forms[form_name].elements[field_name];
	
	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_movie( form_name, field_name )
{
	window.open("../cms/browse.php?form_name=" + form_name + "&field_name=" + field_name + "&type=movie", "browse", "width=600, height=400, scrollbars=1, resizable=1");
}

function cms_selectMovie( form_name, field_name, file_id )
{
	var align = null;
	var field = window.opener.document.forms[form_name].elements[field_name];
	
	cms_insertAtCursor( field, "[MOV ID=" + file_id + "]");
}

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

function cms_selectFile( form_name, field_name, file_id )
{
	var field = window.opener.document.forms[form_name].elements[field_name];
	var display = null;
	var sel = cms_getSelection( field );
	
	if ( sel == "" )
	{
		display = window.prompt("Enter the text to be displayed for the link (optional):");
		
		if ( display != null && display != "" )
			cms_insertAtCursor( field, "[FILE ID=" + file_id + "]" + display + "[/FILE]");
		else
			cms_insertAtCursor( field, "[FILE ID=" + file_id + "]click here[/FILE]");
	}
	else
		cms_insertAtCursor( field, "[FILE ID=" + file_id + "]" + sel + "[/FILE]");
}

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

function cms_selectTopic( form_name, field_name, topic_id )
{
	var field = window.opener.document.forms[form_name].elements[field_name];
	var display = null;
	var sel = cms_getSelection( field );
	
	if ( sel == "" )
	{
		display = window.prompt("Enter the text to be displayed for the link (optional):");
		
		if ( display != null && display != "" )
			cms_insertAtCursor( field, "[TOPIC ID=" + topic_id + "]" + display + "[/TOPIC]");
		else
			cms_insertAtCursor( field, "[TOPIC ID=" + topic_id + "]click here[/TOPIC]");
	}
	else
		cms_insertAtCursor( field, "[TOPIC ID=" + topic_id + "]" + sel + "[/TOPIC]");
}

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_preview( form, fieldName )
{
	var oldAction = form.action;
	var oldTarget = form.target;
	
	form.action = "../cms/preview.php?cms_source=" + fieldName;
	form.target = "_blank";
	
	form.submit();
	
	form.action = oldAction;
	form.target = oldTarget;
}

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_ignoreTerm( field )
{
	var sel = cms_getSelection( field );
	var text = null;
	
	if ( sel == "" )
	{
		text = window.prompt("Enter the text to have the glossary ignore.");
		
		if ( text == null || text == "" )
			return;
	}
	
	cms_insertAtCursor( field, "[IGNORE]" + (text != null ? text : (sel != "" ? sel : "")) + "[/IGNORE]" );
}

function cms_insertAtCursor( field, value )
{
	// internet explorer
	if ( document.selection )
	{
		field.focus();
		sel = document.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;
}