//<script>
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// Editor functions
//
// Note: the 'element id' is always the id of the div wraping the textarea.
//       it's needed so that multiple editors per screen are possible
//
// dhtml_action( element id, action id, value )
// show_table_borders( element id, state )
// wrap_text( element id, start tag, end tag )
// paragraph_change( element id, type )
// font_size_change( element id, size )
// font_type_change( element id, type )
// switch_fg_color( element id, color )
// switch_bg_color( element id, color )
// image_prompt( target )
// view_source( element id, view source )
// borders_shading( element id )
// insert_table( element id )
// insert_hyperlink( element id )
// table_properties( element id )
// decrease_padding( html DOM element )
// roll_over_src( html dom image element )
// function paste_text( e_id )
// delete_column( e_id )
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------

//
// Global variables
//

// For random colours ...
// By no means comprehensive, just a few random ones
var COLORS = new Array();

COLORS[ 0 ]  = "#00FF00"; COLORS[ 1 ]  = "#009966"; COLORS[ 2 ]  = "#0033CC";
COLORS[ 3 ]  = "#0099FF"; COLORS[ 4 ]  = "#00FFFF"; COLORS[ 5 ]  = "#660000";
COLORS[ 6 ]  = "#660066"; COLORS[ 7 ]  = "#6600CC"; COLORS[ 8 ]  = "#6699FF";
COLORS[ 9 ]  = "#66FFFF"; COLORS[ 10 ] = "#66FF99"; COLORS[ 11 ] = "#669999";
COLORS[ 12 ] = "#669900"; COLORS[ 13 ] = "#993300"; COLORS[ 14 ] = "#996666";
COLORS[ 15 ] = "#9966CC"; COLORS[ 16 ] = "#9999FF"; COLORS[ 17 ] = "#CC33FF";
COLORS[ 18 ] = "#CCFF33"; COLORS[ 19 ] = "#CC6633"; COLORS[ 20 ] = "#CCFFFF";
COLORS[ 21 ] = "#009999"; COLORS[ 22 ] = "#FF0033"; COLORS[ 23 ] = "#FF33FF";
COLORS[ 24 ] = "#33CC99"; COLORS[ 25 ] = "#FF6600"; COLORS[ 26 ] = "#FF9933";
COLORS[ 27 ] = "#66CC66"; COLORS[ 28 ] = "#0000CC"; COLORS[ 29 ] = "#FFFF00";

var SHOW_ALL_BORDERS = false;					// flag indicates if the table borders have been activated

//--------------------------------------------------------
// dhtml_action( element ID, action ID, value )
//
// Execute a DHTML editor action
//--------------------------------------------------------
function dhtml_action( e_id, a_id, val )
{
	if( SOURCE_MODE[e_id] ) {						// actions cannot be preformed while viewing source code
		source_error();								// tell user
		return;										// abort action
	}

	var editor = get_editor_object( e_id );			// get the DHTML editor object reference

	try {
		editor.ExecCommand( a_id, OLECMDEXECOPT_DODEFAULT, val );
	}
	catch( e ) {									// catch any execution errors
		execution_error();							// tell user
		return;
	}
	focus();										// return focus to editing window
}

//------------------------------------------------------------------
// execution_error()
//
// prints a message to notify a user that an error occured, and
// offers advice on how to fix the error
//------------------------------------------------------------------
function execution_error()
{
	alert( translate( "execution_error" ) );
}

//------------------------------------------------------------------
// source_error()
//
// prints a message to notify a user that commands are not allowed in
// source mode and offers advice on how to fix the error
//------------------------------------------------------------------
function source_error()
{
	alert( translate( "source_error" ) );
}

//------------------------------------------------------------------
// paste_text( e_id )
//
// allows a text paste into the editor
// additionally strips the MS word formatting tags out of the
// html source.
//------------------------------------------------------------------
function paste_text( e_id )
{
	dhtml_action( e_id, DECMD_PASTE );

	if( ! SOURCE_MODE[ e_id ] )
		replace_word_junk( e_id );
}

//------------------------------------------------------------------
// selection_error()
//
// prints a message to notify a user that an incorrect element
// was selected for the operation to be preformed.
//------------------------------------------------------------------
function selection_error()
{
	alert( translate( "selection_error" ) );
}

//----------------------------------------------------------------------------
// show_table_borders( element id, state )
//
// if state is "show" - reveils all table borders, each with a different colour
// if state is "hide" - restores original borders
//-----------------------------------------------------------------------------
function show_table_borders( e_id, state )
{
	var editor = get_editor_object( e_id );
	var html = "";						// the contents of the editor

	try {							// try it, sometimes an empty editor will barf
		html = editor.DOM.all;
	}
	catch( e ) {
		return;						// no html to work on, so no work to do ...
	}

	if( state == "hide" && SHOW_ALL_BORDERS )		// borders are showing, and we want to hide them
	{
		var i = 0;
		for( i = 0; i < html.length; i++ )
		{
			if( html( i ).tagName == "TABLE" )
			{
				if( String( html( i ).getAttribute( "old_border" ) ) != "null" )
				{
					// restore old colors
					html( i ).setAttribute( "border", html( i ).getAttribute( "old_border" ) );
					html( i ).setAttribute( "borderColor", html( i ).getAttribute( "old_border_color" ) );

					// remove old tag attribs
					//
					try
					{
						html( i ).removeAttribute( "old_border" );
						html( i ).removeAttribute( "old_border_color" );
					}
					catch( e )
					{
						// shouldn't ever get here...
					}
				}
			}
		}
		SHOW_ALL_BORDERS = false;

	}
	else if( state == "show" && (! SHOW_ALL_BORDERS) )	// borders are set to default and we want to show 'em all
	{
		var i = 0;
		for( i = 0; i < html.length; i++ )
		{
			if( html( i ).tagName == "TABLE" )
			{

				if( html( i ).border == "" )
				{
					// if no border defined assume that user wanted no border
					html( i ).setAttribute( "old_border", "0" );
				}
				else
				{
					html( i ).setAttribute( "old_border", html( i ).border );	// save old border size
				}

				html( i ).setAttribute( "old_border_color", html( i ).borderColor ); 	// save old border color

				html( i ).setAttribute( "border", "1" );				// set border so it shows
				html( i ).setAttribute( "borderColor", random_color() );		// give it a random color
			}
		}
		SHOW_ALL_BORDERS = true;
	}
}

