
var quick_submit_type = 2; //1 Ctrl+Enter 2 Enter


var _bIsIE5 = document.all==null?false:true;
var IframeID;


function hidden_iframe(PREFIX){
	document.all[PREFIX].style.display = "none";
}

function show_iframe(PREFIX){
	document.all[PREFIX].style.display = "block";
}

function pre_load_smileys(){
	document.getElementById('RTE_EDITOR_FORE_PREFIX').src='RTEForeColor.html';
	document.getElementById('RTE_EDITOR_SMILEYS_PREFIX').src='RTESmileys.html';
	document.getElementById('RTE_EDITOR_FONTS_PREFIX').src='RTEFonts.html';
	document.getElementById('RTE_EDITOR_FONTSIZE_PREFIX').src='RTEFontSize.html';
	document.getElementById('RTE_EDITOR_QUICKREPLY_PREFIX').src='RTEQuickreply.html';
}

function hidden_all_iframe(){
	if(document.all['RTE_EDITOR_FORE_PREFIX'].style.display == "block")
		document.all['RTE_EDITOR_FORE_PREFIX'].style.display = "none";
	if(document.all['RTE_EDITOR_SMILEYS_PREFIX'].style.display == "block")
		document.all['RTE_EDITOR_SMILEYS_PREFIX'].style.display = "none";
}
function _foreColor()
{
		if(document.all['RTE_EDITOR_FORE_PREFIX'].src=="")
		    document.all['RTE_EDITOR_FORE_PREFIX'].src ="RTEForeColor.html?rand=" + Math.random();//dropDown.content;//RTE_EDITOR_FORE_PREFIX
		show_iframe('RTE_EDITOR_FORE_PREFIX');
}

function _smileys(){
		if(document.all['RTE_EDITOR_SMILEYS_PREFIX'].src=="")
		    document.all['RTE_EDITOR_SMILEYS_PREFIX'].src ="RTESmileys.html?rand=" + Math.random();//dropDown.content;//RTE_EDITOR_FORE_PREFIX
		show_iframe('RTE_EDITOR_SMILEYS_PREFIX');
}

function _fonts(){
		if(document.all['RTE_EDITOR_FONTS_PREFIX'].src=="")
		    document.all['RTE_EDITOR_FONTS_PREFIX'].src ="RTEFonts.html?rand=" + Math.random();//dropDown.content;//RTE_EDITOR_FORE_PREFIX
		show_iframe('RTE_EDITOR_FONTS_PREFIX');
}

function _fontsize(){
		if(document.all['RTE_EDITOR_FONTSIZE_PREFIX'].src=="")
		    document.all['RTE_EDITOR_FONTSIZE_PREFIX'].src ="RTEFontSize.html?rand=" + Math.random();//dropDown.content;//RTE_EDITOR_FORE_PREFIX
		show_iframe('RTE_EDITOR_FONTSIZE_PREFIX');
}

function _Quickreply(){
		if(document.all['RTE_EDITOR_QUICKREPLY_PREFIX'].src=="")
		    document.all['RTE_EDITOR_QUICKREPLY_PREFIX'].src ="RTEQuickreply.html?rand=" + Math.random();//dropDown.content;//RTE_EDITOR_FORE_PREFIX
		show_iframe('RTE_EDITOR_QUICKREPLY_PREFIX');
}

function do_QuickReply(str){

	IframeID.document.body.innerHTML = formatting_text(str);

//	IframeID.document.body.innerHTML = "<div></div>";
	CopyData();
	if(_check_message()){_do_post();}
}


function emoticon(i) {
	IframeID.focus();
	text='<img ubbname="'+i+'" src="editorimages/smileys/'+i+'.gif" width="20" height="20" border="0" />';
	var txtarea = IframeID.document.body;
//	text = ' ' + text + ' ';
	if (txtarea.createTextRange) {
	//    if (IframeID.document.selection.type=="None") {
		  var sel=IframeID.document.selection.createRange();
		  sel.pasteHTML(text);
		  sel.select();
	//    }
	} else {
		txtarea.innerHTML  += text;
		txtarea.focus();
	}
}

