//var PanelContentObjs = new Array();
var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined'); 
function XmlHttpObject(node, pid, displayType, byPassCache){
	var display = "";
	if(displayType != null) display = displayType; 
	var xmlhttp= false;
	var path = node;
	var response = false;
	var panelId = pid;
	
	var cache;
	if(!byPassCache){
		if(cache=pathLoaded()){
			cache.dispatch();
		}
	} else {
		cache = false;
	}
	if(!!moz) xmlhttp = new XMLHttpRequest();
	if(!!ie){
		try {
  			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 		} catch (e) {
  			try {
   				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  			} catch (E) {
   				xmlhttp = false;
  			}
		}
	}
	if(!cache && !!pid){
		xmlhttp.onreadystatechange= function (){
			if (xmlhttp.readyState==4) {
				if(!!moz){
					var resp = new Response(xmlhttp.responseXML, panelId, path, display);
				} else {
					var resp = new Response(xmlhttp.responseXML, panelId, path, display);
				}
				resp.dispatch();
			}
		}
	}
	
	function pathLoaded(){
		if( panelContentObjs != null ) {
			for( i in panelContentObjs ){
				if(panelContentObjs[i].contentPath == path) return panelContentObjs[i];
			}
		}
		return false;
	}
	
	function setResponseDom(strXml){
		this.response = strXml;
	}
	
	this.getResponseDom = function(){
		return this.response?this.response:false;
	}
	
	this.sendBack = function(xml){
		
		path = path.replace("node", "postnode");
		xmlhttp.open("POST", path,true);
		xmlhttp.send('<content><![CDATA['+xml+']]></content>');
	}
	
	this.postComment = function(xml){
		
		path = path.replace("/node/", "/postnode/postComment/");
		xmlhttp.open("POST", path,true);
		xmlhttp.send(xml);
	}
	
	this.showComments = function(path){
		path = path.replace('/node/', '/node/comments/');
		xmlhttp.open("POST", path,true);
		xmlhttp.send(null);
	}

	this.connect = function(){
	 	xmlhttp.open("GET", path,true);
		xmlhttp.send(null);
	}
	
	this.designateToPanel = function(pid){
		panelId = pid;
	}
}