//-------------------------------------------------------
// random_color()
//
// returns a random html colour code
//-------------------------------------------------------
function random_color()
{
	return COLORS[ Math.floor( Math.random() * 30 ) ];
}

//------------------------------------------------------
// wrap_text( element_id, start_tag, end_tag )
//
// wrap highlighted text with a tag
//------------------------------------------------------
function wrap_text( e_id, start, end )
{
	var editor	= get_editor_object( e_id );
	var selected	= editor.DOM.selection.createRange();
    	var text	= selected.htmlText;

    	if( text != null ) {
    		selected.pasteHTML( start + text + end );
    		selected.select();
    		focus();
    	}
}

//-------------------------------------------------------------------
// paragraph_change( element id, type )
// changes the paragraph type (ex: p, h1, h2)
// type is entered as a full word, ex: "Normal" or "Heading 1", etc.
//-------------------------------------------------------------------
function paragraph_change( e_id, type )
{
	dhtml_action( e_id, DECMD_SETBLOCKFMT, type );
}

//--------------------------------------------------
// font_size_change( element id, size )
// changes the font size (ex: 1, 2, 3 ....)
//--------------------------------------------------
function font_size_change( e_id, size  )
{
	dhtml_action( e_id, DECMD_SETFONTSIZE, size );
}

//--------------------------------------------------
// font_type_change( element id, type )
// changes the font type (ex: arial, times, etc)
//--------------------------------------------------
function font_type_change( e_id, type )
{
	dhtml_action( e_id, DECMD_SETFONTNAME, type );
}

//----------------------------------------------------
// color_chart( element id, type, v_offset )
//
// type     - either fg or bg - determines if the foreground or the background colour is to be set
// v_offset - vertical offset from the edge of the container (or edit box, if it exists)
// h_offset - horizontal offset " " " ....
//----------------------------------------------------
function colour_chart( e_id, type, h_offset, v_offset, elem )
{
	current_divID = e_id;
	LastColorType = ColorType;
	ColorType     = type;

	var c_menu = document.getElementById( "color_menu" );

	var total_x_offset = 0;
	var total_y_offset = 0;
	var e = elem.offsetParent;

	while( e.tagName != "BODY" )
	{
		total_x_offset += e.offsetLeft;
		total_y_offset += e.offsetTop;

		e = e.offsetParent;
	}

	total_x_offset += eval( elem.offsetLeft ) + eval( v_offset );
	total_y_offset += eval( elem.offsetTop ) + eval( h_offset );

	c_menu.style.top  = total_y_offset;
	c_menu.style.left = total_x_offset;

	if( LastColorType == ColorType ){

		if( c_menu.style.visibility == "hidden" ) {
			c_menu.style.visibility = "visible";
			c_menu.style.display    = "inline";
		} else {
			c_menu.style.visibility = "hidden";
			c_menu.style.display    = "none";
		}
	}
	else {
		c_menu.style.visibility = "visible";
		c_menu.style.display    = "inline";
	}
}

//----------------------------------------------------------
// switch_fg_color( element id, color )
// sets the foreground color
//----------------------------------------------------------
function switch_fg_color( e_id, color )
{
	dhtml_action( e_id, DECMD_SETFORECOLOR, color );
}

//----------------------------------------------------------
// switch_bg_color( element id, color )
// sets the background color
//----------------------------------------------------------
function switch_bg_color( e_id, color )
{
	dhtml_action( e_id, DECMD_SETBACKCOLOR, color );
}

//-------------------------------------------------------------
// insert_char( element_id, chart_path )
//
// Inserts a 'special' character into the editor
//------------------------------------------------------------
function insert_char( e_id, path, x_size, y_size )
{
	if( SOURCE_MODE[e_id] ) {	// actions cannot be preformed while viewing source code
		source_error();		// tell user
		return;			// abort action
	}

	var editor = get_editor_object( e_id );

	if( editor.DOM.selection == null )
		return;

	var selection = null;
	try {
		selection = editor.DOM.selection.createRange();
	}
	catch( e )
	{
		execution_error();
		return;
	}
try
{
	var char = window.showModalDialog(	path,
						"",
						"dialogHeight: "+y_size+"px; dialogWidth: "+x_size+"px; dialogTop:   100px; "+
						"dialogLeft: 150px; center: No; help: No; resizable: No; status: No;" );


	if( char == ""  ||
	    char == null )
		return;

	if( selection.parentElement != null )
	{
		selection.pasteHTML( char );
		selection.select();
    	}

    	focus();
}catch(e)
{	alert("Could not perform this action - if you have popup\nblockers enabled, please disable them for this site.");
}
}

//----------------------------------------------------------
// image_prompt( target )
// target - the id of the page that the image will be used on
// uploads an image to the editor
//----------------------------------------------------------
function image_prompt( target, image_path )
{
	var imageEditor;

//	if( COMPILED_SMARTSITE )
//	{
//		imageEditor = "smartsite.asp/?sscb=administration/images2.asp&p=" + target + "&imagePath="+image_path;
//	}
//	else
//	{
		imageEditor = "images2.asp?p=" + target + "&imagePath="+image_path;
//	}

try
{
	var image = window.showModalDialog(	"/administration/"+ imageEditor,
						"",
						"dialogHeight: 560px; dialogWidth: 585px; dialogTop:   100px; "+
						"dialogLeft: 150px; center: No; help: No; resizable: No; status: No;" );

	return get_relative_path( image );		// path returned is absolute. must convert to relative path
}catch(e)
{	alert("Could not perform this action - if you have popup\nblockers enabled, please disable them for this site.");
}
return null;
}

//-------------------------------------------------------------
// insert_image( element id )
//
// Inserts an image into the editor
//------------------------------------------------------------
function insert_image( e_id, target )
{
	if( SOURCE_MODE[e_id] ) {	// actions cannot be preformed while viewing source code
		source_error();		// tell user
		return;			// abort action
	}

	var editor = get_editor_object( e_id );

	if( editor.DOM.selection == null )
		return;

	var selection = null;
	try {
		selection = editor.DOM.selection.createRange();
	}
	catch( e )
	{
		execution_error();
		return;
	}

	img_src = image_prompt( target, "" );

	if( img_src == "/administration/"  ||		// special case, the image tool will sometimes return
	    img_src == ""                  ||		// '/administration/' if the image selected was null ..
	    img_src == null )
		return;

	if( selection.parentElement != null )
	{
		var new_img = "<img src=\""+ img_src + "\" border=\"0\"/>";
		selection.pasteHTML( new_img );
		selection.select();
    	}
    	else
    	{
		selection.item( 0 ).src = img_src;
    	}
    	focus();
}