function _InitDocument(charset)
{
if (_bIsIE5){
	IframeID=frames["Composition"];
}
else{
	IframeID=document.getElementById("Composition").contentWindow;
	var _bIsNC=true;
}
/*	if (charset!=null)
		_charset=charset;*/
	if (_bIsIE5){
		var _bodyTag="<style type=text/css>.quote{margin:5px 20px;border:1px solid #CCCCCC;padding:5px; background:#F3F3F3 }\nbody{FONT-SIZE:12px;margin-left: 2px;margin-top: 2px;scrollbar-face-color:#fff;scrollbar-highlight-color:#dddddd;scrollbar-3dlight-color:#F5F7FE;scrollbar-darkshadow-color:#fff;scrollbar-shadow-color:#dddddd;scrollbar-arrow-color:#65578C;scrollbar-track-color:#fff;}.HtmlCode{margin:5px 20px;border:1px solid #CCCCCC;padding:5px;background:#FDFDDF;font-size:14px;font-family:Tahoma;font-style : oblique;line-height : normal ;font-weight:bold;}\n</style></head><BODY bgcolor=\"#FFFFFF\" onload=\"parent.init_editor_style();\" onkeydown=\"parent.ctlent();\" onkeyup=\"parent.clear_enter()\">";
	}else
	{
		var _bodyTag="<style type=text/css>.quote{margin:5px 20px;border:1px solid #CCCCCC;padding:5px; background:#F3F3F3 }\nbody{FONT-SIZE:12px;margin-left: 2px;margin-top: 2px;scrollbar-face-color:#fff;scrollbar-highlight-color:#dddddd;scrollbar-3dlight-color:#F5F7FE;scrollbar-darkshadow-color:#fff;scrollbar-shadow-color:#dddddd;scrollbar-arrow-color:#65578C;scrollbar-track-color:#fff;};\n.HtmlCode{margin:5px 20px;border:1px solid #CCCCCC;padding:5px;background:#FDFDDF;font-size:14px;font-family:Tahoma;font-style : oblique;line-height : normal ;font-weight:bold;}\n</style></head><BODY border=1 onload=\"parent.init_editor_style();\" bgcolor=\"#FFFFFF\" onkeydown=\"parent.ctlent(event);\">";
	}
	var h='';
	
/*
	if (navigator.appVersion.indexOf("MSIE 6.0",0)==-1){
		IframeID.document.designMode="On"
	}
*/
	IframeID.document.open();
	IframeID.document.write ('<html><head>');
/*	if (_bIsIE5){
		IframeID.document.write ('<script language="javascript">');
		IframeID.document.write ('function clear_enter(eventobject){');
		IframeID.document.write ('	if(!event.ctrlKey && event.keyCode == 13 && parent.quick_submit_type==2){');
		IframeID.document.write ('		document.body.innerHTML="<div></div>";');
		IframeID.document.write ('	}');
		IframeID.document.write ('}');
		IframeID.document.write ('var ispost=0;');
		IframeID.document.write ('	function ctlent(eventobject){');
//		IframeID.document.write ('if((event.altKey && window.event.keyCode == 83) || (event.ctrlKey && window.event.keyCode == 13) || (event.altKey && window.event.keyCode == 13)){');
		IframeID.document.write ('	if(!event.ctrlKey && (event.keyCode == 13) && parent.quick_submit_type==2){'); 
		IframeID.document.write ('		parent.CopyData();');
		IframeID.document.write ('		if(parent._check_message()){parent._do_post();}');
		IframeID.document.write ('	}else if(((event.altKey && window.event.keyCode == 13) || (event.altKey && window.event.keyCode == 83) || (event.ctrlKey && event.keyCode == 13)) && parent.quick_submit_type==1){');
		IframeID.document.write ('		parent.CopyData();');
		IframeID.document.write ('		if(parent._check_message()){parent._do_post();}');
		IframeID.document.write ('	}else if(parent.quick_submit_type==2 && event.ctrlKey && window.event.keyCode == 13){');
		IframeID.document.write ('		var sel = document.selection;if (sel.type == \'Control\') {return;}var r = sel.createRange();r.pasteHTML(\'<br>\');r.select();r.collapse(false);r.select();r.collapse(false);');
		IframeID.document.write ('	}');
		IframeID.document.write ('}');
		IframeID.document.write ('<\/script>');
	}*/
	IframeID.document.write(_bodyTag);
	IframeID.document.write("<div></div></body>");
	IframeID.document.write("</html>");
	if (h!="")	{
		IframeID.document.body.innerHTML=h;
	}
	IframeID.document.close();
	IframeID.document.body.contentEditable = "True";
	IframeID.document.charset='UTF-8';
	_bLoad=true;
//	_setMode(1)
	IframeID.focus();
}