/*  this becomes the base for the panel object.  */
function Response(respXml, pid, path, displayType){
	var panelId = pid;
	this.id = pid;
	this.displayType;
	if(respXml != null){
		this.responseDom = respXml.documentElement;
		if(!!this.responseDom.attributes){
			for(i=0; i<this.responseDom.attributes.length; i++){
				// pull all the attributes into this object ;-)
				eval("this."+this.responseDom.attributes[i].name+"='"+this.responseDom.attributes[i].value+"';");
				//div.innerHTML += tree.attributes[i].name + '  ' + tree.attributes[i].value + '<br>';
			}
		}
		props = this.responseDom.getElementsByTagName('properties')[0];
		if(!!props){
			for(i=0; i<props.getElementsByTagName('property').length; i++){
				// pull all the attributes into this object ;-)
				if(props.getElementsByTagName('property')[i].getAttribute('name') == "elementPath"){
					eval("this."+props.getElementsByTagName('property')[i].getAttribute('name')+"='"+props.getElementsByTagName('property')[i].firstChild.nodeValue+"';");
				}
			//div.innerHTML += tree.attributes[i].name + '  ' + tree.attributes[i].value + '<br>';
			}
		}
	}
	
	if(displayType != null && displayType != ""){
		this.displayType = displayType;
	}
	this.contentPath = path;
	this.cdata=null;
	this.admMenu = adminMenu(this.contentPath);
	addPanelContentObject(this);
	// each panel has a unique path that - surprise - is also an xpath.
	
	
	this.edit=function(){
		if(!this.editor){
			this.editor= new XhtmlEditor(panelId, this.contentPath);
		}
		this.editor.initRTE('/nebula/ide/rte/images/', '/nebula/ide/rte/', '', false);
		this.editor.writeRichText('rte1', this.cdata, "100%", 200, true, false);
		document.getElementById(panelId).innerHTML = "";
		document.getElementById(panelId).appendChild(this.editor.div);
		this.editor.enableDesignMode('rte1', this.cdata, false);
	}
	
	
	this.dispatch = function(){
		var typeNode;
		if(!!moz){
			var contentNodes = this.responseDom.getElementsByTagName("content");
			typeNode = contentNodes[0].childNodes[1];
		}
		if(!!ie){
			typeNode = this.responseDom.selectSingleNode("//content").firstChild;
		}
		if(!!typeNode.attributes){
			for(i=0; i<typeNode.attributes.length; i++){
				// pull all the attributes into this object ;-)
				eval("this."+typeNode.attributes[i].name+"='"+typeNode.attributes[i].value+"';");
			}
		}
		
		// grab the old html 
		//tmphtml = document.getElementById(pid).innerHTML;
//		// clear the html from the div
		//document.getElementById(pid).innerHTML = "";
		// figure out what to display.
		switch( typeNode.nodeName ){
			// we queried a web mod
			case "BLiveWebMod":
			  // this it the comment mod
			  if(typeNode.getAttribute("implClassName") == "ViewCommentsWebMod"){
				  loader.loadMessage('loading comments for ' + this.elementPath);
				  this.CreateCommentsPanel(typeNode);
				  loader.sparkle();
				// show the comments :-)  
			  }
			break;
			case "ISElement":
				if( typeNode.getAttribute("implClassName") == "ISObjectFolder" ){
					var div = document.createElement('div');
					var tree="";
					if(this.displayType){
						switch(this.displayType){
							case "":
								tree = traverse(typeNode, div);
							break;
							case "blogDir":
								tree = traverseBlog(typeNode, div);
							break;
							default:
							break;
						}
					}else{
						tree = traverse(typeNode, div);
					}
					if(!!moz){
						document.getElementById(panelId).innerHTML = tree.innerHTML;
					}
					if(!!ie){
						document.getElementById(panelId).innerHTML = tree.outerHTML;
					}
				}else if( typeNode.getAttribute("implClassName") == "ISDiscussion" ){
					document.getElementById(pid).innerHTML = "";
					if(this.admMenu.innerHTML=="") this.admMenu = adminMenu(this.contentPath);
					document.getElementById(pid).appendChild(this.admMenu);

					var div = document.createElement('div');
					if(!!moz){
						this.cdata = typeNode.getElementsByTagName("content")[0].firstChild.nodeValue;
						document.getElementById(panelId).innerHTML += this.cdata;
					}
				
					if(!!ie){
						this.cdata = typeNode.selectSingleNode("content").text;
						document.getElementById(panelId).innerHTML += this.cdata;
					}

					document.getElementById(panelId).appendChild(this.createCommentsLink());
				} else {
					document.getElementById(pid).innerHTML = "";
					if(this.admMenu.innerHTML=="") this.admMenu = adminMenu(this.contentPath);
					document.getElementById(pid).appendChild(this.admMenu);
					if(!!moz){
						this.cdata = typeNode.getElementsByTagName("content")[0].firstChild.nodeValue;
						document.getElementById(panelId).innerHTML += this.cdata;
					}
				
					if(!!ie){
						this.cdata = typeNode.selectSingleNode("content").text;
						document.getElementById(panelId).innerHTML += this.cdata;
					}
				}
			break;
			case "BLiveWebScript":
				if(!!moz){
					document.getElementById(pid).innerHTML = typeNode.getElementsByTagName("content")[0].firstChild.nodeValue;
				}
				if(!!ie){
					document.getElementById(pid).innerHTML = typeNode.selectSingleNode("content").text;
				}
			break;
			case "BLiveObjectInfo":
				var div = document.createElement('div');
				var tree;
				if( this.displayType != null ){
					switch(this.displayType){
						case "":
							tree = traverse(typeNode, div);
						break;
						case "blogDir":
							tree = traverseBlog(typeNode, div);
						break;
					}
				} else {
					tree = traverse(typeNode, div);
				}
				if(!!moz){
					document.getElementById(pid).innerHTML = tree.innerHTML;
				}
				if(!!ie){
					document.getElementById(pid).appendChild(tree);
				}

			break;
		}
	}
	
	this.createCommentsLink = function(){
		var linkDiv = document.createElement('div');
		linkDiv.id = "commentLink";
		try {
		  entries = this.responseDom.getElementsByTagName("entries")[0].getElementsByTagName("BLiveObjectInfo");
		} catch(e){
		  entries = 0;	
		}
		linkDiv.innerHTML = '<div style="cursor:pointer" onclick="ShowComments(\''+this.contentPath+'\')">'+entries.length+' comments - add Comment</div>';
		
		return linkDiv;
	}
	
	this.CreateCommentForm = function(){
		var div = document.createElement('div');
		div.id = 'commentContentsWrapper';
		var linkDiv = this.createCommentsLink();
		div.appendChild(linkDiv);
		var comDiv = document.createElement('div');
		comDiv.id='commentPanel';
		comDiv.style.visibility='visible';
		var text = document.createElement('textarea');
		text.id = "commentContent";
		comDiv.appendChild(text);
        var button = document.createElement('input');
		button.type="button";
		//button.onclick = "SendBackComment('"+this.contentPath+"')";
		button.setAttribute("onClick", "SendComment('"+this.contentPath+"')");
		button.value="post";
		comDiv.appendChild(button)
		div.appendChild(comDiv);
		return div;
	}
	
	this.CreateCommentsPanel = function(typeNode){
	    var index = 0;
		if(typeNode.getElementsByTagName('comments').length >= 1){
			for( var i=0; i<typeNode.getElementsByTagName('comments')[0].childNodes.length; i++ ){
		    	try {
	    			if(typeNode.getElementsByTagName('comments')[0].childNodes[i].nodeName == "ISElement" &&
						typeNode.getElementsByTagName('comments')[0].childNodes[i].getAttribute("implClassName") == 'ISDocument'){
						// this is a comment
						index++;
						var div = document.createElement('div');
						div.className = (index % 2) ? "commentOdd" : "commentEven";
						div.innerHTML = typeNode.getElementsByTagName('comments')[0].childNodes[i].getElementsByTagName('content')[0].firstChild.nodeValue;
						alert('this.id '+ this.id);
						document.getElementById(this.id).appendChild(div);
			 		}
				}catch(e){alert(e.description)}
			}
		}
		try {
			document.getElementById(this.id).appendChild(this.CreateCommentForm());
		} catch(e){alert(e.description)}
	}
	
	function traverseDir(tree, div){
		if(tree.nodeName == "BLiveObjectInfo" || tree.nodeName == "entry"){
		// this is an object info node.  all the stuff and things 
		// about this object is kept here.
			var div2 = document.createElement('div');
			div2.setAttribute('onClick', "SwapContent('main', '/node"+tree.getElementsByTagName("Path")[0].firstChild.nodeValue+"');");
			div2.className=tree.getAttribute('implClassName');
			div2.innerHTML = tree.getElementsByTagName("Description")[0].firstChild.nodeValue;
			div.appendChild(div2);
			div.style.paddingLeft="15px";
			div.style.visibility="visible";
			div.style.cursor="pointer";

		}
		
		if(tree.hasChildNodes()) {
			for(var i=0; i<tree.childNodes.length; i++){
				//if( tree.childNodes[i].getAttribute("implClassName") == "ISDiscussion" ){
					var chlddiv = document.createElement('div');
    	        	traverseDir(tree.childNodes[i], chlddiv);
					div.appendChild(chlddiv);
				//}
			}
    	}
		return div;		
	}
	
	
	function traverseBlog(tree, div){
//		alert(tree.nodeName);
		if(tree.nodeName == "BLiveObjectInfo"){
			if(tree.getAttribute('implClassName') == 'isobjectfolder' || tree.getAttribute('implClassName') == 'isdiscussion'){
			// this is an object info node.  all the stuff and things 
			// about this object is kept here.
				var div2 = document.createElement('div');
				div2.setAttribute('onClick', "SwapContent('main', '/node"+tree.getElementsByTagName("Path")[0].firstChild.nodeValue+"');");
				div2.className=tree.getAttribute('implClassName');
				div2.innerHTML = tree.getElementsByTagName("Description")[0].firstChild.nodeValue;
				div.appendChild(div2);
				div.style.paddingLeft="10px";
				div.style.visibility="visible";
				div.style.cursor="pointer";

			}
		 }
		
			if(tree.hasChildNodes()) {
				for(var i=0; i<tree.childNodes.length; i++){
				//if( tree.childNodes[i].getAttribute("implClassName") == "ISDiscussion" ){
					var chlddiv = document.createElement('div');
    	        	traverseBlog(tree.childNodes[i], chlddiv);
					div.appendChild(chlddiv);
				//}
				}
    		}
		return div;		
	}

	function traverse(tree, div) {
		if(tree.nodeName == "BLiveObjectInfo"){
		// this is an object info node.  all the stuff and things 
		// about this object is kept here.
			var div2 = document.createElement('div');
			div2.setAttribute('onClick', "SwapContent('main', '/node"+tree.getElementsByTagName("Path")[0].firstChild.nodeValue+"');");
			div2.className=tree.getAttribute('implClassName');
			div2.innerHTML = tree.getElementsByTagName("Description")[0].firstChild.nodeValue + " by " + tree.getAttribute("authorNick") + ' @ ' + tree.getAttribute("created");
			div.appendChild(div2);
			div.style.paddingLeft="15px";
			div.style.visibility="visible";
			div.style.cursor="pointer";

		}
		
		if(tree.hasChildNodes()) {
			for(var i=0; i<tree.childNodes.length; i++){
				if( tree.childNodes[i].nodeName != "#text" && tree.childNodes[i].nodeName != "#document" ){
					var chlddiv = document.createElement('div');
   	        		traverse(tree.childNodes[i], chlddiv);
					if(chlddiv.innerHTML!=""){
						div.appendChild(chlddiv);
					}
				}
			}
    	}
		return div;
	}
}



