//gb_rte defined in main.js
var gs_range;
var gs_rtecss;
var gs_rteimgs = "images/";
function initRTE(as_css,as_imgpath) {
	gs_rtecss = as_css;
	if (as_imgpath != null) gs_rteimgs=as_imgpath;
}
function rteBlur(as_rte) {
	top.focus();
}
function rteFocus(as_rte) {
	if (gb_rte) {
		if (dojo.isMozilla) 
			rteCommand(as_rte, "removeformat", "");
		else {
			var luo_f = getWin(as_rte);
			if (luo_f == null) {alert("No RTE window"); return;}
			luo_f.focus();
		}
	} else {
		var luo_e = getObj("editbox_" + as_rte);
		if (luo_e != null) luo_e.focus();
	}
}
function geckoKeys(auo_e) {
	var as_rte = auo_e.target.id;
	if (auo_e.ctrlKey) { var ls_k = String.fromCharCode(auo_e.charCode).toLowerCase();
		var ls_cmd = '';
		switch (ls_k) {
			case 'b': ls_cmd = "bold"; break; case 'i': ls_cmd = "italic"; break; case 'u': ls_cmd = "underline"; break;
		};
		if (ls_cmd) { rteCommand(as_rte, ls_cmd, null); auo_e.preventDefault(); auo_e.stopPropagation(); }
 	}
}
function ieKeys(auo_e, as_rte) {
	var lb_k = (auo_e.which || auo_e.charCode || auo_e.keyCode); var stringKey = String.fromCharCode(lb_k).toLowerCase();
	switch (lb_k) { case 13: };	
}
function enableRTE(as_rte, as_htm, ab_readonly) {
	var luo_ref = getObj('ref0_' + as_rte);
	if (luo_ref != null)
		luo_ref.value = thisPageURL();

	if (!gb_rte) return;
	
	if (!document.all){
		document.getElementById(as_rte).contentWindow.document.addEventListener("mousedown", hideCP, true);
		document.getElementById(as_rte).contentWindow.document.addEventListener("keypress", hideCP, true);
	}

	var luo_h = getObj('hdn' + as_rte);
	if (luo_h != null)
		luo_h.value = as_htm;
	
	setRTEText(as_rte, as_htm, ab_readonly);
}
function setRTEText(as_rte, as_htm, ab_readonly, as_css) {
	if (!gb_rte) {
		var luo_e = document.getElementById('editbox_'+as_rte);
		if (luo_e != null) {luo_e.value = stripHTML(as_htm); luo_e.readonly = ab_readonly; }
		return;
	}
	if (as_css == null) as_css = gs_rtecss;
	var ls_htm = "<html id=\"" + as_rte + "\">\n";
	ls_htm += "<head>\n";
	if (as_css.length > 0) {
		ls_htm += "<link media=\"all\" type=\"text/css\" href=\"" + as_css + "\" rel=\"stylesheet\">\n";
	} else {
		ls_htm += "<style>\n";
		ls_htm += "body {\n";
		ls_htm += "	background: #ffffff;\n";
		ls_htm += "	margin: 1px;\n";
		ls_htm += "	padding: 1px;\n";
		ls_htm += "}\n";
		ls_htm += "</style>\n";
	}
	ls_htm += "<style>p {margin:0px;}</style></head>\n";
	ls_htm += "<body>\n";
	ls_htm += as_htm + "\n";
	ls_htm += "</body>\n";
	ls_htm += "</html>";
	if (document.all) {
		var luo_doc = frames[as_rte].document;
		luo_doc.open(); luo_doc.write(ls_htm); luo_doc.close();
		if (!ab_readonly) { luo_doc.designMode = "On"; frames[as_rte].document.attachEvent("onkeypress", function evt_ie_keypress(event) {ieKeys(event,as_rte);}); }
	} else {
		try {
			if (!ab_readonly) document.getElementById(as_rte).contentDocument.designMode = "on";
			try {
				var luo_doc = document.getElementById(as_rte).contentWindow.document;
				luo_doc.open(); luo_doc.write(ls_htm); luo_doc.close();
				if (dojo.isMozilla && !ab_readonly) luo_doc.addEventListener("keypress", geckoKeys, true);
			} catch (e) {
				alert("setRTEText.Exception with content!\n"+e);
			}
		} catch (e) {
			alert("setRTEText().Exception!\n"+e);
//			if (dojo.isMozilla) { setTimeout("enableRTE('" + as_rte + "', '" + as_htm + "', " + ab_readonly + ");", 10); } else { return false; }
		}
	}
}
function updateRTE(as_rte) {
	try {
		if (!gb_rte) return;
		var lb_readonly = false;
		if (document.all) { if (frames[as_rte].document.designMode != "On") lb_readonly = true; } else { if (document.getElementById(as_rte).contentDocument.designMode != "on") lb_readonly = true; }
		if (!lb_readonly) {
			var luo_hidden = document.getElementById('hdn' + as_rte); if (luo_hidden.value == null) luo_hidden.value = "";
			if (document.all) { luo_hidden.value = frames[as_rte].document.body.innerHTML; } else { luo_hidden.value = document.getElementById(as_rte).contentWindow.document.body.innerHTML; }
			if (stripHTML(luo_hidden.value.replace("&nbsp;", " ")) == "" && luo_hidden.value.toLowerCase().search("<hr") == -1 && luo_hidden.value.toLowerCase().search("<img") == -1) luo_hidden.value = "";
		}
	} catch(e){alert("submitRTE().Exception!\n"+e)};
	return true;
}
function clearRTE(as_rte) {
	if (!gb_rte) {var luo_edit = document.getElementById(as_rte); if (luo_edit != null) luo_edit.value = ""; return;}
	var luo_hidden = document.getElementById('hdn' + as_rte); if (luo_hidden != null) luo_hidden.value = "";
	if (document.all) { frames[as_rte].document.body.innerHTML = ""; } else { document.getElementById(as_rte).contentWindow.document.body.innerHTML = ""; }
}
function rteCommand(as_rte, as_cmd, as_option) {
	var luo_doc;
	if (document.all) { luo_doc = frames[as_rte]; } else { luo_doc = document.getElementById(as_rte).contentWindow; luo_doc.document.execCommand("useCSS", false, true); }
	try { luo_doc.focus(); luo_doc.document.execCommand(as_cmd, false, as_option); luo_doc.focus(); } catch (e) {
		alert("rteCommand().Exception!\n"+e);
	}
}
function setFontColor(as_rte,as_color) {
	if (document.all) gs_range.select();
	rteCommand(as_rte, "forecolor", as_color);
}
//function setColor(as_color) {
//	var as_rte = gs_curr_rte; var luo_pcmd = parent.command;
//	if (document.all) { if (luo_pcmd == "hilitecolor") luo_pcmd = "backcolor"; gs_range.select(); }
//	rteCommand(as_rte, luo_pcmd, as_color); showObj('cp' + as_rte, false);
//}
function setFont(as_rte, as_font) {
	if (as_font == null) return;
	rteCommand(as_rte, "fontname", as_font);
}
function setFontSize(as_rte, as_size) {
	if (as_size == null) return;
	rteCommand(as_rte, "fontsize", as_size);
}
function insertLink(as_rte, as_url, as_text) {
	if (as_rte == null || as_url == null || as_url.length == 0) return;
	if (as_text == null || as_text.length == 0) as_text = as_url;
	var ls_range = setRange(as_rte);
	if (document.all) ls_range = ls_range.text;
	if (ls_range == "") { insertHTML(as_rte,'<a href="' + as_url + '">' + as_text + '</a>');
	} else { rteCommand(as_rte, 'CreateLink', as_url); }
}
function selectFont(as_rte, as_select) {
	var idx = document.getElementById(as_select).selectedIndex;
	if (idx != 0) {
		var ls_selected = document.getElementById(as_select).options[idx].value; var ls_cmd = as_select.replace('_' + as_rte, ''); rteCommand(as_rte, ls_cmd, ls_selected);
		document.getElementById(as_select).selectedIndex = 0;
	}
}
function insertImg(as_rte, as_url, as_id, as_fdid) {
	if (as_fdid != null && as_fdid.length == 0) as_fdid = null;
//	if (dojo.isIE){ //gs_curr_rte = as_rte; 
		if (as_id == null || as_id.length == 0) {
			if (as_fdid == null)
				insertHTML(as_rte,'<img src="' + as_url + '">');
			else
				insertHTML(as_rte,'<img fdid="' + as_fdid + '" src="' + as_url + '">');
		} else {
			if (as_fdid == null)
				insertHTML(as_rte,'<img id="' + as_id + '" src="' + as_url + '">');
			else
				insertHTML(as_rte,'<img id="' + as_id + '" fdid="' + as_fdid + '" src="' + as_url + '">');
		}
//	} else { rteCommand(as_rte, 'InsertImage', as_url); }
}
function insertImgFull(as_rte, as_src, ai_w, ai_h, as_url){
	var ls_r = "";
	if (as_url != null)
		ls_r += '<a href="'+as_url+'">';
	ls_r += '<img src="' + as_src + '" border="0"';
	if (ai_w != null && ai_w > 0)
		ls_r += ' width="'+ai_w+'"';
	if (ai_wh != null && ai_h > 0)
		ls_r += ' height="'+ai_h+'"';
	ls_r += '>';
	if (as_url != null)
		ls_r += '</a>';
	
	insertHTML(as_rte, ls_r);
}
function surroundHTML(as_rte, as_opentag, as_closetag){ insertHTML(as_rte,as_opentag + grabSelection(as_rte) + as_closetag); }
function grabSelection(as_rte){
	//gs_curr_rte = as_rte;
	var ls_range;
	if (dojo.isIE) { var ls_range = setRange(as_rte); ls_range = ls_range.htmlText; ls_range = ls_range.replace(/<\/p>/gi,"<br>").replace(/<p>/gi,"");
	} else { ls_range = readSelectionAlt(as_rte); }
	return ls_range;
}
function readSelectionAlt(as_rte){
	var luo_sel = document.getElementById(as_rte).contentWindow.getSelection(); var luo_htm = luo_sel.getRangeAt(0).cloneContents();
	var luo_div = document.createElement('div'); luo_div.appendChild(luo_htm);
	return luo_div.innerHTML;
}
function setRange(as_rte) {
	var luo_doc;
	if (document.all) { luo_doc = frames[as_rte]; var luo_sel = luo_doc.document.selection; if (luo_sel != null) gs_range = luo_sel.createRange();
	} else { luo_doc = document.getElementById(as_rte).contentWindow; var luo_sel = luo_doc.getSelection(); gs_range = luo_sel.getRangeAt(luo_sel.rangeCount - 1).cloneRange(); }
	return gs_range;
}
function insertHTML(as_rte,as_htm) {
	//var as_rte = gs_curr_rte; 
	var luo_doc;
	if (document.all) { luo_doc = frames[as_rte]; } else { luo_doc = document.getElementById(as_rte).contentWindow; }
	luo_doc.focus();
	if (document.all) { var luo_range = luo_doc.document.selection.createRange(); luo_range.pasteHTML(as_htm); luo_range.collapse(false); luo_range.select();
	} else { luo_doc.document.execCommand('insertHTML', false, as_htm); }
}
function writeCP(as_rte, ai_w, ai_h, ai_h2) {
	if (!gb_rte) return;	
	if (ai_w == null) ai_w = 280;
	if (ai_h == null) ai_h = 100;
	if (ai_h2 == null) ai_h2 = 10;
	var ls_cx = new Array(6);
	ls_cx[0] = new Array("00","33","66","99","cc");
	ls_cx[1] = new Array("33","29","1f","15","0b");
	ls_cx[2] = new Array("66","52","3e","2a","16");
	ls_cx[3] = new Array("99","7b","5d","3f","21");
	ls_cx[4] = new Array("ff","cc","99","66","33");
	ls_cx[5] = new Array("00","00","00","00","00");
	var li_ca = new Array(0,0,0,2,1,3,5,1,2,5,5,4,5,4,5,4,4,5,4,3,5,4,5,5);
	var ls_cz = new Array("ffffff","b299cc","6699b2","9999ff","99ff99","ffff99","ffcc99","ff9999");
	document.writeln('<div id="cp'+as_rte+'" style="position: absolute; top:0px; left:0px; width:'+ai_w+'px; height:'+ai_h+'px; margin:1px; padding:1px; border:2px solid #CCCCCC; z-index:9999; visibility:hidden">');
	document.writeln('<table width="100%" cellpadding="0" cellspacing="0" align="center">');
	document.writeln('<tr>');
	document.writeln('<td>');
	document.writeln('<table width="100%" cellpadding="0" cellspacing="0" align="center">');
	var ls_rgb;
	for (var li_i = 0; li_i < 5; li_i++) {
		document.writeln('<tr>');
		for (var li_ii = 0; li_ii < li_ca.length; li_ii+=3) {
			ls_rgb = ls_cx[li_ca[li_ii]][li_i] + ls_cx[li_ca[li_ii+1]][li_i] + ls_cx[li_ca[li_ii+2]][li_i];
			document.writeln('<td id="#' + ls_rgb + '" style="border:1px solid #cccccc;" bgcolor="#' + ls_rgb + '" height="'+ai_h2+'" onclick="setColor(this.id)" onmouseover="this.style.border = \'1px dotted red\'"  onmouseout="this.style.border = \'1px solid #cccccc\'"><img width="1" height="1"></td>')
		}
		document.writeln('</tr>');		
	}
	document.writeln('<tr>');
	for (li_ii = 0; li_ii < ls_cz.length; li_ii++) {
		document.writeln('<td id="#' + ls_cz[li_ii] + '" style="border:1px solid #cccccc;" bgcolor="#' + ls_cz[li_ii] + '" height="'+ai_h2+'" onclick="setColor(this.id)" onmouseover="this.style.border = \'1px dotted red\'"  onmouseout="this.style.border = \'1px solid #cccccc\'"><img width="1" height="1"></td>')
	}
	document.writeln('</tr>');
	document.writeln('</table>');
	document.writeln('</td>');
	document.writeln('</tr>');
	document.writeln('</table>');
	document.writeln('</div>');
}
function processCP(as_rte, as_cmd) {
	setRange(as_rte);
	var luo_pallete = document.getElementById('cp' + as_rte);
	var luo_btne = document.getElementById(as_cmd + "_" + as_rte);
	var li_lp = getOffsetLeft(luo_btne) + gi_offset_dw;
	var li_tp = getOffsetTop(luo_btne) + (luo_btne.offsetHeight) + gi_offset_dh;
	luo_pallete.style.left = li_lp+"px";luo_pallete.style.top =li_tp+"px";
	if (luo_pallete.style.visibility == "hidden") luo_pallete.style.visibility = "visible"; else luo_pallete.style.visibility = "hidden";
}
function hideCP(){
	var luo_pallete = document.getElementById("cp"+this.id);
	luo_pallete.style.visibility = "hidden";
}
var gi_pallete_timeout;
function MouseOut(auo_x){
	auo_x.onmouseout=function(){ Obj=this; gi_pallete_timeout=setTimeout('Obj.style.visibility=\'hidden\'', 250); }
	auo_x.onmouseover=function(){ clearTimeout(gi_pallete_timeout); }
}
function initCP(as_rte) {
	if (!gb_rte) return;
	document.getElementById('cp'+as_rte).onmouseover = function(){MouseOut(this)};
}