function clear_enter(eventobject){
	if(postFlag /*&& !IframeID.event.ctrlKey && IframeID.event.keyCode == 13 && quick_submit_type==2*/){
		IframeID.document.body.innerHTML="";
		postFlag = false;
	}
}

var postFlag=false;


function ctlent(eventobject){
	if (_bIsIE5)
	{
		if(!IframeID.event.ctrlKey && (IframeID.event.keyCode == 13) && quick_submit_type==2){ 
			CopyData();
			if(_check_message()){postFlag=_do_post();}
		}else if(((IframeID.event.altKey && IframeID.event.keyCode == 13) || (IframeID.event.altKey && IframeID.event.keyCode == 83) || (IframeID.event.ctrlKey && IframeID.event.keyCode == 13)) && quick_submit_type==1){
			CopyData();
			if(_check_message()){postFlag=_do_post();}
		}else if(quick_submit_type==2 && IframeID.event.ctrlKey && IframeID.event.keyCode == 13){
			var sel = IframeID.document.selection;if (sel.type == 'Control') {return;}
			var r = sel.createRange();r.pasteHTML('<br>');
			r.select();
			r.collapse(false);
			r.select();
			r.collapse(false);
		}
	}
	else
	{
		if(!eventobject.ctrlKey && (eventobject.keyCode == 13) && quick_submit_type==2){ 
			CopyData();
			if(_check_message()){postFlag=_do_post();}
		}else if(((eventobject.altKey && eventobject.keyCode == 13) || (eventobject.altKey && eventobject.keyCode == 83) || (eventobject.ctrlKey && eventobject.keyCode == 13)) && quick_submit_type==1){
			CopyData();
			if(_check_message()){postFlag=_do_post();}
		}else if(quick_submit_type==2 && eventobject.ctrlKey && eventobject.keyCode == 13){
			var sel = IframeID.document.selection;if (sel.type == 'Control') {return;}
			var r = sel.createRange();r.pasteHTML('<br>');
			r.select();
			r.collapse(false);
			r.select();
			r.collapse(false);
		}
	}
}

function replace_p_to_br(){
/*	var sMessage = IframeID.document.body.innerHTML;
	sMessage = sMessage.replace( /<p(\s+[^>]*)>/gi, "" );  
	sMessage = sMessage.replace( /<p\s*>/gi, "" );  
	sMessage = sMessage.replace( /<\/p>/gi, "<br>" );
	sMessage = sMessage.replace( /\s/g, " " );
	sMessage = sMessage.replace( /\n/g, " " );
	IframeID.document.body.innerHTML=sMessage;*/
}

var colour;
function FormatText(command, option){
	var codewrite
	if (_bIsIE5){
			if (option=="removeFormat"){
				command=option;
				option=null;
			}
			IframeID.focus();
			IframeID.document.execCommand(command, false, option);
			_pureText = false;
			IframeID.focus();
			
	}else{
			if ((command == 'forecolor') || (command == 'backcolor')) {
				parent.command = command;
				buttonElement = document.getElementById(command);
				IframeID.focus();
				document.getElementById("colourPalette").style.left = getOffsetLeft(buttonElement) + "px";
				document.getElementById("colourPalette").style.top = (getOffsetTop(buttonElement) + buttonElement.offsetHeight) + "px";
			
				if (document.getElementById("colourPalette").style.visibility=="hidden")
					{document.getElementById("colourPalette").style.visibility="visible";
				}else {
					document.getElementById("colourPalette").style.visibility="hidden";
				}
			
				//get current selected range
				var sel = IframeID.document.selection; 
				if (sel != null) {
					colour = sel.createRange();
				}
			}
			else{
			IframeID.focus();
			IframeID.document.execCommand(command, false, option);
			_pureText = false;
			IframeID.focus();
			}
	}
}