function adminMenu(path){
	
	var admDiv = document.createElement('div');
	admDiv.style.position='absolute';
	admDiv.style.height='12px';
	
//	admDiv.style.visibility='hidden';
	var img = document.createElement('img');
	img.src='/nebula/popup/btn_edit.gif';
	if(!!ie){
		img.setAttribute("onclick", "this.parentElement.childNodes(2).setAttribute('onmouseover','this.style.visibility=\"visible\"');this.parentElement.childNodes(2).style.visibility='visible'");
	} else {
		img.setAttribute("onclick","this.nextSibling.setAttribute('onmouseover', \"this.style.visibility='visible'\");this.nextSibling.style.visibility='visible'");
	}
	admDiv.appendChild(img);
	
	var popupMenu = document.createElement('div');
	
	popupMenu.setAttribute('onmouseout', 'this.style.visibility=\'hidden\'');
	popupMenu.style.visibility = 'hidden';
	popupMenu.innerHTML = 
  '<div class="admMenu">'+
    '<div class="admMenuItem" onmouseover=\'this.className="admMenuItemHover"\' onmouseout=\'this.className="admMenuItem"\' onclick="SetEditable(\''+path+'\')\">Edit</div>'+
    '<div class="admMenuItem" onmouseover=\'this.className="admMenuItemHover"\' onmouseout=\'this.className="admMenuItem"\'>Popup Page</div>'+
    '<div class="admMenuItem" onmouseover=\'this.className="admMenuItemHover"\' onmouseout=\'this.className="admMenuItem"\'>Properties</div>'+
    '<div class="admMenuItem" onmouseover=\'this.className="admMenuItemHover"\' onmouseout=\'this.className="admMenuItem"\'>Delete</div>'+
  '</div>';
  
	admDiv.appendChild(popupMenu);
//	alert(admDiv.innerHTML);
	return admDiv;
}



function Loader(panel){
	this.parent = panel;
	this.div=false;
	
	this.loadMessage = function(msg){
		this.div = document.getElementById(this.parent);
		if(!!this.div){
			if(this.div.hasChildNodes()){
				this.div.innerHTML = "";
			}
			if(msg!=""){
				for(i=0; i<msg.length; i++){
					var span = document.createElement('span');
					span.id = 'ld_' + i;
					span.style.color="#CCCCCC";
					if(!!moz){
						span.appendChild(document.createTextNode(msg.charAt(i)));
					}else {
						span.innerText = msg.charAt(i);
					}
					this.div.appendChild(span);
				}
			}
//		document.getElementById(this.parent).appendChild(this.div);
		}
	}
	
	this.sparkle = function(){
		var parent = false;
		parent = document.getElementById(this.parent);
		if(!!parent){
	  		for( i=0; (document.getElementById('ld_'+i)||document.getElementById('ld_'+(i+=1))); i++ ){
    			id = 'ld_'+i;
    			try {
	  				setTimeout("document.getElementById(\""+id+"\").style.color=\"#E9C9BE\";", i*20);
	  				setTimeout("document.getElementById(\""+id+"\").style.color=\"#D18C78\";", i*35); 
				} catch(e){
	  				alert( e.description );
				}
			}
  		}
	}
	
}


