// bbCode control by
// subBlue design
// www.subBlue.com


// Define the bbCode tags
bbcode = new Array();
bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[img]','[/img]','[url]','[/url]','[move]','[/move]');
imageTag = false;


// Replacement for arrayname.length property
function getarraysize(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

// Replacement for arrayname.push(value) not implemented in IE until version 5.5
// Appends element to the array
function arraypush(thearray,value) {
	thearray[ getarraysize(thearray) ] = value;
}

// Replacement for arrayname.pop() not implemented in IE until version 5.5
// Removes and returns the last element of an array
function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}




function bbfontstyle(formObj, bbopen, bbclose) {
	if ((parseInt(navigator.appVersion)>= 4) && (navigator.appName == "Microsoft Internet Explorer")) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			formObj.detail.value += bbopen + bbclose;
			formObj.detail.focus();
			return;
		}
		document.selection.createRange().text = bbopen + theSelection + bbclose;
		formObj.detail.focus();
		return;
	} else {
		formObj.detail.value += bbopen + bbclose;
		formObj.detail.focus();
		return;
	}
}





function BBstyle(formObj, bbnumber) {

	donotinsert = false;
	theSelection = false;
	bblast = 0;

	if (bbnumber == -1) { // Close all open tags & default button names
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;
			formObj.detail.value += bbtags[butnumber + 1];
			buttext = eval('formObj.addBBcode' + butnumber + '.value');
			eval('formObj.addBBcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		formObj.detail.focus();
		return;
	}

	if ((parseInt(navigator.appVersion)>= 4) && (navigator.appName == "Microsoft Internet Explorer"))
		theSelection = document.selection.createRange().text; // Get text selection

	if (theSelection) {
		// Add tags around selection
		document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
		formObj.detail.focus();
		theSelection = '';
		return;
	}

	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < bbcode.length; i++) {
		if (bbcode[i] == bbnumber+1) {
			bblast = i;
			donotinsert = true;
		}
	}

	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (bbcode[bblast]) {
				butnumber = arraypop(bbcode) - 1;
				formObj.detail.value += bbtags[butnumber + 1];
				buttext = eval('formObj.addBBcode' + butnumber + '.value');
				eval('formObj.addBBcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				imageTag = false;
			}
			formObj.detail.focus();
			return;
	} else { // Open tags

		if (imageTag && (bbnumber != 6)) {		// Close image tag before adding another
			formObj.detail.value += bbtags[7];
			lastValue = arraypop(bbcode) - 1;	// Remove the close image tag from the list
			formObj.addBBcode6.value = "Img";	// Return button back to normal state
			imageTag = false;
		}

		// Open tag
		formObj.detail.value += bbtags[bbnumber];
		if ((bbnumber == 6) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(bbcode,bbnumber+1);
		eval('formObj.addBBcode'+bbnumber+'.value += "*"');
		formObj.detail.focus();
		return;
	}

}


if((navigator.appName == "Netscape" && navigator.appVersion.charAt(0)>= 5) || (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.charAt(0)>= 4) || (navigator.appName == "Opera" && navigator.appVersion.charAt(0)> 4)) {

document.write('<style type="text/css"> INPUT.button { FONT-SIZE: 11px; COLOR: 000000; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; BACKGROUND-COLOR: #EFEFEF } </style>');
document.write('<table cellspacing=0 cellpadding=2 border=0><tr valign=center>');
document.write('<td><input onClick=BBstyle(this.form,0) type=button value=" B " name=addBBcode0 class="button"></td>');
document.write('<td><input onClick=BBstyle(this.form,2) type=button value=" I " name=addBBcode2 class="button"></td>');
document.write('<td><input onClick=BBstyle(this.form,4) type=button value=" U " name=addBBcode4 class="button"></td>');
document.write('<td><input onClick=BBstyle(this.form,6) type=button value=Img name=addBBcode6 class="button"></td>');
document.write('<td><input onClick=BBstyle(this.form,8) type=button value=URL name=addBBcode8 class="button"></td>');
document.write('<td><input onClick=BBstyle(this.form,10) type=button value=" Move " name=addBBcode10 class="button"></td>');
document.write('<td><font size="1" face="MS Sans Serif, Microsoft Sans Serif"><a href="javascript:bbCode()">วิธีใช้</a>&nbsp;&nbsp;&nbsp;</font></td>');
document.write('</tr></table>');

}

else {
	document.write('<font size=1>browser not compatible with these buttons</font>');
}


function iconz(item) {
	document.form1.detail.value = document.form1.elements.detail.value + ":" + item + ":";
	document.form1.detail.focus();
}


function bbCode() {
	link1 = window.open("js/bbcode.html","Link1","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=370,height=300,left=180,top=100");
}



if((navigator.appName == "Netscape" && navigator.appVersion.charAt(0)>= 5) || (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.charAt(0)>= 4) || (navigator.appName == "Opera" && navigator.appVersion.charAt(0)> 4)) {

document.write('<img src="js/01.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'01\');">'); 
document.write('<img src="js/02.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'02\');">'); 
document.write('<img src="js/03.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'03\');">'); 
document.write('<img src="js/04.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'04\');">'); 
document.write('<img src="js/05.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'05\');">'); 
document.write('<img src="js/06.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'06\');">'); 
document.write('<img src="js/07.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'07\');">'); 
document.write('<img src="js/08.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'08\');">'); 
document.write('<br>');
document.write('<img src="js/09.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'09\');">'); 
document.write('<img src="js/10.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'10\');">'); 
document.write('<img src="js/11.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'11\');">'); 
document.write('<img src="js/12.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'12\');">'); 
document.write('<img src="js/13.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'13\');">'); 
document.write('<img src="js/14.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'14\');">'); 
document.write('<img src="js/15.gif" width="15" height="15" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'15\');">'); 
document.write('<img src="js/16.gif" width="21" height="19" align="absmiddle" hspace="4" vspace="2"border="0" onClick="javascript:iconz(\'16\');">'); 

// Font

document.write('<br><font size="1" face="MS Sans Serif, Microsoft Sans Serif">Font color: <select name="addbbcode18" onChange="bbfontstyle(this.form, \'[color=\' + this.form.addbbcode18.options[this.form.addbbcode18.selectedIndex].value + \']\', \'[/color]\')">');
document.write('<option style="color:black; background-color: #FFFFFF " value="#004c75">Default</option>');
document.write('<option style="color:darkred; background-color: #DEE3E7" value="darkred">Dark Red</option>');
document.write('<option style="color:red; background-color: #DEE3E7" value="darkred">Red</option>');
document.write('<option style="color:orange; background-color: #DEE3E7" value="orange">Orange</option>');
document.write('<option style="color:brown; background-color: #DEE3E7" value="brown">Brown</option>');
document.write('<option style="color:yellow; background-color: #DEE3E7" value="yellow">Yellow</option>');
document.write('<option style="color:green; background-color: #DEE3E7" value="green">Green</option>');
document.write('<option style="color:olive; background-color: #DEE3E7" value="olive">Olive</option>');
document.write('<option style="color:cyan; background-color: #DEE3E7" value="cyan">Cyan</option>');
document.write('<option style="color:blue; background-color: #DEE3E7" value="blue">Blue</option>');
document.write('<option style="color:darkblue; background-color: #DEE3E7" value="darkblue">Dark Blue</option>');
document.write('<option style="color:indigo; background-color: #DEE3E7" value="indigo">Indigo</option>');
document.write('<option style="color:violet; background-color: #DEE3E7" value="violet">Violet</option>');
document.write('<option style="color:white; background-color: #DEE3E7" value="white">White</option>');
document.write('<option style="color:black; background-color: #DEE3E7" value="black">Black</option>');
document.write('</select>');
document.write('&nbsp;Font size: ');
document.write('<select name="addbbcode20" onChange="bbfontstyle(this.form, \'[size=\' + this.form.addbbcode20.options[this.form.addbbcode20.selectedIndex].value + \']\', \'[/size]\')">');
document.write('<option value="1">เล็กมาก</option>');
document.write('<option value="2">เล็ก</option>');
document.write('<option value="3" selected>กลาง</option>');
document.write('<option value="5">ใหญ่</option>');
document.write('<option value="7">ใหญ่มาก</option>');
document.write('</select></font>');

}
else {
	document.write("<font size=1>browser not compatible with these buttons</font>");
}