function CopyData(){
	var sMessage = IframeID.document.body.innerHTML;
	var init_str = '<div>';
	init_str += '</div>';
	IframeID.document.body.innerHTML=init_str;
	sMessage = sMessage.replace( /<p(\s+[^>]*)>/gi, "" );  
	sMessage = sMessage.replace( /<p\s*>/gi, "" );  
	sMessage = sMessage.replace( /<\/p>/gi, "<br>" );
	sMessage = sMessage.replace( /\s/g, " " );
//	sMessage = sMessage.replace( /\n/g, " " );
	document.forms[0].msg.value=sMessage;
}

function showHTML(){
str = IframeID.document.body.innerHTML;
alert(str);
alert(trans_html_ubb(str));
}

function trans_ubb_html(str){
	str = str.replace(/</gi,"&lt;");
	str = str.replace(/>/gi,"&gt;");
	regexp =/\[em(.*?)\]/ig;
	str = str.replace(regexp,"<img src='/editorimages/smileys/$1.gif' ubbname='$1'>");
	str = str.replace( /\n/g, "<br/>" );
	str = edit_hrefs(str);
	return str;
}

function trans_html_ubb(str){
	regexp = /<img[^>]+ubbname="([^"]+)"[^>]*>/ig;
	str = str.replace(/&nbsp;/g," ");
	str = str.replace(/<br>/gi,'\n');
	str = str.replace(regexp,"[EM$1]");
	str = str.removeTags();
	return str;
}

function init_editor_style(){
	var fontWeight = rCk("fontWeight");
	IframeID.document.body.style.fontWeight = fontWeight;
	if(fontWeight == 'bold'){
		document.forms[0].is_b.value = 1;
	}else{
		document.forms[0].is_b.value = 0;
	}


	var fontStyle = rCk("fontStyle");
	IframeID.document.body.style.fontStyle = fontStyle;
	if(fontStyle == 'italic'){
		document.forms[0].is_i.value=1;
	}else{
		document.forms[0].is_i.value=0;
	}

	var textDecoration = rCk("textDecoration");
	IframeID.document.body.style.textDecoration =textDecoration;
	if(textDecoration ==""){
		document.forms[0].is_u.value =0;
	}else{
		document.forms[0].is_u.value =1;
	}

	var fontFamily = rCk("body_fontFamily");
	IframeID.document.body.style.fontFamily =fontFamily;
	document.forms[0].fonts.value = fontFamily;

	var fontSize = rCk("body_fontSize");
	if(fontSize==null || fontSize.length <=0) fontSize='10';
	var read_size =fontSize;
	IframeID.document.body.style.fontSize = read_size+"pt";
	document.forms[0].fontsize.value = fontSize;

	var fontColor = rCk("body_fontColor");
	if(fontColor==null || fontColor.length <=0) fontColor='000000';
	IframeID.document.body.style.color = "#"+fontColor;
	document.forms[0].fontcolor.value = h2d(fontColor);
}

function set_is_b(){
	if(document.forms[0].is_b.value=="0"){
		document.forms[0].is_b.value=1;
		IframeID.document.body.style.fontWeight ="bold";
		wCk("body_fontWeight", "bold", 24*30);
	}else{
		document.forms[0].is_b.value=0;
		IframeID.document.body.style.fontWeight ="normal";
		wCk("body_fontWeight", "normal", 24*30);
	}
}

function set_is_i(){
	if(document.forms[0].is_i.value=="0"){
		document.forms[0].is_i.value=1;
		IframeID.document.body.style.fontStyle ="italic";
		wCk("body_fontStyle", "italic", 24*30);

	}else{
		document.forms[0].is_i.value=0;
		IframeID.document.body.style.fontStyle ="normal";
		wCk("body_fontStyle", "normal", 24*30);
	}
}

function set_is_u(){
	if(document.forms[0].is_u.value=="0"){
		document.forms[0].is_u.value=1;
		IframeID.document.body.style.textDecoration ="underline";
		wCk("body_textDecorationt", "underline", 24*30);
	}else{
		document.forms[0].is_u.value=0;
		IframeID.document.body.style.textDecoration ="";
		wCk("body_textDecoration", "", 24*30);
	}
}