function XhtmlEditor(panelId, path){
  this.div = document.createElement('div');
  
  

//init variables
	this.isRichText = false;
	//parent.setAttribute("command", "");
	this.rng;
	this.currentRTE;
	this.allRTEs = "";

	this.isIE;
	this.isGecko;
	this.isSafari;
	this.isKonqueror;

	this.imagesPath;
	this.includesPath;
	this.cssFile;
	this.generateXHTML;

	this.lang = "en";
	this.encoding = "iso-8859-1";


	this.initRTE = function(imgPath, incPath, css, genXHTML) {
	//set browser vars
		var ua = navigator.userAgent.toLowerCase();
		this.isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1)); 
		this.isGecko = (ua.indexOf("gecko") != -1);
		this.isSafari = (ua.indexOf("safari") != -1);
		this.isKonqueror = (ua.indexOf("konqueror") != -1);
	
		this.generateXHTML = genXHTML;
	
		//check to see if designMode mode is available
		//Safari/Konqueror think they are designMode capable even though they are not
		if (document.getElementById && document.designMode && !this.isSafari && !this.isKonqueror) {
			this.isRichText = true;
		}
	
		if (this.isIE) {
			document.onmouseover = this.raiseButton;
			document.onmouseout  = this.normalButton;
			document.onmousedown = this.lowerButton;
			document.onmouseup   = this.raiseButton;
		}
	
		//set paths vars
		this.imagesPath = imgPath;
		this.includesPath = incPath;
		this.cssFile = css;
	