//-------------------------------------------------------------
// insert_media( element id )
//
// Inserts an image into the editor
//------------------------------------------------------------
function insert_media( e_id, target )
{
	if( SOURCE_MODE[e_id] ) {	// actions cannot be preformed while viewing source code
		source_error();		// tell user
		return;			// abort action
	}

	var editor = get_editor_object( e_id );

	if( editor.DOM.selection == null )
		return;

	var selection = null;
	try {
		selection = editor.DOM.selection.createRange();
	}
	catch( e )
	{
		execution_error();
		return;
	}

	media_src = media_prompt( target, "" );

	if( media_src == "/administration/"  ||		// special case, the image tool will sometimes return
	    media_src == ""                  ||		// '/administration/' if the image selected was null ..
	    media_src == null )
		return;

	if( selection.parentElement != null )
	{
		var media_tag = get_media_tag( media_src );
		selection.pasteHTML( media_tag );
		selection.select();
    	}
    	else
    	{
		selection.item( 0 ).src = media_tag;
    	}
    	focus();
}

//----------------------------------------------------------
// media_prompt( target )
// target - the id of the page that the image will be used on
// uploads an image to the editor
//----------------------------------------------------------
function media_prompt( target, media_path )
{
	var mediaEditor;

//	if( COMPILED_SMARTSITE )
//	{
//		imageEditor = "smartsite.asp/?sscb=administration/mediaBrowser&p=" + target + "&imagePath="+image_path;
//	}
//	else
//	{
		mediaEditor = "mediaBrowser.asp?p=" + target + "&mediaPath="+media_path;
//	}

try
{	var media = window.showModalDialog(	"/administration/"+ mediaEditor,
						"",
						"dialogHeight: 560px; dialogWidth: 585px; dialogTop:   100px; "+
						"dialogLeft: 150px; center: No; help: No; resizable: No; status: No;" );

	return get_relative_path( media );		// path returned is absolute. must convert to relative path
}catch(e)
{	alert("Could not perform this action - if you have popup\nblockers enabled, please disable them for this site.");
}
return null;
}

function get_media_tag( src )
{
	String(src.toLowerCase()).match( /.*\.([^.]*)/ );
	var ext = RegExp.$1;
	var tag = new Array();

	switch( ext )
	{
		case "mp3" :	// fall through
		case "ra"  : 	tag = "<EMBED src=\""+src+"\" autostart=true hidden=true>";
				break;
		case "wav" : 	tag = "<EMBED src=\"file.wav\" autostart=true loop=false volume=100 "+
				      "hidden=true><NOEMBED><BGSOUND src=\""+src+"\"></NOEMBED>";
				break;

		case "wma" : 	tag = "<OBJECT> ID=\"mediaPlayer\" CLASSID=\"CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95\" "+
					"CODEBASE=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701\" "+
					"STANDBY=\"Loading Microsoft Windows Media Player components...\" "+
					"TYPE=\"application/x-oleobject\"> "+
					"<PARAM NAME=\"fileName\" VALUE=\""+src+"\"> "+
					"<PARAM NAME=\"animationatStart\" VALUE=\"true\"> "+
					"<PARAM NAME=\"transparentatStart\" VALUE=\"true\"> "+
					"<PARAM NAME=\"autoStart\" VALUE=\"true\"> "+
					"<PARAM NAME=\"showControls\" VALUE=\"true\"> "+
					"<Embed type=\"application/x-mplayer2\" "+
					"pluginspage=\"http://www.microsoft.com/isapi/redir.dll?prd=windows&sbp=mediaplayer&ar=Media&sba=Plugin&\" "+
					"src=\""+src+"\" Name=MediaPlayer "+
					"AutoStart=1 "+
					"Width=some_number Height=some_number "+
					"transparentAtStart=0 "+
					"animationAtStart=1 "+
					"ShowControls=1 "+
					"ShowStatusBar=1 "+
					"autoSize=0 "+
					"displaySize=0 "+
					"</embed></OBJECT>";
				break;

		case "mov" :	tag = "<embed src=\""+src+"\" width=\"320\" height=\"256\"></embed>";
				break;
		case "mpg" :	tag = "<object data=\""+src+"\" type=\"video/mpg\" standby=\"Loading Video\" width=\"200\" height=\"200\"> "+
  					"<embed src=\""+src+"\" autostart=\"true\" loop=\"true\" width=\"200\" height=\"200\"></embed> "+
					"</object>";
				break;
		case "rm"  :	tag = "<OBJECT ID=\"klick_rm\" CLASSID=\"clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA\" "+
  					"HEIGHT=\"100\" WIDTH=\"200\"> "+
  					"<PARAM NAME=\"console\" VALUE=\"Clip1\"> "+
  					"<PARAM NAME=\"controls\" VALUE=\"All\"> "+
  					"<PARAM NAME=\"autostart\" VALUE=\"false\"> "+
  					"<PARAM NAME=\"src\" VALUE=\""+src+"\"> "+
  					"<EMBED SRC=\""+src+"\" TYPE=\"audio/x-pn-realaudio-plugin\" "+
					"CONSOLE=\"Clip1\" CONTROLS=\"All\" "+
    					"HEIGHT=\"100\" WIDTH=\"200\" AUTOSTART=\"false\"> "+
   					"</EMBED></OBJECT>";
				break;
		case "wmv" :	tag = "<object name=\"wmtPlayer\" width=\"300\" height=\"280\" "+
                   			"classid=\"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6\"> "+
                   			"<param name=\"url\" value=\""+src+"\"> "+
                   			"<param name=\"autostart\" value=\"1\"> "+
                   			"<param name=\"uiMode\" value=\"mini\"> "+
                   			"<embed type=\"application/x-mplayer2\" "+
                       			"name=\"wmtPlayer\" "+
                       			"src=\""+src+"\" "+
                       			"autostart=\"1\" "+
                       			"uimode=\"mini\" "+
                       			"width=300 height=280> "+
                   			"</embed></object>";
				break;

		default    : 	tag = src;
				break;
	}

	return tag;
}

