// แก้จาก emotion เป็น emo แก้ 4 จุด
var uagent=navigator.userAgent.toLowerCase();
var is_safari=((uagent.indexOf('safari')!=-1)||(navigator.vendor=="Apple Computer, Inc."));
var is_opera=(uagent.indexOf('opera')!=-1);
var is_webtv=(uagent.indexOf('webtv')!=-1);
var is_ie=((uagent.indexOf('msie')!=-1)&&(!is_opera)&&(!is_safari)&&(!is_webtv));
var is_ie4=((is_ie)&&(uagent.indexOf("msie 4.")!=-1));
var is_ie7=((is_ie)&&(uagent.indexOf("msie 7.")!=-1));
var is_moz=(navigator.product=='Gecko');
var is_ns=((uagent.indexOf('compatible')==-1)&&(uagent.indexOf('mozilla')!=-1)&&(!is_opera)&&(!is_webtv)&&(!is_safari));
var is_ns4=((is_ns)&&(parseInt(navigator.appVersion)==4));
var is_kon=(uagent.indexOf('konqueror')!=-1);
var is_win=((uagent.indexOf("win")!=-1)||(uagent.indexOf("16bit")!=-1));
var is_mac=((uagent.indexOf("mac")!=-1)||(navigator.vendor=="Apple Computer, Inc."));
var mode="basic";

function $(id) {
	return document.getElementById(id);
}

var iframe;
var editor_box;

var htmltext; // variable to save the html data
var finishedtext;

function item(pos, font, face, size, color){
	this.pos = pos;
	this.font = font;
	this.face = face;
	this.size = size;
	this.color = color;
}

fontlist = new item(50);
fontclose = new item(50);
alignlist = new item(50);
alignclose = new item(50);
anchorlist = new item(50);
textarealist = new item(20);

function do_font(fonttext){
	var i = 0;
	var j = 0;
	var n;
	var pos;
	var subfonttext;	

	fonttext = fonttext.toUpperCase();

	for (pos = 0; pos != -1; pos){
		pos = fonttext.indexOf("<FONT", pos);
		if (pos != -1){
			n = fonttext.indexOf(">", pos);
			fontlist[i] = new item(0, 0, 0, 0, 0);
			fontlist[i].pos = pos;
			fontlist[i].font = 1;
			subfonttext = fonttext.substring(pos, n);
			if (subfonttext.search(/FACE/) != -1){ fontlist[i].face = 1; }
			else { fontlist[i].face = 0; }
			if (subfonttext.search(/SIZE/) != -1){ fontlist[i].size = 1; }
			else { fontlist[i].size = 0; }
			if (subfonttext.search(/COLOR/) != -1) { fontlist[i].color = 1; }
			else { fontlist[i].color = 0; }
			pos++;
			i++;
		} 
	}

	for (pos = 0; pos != -1; pos){
		pos = fonttext.indexOf("</FONT>", pos++);
		if (pos != -1){
			fontclose[j] = new item(0, 0, 0, 0, 0);
			fontclose[j].pos = pos;
			fontclose[j].font = 1;	
			for (ii = i-1; ii >= 0; ii--){
				if (fontlist[ii].pos < pos){
					if (fontlist[ii].font == 1){
						fontlist[ii].font = 0;
						fontclose[j].color = fontlist[ii].color;
						fontclose[j].size = fontlist[ii].size;
						fontclose[j].face = fontlist[ii].face;
						ii = -1;
					}
				}
			}
			pos++;
			j++;
		}else{
			fontclose[j] = new item(0, 0, 0, 0, 0);
			fontclose[j].font = 0;
		}		
	}
}  

function do_align(aligntext){
	var i = 0;
	var j = 0;
	var n;
	var pos;
	var subaligntext;	

	aligntext = aligntext.toUpperCase();

	for (pos = 0; pos != -1; pos){
		pos = aligntext.indexOf("<DIV ALIGN", pos);
		if (pos != -1){
			n = aligntext.indexOf(">", pos);
			alignlist[i] = new item(0, 0, 0, 0, 0);
			alignlist[i].pos = pos;
			alignlist[i].align = 1;

			subaligntext = aligntext.substring(pos, n);
			if (subaligntext.search(/LEFT/) != -1) { alignlist[i].left = 1; }
			else { alignlist[i].left = 0; }
			if (subaligntext.search(/CENTER/) != -1) { alignlist[i].center = 1; }
			else { alignlist[i].center = 0; }
			if (subaligntext.search(/RIGHT/) != -1)  { alignlist[i].right = 1; }
			else { alignlist[i].right = 0; }
			pos++;
			i++;
		} 
	}

	for (pos = 0; pos != -1; pos){
		pos = aligntext.indexOf("</DIV>", pos++);
		if (pos != -1) {
			alignclose[j] = new item(0, 0, 0, 0, 0);
			alignclose[j].pos = pos;
			alignclose[j].align = 1;	
			for (ii = i-1; ii >= 0; ii--){
				if (alignlist[ii].pos < pos){	
					if (alignlist[ii].align == 1){
						alignlist[ii].align = 0;
						alignclose[j].right = alignlist[ii].right;
						alignclose[j].center = alignlist[ii].center;
						alignclose[j].left = alignlist[ii].left;
						ii = -1;
					}
				}
			}
			pos++;
			j++;
		}else{
			alignclose[j] = new item(0, 0, 0, 0, 0);
			alignclose[j].align = 0;
		}
	}
}  