//		if (this.isRichText) document.writeln('<style type="text/css">@import "' + this.includesPath + 'rte.css";</style>');
	
	//for testing standard textarea, uncomment the following line
	//isRichText = false;
	}

	this.writeRichText = function(rte, html, width, height, buttons, readOnly) {
		if (this.isRichText) {
		if (this.allRTEs.length > 0) this.allRTEs += ";";
		this.allRTEs += rte;
		
		if (this.readOnly) buttons = false;
		
		//adjust minimum table widths
		// where does 540 come from?  
		if (this.isIE) {
			if (buttons && (width < 540)) width = 540;
			var tablewidth = width;
		} else {
			if (buttons && (width < 540)) width = 540;
			var tablewidth = width + 4;  // whats with the moz +4 px fix?
		}
		tablewidth = "100%";

		// change to write to a buffer to wrap the panel html with
		rteHtml = "";
		rteHtml += ('<div class="rteDiv">');
		if (buttons == true) {
		/*	rteHtml += ('<table class="rteBack" cellpadding=2 cellspacing=0 id="Buttons1_' + rte + '" width="' + tablewidth + '">');
			rteHtml += ('	<tr>');
			rteHtml += ('		<td>');
			rteHtml += ('			<select id="formatblock_' + rte + '" onchange="selectFont(\'' + rte + '\', this.id);">');
			// change to pull in site styles.
			
			// take all this out maybe.  
			rteHtml += ('				<option value="">[Style]</option>');
			
			rteHtml += ('				<option value="<p>">Paragraph &lt;p&gt;</option>');
			rteHtml += ('				<option value="<h1>">Heading 1 &lt;h1&gt;</option>');
			rteHtml += ('				<option value="<h2>">Heading 2 &lt;h2&gt;</option>');
			rteHtml += ('				<option value="<h3>">Heading 3 &lt;h3&gt;</option>');
			rteHtml += ('				<option value="<h4>">Heading 4 &lt;h4&gt;</option>');
			rteHtml += ('				<option value="<h5>">Heading 5 &lt;h5&gt;</option>');
			rteHtml += ('				<option value="<h6>">Heading 6 &lt;h6&gt;</option>');
			rteHtml += ('				<option value="<address>">Address &lt;ADDR&gt;</option>');
			rteHtml += ('				<option value="<pre>">Formatted &lt;pre&gt;</option>');
			rteHtml += ('			</select>');
			rteHtml += ('		</td>');
			rteHtml += ('		<td>');
			rteHtml += ('			<select id="fontname_' + rte + '" onchange="selectFont(\'' + rte + '\', this.id)">');
			rteHtml += ('				<option value="Font" selected>[Font]</option>');
			rteHtml += ('				<option value="Arial, Helvetica, sans-serif">Arial</option>');
			rteHtml += ('				<option value="Courier New, Courier, mono">Courier New</option>');
			rteHtml += ('				<option value="Times New Roman, Times, serif">Times New Roman</option>');
			rteHtml += ('				<option value="Verdana, Arial, Helvetica, sans-serif">Verdana</option>');
			rteHtml += ('			</select>');
			rteHtml += ('		</td>');
			rteHtml += ('		<td>');
			rteHtml += ('			<select unselectable="on" id="fontsize_' + rte + '" onchange="selectFont(\'' + rte + '\', this.id);">');
			rteHtml += ('				<option value="Size">[Size]</option>');
			rteHtml += ('				<option value="1">1</option>');
			rteHtml += ('				<option value="2">2</option>');
			rteHtml += ('				<option value="3">3</option>');
			rteHtml += ('				<option value="4">4</option>');
			rteHtml += ('				<option value="5">5</option>');
			rteHtml += ('				<option value="6">6</option>');
			rteHtml += ('				<option value="7">7</option>');
			rteHtml += ('			</select>');
			rteHtml += ('		</td>');
			rteHtml += ('		<td width="100%">');
			rteHtml += ('		</td>');
			rteHtml += ('	</tr>');
			rteHtml += ('</table>');
			*/
			// buttons ...
			rteHtml += ('<table class="rteBack" cellpadding="0" cellspacing="0" id="Buttons2_' + rte + '" width="' + tablewidth + '">');
			rteHtml += ('	<tr>');
			rteHtml += ('		<td><img id="bold" class="rteImage" src="' + this.imagesPath + 'bold.gif" width="25" height="24" alt="Bold" title="Bold" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'bold\', \'\')"></td>');
			rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'italic.gif" width="25" height="24" alt="Italic" title="Italic" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'italic\', \'\')"></td>');
			rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'underline.gif" width="25" height="24" alt="Underline" title="Underline" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'underline\', \'\')"></td>');
			rteHtml += ('		<td><img class="rteVertSep" src="' + this.imagesPath + 'blackdot.gif" width="1" height="20" border="0" alt=""></td>');
			rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'left_just.gif" width="25" height="24" alt="Align Left" title="Align Left" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'justifyleft\', \'\')"></td>');
			rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'centre.gif" width="25" height="24" alt="Center" title="Center" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'justifycenter\', \'\')"></td>');
			rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'right_just.gif" width="25" height="24" alt="Align Right" title="Align Right" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'justifyright\', \'\')"></td>');
			rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'justifyfull.gif" width="25" height="24" alt="Justify Full" title="Justify Full" onclick="rteCommand(\'' + path + '\', \'' + rte + '\', \'justifyfull\', \'\')"></td>');
			rteHtml += ('		<td><img class="rteVertSep" src="' + this.imagesPath + 'blackdot.gif" width="1" height="20" border="0" alt=""></td>');
			rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'hr.gif" width="25" height="24" alt="Horizontal Rule" title="Horizontal Rule" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'inserthorizontalrule\', \'\')"></td>');
			rteHtml += ('		<td><img class="rteVertSep" src="' + this.imagesPath + 'blackdot.gif" width="1" height="20" border="0" alt=""></td>');
			rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'numbered_list.gif" width="25" height="24" alt="Ordered List" title="Ordered List" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'insertorderedlist\', \'\')"></td>');
			rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'list.gif" width="25" height="24" alt="Unordered List" title="Unordered List" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'insertunorderedlist\', \'\')"></td>');
			rteHtml += ('		<td><img class="rteVertSep" src="' + this.imagesPath + 'blackdot.gif" width="1" height="20" border="0" alt=""></td>');
			rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'outdent.gif" width="25" height="24" alt="Outdent" title="Outdent" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'outdent\', \'\')"></td>');
			rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'indent.gif" width="25" height="24" alt="Indent" title="Indent" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'indent\', \'\')"></td>');
			rteHtml += ('		<td><div id="forecolor_' + rte + '"><img class="rteImage" src="' + this.imagesPath + 'textcolor.gif" width="25" height="24" alt="Text Color" title="Text Color" onClick="dlgColorPalette(\'' + path + '\',\'' + rte + '\', \'forecolor\', \'\')"></div></td>');
			rteHtml += ('		<td><div id="hilitecolor_' + rte + '"><img class="rteImage" src="' + this.imagesPath + 'bgcolor.gif" width="25" height="24" alt="Background Color" title="Background Color" onClick="dlgColorPalette(\'' + path + '\',\'' + rte + '\', \'hilitecolor\', \'\')"></div></td>');
			rteHtml += ('		<td><img class="rteVertSep" src="' + this.imagesPath + 'blackdot.gif" width="1" height="20" border="0" alt=""></td>');
			rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'hyperlink.gif" width="25" height="24" alt="Insert Link" title="Insert Link" onClick="dlgInsertLink(\'' + path + '\',\'' + rte + '\', \'link\')"></td>');
			rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'image.gif" width="25" height="24" alt="Add Image" title="Add Image" onClick="addImage(\'' + path + '\',\'' + rte + '\')"></td>');
			rteHtml += ('		<td><div id="table_' + rte + '"><img class="rteImage" src="' + this.imagesPath + 'insert_table.gif" width="25" height="24" alt="Insert Table" title="Insert Table" onClick="dlgInsertTable(\'' + path + '\',\'' + rte + '\', \'table\', \'\')"></div></td>');
			if (this.isIE) {
				rteHtml += ('		<td><img class="rteImage" src="' + this.imagesPath + 'spellcheck.gif" width="25" height="24" alt="Spell Check" title="Spell Check" onClick="checkspell(\'' + path + '\')"></td>');
			}
	//		rteHtml += ('		<td><img class="rteVertSep" src="' + imagesPath + 'blackdot.gif" width="1" height="20" border="0" alt=""></td>');
	//		rteHtml += ('		<td><img class="rteImage" src="' + imagesPath + 'cut.gif" width="25" height="24" alt="Cut" title="Cut" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'cut\')"></td>');
	//		rteHtml += ('		<td><img class="rteImage" src="' + imagesPath + 'copy.gif" width="25" height="24" alt="Copy" title="Copy" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'copy\')"></td>');
	//		rteHtml += ('		<td><img class="rteImage" src="' + imagesPath + 'paste.gif" width="25" height="24" alt="Paste" title="Paste" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'paste\')"></td>');
	//		rteHtml += ('		<td><img class="rteVertSep" src="' + imagesPath + 'blackdot.gif" width="1" height="20" border="0" alt=""></td>');
	//		rteHtml += ('		<td><img class="rteImage" src="' + imagesPath + 'undo.gif" width="25" height="24" alt="Undo" title="Undo" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'undo\')"></td>');
	//		rteHtml += ('		<td><img class="rteImage" src="' + imagesPath + 'redo.gif" width="25" height="24" alt="Redo" title="Redo" onClick="rteCommand(\'' + path + '\', \'' + rte + '\', \'redo\')"></td>');
			rteHtml += ('		<td width="100%"></td>');
			rteHtml += ('	</tr>');
			rteHtml += ('</table>');
		}

		rteHtml += ('<iframe id="' + rte + '" name="' + rte + '" width="' + width + 'px" height="' + height + 'px" src="' + this.includesPath + 'blank.htm"></iframe>');
		if (!this.readOnly) rteHtml += ('<br /><input type="checkbox" id="chkSrc' + rte + '" onclick="toggleHTMLSrc(\'' + path + '\',\'' + rte + '\',' + buttons + ');" />&nbsp;<label for="chkSrc' + rte + '">View Source</label>');
		rteHtml += ('<input type="button" onclick="saveContent(\'' + panelId + '\',\'' + path + '\',\'' + rte + '\')" value="save changes" />');
		rteHtml += ('<iframe width="154" height="104" id="cp' + rte + '" src="' + this.includesPath + 'palette.htm" marginwidth="0" marginheight="0" scrolling="no" style="visibility:hidden; position: absolute;"></iframe>');
		rteHtml += ('<input type="hidden" id="hdn' + rte + '" name="' + rte + '" value="">');
		rteHtml += ('</div>');
		this.html = html;
		
		//document.getElementById('hdn' + rte).value = html;
		
	} else {
		if (!this.readOnly) {
			rteHtml += ('<textarea name="' + rte + '" id="' + rte + '" style="width: ' + width + 'px; height: ' + height + 'px;">' + html + '</textarea>');
		} else {
			rteHtml += ('<textarea name="' + rte + '" id="' + rte + '" style="width: ' + width + 'px; height: ' + height + 'px;" readonly>' + html + '</textarea>');
		}
	}
	this.div.innerHTML = rteHtml;
}

