// DAO Page Editor
// Based partially on RTE - http://www.kevinroth.com/rte/demo.htm Written by Kevin Roth 

var imPth="images/cms/";var incPth="rte/";var cssFile="styles.css";

var fn = new Array ('bold','italic','underline','','left_just','centre','right_just','justifyfull','','hr','','numbered_list','list','','outdent','indent','textcolor','bgcolor','');
var fb = new Array("Bold","Italic","Underline","","Align Left","Centre","Align Right","Justify Full","","Horizontal Rule","","Ordered List","Unordered List","","Outdent","Indent","Text Colour","Background Colour","");
var fc = new Array("bold","italic","underline","","justifyleft","justifycenter","justifyright","justifyfull","","inserthorizontalrule","","insertorderedlist","insertunorderedlist","","outdent","indent","forecolor","hilitecolor","");
var da = document.all;var ua=navigator.userAgent.toLowerCase();var isRT=false;var rng;var isGk=(ua.indexOf("gecko")!=-1);var isSafari=(ua.indexOf("safari")!=-1);var isKonq=(ua.indexOf("konqueror")!=-1);
		
var u=location.href.split("?");
if (u[1]){ut=u[1].split(",");
	if (ut[0] == "edit"){
		if(document.getElementById && document.designMode && !isSafari && !isKonq){isRT=true;document.writeln('<style type="text/css">@import "/rte/rte.css";</style>');}
//		document.onload=writeedit;
		addListener(this, "load", function() { writeedit();});
	}
}

function addListener(element, event, listener, bubble) {
	if(element.addEventListener) {
		if(typeof(bubble) == "undefined") bubble = false;
		element.addEventListener(event, listener, bubble);
	} 
	else if(this.attachEvent) {
		element.attachEvent("on" + event, listener);
	}
}

function getObj(obj) {if (document.layers) {if (typeof obj == "string") {return document.layers[obj];}return obj;}
	if (document.all) {if (typeof obj == "string") {return document.all(obj);}return obj;}
	if (document.getElementById) {if (typeof obj == "string") {return document.getElementById(obj);}return obj;}return 0;}


function writeedit(){
	var b = document.getElementById('eventcontent');
	if(!b) {b = document.getElementById('content');}
	var width = document.body.scrollWidth-300; if(width<640)width=640;
	var height = document.body.scrollHeight-250;if(height<600)height=600;
	var ed = "<form name='editdoc' action='/cgi-bin/editfile.cgi' method='post' onsubmit=\"return submitForm();\"><input type='hidden' name='edits'><input type='hidden' name='pw'><input type='hidden' name='pn' value='"+document.location.href+"'>";
	if(isRT){
		ed += '<div class="rteDiv" style="padding-top:40px;"><table class="rteBack" cellpadding=0 cellspacing=0 id="Buttons1" width="'+((((ua.indexOf("msie")!=-1)&&(ua.indexOf("opera")==-1)&&(ua.indexOf("webtv")==-1)))?width:width+4)+'">'+
			'<tr><td><input type="image" class="rteImage" src="'+imPth+'save.gif"></td><td><select id="formatblock" onchange="selectFont(this);">' +
			'<option value="">[Style]</option><option value="<p>">Paragraph &lt;p&gt;</option>';
		for(i=1;i<6;i++){ed += '<option value="<h'+i+'>">Heading '+i+' &lt;h'+i+'&gt;</option>';}
		ed += '<option value="<address>">Address &lt;ADDR&gt;</option><option value="<pre>">Formatted &lt;pre&gt;</option></select></td>';
		var sep = '<td><img class="rteVertSep" src="'+imPth+'blackdot.gif" width="1" height="20" border="0" alt=""></td>\n';
		for(i=0;i<18;i++) {
			if (i > 15 && i < 18)	{ed += '<td><div id="'+fc[i]+'"><img class="rteImage" src="'+imPth+fn[i]+'.gif" width="25" height="24" alt="'+fb[i]+'" title="'+fb[i]+'" onClick="dlgColorPalette(\''+fc[i]+'\')"></div></td>\n';}
			else if (fc[i] != ""){ed += '<td><img id="'+fc[i]+'" class="rteImage" src="'+imPth+fn[i]+'.gif" width="25" height="24" alt="'+fb[i]+'" title="'+fb[i]+'" onClick="rCom(\''+fc[i]+'\', \'\')"></td>\n';}
			else {ed += sep;}
		}
		ed += '<td><img class="rteImage" src="'+imPth+'hyperlink.gif" width="25" height="24" alt="Insert Link" title="Insert Link" onClick="insertLink()"></td>' +
			'<td><img class="rteImage" src="'+imPth+'image.gif" width="25" height="24" alt="Add Image" title="Add Image" onClick="addImage()"></td>' +
			'<td><div id="table_doc"><img class="rteImage" src="'+imPth+'insert_table.gif" width="25" height="24" alt="Insert Table" title="Insert Table" onClick="dlgInsertTable(\'table\', \'\')"></div></td>' +
			'<td width="100%"></td></tr></table>' + 
			'<iframe id="doc" name="doc" width="'+width+'px" height="'+height+'px" src="/rte/blank.htm"></iframe>' +
			'<p><input type="checkbox" id="chkSrcdoc" onclick="toggleHTMLSrc();" />&nbsp;View Source</p>' +
			'<iframe width="154" height="104" id="cpdoc" src="/rte/palette.htm" marginwidth="0" marginheight="0" scrolling="no" style="visibility:hidden; position: absolute;"></iframe</div>';
	} 
	else {ed += '<textarea name="doc" id="doc" style="width:'+width+'px;height:'+height+'px;">'+b.innerHTML+'</textarea><br><input type="submit" value="Save">';}
	var y = document.createElement('div');
	y.innerHTML = ed+'</form>';
	b.parentNode.insertBefore(y,b);
	if (isRT){
		var date = new Date();var curDate = null; do { curDate = new Date(); }  while(curDate-date < 500);
		eDM(b.innerHTML);
	}
	b.parentNode.removeChild(b);
	rCom("useCSS",false);
}