function set_wrap( e_id, mode )
{
	WRAP[e_id] = mode;

	if( SOURCE_MODE[e_id] )
	{
		var editor_div = document.getElementById( e_id );
		var textarea   = document.getElementById( editor_div.editBoxFormField );
		if( textarea )
		{
			textarea.wrap = mode;
		}
	}
}

//------------------------------------------------------
// view_source( element id, view source )
// if view source is true, then view the source code, else
// view the rendered document
//------------------------------------------------------
function view_source( e_id, v_source )
{
	var editor_div = document.getElementById( e_id );
	if( ! SOURCE_MODE[e_id] && v_source )					// want to view the source
	{
		show_table_borders( e_id, 'hide' );					// don't want to mess up the borders ...

		SOURCE_MODE[e_id] = true;
	      var editor        = get_editor_object( e_id );

		try {
			editor_div.current_content = new String(clean_HTML( editor.DocumentHTML ));

		} catch(e) {

			editor_div.current_content = new String(clean_HTML( HTML[e_id] ));
			alert( "The MS Editing Component had some trouble with the source code. "+
				"You will be shown the original source instead." );
		}

		editor_div.innerHTML = "<textarea \
					 id=" + editor_div.editBoxFormField + " \
					 name=" + editor_div.editBoxFormField + " \
					 wrap=\""+ WRAP[e_id] + "\" \
					 onchange=\"try{expire_content();}catch(e){;}\" \
					 style=\"color:darkgreen; \
                                 	 border:0; \
                                   	 border-width:0; \
                                 	 border-height:0; \
                                 	 width:"+ WIDTH[e_id] + "; \
                                 	 height:"+ HEIGHT[e_id] +";\" \
					> \
					</textarea>";

		editor_div.textArea       = document.getElementById( editor_div.editBoxFormField );
		editor_div.textArea.value = editor_div.current_content;
	}
	else if( SOURCE_MODE[e_id] && ! v_source )			// want to view the document
	{
		SOURCE_MODE[e_id] = false;
		var currentContent;
		editor_div.textArea	 =	document.getElementById( editor_div.editBoxFormField );
		current_content      =	editor_div.textArea.value;
		editor_div.innerHTML =	get_dhtml_editor_source( editor_div.editBoxFormField,
								 HEIGHT[e_id],
								 WIDTH[e_id] ) +
								"<input \
								 type=hidden \
								 id=" + editor_div.editBoxFormField + " \
								 name=" + editor_div.editBoxFormField + " \
								>";
      var editor     = get_editor_object( e_id );
      editor.DocumentHTML = "&nbsp;"+current_content;
      editor.baseURL      = "http://" + location.host + "/";
  }
}

//----------------------------------------------------
// borders_shading( element id )
// popup the borders and shading dialog
//----------------------------------------------------
function borders_shading( e_id )
{
	if( SOURCE_MODE[e_id] ) {					// actions cannot be preformed while viewing source code
		source_error();							// tell user
		return;									// abort action
	}

	var editor = get_editor_object( e_id );

	if( editor.DOM.selection == null )
		return;

	var selection = null;
	try {
		selection = editor.DOM.selection.createRange().parentElement();
	}
	catch( e )
	{
		execution_error();
		return;
	}

	selection = valid_table_el( selection );			// make sure that we have a valid part of a table.

	if( selection == null )
	{
		execution_error();
		return;
	}

	t_element = get_table( selection );

	var args = new Array();
	var arr = null;
	// Display table information dialog
	args["bgColor"]			= t_element.bgColor;
	args["bWidth"]			= String(t_element.border);
	args["borderColor"]		= t_element.borderColor;
	args["bStyle"]			= t_element.style.borderStyle;
	args["bgColor_td"]		= selection.bgColor;
	args["bWidth_td"]		= String(selection.style.borderWidth).replace( "px" , "" );
	args["borderColor_td"]	= selection.borderColor;
	args["bStyle_td"]		= selection.style.borderStyle;
	args["eType"]			= selection.tagName;

	if( args[ "bStyle" ] == "" )
		args[ "bStyle" ] = "solid";

	if( args[ "bStyle_td" ] == "" )
		args[ "bStyle_td" ] = "solid";

try
{
	arr = showModalDialog( translate( "border_shading_page" ),
						   args,
						   "font-family:Verdana; font-size:12; dialogWidth:330px; dialogHeight:310px; help:no; scroll:no; status:no;"
						 );

	if( arr != null ) {
		if( arr[ "eType" ] == "TD" )
		{
			if( ! isNaN( parseInt( arr[ "bWidth" ] ) ) )
				selection.style.borderWidth			= (arr[ "bWidth" ]+ "px");

			selection.bgColor					= arr[ "bgColor" ];
			selection.borderColor				= arr[ "borderColor" ];
			selection.style.borderStyle				= arr[ "bStyle" ];
		}
		else
		{
			if( ! isNaN( parseInt( arr[ "bWidth" ] ) ) ) {
				t_element.border		    = (arr[ "bWidth" ] + "px");
				t_element.style.borderWidth = (arr[ "bWidth" ] + "px");
			}

			t_element.bgColor				= arr[ "bgColor" ];
			t_element.borderColor			= arr[ "borderColor" ];
			t_element.style.borderStyle		= arr[ "bStyle" ];
		}
	}
}catch(e)
{	alert("Could not perform this action - if you have popup\nblockers enabled, please disable them for this site.");
}
}