this.enableDesignMode=function(rte, html, readOnly) {
	var frameHtml = "<html id=\"" + rte + "\">\n";
	frameHtml += "<head>\n";
	//to reference your stylesheet, set href property below to your stylesheet path and uncomment
	if (this.cssFile.length > 0) {
		frameHtml += "<link media=\"all\" type=\"text/css\" href=\"" + this.cssFile + "\" rel=\"stylesheet\">\n";
	} else {
		
		frameHtml += "<style>\n";
		inherCss = "";
		if( !!moz ){
			for( i in document.styleSheets[0].cssRules){
				inherCss += document.styleSheets[0].cssRules[i].cssText;
			}
			frameHtml += inherCss;
		} else if(!!ie){
			inherCss = document.styleSheets(0).cssText;
			frameHtml += inherCss;
		}
		frameHtml += "</style>\n";
	}
	frameHtml += "</head>\n";
	frameHtml += "<body>\n";
	frameHtml += html + "\n";
	frameHtml += "</body>\n";
	frameHtml += "</html>";
	if (document.all) {
		var oRTE = document.frames[rte].document;
		oRTE.open();
		oRTE.write(frameHtml);
		oRTE.close();
		if (!this.readOnly) {
			oRTE.designMode = "On";
			document.frames[rte].document.attachEvent("onkeypress", function evt_ie_keypress(event) {this.ieKeyPress(event, rte);});
		}
	} else {
		try {
			if (!this.readOnly) document.getElementById(rte).contentDocument.designMode = "on";
			try {
				var oRTE = document.getElementById(rte).contentWindow.document;
				
				oRTE.open();
				oRTE.write(frameHtml);
				oRTE.close();
				if (this.isGecko && !this.readOnly) {
					//attach a keyboard handler for gecko browsers to make keyboard shortcuts work
					oRTE.addEventListener("keypress", this.geckoKeyPress, true);
				}
			} catch (e) {
				alert("Error preloading content.");
			}
		} catch (e) {
			alert(e.description);
			//gecko may take some time to enable design mode.
			//Keep looping until able to set.
			//if (this.isGecko) {
			//	setTimeout("enableDesignMode('" + rte + "', '" + html + "', " + readOnly + ");", 10);
			//} else {
				return false;
			//}
		}
	}
}

this.updateRTE=function(rte) {
	if (!this.isRichText) return;
	
	//check for readOnly mode
	var readOnly = false;
	if (document.all) {
		if (document.frames[rte].document.designMode != "On") readOnly = true;
	} else {
		if (document.getElementById(rte).contentDocument.designMode != "on") readOnly = true;
	}
	
	if (this.isRichText && !readOnly) {
		//if viewing source, switch back to design view
		if (document.getElementById("chkSrc" + rte).checked) document.getElementById("chkSrc" + rte).click();
		this.setHiddenVal(rte);
	}
}

this.setHiddenVal=function(rte) {
	//set hidden form field value for current rte
	var oHdnField = document.getElementById('hdn' + rte);
	
	//convert html output to xhtml (thanks Timothy Bell and Vyacheslav Smolin!)
	if (oHdnField.value == null) oHdnField.value = "";
	if (document.all) {
		if (this.generateXHTML) {
			oHdnField.value = get_xhtml(document.frames[rte].document.body, this.lang, this.encoding);
		} else {
			oHdnField.value = document.frames[rte].document.body.innerHTML;
		}
	} else {
		if (this.generateXHTML) {
			oHdnField.value = get_xhtml(document.getElementById(rte).contentWindow.document.body, this.lang, this.encoding);
		} else {
			oHdnField.value = document.getElementById(rte).contentWindow.document.body.innerHTML;
		}
	}
	
	//if there is no content (other than formatting) set value to nothing
	if (this.stripHTML(oHdnField.value.replace("&nbsp;", " ")) == "" &&
		oHdnField.value.toLowerCase().search("<hr") == -1 &&
		oHdnField.value.toLowerCase().search("<img") == -1) oHdnField.value = "";
}

