//<script language="javascript">
/*=============================================================================
 WebSolvers Framework Library
 Copyright 2003, WebSolvers, Inc., All Rights Reserved.                

 Library DHTML
 Cross-Browser/Platform Rich-text editor
 
 Revision History:
 5-30-03 Revamped code to new specifications

 Public Functions:

=============================================================================

 The WebSolvers Framework Library may be used and/or modified by anyone owning
 the original work as it was incorporated into an original development project
 so long as this copyright notice and the comments above remain intact.  

 By using this code you agree to indemnify WebSolvers, Inc. from any liability
 that might arise from its use.

 This code may not be sold exclusively or as a part of other code without prior 
 written consent and is expressly forbidden.

 Obtain permission before redistributing this software over the Internet or 
 in any other medium. In all cases the copyright and header must remain intact. 
============================================================================= */

var LRTE_Stack = new Array();

function procRTETag(elem) {
	if(!dhtml.IE || dhtml.mac)
		return elem;

  //need to delay the processing until the element is rendered.
  if(elem.offsetWidth == 0 || elem.offsetHeight == 0) {
    LRTE_Stack.push(elem);
    window.setTimeout("procRTETag(LRTE_Stack.pop());",1);
    return elem;
  }

	var ep = elem.getParent();
	var strName = elem.getAttribute("name");
	var strTxtName = elem.getAttribute("tname");
	var intWidth = parseInt(elem.getAttribute("width"));
	var intHeight = parseInt(elem.getAttribute("height"));
	var strValue = elem.getAttribute("value");
	var strText = elem.getAttribute("text");
	var src = elem.getAttribute("src");
	var imageSrc = elem.getAttribute("imagesrc");
	var fileSrc = elem.getAttribute("filesrc");
	var emoteSrc = elem.getAttribute("emotesrc");
	var skin = elem.getAttribute("skin");
	var tbhide = elem.getAttribute("tbhide");
	var classes = elem.getAttribute("classes");
	var norel = elem.getAttribute("norel");
	var styles = elem.getAttribute("styles");
	var stylesheet = elem.getAttribute("stylesheet");

	var etext = elem;
//	etext = ((etext.length) ? etext[0] : null );
	var efrm = etext.form;

	var frag = document.newFragment();
	var fh = document.newElement('INPUT', 'hidden');
	var fh2 = document.newElement('INPUT', 'hidden');
	var iframe = document.newElement('IFRAME', strName);

	iframe.scrolling="no";
	iframe.frameBorder=0;
	iframe.marginWidth=0;
	iframe.marginHeight=0;
	iframe.style.border="none";
	iframe.style.padding="0px";
	iframe.style.margin="0px";
	
	if(!strTxtName)
		strTxtName = 'txt' + strName;
	
	frag.appendElem(fh);
	frag.appendElem(fh2);
	frag.appendElem(iframe);
	
	if(etext) {
		fh.name = etext.name;
		fh.value = etext.value;
		iframe.style.width = etext.offsetWidth;
		iframe.style.height = etext.offsetHeight;
	} else {
		fh.name = "HTML"
		fh.value = ""
	}

	tbhide = ((tbhide) ? tbhide : "");
		
	if(tbhide.substring(0,1) == ",") {
		tbhide = tbhide.substring(1);
	}
	
	if(strName)	fh.name = strName;
	if(strValue) fh.value = strValue;
	fh2.name = ((strTxtName) ? strTxtName : fh.name + '_TEXT');
	if(strText) fh2.value = strText;

	iframe.id = 'rte_' + fh.name;
	iframe.name = 'rte_' + fh.name;
	
	if(intWidth) iframe.style.width = intWidth + 'px';
	if(intHeight) iframe.style.height = intHeight + 'px';
	iframe.src = ((src) ? src : dhtml.sharedPath + (dhtml.compMode ? '' : 'DHTML/') + 'Base_RTE.html');
	iframe.skin = ((skin) ? skin : "");
	iframe.tbhide = tbhide;
	iframe.classes = ((classes) ? classes : '');
	iframe.styles = ((styles) ? styles : '');
	iframe.stylesheet = ((stylesheet) ? stylesheet : '');

	iframe.imageSrc = ((imageSrc) ? imageSrc : '');
	iframe.fileSrc = ((fileSrc) ? fileSrc : '');
	iframe.emoteSrc = ((emoteSrc) ? emoteSrc : '');
	iframe.norel = norel;

	iframe.onblur = rte_onblur;
	iframe.onfocus = rte_onfocus;

	iframe.field = fh;	
	iframe.txtField = fh2
	fh.rte = iframe;
	fh.txt = fh2;
	fh2.rte = iframe;
	fh2.html = fh;

	ep.replaceElem(elem, frag);

	if(!fh.form)
		efrm.appendChild(fh);
	if(!fh2.form)
		efrm.appendChild(fh2);
		
	if(!fh.form.elements[fh.name])
		fh.form.elements[fh.name] = fh;
	if(!fh2.form.elements[fh2.name])
		fh2.form.elements[fh2.name] = fh2;

	window.setTimeout('rte_checkInit("' + iframe.name + '");', 100);
	
	return frag;
}