function set_fonts(value){
	document.forms[0].fonts.value=value;
	IframeID.document.body.style.fontFamily =value;
	wCk("body_fontFamily", value, 24*30);
}
function set_fontsize(value){
	var size_save = value;
	document.forms[0].fontsize.value=value;
	IframeID.document.body.style.fontSize = value+'pt';
	wCk("body_fontSize", size_save, 24*30);
}
function set_fontcolor(value,color_value){
	document.forms[0].fontcolor.value=value;
	IframeID.document.body.style.color = "#"+color_value;
	wCk("body_fontColor", color_value, 24*30);
}


var hD="0123456789ABCDEF";

function d2h(d) {
	var aa =d ;
	var h = hD.substr(d&15,1);
	while(d>15) {d>>=4;h=hD.substr(d&15,1)+h;}
	if(h=="0"){
		h="000000";
	}else{
		if(aa<16){
			h="0"+h;
		}
		len = 6-h.length;
		for (i=1;i<=len ;i++ ){
			if( aa>65280 || aa<255 ){
				h+="0";
			}else{
				h="0"+h;
			}
		}
	}

	h = h.substring(4,6) + h.substring(2,4) + h.substring(0,2);

	return h;
}

function h2d(h) {
	h = h.substring(4,6) + h.substring(2,4) + h.substring(0,2);
	return parseInt(h,16);
}

function formatting_text(str){
	var is_b = document.forms[0].is_b.value;
	var is_i = document.forms[0].is_i.value;
	var is_u = document.forms[0].is_u.value;
	var font = document.forms[0].fonts.value;
	var fontsize = document.forms[0].fontsize.value;
	var fontcolor = rCk("body_fontColor");
	var init_str = '<div>';
	if(is_b >0)init_str +='<b>';
	if(is_i >0)init_str +='<EM>';
	if(is_u >0)init_str +='<U>';
	if(font!=null && font!=''){
		init_str +='<font face="'+font+'"';
		if(fontsize!=null && fontsize!=''){
			init_str +=' style="font-size:'+fontsize+'pt"';
		}
		if(fontcolor!=null && fontcolor!=''){
			init_str +=' color="#' + fontcolor +'"';
		}
		init_str +='>'+str+'</font>';
	}else if(fontsize!=null && fontsize!=''){
		init_str +='<font style="font-size:'+fontsize+'pt"';
		if(fontcolor!=null && fontcolor!=''){
			init_str +=' color="#' + fontcolor +'"';
		}
		init_str +='>'+str+'</font>';
	}else if(fontcolor!=null && fontcolor!=''){
			init_str +='<font color="#'+fontcolor+'">'+str+'</font>';
	}else{
		init_str += str;
	}
	if(is_b >0)init_str +='</b>';
	if(is_i >0)init_str +='</EM>';
	if(is_u >0)init_str +='</U>';
	init_str += '</div>';
	return init_str;

}

function edit_hrefs(s_html){
	s_str = s_html;
	s_str = s_str.replace(/\bhttp\:\/\/www(\.[\w+\.\:\/\_\-\?\=]+)/gi, 
	"http\:\/\/¬¤¸$1");

	s_str = s_str.replace(/\b(http\:\/\/[A-Za-z0-9_-]+\.[\w+\.\:\/\_\-\?\=]+)/gi,
	"<a href=\"$1\" target=\"_blank\">$1<\/a>");

//	s_str = s_str.replace(/\b(http\:\/\/\w+\.[\w+\.\:\/\_\-\?\=]+)/gi,
//	"<a href=\"$1\" target=\"_blank\">$1<\/a>");

	s_str = s_str.replace(/\b(www\.[\w+\.\:\/\_\-\?\=]+)/gi, 
	"<a href=\"http://$1\" target=\"_blank\">$1</a>");

	s_str = s_str.replace(/\bhttp\:\/\/¬¤¸(\.[\w+\.\:\/\_\-\?\=]+)/gi,
	"<a href=\"http\:\/\/www$1\" target=\"_blank\">http\:\/\/www$1</a>");

	s_str = s_str.replace(/\b(\w+@[\w+\.?]*)/gi, 
	"<a href=\"mailto\:$1\" target=\"_blank\">$1</a>");
	return s_str;
}
