var clientPC = navigator.userAgent.toLowerCase();
var clientVer = parseInt(navigator.appVersion); 
var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);

function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

function showpop(starttag,endtag){
		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 == '[color=red]' ) { label = 'ใส่ข้อความที่คุณต้องการให้เป็นสีแดง'; }
		if (starttag == '[color=green]' ) { label = 'ใส่ข้อความที่คุณต้องการให้เป็นสีเขียว'; }
		if (starttag == '[color=blue]' ) { label = 'ใส่ข้อความที่คุณต้องการให้เป็นสีน้ำเงิน'; }
		if (starttag == '[color=orange]' ) { label = 'ใส่ข้อความที่คุณต้องการให้เป็นสีส้ม'; }
		if (starttag == '[color=pink]' ) { label = 'ใส่ข้อความที่คุณต้องการให้เป็นสีชมพู'; }
		if (starttag == '[color=gray]' ) { label = 'ใส่ข้อความที่คุณต้องการให้เป็นสีเทา'; }
		var temp = window.prompt(label,''); 
		temp = temp.replace(/^\s*|\s*$/g,""); //trim
		if(temp) setemo(starttag+temp+endtag);
}

function bbtag(starttag,endtag) {
		if ((clientVer >= 4) && is_ie && is_win) {
				var txtarea = document.post.note;
				theSelection = document.selection.createRange().text; // Get text selection
				if (theSelection) {
							theSelection = theSelection.replace(/^\s*|\s*$/g,""); //trim
							document.selection.createRange().text = starttag + theSelection + endtag;
							txtarea.focus();
							theSelection = '';
							return;
				} else {
					showpop(starttag,endtag);
				}
		} else {
				showpop(starttag,endtag);
		}
}

function setemo(text) {
	var txtarea = document.post.note;
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}


function validate(){
	 if (document.post.name.value == "") {
			alert("กรุณากรอก ชื่อผู้ตอบ กระทู้");
			document.post.name.focus();
			return false;
     }
	 if (document.post.note.value == "") {
			alert("กรุณาใส่รายละเอียดการตอบกระทู้");
			document.post.note.focus();
			return false;
     }
	 if (document.post.title.value == "") {
			alert("กรุณากรอก หัวข้อกระทู้");
			document.post.title.focus();
			return false;
     }
	 if (document.post.captchanum.value == "") {
			alert("กรุณากรอกตัวเลข 4 หลัก ตามภาพ");
			document.post.captchanum.focus();
			return false;
     }
	 if (document.post.group.value == "") {
			alert("กรุณาเลือก หมวดหมู่");
			document.post.group.focus();
			return false;
     }
}

function DelectConfirm(object) {
		if (confirm("ยืนยันการลบข้อมูล") == true) {
				return true;
		}
		return false;
}