function rte_checkInit(strName) {
	var iFrm = document.getElementById(strName);
	var iWin = null;
	var i = 0, tb = null, styles = null, classes = null, stylesheet = '';

	if(!iFrm) {
		window.setTimeout('rte_checkInit("' + strName + '");', 100);
		return;
	}
	
	iWin = iFrm.contentWindow;
	if(!iWin && document.frames)
			iWin = document.frames[strName];
			
	if(!iWin && window.frames)
		iWin = window.frames[strName];
		
	if(!iFrm || !iWin || !iWin.g_state) {
		window.setTimeout('rte_checkInit("' + strName + '");', 100);
		return;
	}
	
	if(!iWin.rteLoaded) {
		window.setTimeout('rte_checkInit("' + strName + '");', 100);
		return;
	}
	
	if(!iFrm.contentWindow)
		iFrm.contentWindow = iWin;
		
	if(!iWin.parentFrame)
		iWin.parentFrame = iFrm;
		
	if(!iWin.parentWindow)
		iWin.parentWindow = window;

	tb = iFrm.tbhide.split(',');
	for(i = 0; i < tb.length; i++) {
		iWin.setToolbar("tb" + tb[i], false);
	}
	
	if(iFrm.classes.length)
		classes = iFrm.classes.split(',');
	else
		classes = "";
	
	if(iFrm.styles.length)
		styles = iFrm.styles.split(',');
	else
		styles = "";
	
	if(iFrm.stylesheet.substring(1,0) == "~")
		stylesheet = document.styleSheets[iFrm.stylesheet.substring(1)].cssText;
	else
		stylesheet = iFrm.stylesheet;
	
	if(stylesheet && stylesheet.length && classes.length && styles.length)
		iWin.setStyles((stylesheet ? stylesheet : ''), classes, styles);
	else if(stylesheet && stylesheet.length) {
		iWin.setStyles(stylesheet, classes, styles);
		iWin.setToolbar("tbcss", false);
	} else
		iWin.setToolbar("tbcss", false);
		
	if(!stylesheet) {
		stylesheet = document.getCSS();
		iWin.setStyles(stylesheet);
	} 

  var $bar7 = 3;

	if(iFrm.imageSrc && iFrm.imageSrc.length)
		iWin.setImageSource(iFrm.imageSrc);
	else {
		iWin.setToolbar("tbuplimage", false);
		$bar7--;
  }

	if(iFrm.fileSrc && iFrm.fileSrc.length)
		iWin.setFileSource(iFrm.fileSrc);
	else {
		iWin.setToolbar("tbuplfile", false);
		$bar7--;
	}

	if(iFrm.emoteSrc && iFrm.emoteSrc.length)
		iWin.setEmoteSource(iFrm.emoteSrc);
	else {
		iWin.setToolbar("tbuplemoticon", false);
		$bar7--;
	}

  if(iFrm.norel) {
    iWin.setRelFix(0);
  }
	
	if($bar7 <= 0) {
		iWin.setToolbar("tbbar7", false);
	}

	iWin.setSkin(iFrm.skin);

	iWin.rteActivate = false;
	iWin.setHTML(iFrm.field.value);
	iFrm.txtField.value = iWin.getText();
	iWin.rteActivate = true;
}

function rte_onblur() {
	this.contentWindow.rteActivate = false;
	this.contentWindow.rteActive = false;
	this.field.value = this.contentWindow.getHTML();
	this.txtField.value = this.contentWindow.getText();
	this.contentWindow.rteActivate = true;
}

function rte_onfocus() {
	this.contentWindow.rteActive = true;
}

if(window.dhtml) {
	dhtml.addTag('RTE', procRTETag);
}