//----------------------------------------------------
// cell_edit( element id )
// popup the cell editor dialog
//----------------------------------------------------
function cell_edit( e_id )
{
	if( SOURCE_MODE[ e_id ] ) {					// actions cannot be preformed while viewing source code
		source_error();							// tell user
		return;									// abort action
	}

	var editor = get_editor_object( e_id );

	if( editor.DOM.selection == null )
		return;

	var selection = null;
	try {
		selection = editor.DOM.selection.createRange().parentElement();
	}
	catch( e )
	{
		execution_error();
		return;
	}

	cell = get_col( selection );

	if( cell.tagName != "TD" )
	{
		execution_error();
		return;
	}

	var args = new Array();
	var arr = null;
	// Display table information dialog
	args["rowSpan"]			= cell.rowSpan;
	args["colSpan"]			= cell.colSpan;
	args["height"]			= cell.height;
	args["width"]			= cell.width;
	args["vAlign"]			= cell.vAlign;
	args["align"]			= cell.align;
try
{
	arr = showModalDialog( translate( "cell_editor_page" ),
						   args,
						   "font-family:Verdana; font-size:12; dialogWidth:290px; dialogHeight:390px; help:no; scroll:no; status:no;" );

	if( arr != null ) {

			if( ! isNaN( parseInt( arr[ "rowSpan" ] ) ) )
				cell.rowSpan	= arr[ "rowSpan" ];

			if( ! isNaN( parseInt( arr[ "colSpan" ] ) ) )
				cell.colSpan	= arr[ "colSpan" ];

			if( ! isNaN( parseInt( arr[ "height" ] ) ) )
				cell.height	= arr[ "height" ];

			if( ! isNaN( parseInt( arr[ "width" ] ) ) )
				cell.width	= arr[ "width" ];

			cell.vAlign	= arr[ "vAlign" ];
			cell.align	= arr[ "align" ];
	}
}catch(e)
{	alert("Could not perform this action - if you have popup\nblockers enabled, please disable them for this site.");
}
}

//-----------------------------------------------------------------
// insert_table( element id )
// pops up a table insertion dialog and based on the entered values
// a table is inserted
//-----------------------------------------------------------------
function insert_table( e_id )
{
  try
  {
	var editor = get_editor_object( e_id );			// get the DHTML editor object reference

	if( SOURCE_MODE[e_id] )
	{
		source_error();
		return;
	}

	// check to see if an image is selected  ... it's kinda dumb to try to put a table inside an image (and it
	// crashes the editor... )

	var selection = null;
	try {
		selection = editor.DOM.selection.createRange();
	}
	catch( e )
	{
		; // no image, no worries
	}

	if( selection != null )
	{
		if( selection.parentElement == null )
		{
			selection_error();
			return;
		}
	}

	new_table = new ActiveXObject( "DEInsertTableParam.DEInsertTableParam" );
	var args = new Array();
	var arr = null;

	// Display table information dialog
	args[ "NumRows" ]    	= new_table.NumRows;
	args[ "NumCols" ]    	= new_table.NumCols;
	args[ "CellPad" ]    	= 2;
	args[ "CellSpace" ]  	= 0;
    	args[ "RowHeight" ]  	= "";
    	args[ "ColWidth" ]   	= "";
	args[ "Thickness" ]  	= 2;
	args[ "FGColor" ]	= "black";
	args[ "BGColor" ]	= "white";
	args[ "Type" ]		= "solid";
	args[ "Width" ]		= 75;
	args[ "WidthUnit" ]	= "%";
	args[ "FontFamily" ]	= "Arial";
	args[ "FontSize" ]	= "10";
	args[ "HorizAlign" ]    = "";
	args[ "VertAlign" ]     = "";

	arr = null;
	arr = window.showModalDialog( translate( "insert_table_page" ),
					   args,
					   "font-family:Verdana; font-size:12; dialogWidth:465px; dialogHeight:510px; help:no; scroll:no; status:no;" );

	if( arr != null ) {
		// Initialize table object
		var table_atribs  = "";
		var cell_atribs   = "";
		new_table.NumRows = arr[ "NumRows" ];
		new_table.NumCols = arr[ "NumCols" ];
		table_atribs = "cellpadding="+arr["CellPad"]+" cellspacing="+arr["CellSpace"] +
					   " bordercolor="+arr["FGColor"]+" bgcolor="+arr["BGColor"] +
					   " border="+arr["Thickness"]+ " width=\""+arr["Width"]+arr["WidthUnit"]+"\"" +
					   " Style=\"border-style:"+arr["Type"]+"; font-family:"+arr["FontFamily"] +";"+
					   " font-size:"+arr["FontSize"]+"pt;\"";

		if( arr["RowHeight"] > 0 )
			cell_atribs += " height="+arr["RowHeight"];

		if( arr["HorizAlign"] != "" )
			cell_atribs += " align=\"" + arr["HorizAlign"] + "\"";

		if( arr["VertAlign"] != "" )
			cell_atribs += " valign=\"" + arr["VertAlign"] + "\"";

		if( arr["ColWidth"] > 0 )
			cell_atribs += " width="+arr["ColWidth"];


		new_table.TableAttrs = table_atribs;
		new_table.CellAttrs  = cell_atribs;

		editor.ExecCommand( DECMD_INSERTTABLE, OLECMDEXECOPT_DODEFAULT, new_table );
	}
}catch(e)
{	alert("Could not perform this action - if you have popup\nblockers enabled, please disable them for this site.");
}
}