this.updateRTEs = function() {
	var vRTEs = allRTEs.split(";");
	for (var i = 0; i < vRTEs.length; i++) {
		this.updateRTE(vRTEs[i]);
	}
}

this.rteCommand=function(rte, command, option) {
	//function to perform command
	var oRTE;
	if (document.all) {
		oRTE = document.frames[rte];
	} else {
		oRTE = document.getElementById(rte).contentWindow;
	}
	
	try {
		oRTE.focus();
	  	oRTE.document.execCommand(command, false, option);
		oRTE.focus();
	} catch (e) {
//		alert(e);
//		setTimeout("rteCommand('" + rte + "', '" + command + "', '" + option + "');", 10);
	}
}

this.toggleHTMLSrc=function(rte, buttons) {
	//contributed by Bob Hutzel (thanks Bob!)
	var oHdnField = document.getElementById('hdn' + rte);
	
	if (document.getElementById("chkSrc" + rte).checked) {
		//we are checking the box
		if (buttons) {
		//	this.showHideElement("Buttons1_" + rte, "hide");
			this.showHideElement("Buttons2_" + rte, "hide");
		}
		this.setHiddenVal(rte);
		if (document.all) {
			document.frames[rte].document.body.innerText = oHdnField.value;
		} else {
			var oRTE = document.getElementById(rte).contentWindow.document;
			var htmlSrc = oRTE.createTextNode(oHdnField.value);
			oRTE.body.innerHTML = "";
			oRTE.body.appendChild(htmlSrc);
		}
	} else {
		//we are unchecking the box
		if (buttons) {
//			this.showHideElement("Buttons1_" + rte, "show");
			this.showHideElement("Buttons2_" + rte, "show");
		}
		if (document.all) {
			//fix for IE
			var output = escape(document.frames[rte].document.body.innerText);
			output = output.replace("%3CP%3E%0D%0A%3CHR%3E", "%3CHR%3E");
			output = output.replace("%3CHR%3E%0D%0A%3C/P%3E", "%3CHR%3E");
			document.frames[rte].document.body.innerHTML = unescape(output);
		} else {
			var oRTE = document.getElementById(rte).contentWindow.document;
			var htmlSrc = oRTE.body.ownerDocument.createRange();
			htmlSrc.selectNodeContents(oRTE.body);
			oRTE.body.innerHTML = htmlSrc.toString();
		}
	}
}

this.dlgColorPalette=function(rte, command) {
	//function to display or hide color palettes
	this.setRange(rte);
	
	//get dialog position
	var oDialog = document.getElementById('cp' + rte);
	var buttonElement = document.getElementById(command + '_' + rte);
	var iLeftPos = this.getOffsetLeft(buttonElement);
	var iTopPos = this.getOffsetTop(buttonElement) + (buttonElement.offsetHeight + 4);
	oDialog.style.left = (iLeftPos) + "px";
	oDialog.style.top = (iTopPos) + "px";
	
	if ((command == parent.command) && (rte == this.currentRTE)) {
		//if current command dialog is currently open, close it
		if (oDialog.style.visibility == "hidden") {
			this.showHideElement(oDialog, 'show');
		} else {
			this.showHideElement(oDialog, 'hide');
		}
	} else {
		//if opening a new dialog, close all others
		var vRTEs = this.allRTEs.split(";");
		for (var i = 0; i < vRTEs.length; i++) {
			this.showHideElement('cp' + vRTEs[i], 'hide');
		}
		this.showHideElement(oDialog, 'show');
	}
	
	//save current values
	//parent.command = command;
	this.currentRTE = rte;
}

this.dlgInsertTable=function(rte, command) {
	//function to open/close insert table dialog
	//save current values
	parent.command = command;
	this.currentRTE = rte;
	this.InsertTable = this.popUpWin(this.includesPath + 'insert_table.htm', 'InsertTable', 360, 180, '');
}

this.dlgInsertLink = function(rte, command) {
	//function to open/close insert table dialog
	//save current values
	parent.command = command;
	this.currentRTE = rte;
	this.InsertLink = this.popUpWin(this.includesPath + 'insert_link.htm', 'InsertLink', 360, 180, '');
	
	//get currently highlighted text and set link text value
	this.setRange(rte);
	var linkText = '';
	if (this.isIE) {
		linkText = this.stripHTML(this.rng.htmlText);
	} else {
		linkText = this.stripHTML(this.rng.toString());
	}
	this.setLinkText(linkText);
}

this.setLinkText=function(linkText) {
	//set link text value in insert link dialog
	try {
		window.InsertLink.document.linkForm.linkText.value = linkText;
	} catch (e) {
		//may take some time to create dialog window.
		//Keep looping until able to set.
		setTimeout("setLinkText('" + linkText + "');", 10);
	}
}

this.popUpWin = function(url, win, width, height, options) {
	var leftPos = (screen.availWidth - width) / 2;
	var topPos = (screen.availHeight - height) / 2;
	options += 'width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos;
	return window.open(url, win, options);
}

this.setColor=function(color) {
	//function to set color
	var rte = this.currentRTE;
	var parentCommand = parent.command;
	
	if (document.all) {
		if (parentCommand == "hilitecolor") parentCommand = "backcolor";
		
		//retrieve selected range
		this.rng.select();
	}
	
	this.rteCommand(rte, parentCommand, color);
	this.showHideElement('cp' + rte, "hide");
}

this.addImage = function(rte) {
	//function to add image
	this.imagePath = prompt('Enter Image URL:', 'http://');				
	if ((this.imagePath != null) && (this.imagePath != "")) {
		this.rteCommand(rte, 'InsertImage', this.imagePath);
	}
}