function eDM(h){
	var f="<html id=\"doc\">\n<head>\n<link media=\"all\" type=\"text/css\" href=\""+cssFile+"\" rel=\"stylesheet\">\n</head>\n<body>\n"+h+"\n</body>\n</html>";
	var oRTE=get_oRTE(); var o = oRTE.document;
	if(da){o.open();o.write(f);o.close();o.designMode="On";}
	else {
		try {
			o.designMode="on";
			try {
				o.open();o.write(f);o.close();
				if(isGk){o.addEventListener("keypress",kb_handler,true);}
			}
			catch (e){}
		} 
		catch (e){if(isGk){setTimeout("eDM('"+h+"');",10);}else{return false;}}
	}
}
		
function submitForm() {
	var pass = window.prompt("Password?","");if (!pass){return false;}
	var d = document.forms[0];
	if(!isRT) {d.edits = d.doc.value;return true;}
	if(document.getElementById("chkSrcdoc").checked){document.getElementById("chkSrcdoc").checked=false;toggleHTMLSrc();}
	var o=get_oRTE();d.edits.value = o.document.body.innerHTML;d.pw.value=pass;
	return true;
}

function rCom(c, opt){var o=get_oRTE();try{o.focus();o.document.execCommand(c,false,opt);o.focus();}catch (e){}}
function toggleHTMLSrc(){
	var oRTE=get_oRTE();var od = oRTE.document;
	if(document.getElementById("chkSrcdoc").checked){showHide("Buttons1","hidden");
		var ss = od.body.innerHTML;
		ss = ss.replace(/>\s*</g,">\n<");
                if(da){od.body.innerText=ss;}
                else {var h=od.createTextNode(ss);od.body.innerHTML="";od.body.appendChild(h);}
 //		if(da){od.body.innerText=od.body.innerHTML;}
//		else {var h=od.createTextNode(od.body.innerHTML);od.body.innerHTML="";od.body.appendChild(h);}
	} 
	else {showHide("Buttons1","visible");
		if(da){var o=escape(od.body.innerText);o=o.replace("%3CP%3E%0D%0A%3CHR%3E","%3CHR%3E");o=o.replace("%3CHR%3E%0D%0A%3C/P%3E", "%3CHR%3E");od.body.innerHTML=unescape(o);} 
		else {var h=od.body.ownerDocument.createRange();h.selectNodeContents(od.body);od.body.innerHTML=h.toString();}
	}
}

function dlgColorPalette(c){
	setRange();var oD=document.getElementById('cpdoc');var bE=document.getElementById(c);
	oD.style.left=getOffsetLeft(bE)+"px";oD.style.top=(getOffsetTop(bE)+(bE.offsetHeight+4))+"px";
	if((c==parent.command)){if(oD.style.visibility=="hidden"){showHide(oD,'visible');}else{showHide(oD, 'hidden');}} 
	else{showHide('cpdoc','hidden');showHide(oD,'visible');}
	parent.command=c;
}

function dlgInsertTable(c){
	setRange();parent.command=c;
	window.open(incPth+'insert_table.htm','InsertTable','history=no,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=no,width=360,height=200');
}

function insertLink(){var u=prompt("Enter a URL:","");try{rCom("Unlink",null);rCom("CreateLink",u);}catch(e){}}

function setColor(c){
	var p=parent.command;
	if(da){var s=frames["doc"].document.selection;if(p=="hilitecolor")p="backcolor";if(s !=null){var n=s.createRange();n=rng;n.select();}}
	rCom(p,c);showHide('cpdoc',"hidden");
}
function addImage(){var im=prompt('Enter Image URL:','http://');if((im !=null)&&(im !="")){rCom('InsertImage',im);}}
function getOffsetTop(el){var mOT=el.offsetTop;var mOP=el.offsetParent;var p_up=2;while(p_up>0){mOT += mOP.offsetTop;mOP=mOP.offsetParent;p_up--;}return mOT;}
function getOffsetLeft(elm){var mOL=elm.offsetLeft;var mOP=elm.offsetParent;var p_up=2;while(p_up>0){mOL += mOP.offsetLeft;mOP=mOP.offsetParent;p_up--;}return mOL;}
function selectFont(f){var idx=f.selectedIndex;if(idx != 0){var s=f.options[idx].value;rCom("formatblock",s);f.selectedIndex=0;}}
function kb_handler(ev){
	if(ev.ctrlKey){var cmd='';
		switch (String.fromCharCode(ev.charCode).toLowerCase()){case 'b':cmd="bold";break;case 'i':cmd="italic";break;case 'u':cmd="underline";break;};
		if(cmd){rCom(cmd,null);ev.preventDefault();ev.stopPropagation();}
 	}
}
function insertHTML(h){var o=get_oRTE();o.focus();if(da){o.document.selection.createRange().pasteHTML(h);}else {o.document.execCommand('insertHTML', false, h);}}
function showHide(o,s){if(document.getElementById(o)){o=document.getElementById(o);}o.style.visibility=s;}
function setRange(){var o=get_oRTE();if(da){var s=o.document.selection; if(s != null) rng=s.createRange();}else{var s=o.getSelection();rng=s.getRangeAt(s.rangeCount-1).cloneRange();}}
function get_oRTE(){return(da)?frames["doc"]:document.getElementById("doc").contentWindow;}