//-------------------------------------------------------
// insert_hyperlink( element id )
// displays a hyperlink dialog and based on the
// enter values inserts a smartsite, http or mailto link
//-------------------------------------------------------
function insert_hyperlink( e_id )
{
	var editor = get_editor_object( e_id );			// get the DHTML editor object reference

	// defaults
	var url		= "";
	var type	= "smartsite";
	var pop_height	= "100";
	var pop_width	= "100";
	var new_window  = "no";
	var target	= "";

	var args = new Array();
	var arr = null;
    	var selected = null;

	try
	{
		selected = editor.DOM.selection.createRange();
	}
	catch(e)
	{
		execution_error()
		return;
	}

    	text      = selected.htmlText;
    	var a_tag = null;
	var image = null;


    	if( String( selected.parentElement ) == "undefined" )			// it's an image
	{
		image = selected.item(0);
		a_tag= image.parentElement;
	}
	else
	{
		a_tag= selected.parentElement();
	}

	while( a_tag.tagName != "A" ) {				// check to see if we're editing an existing link
		a_tag = a_tag.parentElement;

		if( a_tag.tagName == "HTML" ) {
			a_tag = null;
			break;
		}
	}

	if( a_tag != null ) {						// if we're editing an existing link then parse it to get the tag values

		target = String( a_tag.target );
		url = get_url( a_tag );

		if( url.substring( url.length-1, url.length ) == "/" )
			url = url.substring( 0, url.length - 1 );

		nw_match = url.search( new RegExp( "javascript:void", "i" ) );

		if( nw_match != -1 )
		{
			new_window = "yes";

			var params = String( a_tag.onclick );
			params = params.replace( "window.open", "" );
			all_params = params.split( "," );

			url = String(all_params[0]).substring( 2, String(all_params[0]).length-1 );

			pop_width = String( all_params[8] ).substring( 6, String( all_params[8] ).length );

			// height is a bit more tricky ..
			height = String( all_params[9] ).substring( 7, String( all_params[9] ).length );
			height2 = height.split( "\"" );
			pop_height = height2[0];
		}

		ss_match = url.search( new RegExp( "page\\[", "i" ) );			// check for smartsite page match

		if( ss_match != -1 )							// possibly a smartsite page.. check special case
		{
			url = url.substr( ss_match, url.length );			// get the page[] part

			new_url  = parse_smartsite_link( url, ss_match );

			if( new_url == url )						// if the url is unmodified, then it is
			{								// a smartsite module page with parameters
				type = "http";						// treat it as an http page
			}
			else
			{
				type = "smartsite";
				url = new_url;
			}
		}
		else
		{
			email_match = url.search( new RegExp( "mailto:", "i" ) );

			if( email_match != -1 )
			{
				url  = url.substring( email_match+7, url.length );
				type = "email";
			}
			else
			{
				http_match = url.search( new RegExp( "http:", "i" ) );
				var https_match = url.search( new RegExp( "https:", "i" ) );

				if( http_match != -1 )
				{
					type = "http";
					url = url.replace( "http://", "" );
					//if( new_window == "yes" )
					//	url = url.substring( 7, url.length );
				}
				else if(  https_match != -1 )
				{
					type = "https";
					url = url.replace( "https://", "" );
				}
				else
				{
					type = "file";
				}
			}
		}
	}

	// Set Defaults
	args[ "target" ]	= target;
	args[ "type" ]		= type;
	args[ "url" ]		= url;
	args[ "new_window" ]	= new_window;
	args[ "pop_height" ]    = pop_height;
	args[ "pop_width" ]	= pop_width;
	args[ "url_sources" ]   = URL_SOURCES[ e_id ];

	arr = null;
try
{
	arr = showModalDialog( translate( "links_page" ),
				   args,
				  "font-family:Verdana; font-size:12; dialogWidth:400px; dialogHeight:430px; help:no; scroll:no; status:no;"
				 );
	if( arr == null )
	{
		return;
	}

    	var link = "";

    	switch( arr["type"] )
    	{
		case "smartsite" :	link = "page[" + arr["url"] + "]";
					break;
		case "https"	 :	link = parse_http_link( arr["url"], true );
					break;
		case "http"      :	link = parse_http_link( arr["url"] );
					break;
		case "email"     :	link = "mailto:" + arr["url"];
					break;
		case "file"	 :	link = arr["url"];
					break;
    		default		 :	link = arr["url"];
					break;
    }


    if( arr["new_window"] == "no" )
	{
		var url = "<a href=\"" + link + "\" target=\"" + arr["target"] +"\">";
	}
	else
	{
		var url =	"<a href=\"javascript:void(0);\" target=\"" + arr["target"] +"\""+
					"onClick='window.open(\""+ link +"\","+
					"\"browser\", \"dependent=no,resizable=yes,scrollbars=yes,top=0,left=0,status=no,width="+
					arr["pop_width"] +",height="+arr["pop_height"]+"\");'>";
	}

	if( image == null )								// a regular link, not an image link
	{
		if( a_tag == null )							// a new link
		{
			selected.pasteHTML( url + selected.htmlText + "</a>" );
			selected.select();
		}
		else										// an existing link
		{
			a_tag.outerHTML = url + a_tag.innerHTML + "</a>";
		}
	}
	else											// an image link
	{
		var image_tag = image.outerHTML;
		if( a_tag == null )													// if the image wasn't already hyperlinked
			image.outerHTML = url + image_tag + "</a>";
		else															// if the image was already hyperlinked
			a_tag.outerHTML = url + image_tag + "</a>";
	}
}catch(e)
{	alert("Could not perform this action - if you have popup\nblockers enabled, please disable them for this site.");
}
}

//-----------------------------------------------
// get_url( a_tag )
//
// Given an 'A tag' object, retrieves the href
//
// Yes, the DOM will do this, but it has the
// nasty habit of returning an absolute url
// which makes dealing with relative url's
// difficult
//-----------------------------------------------
function get_url( a_tag )
{
	if( a_tag.href == "" )
		return "";										// forget parsing, nothing there to parse

	var html = a_tag.outerHTML;

	var html_components = String( html ).split( ">" );	 // split along tag lines
	var a_html = html_components[ 0 ];					 // a tag is the first one

	var a_components = String( a_html ).split( "href" );			// split on the component we want
	var href_components = String( a_components[1] ).split( "\"" );	// split on the quotes
	var href_html = href_components[ 1 ];							// the href is between the quotes..

	return href_html;
}

//----------------------------------------------------
// parse_smartsite_link( url )
//
// Parses a link retived from the editor, and determines
// if the page should be parsed on weather or not the
// page takes parameters
//
// ex:	page[booga]				- don't parse
//		page[booga2]?myvar=2	- parse
//
//----------------------------------------------------
function parse_smartsite_link( url )
{
	var after_page = url.split( "]" );

	if( String( after_page[1]).length > 1 )									// it's the special case
	{
		return url;
	}
	else
		return url.substring( 5, url.length-1 );				// it's a normal smartsite page
}

//----------------------------------------------------
// parse_http_link( url )
//
// Parses a link entered in the hyperlink tool
// Proccesses a special case where a smartsite page
// with parameters is entered
//----------------------------------------------------
function parse_http_link( url, secure )
{
	if( String( url ).substr( 0, 5 ) == "page[" )			// special case ... allows parameters to smartsite pages .. a hack for advanced users
	{
		return url;
	}
	else
	{
		url = String( url ).replace( "http://", "" );		// in case people type in "http://" before the link
		url = (secure) ? "https://" + url : "http://" + url;
		return url;
	}
}

