/*
 * FCKeditor - The text editor for internet
 * Copyright (C) 2003 Frederico Caldeira Knabben
 *
 * Licensed under the terms of the GNU Lesser General Public License
 * (http://www.opensource.org/licenses/lgpl-license.php)
 *
 * For further information go to http://www.fredck.com/FCKeditor/ 
 * or contact fckeditor@fredck.com.
 *
 * fckeditor.js: Inserts a FCKeditor instance in a HTML page.
 *
 * Authors:
 *   Frederico Caldeira Knabben (fckeditor@fredck.com)
 */

var FCKeditorBasePath = '/JSLib/FCKeditor/' ;

var isOpera = (navigator.userAgent.indexOf("Opera") > 0) ;

// Check if the browser is compatible with the Editor:
//		- Internet Explorer 5 or above
var isCompatible = (!isOpera && navigator.appName == 'Microsoft Internet Explorer') ;
if (isCompatible)
{
	var browserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
	isCompatible = (browserVersion >= 5) ;
}

function DoBr(obj) {
    obj.value += "<br>";
    obj.focus();
}

function DoEmphasis(obj, type) {
    var str, txt;
    txt = prompt("Enter " + type + " text");
    switch(type) {
    case 'bold':      str = "<b>" + txt + "</b>"; break;
    case 'italic':    str = "<i>" + txt + "</i>"; break;
    case 'underline': str = "<u>" + txt + "</u>"; break;
    case 'center': str = "<center>" + txt + "</center>"; break;
    }
    obj.value += str;
    obj.focus();
}

// FCKeditor class
function FCKeditor(instanceName, width, height, toolbarSet, value)
{
	this.InstanceName	= instanceName ;
	this.Width		= width			|| '100%' ;
	this.Height		= height		|| '200' ;
	this.ToolbarSet		= toolbarSet ;
	this.Value		= value			|| '' ;

	this.CanUpload		= null ;	// true / false
	this.CanBrowse		= null ;	// true / false
}

FCKeditor.prototype.Create = function()
{
	if (isCompatible)
	{
		var sLink = FCKeditorBasePath + 'fckeditor.html?FieldName=' + this.InstanceName ;
		if (this.ToolbarSet) sLink += '&Toolbar=' + this.ToolbarSet ;
		if (this.CanUpload != null) sLink += '&Upload=' + (this.CanUpload ? "true" : "false") ;
		if (this.CanBrowse != null) sLink += '&Browse=' + (this.CanBrowse ? "true" : "false") ;
		document.write('<IFRAME src="' + sLink + '" width="' + this.Width + '" height="' + this.Height + '" frameborder="no" scrolling="no"></IFRAME>') ;
		document.write('<INPUT type="hidden" name="' + this.InstanceName + '" value="' +  HTMLEncode( this.Value ) + '">') ;
	}
	else
	{
		var sWidth  = this.Width.toString().indexOf('%')  > 0 ? this.Width  : this.Width  + 'px' ;
		var sHeight = this.Height.toString().indexOf('%') > 0 ? this.Height : this.Height + 'px' ;
		var dStr;
		var tbPath = '/JSLib/FCKeditor/images/toolbar';
		dStr  = '<a href="#" ';
		dStr += 'onClick=\'DoEmphasis(' + this.InstanceName;
		dStr += ', "bold")\'><img ';
		dStr += 'border=0 ';
		dStr += 'src="' + tbPath + '/button.bold.gif">';
		dStr += '</a>\n';
		dStr += '<a href="#" ';
		dStr += 'onClick=\'DoEmphasis(' + this.InstanceName;
		dStr += ', "italic")\'><img ';
		dStr += 'border=0 ';
		dStr += 'src="' + tbPath + '/button.italic.gif">' ;
		dStr += '</a>\n';
		dStr += '<a href="#" ';
		dStr += 'onClick=\'DoEmphasis(' + this.InstanceName;
		dStr += '\, "underline")\'><img ';
		dStr += 'border=0 ';
		dStr += 'src="' + tbPath + '/button.underline.gif">';
		dStr += '</a>\n';
		dStr += '<a href="#" ';
		dStr += 'onClick=\'DoEmphasis(' + this.InstanceName;
		dStr += '\, "center")\'><img ';
		dStr += 'border=0 ';
		dStr += 'src="' + tbPath + '/button.justifycenter.gif">';
		dStr += '</a>\n';
		dStr += '<a href="#" ';
		dStr += 'onClick=\'DoBr(' + this.InstanceName + ')\'><img ';
		dStr += 'border=0 ';
		dStr += 'src="' + tbPath + '/button.return.gif">';
		dStr += '</a><br>\n';

		dStr += '<TEXTAREA name="' + this.InstanceName;
		dStr += '" cols=' + this.width + ' rows=' + this.height;
		dStr += ' wrap="virtual">';
		dStr += HTMLEncode( this.Value ) + '<\/TEXTAREA>';

		document.write(dStr);
	}
}

function HTMLEncode(text)
{
	text = text.replace(/"/g, "&quot;") ;
	text = text.replace(/</g, "&lt;") ;
	text = text.replace(/>/g, "&gt;") ;
	text = text.replace(/'/g, "&#146;") ;

	return text ;
}