function do_anchor(fonttext){
	var i = 0;
	var j = 0;
	var n;
	var pos;
	var subfonttext;	
		
	fonttext = fonttext.toUpperCase();
	
	for (pos = 0; pos != -1; pos){
		pos = fonttext.indexOf("<A HREF", pos);
		if (pos != -1){
			n = fonttext.indexOf(">", pos);
			anchorlist[i] = new item(0, 0, 0, 0, 0);
			anchorlist[i].font = 1;
			subfonttext = fonttext.substring(pos, n);
		
			if (subfonttext.search(/MAILTO:/) != -1){
				subfonttext = subfonttext.replace(/<A HREF=MAILTO:/, "");
				subfonttext = subfonttext.replace(/\"/, "");
				subfonttext = subfonttext.replace(/\'/, "");
				anchorlist[i].pos = 1; // mailto: flag
				subfonttext = subfonttext.toLowerCase();
				anchorlist[i].face = subfonttext; // email address
			}else{
				anchorlist[i].pos = 2; // http: flag
			}
			pos++;
			i++;
		}else{
			anchorlist[i] = new item(0, 0, 0, 0, 0);
			anchorlist[i].pos = 0; // no more anchors flag
		}	
	} 
} 


function backtohtml(){
	editor_document.body.innerHTML = htmltext;
} 


function convertback(){
	//var texthtmlshow = editor_document.body.innerHTML;

	htmltext = this.text_obj.value
	bbcodetext = this.text_obj.value

	var txtarea =this.text_obj.value;
	//var txtarea = document.post.note;
	txtarea.value = htmltext;

	htmltext = htmltext.replace ( /</gi , "&lt;") ;
	htmltext = htmltext.replace ( />/gi	  , "&gt;") ;

	htmltext = htmltext.replace(/=\"/gi, "=");
	htmltext = htmltext.replace(/=\'/gi, "=");
	htmltext = htmltext.replace(/\"\]/gi, "]");
	htmltext = htmltext.replace(/\'\]/gi, "]");


	//htmltext = htmltext.replace( "\n", "<br>") ;
	// ทำมาเพื่อแก้ปัญหา เว้นบรรทัดเกินจริงๆ เพราะหลังจากปิด </div> มันจะเว็นบรรทัดอยู่แล้ว ไม่ต้องใส่ <br> ลงไปอีก
	htmltext = htmltext.replace( /\n\[left\]/gi, "[left]") ;
	htmltext = htmltext.replace( /\n\[center\]/gi, "[center]") ;
	htmltext = htmltext.replace( /\n\[right\]/gi, "[right]") ;
	htmltext = htmltext.replace( /\[\/center\]\n/gi, "[/left]") ;
	htmltext = htmltext.replace( /\[\/center\]\n/gi, "[/center]") ;
	htmltext = htmltext.replace( /\[\/center\]\n/gi, "[/right	]") ;

	htmltext = htmltext.replace( /\n/gi, "<br>") ;
	htmltext = htmltext.replace(/\[b\]/gi, "<b>");
	htmltext = htmltext.replace(/\[\/b\]/gi, "</b>");
	htmltext = htmltext.replace(/\[i\]/gi, "<i>");
	htmltext = htmltext.replace(/\[\/i\]/gi, "</i>");
	htmltext = htmltext.replace(/\[u\]/gi, "<u>");
	htmltext = htmltext.replace(/\[\/u\]/gi, "</u>");
	htmltext = htmltext.replace(/\[s\]/gi, "<s>");
	htmltext = htmltext.replace(/\[\/s\]/gi, "</s>");
	htmltext = htmltext.replace(/\[sup\]/gi, "<sup>");
	htmltext = htmltext.replace(/\[\/sup\]/gi, "</sup>");
	htmltext = htmltext.replace(/\[sub\]/gi, "<sub>");
	htmltext = htmltext.replace(/\[\/sub\]/gi, "</sub>");
	htmltext = htmltext.replace(/\[left\]/gi, "<DIV ALIGN=LEFT>");
	htmltext = htmltext.replace(/\[\/left\]/gi, "</DIV>");
	htmltext = htmltext.replace(/\[center\]/gi, "<DIV ALIGN=CENTER>");
	htmltext = htmltext.replace(/\[\/center\]/gi, "</DIV>");
	htmltext = htmltext.replace(/\[right\]/gi, "<DIV ALIGN=RIGHT>");
	htmltext = htmltext.replace(/\[\/right\]/gi, "</DIV>");
	htmltext = htmltext.replace(/\[hr\]/gi, "<HR>");

	//htmltext = htmltext.replace(/\[:([A-Z]+):\]/gi, "<IMG SRC='IMAGE\/EMO_$1.gif' STYLE='VERTICAL-ALIGN:MIDDLE'>");
	//htmltext = htmltext.replace(/\[:([A-Z]+):\]/gi, "<IMG SRC='IMAGE\/EMO_$1.gif' STYLE='VERTICAL-ALIGN:MIDDLE'>");
	//var path = 'http://localhost/clubdara/';
	//htmltext = htmltext.replace(/\[:([A-Z]+):\]/gi, "<IMG SRC='"+path+"IMAGE\/EMO_$1.gif' STYLE='VERTICAL-ALIGN:MIDDLE'>");
	//htmltext = htmltext.replace(/\[:([A-Z]+):\]/gi, "<IMG SRC=http:\/\/localhost\/clubdara\/image\/emo_$1.gif' STYLE='VERTICAL-ALIGN:MIDDLE'>");
	//htmltext = htmltext.replace(/\[:([A-Z]+):\]/gi, "<IMG SRC=http:\/\/localhost\/clubdara\/image\/emo_$1.gif>");
	htmltext = htmltext.replace(/\[:([A-Z]+):\]/gi, "<IMG SRC='"+path+"image\/emo_$1.gif' STYLE='VERTICAL-ALIGN:MIDDLE'>");

	htmltext = htmltext.replace(/\[img\]([\s\S]*?)\[\/img\]/gi, "<IMG SRC=$1>");
	htmltext = htmltext.replace(/\[url=([\s\S]*?)\]([\s\S]*?)\[\/url\]/gi, "<A HREF=$1>$2</a>");
	htmltext = htmltext.replace(/\[url\]([\s\S]*?)\[\/url\]/gi, "<A HREF=$1>$1</a>");
	
	htmltext = htmltext.replace(/\[email=([\s\S]*?)\]([\s\S]*?)\[\/email\]/gi, "<A HREF=mailto:$1>$2</a>");
	htmltext = htmltext.replace(/\[email\]([\s\S]*?)\[\/email\]/gi, "<A HREF=mailto:$1>$1</a>");
	
	htmltext = htmltext.replace(/\[size=([\s\S]*?)\]/gi, "<FONT SIZE=$1>");
	htmltext = htmltext.replace(/\[\/size\]/gi, "</FONT>");
	htmltext = htmltext.replace(/\[color=([\s\S]*?)\]/gi, "<FONT COLOR=$1>");
	htmltext = htmltext.replace(/\[\/color\]/gi, "</FONT>");

	return htmltext;
}

function setaligntag(){
	var n;
	var pos;
	var subaligntext;	
	var storetag;
	var newtag;
	var starttext;
	var endtext;

	editor_document.body.innerHTML = editor_document.body.innerHTML.replace(/\n/gi, "</DIV>");
	editor_document.body.innerHTML = editor_document.body.innerHTML.replace(/<DIV><\/DIV>/gi, "");
	editor_document.body.innerHTML = editor_document.body.innerHTML.replace(/<P><\/P>/gi, "");

	for (pos = -1; pos != 0; pos){
		pos = editor_document.body.innerHTML.indexOf("<DIV align", pos);
		if(pos != -1){
			n = editor_document.body.innerHTML.indexOf(">", pos);
			subaligntext = editor_document.body.innerHTML.substring(pos, n);
			if (subaligntext.search(/LEFT/i) != -1) { newtag =  "left"; }
			if (subaligntext.search(/CENTER/i) != -1) { newtag =  "center"; }
			if (subaligntext.search(/RIGHT/i) != -1) { newtag =  "right"; }
			if(storetag){
				if(storetag == newtag){ 
					starttext = editor_document.body.innerHTML.substring(0,pos-8);
					endtext = editor_document.body.innerHTML.substring(pos);
					endtext = endtext.replace("<DIV align="+newtag+">", "<br>");
					editor_document.body.innerHTML = starttext+endtext;
				}
				storetag = newtag;
			} else {
				storetag = newtag;
			}
			
		}
		pos++;
	}
	
}


function convert(){
	var txtarea = this.text_obj.value

	editor_document.body.innerHTML = editor_document.body.innerHTML.replace(/<P align/gi, "<div align");
	if(this.is_ie){ setaligntag(); 
	}
	var bbcodetext = editor_document.body.innerHTML;
	var codetype;
	var vbcodeoption;
	var ipbcodeoption;



	if (finishedtext == bbcodetext){
		editor_document.body.innerHTML = htmltext;
		bbcodetext = editor_document.body.innerHTML;
	}		
	htmltext = editor_document.body.innerHTML;

	// บรรทัดนี้เพิ่มเข้ามา เพื่อให้ FF แปลงค่าเป็น [center] [left] [right] ได้ แต่ยังมีปัญหาอยู่นิดหน่อยตรงเรื่องมันจะเว้นบรรทักมา 1 บรรทัด
	bbcodetext = bbcodetext.replace(/ style=[\"\']text-align: (left|center|right);[\'\"]/gi, " align=$1");

	do_font(bbcodetext);
	do_align(bbcodetext);

	bbcodetext = bbcodetext.replace(/ = /gi, "=");
	bbcodetext = bbcodetext.replace(/=\"/gi, "=");
	bbcodetext = bbcodetext.replace(/=\'/gi, "=");

	bbcodetext = bbcodetext.replace(/ BORDER=[^\'\">]*[\'\">]/gi, "");
	bbcodetext = bbcodetext.replace(/ TARGET=[^\'\">]*[\'\">]/gi, "");
	bbcodetext = bbcodetext.replace(/ CLASSID=[^\'\">]*[\'\">]/gi, "");
	bbcodetext = bbcodetext.replace(/ ID=[^\'\">]*[\'\">]/gi, "");
	bbcodetext = bbcodetext.replace(/ NAME=[^\'\">]*[\'\">]/gi, "");
	bbcodetext = bbcodetext.replace(/ STYLE=[^\'\">]*[\'\">]/gi, "");
	bbcodetext = bbcodetext.replace(/ CLASS=[^\'\">]*[\'\">]/gi, "");
	bbcodetext = bbcodetext.replace(/ ALT=[^\'\">]*[\'\">]/gi, "");

		//var txtarea = document.post.note;
		//txtarea.value = bbcodetext;

	do_anchor(bbcodetext);

	bbcodetext = bbcodetext.replace(/<BR>/gi, "\r");
	bbcodetext = bbcodetext.replace(/<BR \/>/gi, "\r");
	//bbcodetext = bbcodetext.replace(/<P ALIGN/gi, "<DIV align");
	//bbcodetext = bbcodetext.replace(/<P>/gi, "\r\r");
	bbcodetext = bbcodetext.replace(/<P>/gi, "");
	//bbcodetext = bbcodetext.replace(/<P [^>]*>/gi, "\r\r");
	bbcodetext = bbcodetext.replace(/<\/P>/gi, "");
	bbcodetext = bbcodetext.replace(/&nbsp;/gi, "");
	bbcodetext = bbcodetext.replace(/<CODE>/gi, "[code]");
	bbcodetext = bbcodetext.replace(/<\/CODE>/gi, "[/code]");
	bbcodetext = bbcodetext.replace(/<BLOCKQUOTE>/gi, "[quote]");
	bbcodetext = bbcodetext.replace(/<\/BLOCKQUOTE>/gi, "[/quote]");
	bbcodetext = bbcodetext.replace(/<UL[^>]*>/gi, "[list]");
	bbcodetext = bbcodetext.replace(/<\/UL>/gi, "[/list]");
	bbcodetext = bbcodetext.replace(/<OL[^>]*>/gi, "[list=1]");
	bbcodetext = bbcodetext.replace(/<\/OL>/gi, "[/list]");
	bbcodetext = bbcodetext.replace(/<LI>/gi, "[*]");
	bbcodetext = bbcodetext.replace(/<HR>/gi, "[hr]");

	//bbcodetext = bbcodetext.replace(/<IMG[\s\S]*?SRC=([\s\S]*?)IMAGE\/EMOTION_([A-Z]+).GIF\"[\s\S]*?>/gi, "[:$2:]"); // อันนี้มีปัญหา
	bbcodetext = bbcodetext.replace(/<IMG SRC=([^ ]*)IMAGE\/EMO_([A-Z]+).GIF\">/gi, "[:$2:]");

	bbcodetext = bbcodetext.replace(/<IMG[\s\S]*?SRC=([\s\S]*?)\"[\s\S]*?>/gi, "[img]$1[\/img]");
	bbcodetext = bbcodetext.replace(/<IMG[\s\S]*?SRC=([\s\S]*?)'[\s\S]*?>/gi, "[img]$1[\/img]");

	bbcodetext = bbcodetext.replace(/<BIG>/gi, "[b]");
	bbcodetext = bbcodetext.replace(/<\/BIG>/gi, "[/b]");
	bbcodetext = bbcodetext.replace(/<B>/gi, "[b]");
	bbcodetext = bbcodetext.replace(/<\/B>/gi, "[/b]");
	bbcodetext = bbcodetext.replace(/<STRONG>/gi, "[b]");
	bbcodetext = bbcodetext.replace(/<\/STRONG>/gi, "[/b]");
	bbcodetext = bbcodetext.replace(/<U>/gi, "[u]");
	bbcodetext = bbcodetext.replace(/<\/U>/gi, "[/u]");
	bbcodetext = bbcodetext.replace(/<I>/gi, "[i]");
	bbcodetext = bbcodetext.replace(/<\/I>/gi, "[/i]");
	bbcodetext = bbcodetext.replace(/<EM>/gi, "[i]");
	bbcodetext = bbcodetext.replace(/<\/EM>/gi, "[/i]");
	bbcodetext = bbcodetext.replace(/<STRIKE>/gi, "[s]");
	bbcodetext = bbcodetext.replace(/<\/STRIKE>/gi, "[/s]");
	bbcodetext = bbcodetext.replace(/<SUP>/gi, "[sup]");
	bbcodetext = bbcodetext.replace(/<\/SUP>/gi, "[/sup]");
	bbcodetext = bbcodetext.replace(/<SUB>/gi, "[sub]");
	bbcodetext = bbcodetext.replace(/<\/SUB>/gi, "[/sub]");
	
	if (codetype == "bbcode"){
		bbcodetext = bbcodetext.replace(/<FONT Face[^\'\">]*[\'\">]/gi, "<FONT");
		bbcodetext = bbcodetext.replace(/ FACE=[^\'\"]*[\'\"]/gi, "");
	}  

	bbcodetext = bbcodetext.replace(/<FONT SIZE=/gi, "[size=");
	bbcodetext = bbcodetext.replace(/<FONT color=/gi, "[color=");
	//bbcodetext = bbcodetext.replace(/ color=/gi, "][color=");
	//bbcodetext = bbcodetext.replace(/ size=/gi, "][size=");

	bbcodetext = bbcodetext.replace(/<DIV align=left/gi, "[left");
	bbcodetext = bbcodetext.replace(/<DIV align=center/gi, "[center");
	bbcodetext = bbcodetext.replace(/<DIV align=right/gi, "[right");


	var fonttag;
	for (i = 0; fontclose[i].font != 0; i++){
		fonttag = "";
		if (fontclose[i].color == 1)
			fonttag = fonttag + "[/color]";
		if (fontclose[i].size == 1)
			fonttag = fonttag + "[/size]";
		
		bbcodetext = bbcodetext.replace(/<\/FONT>/i, fonttag);
	}

	// อันนี้ทำมาเพิ่ม แก้ปัญหา font มันมีทั้ง color และ size ในอันเดียวกัน แก้แบบห่วยๆไปก่อน
	bbcodetext = bbcodetext.replace(/\[color=([^\] ]+) size=/gi, "[color=$1][size=");

	var aligntag;
	for (i = 0; alignclose[i].align != 0; i++){
		aligntag = "";
		if (alignclose[i].right == 1)
			aligntag = aligntag + "[/right]";
		if (alignclose[i].center == 1)
			aligntag = aligntag + "[/center]";
		if (alignclose[i].left == 1)
			aligntag = aligntag + "[/left]";
		//document.fm.textbox.value += aligntag+"-";

		bbcodetext = bbcodetext.replace(/<\/DIV>/i, aligntag);
	}


	for (i = 0; anchorlist[i].pos != 0; i++)
	{
		if (anchorlist[i].pos == 2)  // if URL
		{
			bbcodetext = bbcodetext.replace(/<A HREF/i, "[url");
			bbcodetext = bbcodetext.replace(/<\/A>/i, "[/url]");
		}  // end if URL
		if (anchorlist[i].pos == 1)  // if mailto:
		{
			if (codetype == "bbcode")
				bbcodetext = bbcodetext.replace(/<A HREF[^<]*<\/A>/i, anchorlist[i].face);	
			else  // if codetype is smf or vbcode
			{
				bbcodetext = bbcodetext.replace(/<A HREF=MAILTO:/i, "[email=");
				bbcodetext = bbcodetext.replace(/<\/A>/i, "[/email]");
			}
		}  // end if mailto:
	}  // end for loop for anchor tags



	bbcodetext = bbcodetext.replace(/<[^>]*>/g, "");


	bbcodetext = bbcodetext.replace(/>/g, "]");
	bbcodetext = bbcodetext.replace(/\'>/g, "]");
	bbcodetext = bbcodetext.replace(/\">/g, "]");
	bbcodetext = bbcodetext.replace(/\']/g, "]");
	bbcodetext = bbcodetext.replace(/\"]/g, "]");

	bbcodetext = bbcodetext.replace ( /&lt;/gi , "<") ;
	bbcodetext = bbcodetext.replace ( /&gt;/gi, ">") ;

	//document.fm.textbox.value = bbcodetext;
	//document.fm.textbox.value = bbcodetext;
	//finishedtext = document.fm.textbox.value;

	//txtarea.value = bbcodetext;
	//txtarea.value = bbcodetext;
	finishedtext = bbcodetext;
	return bbcodetext;
} // end function convert()

this.editor_set_content=function(){
	if(!this.is_ie){return false;}
	if(iframe){
		mode = "basic";
		var bbcode = convert();
		//var bbcode =editor_document.body.innerHTML;
		this.text_obj.style.display='';
		this.text_obj.style.width=iframe.style.width;
		this.text_obj.style.height=iframe.style.height;
		this.text_obj.value = bbcode;
		this.text_obj.focus();
		iframe.parentNode.removeChild(iframe);
		iframe = "";
	} else {
		mode = "advance";
		this.text_obj=document.getElementById('textarea');
		iframe=document.createElement('iframe');
		this.editor_box=this.text_obj.parentNode.appendChild(iframe);
		this.editor_box.style.border='1px solid #999999';
		this.editor_box.style.width=this.text_obj.style.width;
		this.editor_box.style.height=this.text_obj.style.height;
		this.editor_box.className=this.text_obj.className;
		this.text_obj.style.display='none';
		this.editor_window=this.editor_box.contentWindow;
		this.editor_document=this.editor_window.document;
		this.editor_document.designMode='on';
		this.editor_document.open('text/html','replace');
		this.editor_document.write('<style type="text/css">body{margin:0px;border:0px;padding:5;font-family:sans-serif;font-size:10pt;}p{margin:0 !important;padding:0;}ol{border:solid 1px silver;margin:0 0 0 0;padding:5px}ol li{list-style:none;}</style>');
		//var html = this.text_obj.value
		var html = convertback();
		this.editor_document.write(html);
		this.editor_document.close();
		//this.editor_window.focus();
	}
};


window.onload = function(){
	text_obj=document.getElementById('textarea'); // เพิ่มตรงนี้มาไว้เช็คสำหรับถ้าหน้าไหนไม่มีช่องให้ใส่ข้อมูล มันก็จะไม่ error
	if(this.is_ie && text_obj){ this.editor_set_content(); }
};


this.editor_check_focus=function(){
	if(!this.editor_window.has_focus){
		if(this.is_opera){
			this.editor_window.focus();
		}
		this.editor_window.focus();
	}
};


this.get_selection=function(){
	if(this.is_moz || this.is_opera){
		/* ---------------------------*/
	}else{
		var rng=this._ie_cache?this._ie_cache:this.editor_document.selection.createRange();
		if(rng.htmlText){
			return rng.htmlText;
		}else{
			var rtn='';
			for(var i=0;i<rng.length;i++){
				rtn+=rng.item(i).outerHTML;
			}
		}
		return rtn;
	}
};

function tag(tag,value){
	showdata("menucolor","hide");
	showdata("menusize","hide");
	if(mode == "advance"){
		if(tag == "img"){ img(); }
		else if(tag == "link")  { link();}
		else if(tag == "youtube" || tag == "mthai")  { addbb(tag);}
		else if(tag == "quote")  { addquote();}
		//else if(tag == "inserthorizontalrule")  { addonetag(tag);}
		else { this.editor_check_focus(); editor_document.execCommand(tag, false, value);	}
	} else {
		gettag(tag,value);
	}
}

function addquote(){
	if(this.is_ie){
		var selection = this.get_selection();
		selection = "[quote]"+selection+"[/quote]";
		this.editor_check_focus();
		if(typeof(this.editor_document.selection)!='undefined'&&this.editor_document.selection.type!='Text'&&this.editor_document.selection.type!='None'){
			this.editor_document.selection.clear();
		}
		var sel=this.editor_document.selection.createRange();
		sel.pasteHTML(selection);
		sel.select();
	}
}

function addonetag(tag){
	if(mode == "advance"){
		if(tag == "img"){ img(); }
		else if(tag == "link")  { link();}
		else if(tag == "youtube" || tag == "mthai")  { addbb(tag);}
		else if(tag == "quote")  { addquote();}
		else if(tag == "inserthorizontalrule")  { addonetag(tag);}
		else { this.editor_check_focus(); editor_document.execCommand(tag, false, value);}
	} else {
		gettag(tag,value);
	}
}

function gettag(tag,value){
		this.starttag = '';
		this.endtag = '';
		if (tag == 'bold' ) { this.starttag = '[b]'; }
		if (tag == 'italic' ) { this.starttag = '[i]'; }
		if (tag == 'underline' ) { this.starttag = '[u]'; }
		if (tag == 'strikethrough' ) { this.starttag = '[s]'; }
		if (tag == 'superscript' ) { this.starttag = '[sup]'; }
		if (tag == 'subscript' ) { this.starttag = '[sub]'; }
		if (tag == 'justifyleft' ) { this.starttag = '[left]'; }
		if (tag == 'justifycenter' ) { this.starttag = '[center]'; }
		if (tag == 'justifyright' ) { this.starttag = '[right]'; }
		if (tag == 'img' ) { this.starttag = '[img]'; }
		if (tag == 'link' ) { this.starttag = '[url]'; }
		if (tag == 'youtube' ) { this.starttag = '[youtube]'; }
		if (tag == 'mthai' ) { this.starttag = '[mthai]'; }
		if (tag == 'quote' ) { this.starttag = '[quote]'; }
		if (tag == 'undo' ) { this.starttag = ''; }
		if (tag == 'redo' ) { this.starttag = ''; }
		if (tag == 'unlink' ) { this.starttag = ''; }
		if (tag == 'forecolor' ) { this.starttag = '[color='+value+']'; this.endtag = '[/color]';}
		if (tag == 'fontsize' ) { this.starttag = '[size='+value+']'; this.endtag = '[/size]';}
		if ( this.starttag){
			if(!this.endtag){this.endtag = this.starttag.replace(/\[/g,"[/"); }
			if(this.is_moz || this.is_opera){ 
				var startselection = document.getElementById('textarea').selectionStart; 
				var endselection = document.getElementById('textarea').selectionEnd; 
				var selection = document.getElementById('textarea').value.substr(startselection, endselection-startselection);
			}else{ selection = document.selection.createRange().text; }
			if(selection){addtag(selection);
			}else{showpop(this.starttag);}
		}
}

function showpop(starttag){
		if (starttag == '[url]' ) { label = 'ใส่ URL ที่คุณต้องการสร้างเป็นลิงค์'; }
		if (starttag == '[img]' ) { label = 'ใส่ URL ของรูปที่คุณต้องการให้แสดงในคำตอบของคุณ'; }
		if (starttag == '[quote]' ) { label = 'ใส่ข้อความที่คุณต้องการอ้างอิง'; }
		if (starttag == '[b]' ) { label = 'ใส่ข้อความที่คุณต้องการทำเป็นตัวหนา'; }
		if (starttag == '[i]' ) { label = 'ใส่ข้อความที่คุณต้องการทำเป็นตัวเอียง'; }
		if (starttag == '[u]' ) { label = 'ใส่ข้อความที่คุณต้องการให้มีเส้นใต้'; }
		if (starttag == '[s]' ) { label = 'ใส่ข้อความที่คุณต้องการให้มีเส้นขั้น'; }
		if (starttag == '[sup]' ) { label = 'ใส่ข้อความที่คุณต้องการให้เป็นตัวยก'; }
		if (starttag == '[sub]' ) { label = 'ใส่ข้อความที่คุณต้องการให้เป็นตัวห้อย'; }
		if (starttag == '[left]' ) { label = 'ใส่ข้อความที่คุณต้องการจัดชิดซ้าย'; }
		if (starttag == '[center]' ) { label = 'ใส่ข้อความที่คุณต้องการจัดกลาง'; }
		if (starttag == '[right]' ) { label = 'ใส่ข้อความที่คุณต้องการจัดชิดขวา'; }
		if (starttag == '[youtube]' ) { label = 'ใส่URL ของ youtube เพื่อแสดง clip'; }
		if (starttag == '[mthai]' ) { label = 'ใส่URL ของ mthai เพื่อแสดง clip'; }
		if (starttag == '[quote]' ) { label = 'ใส่ ข้อความที่ต้องการอ้างอิง'; }
		if (starttag == '[url]' ) { var urllink=window.prompt('Enter URL for link','http://'); }
		if (endtag == '[/color]' ) { label = 'ใส่ข้อความที่ต้องการให้แสดงเป็นสี '; }
		if (endtag == '[/size]' ) { label = 'ใส่ข้อความที่ต้องการกำหนดขนาด'; }
		var temp = window.prompt(label,''); 
		if(temp){
			temp = temp.replace(/^\s*|\s*$/g,""); //trim
			addtag(temp,urllink);
		}
}




function test(id) {
	var obj = $(id);
	var left = obj.offsetLeft;
	var top = obj.offsetTop;
	var width = obj.offsetWidth;
	var height = obj.offsetHeight;
	var emoshow = document.getElementById('emoshow'); 
	//var emo = document.createElement('emoshow');
	var div = document.createElement('div');
	emoshow.appendChild(div);



	//showemo.id = 'aaa';
	/*
	showemo.style.width = '60px';
	showemo.style.height = '60px';
	showemo.style.left = left ;
	showemo.style.top = top ;
	emo.appendChild(showemo);
	*/
	/*
	showemo.id = 'aaa';
	showemo.style.position = "relative";
	showemo.style.left = left+"px" ;
	showemo.style.top = top+"px" ;
	showemo.style.width = '60px';
	showemo.style.height = '60px';
	showemo.style.backgroundColor = '#FF00FF';
	*/
	//showemo.innerHTML = left+"..."+top;
	//emo.parentNode.appendChild(showemo);

	//$("aaa").innerHTML ="46546464646465464654654";



/*
	$("emo").style.position = "relative";
	$("emo").style.left = left+"px" ;
	$("emo").style.top = top+"px" ;
	$("emo").style.width = '60px';
	$("emo").style.height = '60px';
	$("emo").style.backgroundColor = '#FF00FF';
	$("showemo").innerHTML = left+"..."+top;

	/*
	$("emoshow").style.position = "relative";
	$("emoshow").style.left = left+"px" ;
	$("emoshow").style.top = top+"px" ;
	$("emoshow").style.width = '60px';
	$("emoshow").style.height = '60px';
	$("emoshow").style.backgroundColor = '#FF00FF';
	$("emoshow").innerHTML = left+"..."+top;
*/



	/*
	showemo.id = smiley.id + '_menu';
	showemo.style.display = 'none';
	showemo.style.width = '60px';
	showemo.style.height = '60px';
	showemo.style.left = left ;
	showemo.style.top = top ;
	showemo.innerHTML = "4654646464";
	*/


}


function smileyMenu(ctrl) {
	var smiley = ctrl.firstChild;
	ctrl.style.cursor = 'pointer';
	if(smiley.alt) {
		smiley.code = smiley.alt;
		smiley.alt = '';
	}
	if(smiley.title) {
		smiley.lw = smiley.title;
		smiley.title = '';
	}
	smdiv[ctrl.id] = document.createElement('div');
	smdiv[ctrl.id].id = smiley.id + '_menu';
	smdiv[ctrl.id].style.display = 'none';
	smdiv[ctrl.id].style.width = '60px';
	smdiv[ctrl.id].style.height = '60px';
	smdiv[ctrl.id].className = 'popupmenu_popup';
	$('smilieslist').appendChild(smdiv[ctrl.id]);
	smdiv[ctrl.id].innerHTML = '<table width="100%" height="100%"><tr><td align="center" valign="middle"><img src="' + smiley.src + '" border="0" width="' + smiley.lw + '" /></td></tr></table>';
	showMenu(ctrl.id, 0, 0, 1, 0, 0, smiley.id);
}



















function showdata(type,status){
	if(type == "uploaddetail"){
		if(status == "hide"){$("uploaddetail").style.visibility='hidden';}
		else {$("uploaddetail").style.visibility='visible';}
	}

	if(type == "menucolor"){
		if(status != "hide"){
			var color = ['000000','666666','999999','BBBBBB','006600','008800','00BB00','BBAA00','994400','CC4400','FF6600','CC7700','880000','FF0000','FF4444','FF0066','6600FF','8800FF','BB00FF','FF00FF','0000AA','0000FF','0088FF','00AADD'];
			$("menucolor").style.position = "absolute";
			var colorbox = "<div style='width:88px;position:absolute;border:1px solid #999999;background-color:#F5F5F5;padding:2px;')'>";
			for (var i in color) {
				colorbox +=  "<img src='image/blank.gif' width='20' height='15' style='margin:1px;background:#"+color[i]+";cursor:hand;cursor:pointer;' alt='' onclick='tag(\"forecolor\",\""+color[i]+"\")' />";
			}
			colorbox +=  "<div style='text-align:right;'><img src='image/icon_close.gif' style='width:32px;height:12px;margin-top:2px;cursor:hand;cursor:pointer;' onclick='showdata(\"menucolor\",\"hide\")'></div></div>";
			$("menucolor").innerHTML = colorbox;
		} else {$("menucolor").innerHTML = "";}
	}

	if(type == "menusize"){
		if(status != "hide"){
			$("menusize").style.position = "absolute";
			var sizebox = "<div style='width:40px;position:absolute;border:1px solid #999999;background-color:#F5F5F5;padding:2px;'>";
			for (i=1;i<=7;i++){
				sizebox  += "<div style='border-bottom:1px solid #CCCCCC;text-align:center;'><a href='javascript:tag(\"fontsize\",\""+i+"\")' style='display:block;cursor:hand;cursor:pointer;'><font size='"+i+"'>"+i+"</font></a></div>";
			}
			sizebox  += "<div style='text-align:center;'><img src='image/icon_close.gif' style='width:32px;height:12px;margin-top:2px;cursor:hand;cursor:pointer;' onclick='showdata(\"menusize\",\"hide\")'></div></div>";
			$("menusize").innerHTML = sizebox;
		} else {$("menusize").innerHTML = "";}
	}
}



function addtag(text,urllink){
	var txtarea = document.getElementById('textarea');
	if(text == "is_emo") { text = "[:"+urllink+":]";}
	else if(urllink){	text =  "[url="+urllink+"]"+text+this.endtag;} 
	else {	text =  this.starttag+text+this.endtag; }
	if (this.is_ie) {
		if( !document.getElementById('textarea').focus() ) document.getElementById('textarea').focus();
        document.selection.createRange().text=text;
	} else {
		var startselection = document.getElementById('textarea').selectionStart; 
		var endselection = document.getElementById('textarea').selectionEnd; 
		var textbefor = document.getElementById('textarea').value.substr(0, startselection);
		var textafter = document.getElementById('textarea').value.substr(endselection);
		document.getElementById('textarea').value = textbefor +text +textafter;
	}
}

function addbb(tag){
		this.editor_check_focus();
		if(this.is_ie){
			var temp = window.prompt("ใส่ URL ของ clip",''); 
			if(temp){
				var sel = this.editor_document.selection.createRange();
				sel.pasteHTML('['+tag+']'+temp+'[/'+tag+']');
				sel.select();
			}
		}else {
			var temp = window.prompt("ใส่ URL ของ clip",''); 
			if(temp){
				tag = '['+tag+']'+temp+'[/'+tag+']';
				this.editor_check_focus();
				var random_string = "insert_link_" + Math.round(Math.random()*100000000);
				this.editor_document.execCommand("insertimage",false, random_string);
				var pat = new RegExp("<[^<]*" + random_string + "[^>]*>");
				editor_document.body.innerHTML = editor_document.body.innerHTML.replace(pat,tag);
			}
		}
}



function img(){
	var imageurl=window.prompt('Enter URL for image (full url with http://)','http://');
	if(imageurl && imageurl != "http://"){
			this.editor_check_focus();
			editor_document.execCommand('insertImage',false,imageurl);
	}
}

function link(){
	if(this.is_moz || this.is_opera){ selection = editor_document.getSelection(); } 
	else { selection = editor_document.selection.createRange().text;  }

	if (selection) {
		var urllink=window.prompt('Enter URL for link','http://');
		var titlelink = selection;
	}else{
		var urllink=window.prompt('Enter URL for link','http://');
		var titlelink=window.prompt('Enter Title','');
	}
	//var linktag = '<a href="'+urllink+'" target="_blank">'+titlelink+'</a>';
	//urllink = "http://";
	//if(urllink != "" || urllink != "http://" || urllink != null){
	if(urllink != "" && urllink != "http://" && urllink != null){
		//document.write(urllink);
		if(titlelink == "" || titlelink == null){ titlelink = urllink; }
		var linktag = '<a href="'+urllink+'">'+titlelink+'</a>';
		//var linktag = '55555555';
		if(this.is_moz || this.is_opera){
			this.editor_check_focus();
			var random_string = "insert_link_" + Math.round(Math.random()*100000000);
			this.editor_document.execCommand("insertimage",false, random_string);
			var pat = new RegExp("<[^<]*" + random_string + "[^>]*>");
			editor_document.body.innerHTML = editor_document.body.innerHTML.replace(pat,linktag);
		} else {
			this.editor_check_focus();
			var sel = this.editor_document.selection.createRange();
			sel.pasteHTML(linktag);
			sel.select();
		}	
	}
}

function showemolist(emolist){
	document.getElementById('emo').style.display="none";
	document.getElementById('emo_oni').style.display="none";
	document.getElementById(emolist).style.display="block";
}


function emo(emoname){
	if(mode == "advance"){
		this.editor_check_focus();
		if(this.is_ie){
			var sel = this.editor_document.selection.createRange();
			//sel.pasteHTML('<img src="image/emo_'+emoname+'.gif" style="vertical-align:middle">');
			sel.pasteHTML('<img src="'+path+'image/emo_'+emoname+'.gif" style="vertical-align:middle">');
			sel.select();
		}else {
			var emourl = path+'image/emo_'+emoname+'.gif';
			this.editor_document.execCommand('InsertImage',false,emourl);
		}
	} else {
		addtag('is_emo',emoname);
	}
}

function resize(direction){
	if(mode == "advance"){
		if(direction == 'larger') {
			var newheight =parseInt(this.editor_box.style.height)+100;
		}
		if(direction == 'small') {
			var newheight =parseInt(this.editor_box.style.height)-100;
		}
		if (newheight >= 200 && newheight <= 800){
			this.editor_box.style.height=newheight+'px';
		}
	}
}



function hiddeninputupload(){
		var file=document.getElementById('file');
		file.disabled=true;
}

function showinputupload(){
		var file=document.getElementById('file');
		file.disabled=false;
}


function validate(){
//document.write('5555');
	var post = document.post.post;
	var text_obj=document.getElementById('textarea');

	if(mode == "basic"){post.value = text_obj.value;
	}else {post.value = convert();}

	// เอาค่าจาก group ไปใส่ใน subgroup
	/*
	var formdata = document.forms['post'].elements;
	var selectoption = formdata['group'].selectedIndex;
	if(selectoption) {document.post.subgroup.value = formdata['group'].options[selectoption].value;}
	else {document.post.subgroup.value = document.getElementById('group').value;}
	*/

	if (document.post.name.value == "") { alert("กรุณากรอกชื่อ"); return false; }


	if (document.post.status.value == "topic") {

		//document.write(document.post.status.value);
		if (document.post.title.value == "") { alert("กรุณากรอกหัวข้อกระทู้"); return false; }
		//if (document.post.type.value == "") { alert("กรุณาเลือกชนิดของกระทู้"); return false; }
		if (document.post.category.value == "") { alert("กรุณาเลือกหมวดหมู่หลักของกระทู้"); return false; }
		if (document.post.nation.value == "") { alert("กรุณาเลือกหมวดหมู่ย่อยของกระทู้"); return false; }
		//if (document.post.group.value == "") { alert("กรุณาเลือกกลุ่มของกระทู้"); return false; }
		if (document.post.board.value == "") { alert("กรุณาเลือกกลุ่มของกระทู้"); return false; }
		if (document.post.post.value == "") { alert("กรุณากรอกรายละเอียดการตอบกระทู้"); return false; }
	}
	if (document.post.status.value == "post") {
		if (document.post.file.value == "") {
			if (document.post.post.value == "") {
					alert("กรุณากรอกรายละเอียดการตอบกระทู้");
					return false;
			 }
		 }
	}
	
}


function boardactive(id,status){
	if(status == "off") { document.getElementById(id).style.backgroundColor="#FFFFFF"; }
	else { document.getElementById(id).style.backgroundColor="#F8F8F8"; }
}






















function MultiSelector( list_target, max ){
	this.list_target = list_target;
	this.count = 0;
	this.id = 0;
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};

	this.addElement = function( element ){
		if( element.tagName == 'INPUT' && element.type == 'file' ){
			element.name = 'file_' + this.id++;
			element.multi_selector = this;
			element.onchange = function(){
				var new_element = document.createElement( 'input' );
				new_element.type = 'file';
				this.parentNode.insertBefore( new_element, this );
				this.multi_selector.addElement( new_element );
				this.multi_selector.addListRow( this );
				this.style.position = 'absolute';
				this.style.left = '-1000px';
			};
			if( this.max != -1 && this.count >= this.max ){
				element.disabled = true;
			};
			this.count++;
			this.current_element = element;
		} else {
			alert( 'Error: not a file input element' );
		};
	};

	this.addListRow = function( element ){
		var new_row = document.createElement( 'div' );
		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'image';
		//new_row_button.src = 'http://localhost/clubdara/image/bu_mainmenu.gif';
		new_row_button.src = path+'image/icon_del.gif';
		new_row_button.className='delupload';
		new_row.element = element;
		new_row_button.onclick= function(){
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );
			this.parentNode.parentNode.removeChild( this.parentNode );
			this.parentNode.element.multi_selector.count--;
			this.parentNode.element.multi_selector.current_element.disabled = false;
			return false;
		};
		new_row.innerHTML = "<br><div style='float:left;width:400px;padding-left:10px;border-bottom:1px dotted #DDDDDD;text-align:left;'>"+element.value.replace( /(.*)\\([^\\]+)/gi, "$2")+"</div><div style='float:right;'>";
		new_row.appendChild( new_row_button );
		this.list_target.appendChild( new_row );
	};
};



function delectconfirm(object) {
		if (confirm("ยืนยันการลบข้อมูล") == true) {
				return true;
		}
		return false;
}






























function changepic(id,pic){
	$(id).src = pic;
}





function showtopmenu(status){
	if(status == "close"){
		$("menubotton").src = "image/menu2.gif";
		$("mainmenu").style.display = "none";
	} else {
		$("menubotton").src = "image/menuon.gif";
		try { 
			 $("mainmenu").innerHTML = $("mainmenudata").innerHTML; 
			$("mainmenu").style.display = "block";
		} catch(e) { }
	}
}