//-----------------------------------------------------------------
// table_properties( element id )
// modifies the properties of a selected table
//-----------------------------------------------------------------
function table_properties( e_id )
{
	var editor = get_editor_object( e_id );			// get the DHTML editor object reference

	if( SOURCE_MODE[e_id] )
	{
		source_error();
		return;
	}

	if( editor.DOM.selection == null) {
		execution_error();
		return;
	}

	var element = null;

	try {
		element = editor.DOM.selection.createRange().parentElement();
	}
	catch(e) {
		execution_error()
		return;
	}

	element = valid_table_el( element );			// make sure that we have a valid part of a table.

	if( element == null )
	{
		execution_error();
		return;
	}

	var args = new Array();
	var arr = null;

	element = get_table( element );

	// Display table information dialog
	args["bgColor"]     = element.bgColor;
	args["bWidth"]      = element.border;
	args["borderColor"] = element.borderColor;
	args["CellPadding"] = element.cellPadding;
	args["CellSpacing"] = element.cellSpacing;
	args["TableWidth"]  = element.width;
 try
 {
	arr = showModalDialog( translate( "table_properties_page" ),
						   args,
						   "font-family:Verdana; font-size:12; dialogWidth:235px; dialogHeight:425px; help:no; scroll:no; status:no;"
						 );

	if( arr != null ) {
		element.bgColor     = arr[ "bgColor" ];
		element.border		= arr[ "bWidth" ];
		element.borderColor = arr[ "borderColor" ];
		element.cellPadding = arr[ "CellPadding" ];
		element.cellSpacing = arr[ "CellSpacing" ];
		element.width		= arr[ "TableWidth" ];
	}
}catch(e)
{	alert("Could not perform this action - if you have popup\nblockers enabled, please disable them for this site.");
}
}

//------------------------------------------------
// decrease_padding( html DOM element )
// decreases the padding of the the selected object
//--------------------------------------------------
function decrease_padding( element )
{
	if( element.hspace > 2 )
	{
		element.hspace -= 3 ;
	}

	if( element.vspace > 0)
	{
		element.vspace -= 1 ;
	}
}

//-----------------------------------------------------------
// roll_over_src( html dom image element )
// Prompts user for an image that will be used for a rollover
//-----------------------------------------------------------
function roll_over_src( image, target )
{
	if( ( image.smartSiteRollOver ||
		! image.onmouseover    ) &&
		image.tagName == "IMG" )
	{
		var orginal_rollover = "";
		var mouseover        = image.onmouseover;

		if( mouseover != "" )
		{
			orginal_rollover = String( mouseover ).substring( 10, String( mouseover ).length -1 );
			orginal_rollover = orginal_rollover.substring( 0, orginal_rollover.length );
		}
		// get the image path
		my_image = image_prompt( target, orginal_rollover );

		if( my_image == null )
			return;								// user cancelled

		// set the attributes
		image.onmouseover       = "this.src='" + my_image + "'";
		image.onmouseout        = "this.src='" + get_relative_path( image.src ) + "'";
		image.smartSiteRollOver = my_image;
	}
	else
	{
		alert( translate( "rollover_error" ) );
	}
}

function get_dhtml_editor_source( id, height, width )
{
	return	"<object "+
			 "onfocus=\"if(parent.hideAllMenuScriptlets){parent.hideAllMenuScriptlets();}else if(hideAllMenuScriptlets){hideAllMenuScriptlets();}\" "+
			 "classid='clsid:2D360201-FFF5-11d1-8D03-00A0C959BC0A' "+
			 "id=editBox_" + id + " "+
			 "name = editBox_" + id + " "+
			 "height=" + height + " "+
			 "width=" + width + " "+
			 "progid = 'dhtmlsafe.dhtmlsafe' > "+
			"</object>";
}

function display_about()
{
try
{	showModalDialog("/include/html/about.html",
			"",
			"dialogWidth:280px; dialogHeight:215px; help:no; scroll:no; status:no;");

}catch(e)
{	alert("Could not perform this action - if you have popup\nblockers enabled, please disable them for this site.");
}
}


function resize_editor( e_id, width, height )
{
	var border_adjustment = 1;						// corrects for menu border width

	var editor_div = document.getElementById( e_id );
	var i = 0;

	// save the setting
	WIDTH[ e_id ]  = width;
	HEIGHT[ e_id ] = height;

	// switch to document mode
	view_source( e_id, false );

	for( i = 0; i < editor_div.all.length; i++ )
	{
		if( editor_div.all.item( i ).width )
			editor_div.all.item( i ).width = width;

		if( editor_div.all.item( i ).height )
			editor_div.all.item( i ).height = height;
	}

	document.getElementById( "menu_"+e_id ).width = width - border_adjustment;

	var border_div = document.getElementById( "border_"+e_id );
	border_div.style.width  = width;
	border_div.style.height = height;
}

function id_from_e_id( e_id )
{
	var id_parts = String( e_id ).split( "_" );
	var i;
	var id = "";
	for( i = 1; i < id_parts.length; i++ )					// skip the first part to get the id
		id += id_parts[ i ] + "_";

	return id;
}

function delete_column( e_id )
{
	if( SOURCE_MODE[ e_id ] ) {					// actions cannot be preformed while viewing source code
		source_error();							// tell user
		return;									// abort action
	}

	var editor = get_editor_object( e_id );

	if( editor.DOM.selection == null )
		return;

	var element = null;
	try {
		element = editor.DOM.selection.createRange().parentElement();
	}
	catch( e )
	{
		execution_error();
		return;
	}

	if( element == null )
	{
		execution_error();
		return;
	}

	var table	   = get_table( element );
	var col        = get_col( element );
	var row        = get_row( element );
	var row_col    = row.firstChild;

	var col_number = 1;

	while( row_col != col && row_col != null )
	{
		if( row_col.tagName == "TD" )
		{
			if( Number( row_col.colspan ) > 0 )
			{
				col_number += Number( row_col.colspan );
			}
			else
			{
				col_number++;
			}
		}
		row_col = row_col.nextSibling;
	}

	delete_table_column( table, col_number );
}

function delete_table_column( table, col_num )
{
	var row = get_first_row( table );
	var cur_col_num, cur_col;

	while( row != null )
	{
		cur_col_num = 0;
		cur_col     = row.firstChild

		while( cur_col != null )
		{
			if( Number( cur_col.colspan ) > 0 )
			{
				cur_col_num += Number( cur_col.colspan );
			}
			else
			{
				cur_col_num++;
			}

			if( cur_col_num >= col_num )
			{
				if( Number( cur_col.colspan ) > 0 )
				{
					cur_col.colspan--;
				}
				else
				{
					cur_col.removeNode( true );
				}
			}

			cur_col = cur_col.nextSibling;
		}

		row = row.nextSibling;
	}
}