// Ernst de Moor: Fix the amount of digging parents up, in case the RTE editor itself is displayed in a div.
// KJR 11/12/2004 Changed to position palette based on parent div, so palette will always appear in proper location regardless of nested divs
this.getOffsetTop=function(elm) {
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	var parents_up = 2; //the positioning div is 2 elements up the tree
	
	while(parents_up > 0) {
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
		parents_up--;
	}
	
	return mOffsetTop;
}

// Ernst de Moor: Fix the amount of digging parents up, in case the RTE editor itself is displayed in a div.
// KJR 11/12/2004 Changed to position palette based on parent div, so palette will always appear in proper location regardless of nested divs
this.getOffsetLeft=function(elm) {
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	var parents_up = 2;
	
	while(parents_up > 0) {
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
		parents_up--;
	}
	
	return mOffsetLeft;
}

this.selectFont=function(rte, selectname) {
	//function to handle font changes
	var idx = document.getElementById(selectname).selectedIndex;
	// First one is always a label
	if (idx != 0) {
		var selected = document.getElementById(selectname).options[idx].value;
		var cmd = selectname.replace('_' + rte, '');
		this.rteCommand(rte, cmd, selected);
		document.getElementById(selectname).selectedIndex = 0;
	}
}

this.insertHTML=function(html) {
	//function to add HTML -- thanks dannyuk1982
	var rte = this.currentRTE;
	
	var oRTE;
	if (document.all) {
		oRTE = document.frames[rte];
	} else {
		oRTE = document.getElementById(rte).contentWindow;
	}
	
	oRTE.focus();
	if (document.all) {
		var oRng = oRTE.document.selection.createRange();
		oRng.pasteHTML(html);
		oRng.collapse(false);
		oRng.select();
	} else {
		oRTE.document.execCommand('insertHTML', false, html);
	}
}

this.showHideElement=function(element, showHide) {
	//function to show or hide elements
	//element variable can be string or object
	if (document.getElementById(element)) {
		element = document.getElementById(element);
	}
	
	if (showHide == "show") {
		element.style.visibility = "visible";
	} else if (showHide == "hide") {
		element.style.visibility = "hidden";
	}
}

this.setRange=function(rte) {
	//function to store range of current selection
	var oRTE;
	if (document.all) {
		oRTE = document.frames[rte];
		var selection = oRTE.document.selection; 
		if (selection != null) this.rng = selection.createRange();
	} else {
		oRTE = document.getElementById(rte).contentWindow;
		var selection = oRTE.getSelection();
		this.rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
	}
	return this.rng;
}

this.stripHTML=function(oldString) {
	//function to strip all html
	var newString = oldString.replace(/(<([^>]+)>)/ig,"");
	
	//replace carriage returns and line feeds
   newString = newString.replace(/\r\n/g," ");
   newString = newString.replace(/\n/g," ");
   newString = newString.replace(/\r/g," ");
	
	//trim string
	newString = this.trim(newString);
	
	return newString;
}

this.trim=function(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") return inputString;
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
	
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length - 1, retValue.length);
	
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length - 1);
      ch = retValue.substring(retValue.length - 1, retValue.length);
   }
	
	// Note that there are two spaces in the string - look for multiple spaces within the string
   while (retValue.indexOf("  ") != -1) {
		// Again, there are two spaces in each of the strings
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ") + 1, retValue.length);
   }
   return retValue; // Return the trimmed string back to the user
}

//********************
//Gecko-Only Functions
//********************
function geckoKeyPress(evt) {
	//function to add bold, italic, and underline shortcut commands to gecko RTEs
	//contributed by Anti Veeranna (thanks Anti!)
	var rte = evt.target.id;
	
	if (evt.ctrlKey) {
		var key = String.fromCharCode(evt.charCode).toLowerCase();
		var cmd = '';
		switch (key) {
			case 'b': cmd = "bold"; break;
			case 'i': cmd = "italic"; break;
			case 'u': cmd = "underline"; break;
		};

		if (cmd) {
			rteCommand(rte, cmd, null);
			
			// stop the event bubble
			evt.preventDefault();
			evt.stopPropagation();
		}
 	}
}

//*****************
//IE-Only Functions
//*****************
this.ieKeyPress=function(evt, rte) {
	var key = (evt.which || evt.charCode || evt.keyCode);
	var stringKey = String.fromCharCode(key).toLowerCase();
	
//the following breaks list and indentation functionality in IE (don't use)
//	switch (key) {
//		case 13:
//			//insert <br> tag instead of <p>
//			//change the key pressed to null
//			evt.keyCode = 0;
//			
//			//insert <br> tag
//			currentRTE = rte;
//			insertHTML('<br>');
//			break;
//	};
}

this.checkspell=function() {
	//function to perform spell check
	try {
		var tmpis = new ActiveXObject("ieSpell.ieSpellExtension");
		tmpis.CheckAllLinkedDocuments(document);
	}
	catch(exception) {
		if(exception.number==-2146827859) {
			if (confirm("ieSpell not detected.  Click Ok to go to download page."))
				window.open("http://www.iespell.com/download.php","DownLoad");
		} else {
			alert("Error Loading ieSpell: Exception " + exception.number);
		}
	}
}

this.raiseButton = function(e) {
	var el = window.event.srcElement;
	
	className = el.className;
	if (className == 'rteImage' || className == 'rteImageLowered') {
		el.className = 'rteImageRaised';
	}
}

this.normalButton=function(e) {
	var el = window.event.srcElement;
	
	className = el.className;
	if (className == 'rteImageRaised' || className == 'rteImageLowered') {
		el.className = 'rteImage';
	}
}

this.lowerButton=function (e) {
	var el = window.event.srcElement;
	
	className = el.className;
	if (className == 'rteImage' || className == 'rteImageRaised') {
		el.className = 'rteImageLowered';
	}
}

  
  
  
}