function navigate_words( e_id, editor )
{
	var args = new Array();
	args[ "editor" ] = editor;
	args[ "editorID" ] = e_id;
try
{
	window.showModalDialog(	"/include/html/spelling_check.html",
				args,
				"dialogHeight: 240px; dialogWidth: 400px; dialogTop:   300px; "+
				"dialogLeft: 300px; scroll:no; center: No; help: No; resizable: No; status: No;" );
}catch(e)
{	alert("Could not perform this action - if you have popup\nblockers enabled, please disable them for this site.");
}
}

function check_spelling( e_id )
{
	if( SOURCE_MODE[ e_id ] ) {					// actions cannot be preformed while viewing source code
		source_error();							// tell user
		return;									// abort action
	}

	var editor = get_editor_object( e_id );
	var args = new Array();
	args[ "editor" ] = editor;
	args[ "editorID" ] = e_id;
try
{
	var newHtml = window.showModalDialog(	"/include/html/spelling.html",
						args,
						"dialogHeight: 150px; dialogWidth: 220px; dialogTop:   300px; "+
						"dialogLeft: 300px; center: No; help: No; resizable: No; status: No;" );

	if( newHtml == "" || newHtml == null )
	{
		return;
	}

	editor.documentHtml = newHtml;
	editor.baseURL      = "http://" + location.host + "/";

	navigate_words( e_id, editor );
}catch(e)
{	alert("Could not perform this action - if you have popup\nblockers enabled, please disable them for this site.");
}
}

function replace_html_chars( text )
{
	var s = /(<span id=__special>)(.)(<\/span>)/gi;
	return text.replace( s, function (match, span, char, close) { return span + get_html_char( char ) + close } );
}

function get_html_char( c )
{
	switch( String(String(c).charCodeAt(0)) )
	{
		case "8224" : return "&dagger;"; break;
		case "8225" : return "&Dagger;"; break;
		case "8240" : return "&permil;"; break;
		case "9824" : return "&spades;"; break;
		case "9827" : return "&clubs;"; break;
		case "9829" : return "&hearts;"; break;
		case "9830" : return "&diams;"; break;
		case "8592" : return "&larr;"; break;
		case "8593" : return "&uarr;"; break;
		case "8594" : return "&rarr;"; break;
		case "8595" : return "&darr;"; break;
		case "8482" : return "&trade;"; break;
		case "161" : return "&iexcl;"; break;
		case "162" : return "&cent;"; break;
		case "163" : return "&pound;"; break;
		case "164" : return "&curren;"; break;
		case "165" : return "&yen;"; break;
		case "167" : return "&sect;"; break;
		case "168" : return "&uml;"; break;
		case "169" : return "&copy;"; break;
		case "170" : return "&ordf;"; break;
		case "171" : return "&laquo;"; break;
		case "187" : return "&raquo;"; break;
		case "174" : return "&reg;"; break;
		case "176" : return "&deg;"; break;
		case "177" : return "&plusmn;"; break;
		case "185" : return "&sup1;"; break;
		case "178" : return "&sup2;"; break;
		case "179" : return "&sup3;"; break;
		case "180" : return "&acute;"; break;
		case "181" : return "&micro;"; break;
		case "182" : return "&para;"; break;
		case "183" : return "&middot;"; break;
		case "184" : return "&cedil;"; break;
		case "186" : return "&ordm;"; break;
		case "172" : return "&not;"; break;
		case "188" : return "&frac14;"; break;
		case "189" : return "&frac12;"; break;
		case "190" : return "&frac34;"; break;
		case "191" : return "&iquest;"; break;
		case "247" : return "&divide;"; break;
		case "215" : return "&times;"; break;
		case "915" : return "&Gamma;"; break;
		case "916" : return "&Delta;"; break;
		case "920" : return "&Theta;"; break;
		case "923" : return "&Lambda;"; break;
		case "926" : return "&Xi;"; break;
		case "928" : return "&Pi;"; break;
		case "931" : return "&Sigma;"; break;
		case "934" : return "&Phi;"; break;
		case "936" : return "&Psi;"; break;
		case "937" : return "&Omega;"; break;
		case "945" : return "&alpha;"; break;
		case "946" : return "&beta;"; break;
		case "947" : return "&gamma;"; break;
		case "948" : return "&delta;"; break;
		case "949" : return "&epsilon;"; break;
		case "950" : return "&zeta;"; break;
		case "951" : return "&eta;"; break;
		case "952" : return "&theta;"; break;
		case "953" : return "&iota;"; break;
		case "954" : return "&kappa;"; break;
		case "955" : return "&lambda;"; break;
		case "956" : return "&mu;"; break;
		case "957" : return "&nu;"; break;
		case "958" : return "&xi;"; break;
		case "959" : return "&omicron;"; break;
		case "960" : return "&pi;"; break;
		case "961" : return "&rho;"; break;
		case "962" : return "&sigmaf;"; break;
		case "963" : return "&sigma;"; break;
		case "964" : return "&tau;"; break;
		case "965" : return "&upsilon;"; break;
		case "966" : return "&phi;"; break;
		case "967" : return "&chi;"; break;
		case "968" : return "&psi;"; break;
		case "969" : return "&omega;"; break;
		case "8364" : return "&euro;"; break;
		case "8596" : return "&harr;"; break;
		case "8592" : return "&larr;"; break;
		case "8593" : return "&uarr;"; break;
		case "8594" : return "&rarr;"; break;
		case "8595" : return "&darr;"; break;
		case "8804" : return "&le;"; break;
		case "8734" : return "&infin;"; break;
		case "8805" : return "&ge;"; break;
		case "8706" : return "&part;"; break;
		case "8729" : return "&#8729;"; break;
		case "8800" : return "&ne;"; break;
		case "8801" : return "&equiv;"; break;
		case "8776" : return "&asymp;"; break;
		case "8745" : return "&cap;"; break;
		case "8719" : return "&prod;"; break;
		case "8730" : return "&radic;"; break;
		case "8721" : return "&sum;"; break;
		case "8747" : return "&int;"; break;
	}
	return c;
}

