
btn_bordercolor="#606060";
btn_backcolor_over="cornsilk";
btn_backcolor_down="#000000";

function insertHTML()
{
var fontoptions = new Array("Arial", "Arial Black", "Arial Narrow", "Book Antiqua", "Century Gothic", "Comic Sans MS", "Courier New", "Fixedsys", "Franklin Gothic Medium", "Garamond", "Georgia", "Impact", "Lucida Console", "Lucida Sans Unicode", "Microsoft Sans Serif", "Palatino Linotype", "System", "Tahoma", "Times New Roman", "Trebuchet MS", "Verdana");
var SelectColor = "background-color:white;color:black";
//////////////////
document.write('<select style="' + SelectColor + ';font-family:tahoma;font-size:12px;" onchange="Editor_addCode(\'[size=\'+this[this.selectedIndex].value+\']\',\'[/size]\');this.selectedIndex=0">');
document.write('<option selected>--</option>');
document.write('<option value="1">1</option>');
document.write('<option value="2">2</option>');
document.write('<option value="3">3</option>');
document.write('<option value="4">4</option>');
document.write('<option value="5">5</option>');
document.write('<option value="6">6</option>');
document.write('<option value="7">7</option>');
document.write('<option value="8">8</option>');
document.write('<option value="9">9</option>');
document.write('</select> '),
//////////////////
document.write('<select style="' + SelectColor + ';font-family:tahoma;font-size:12px;" onchange="Editor_addCode(\'[font=\'+this[this.selectedIndex].value+\']\',\'[/font]\');this.selectedIndex=0">');
document.write('<option selected>----</option>');
document.write('<option value="Arial">Arial</option>');
document.write('<option value="Arial Black">Arial Black</option>');
document.write('<option value="Arial Narrow">Arial Narrow</option>');
document.write('<option value="Comic Sans MS">Comic Sans MS</option>');
document.write('<option value="Courier New">Courier New</option>');
document.write('<option value="System">System</option>');
document.write('<option value="Tahoma">Tahoma</option>');
document.write('<option value="Times New Roman">Times New Roman</option>');
document.write('<option value="Simplified Arabic">Simplified Arabic</option>');
document.write('<option value="Verdana">Verdana</option>');
document.write('<option value="Wingdings">Wingdings</option>');
document.write('<option value="MS Sans Serif">MS Sans Serif</option>');
document.write('</select><div style="padding-bottom:5px;"></div>');
}
var buttons = new Array();
function addRow(a)
{
	buttons = buttons.concat("*");
	buttons = buttons.concat(a);
}
function showButtons()
{
addRow(new Array("plain|الغاء التنسيق","cut|قص","copy|نسخ","paste|لصق","delete|إزالة","link|رابط الكتروني","email|بريد الكتروني","image|صورة","color|لون النص","quote|اقتباس","list|قائمة","ltr|تحويل الكتابة من اليمين إلى اليسار","rtl|تحويل الكتابة من اليسار إلى اليمين","bold|داكن","italic|مائل","uline|خط سفلي","strike|خط بالوسط","line|خط فاصل","left|إلى اليسار","center|توسيط","right|إلى اليمين","justify|محاذاة إلى الأطراف"));
addButtons(buttons, 0);
buttons = null;
}
var tip = "";
var keyboardColor,statusColor,tipColor;
var btn_backcolor_over, btn_backcolor_down, btn_bordercolor;
var toolbox_background,toolbox_backcolor;
var btn_obj = "";
function Capture(b) {
	if (document.activeElement.name != "text") {		
		return;
	}
	if (event.srcElement.tagName.toLowerCase() == "select")
		return;
	if (btn_obj) {		
		btn_obj.releaseCapture();
		btn_obj = "";
	} else if (b == 1) {		
		btn_obj = event.srcElement;		
		btn_obj.setCapture();
	}
}
function button_over(eButton, w) {
	if (w == "qkeys") {
		eButton.parentElement.background = "images/toolbox/key_2.gif";
		eButton.style.color = "white";
	} else {
		doButton(btn_backcolor_over, 1, eButton);
	}
}
function button_out(eButton, w) {
	if (w == "qkeys") {
		eButton.parentElement.background = "transparent";
		eButton.style.color = "black";
	} else {
		doButton("transparent", 0, eButton);
	}
}
function button_down(eButton, w) {
	if (w != "qkeys")
		doButton(btn_backcolor_down, 1, eButton);
}
function button_up(eButton, w) {
	if (w != "qkeys")
		doButton(btn_backcolor_over, 1, eButton);
	eButton = null;
}
function doButton(bk, s, btn) {
	if (event.button == 2) return;
	btn.style.backgroundColor = bk;
	if (s == 0)	{
		btn.parentElement.style.backgroundColor = "transparent";		
	} else {		
		btn.parentElement.style.backgroundColor = btn_bordercolor;
	}
}
function do_button(c) {
	var bn, ob, k;
	ob = window.event.srcElement;
	if (bn=ob.tag) {		
		if (bn.indexOf("|||") >= 0) {
			if (ob.tagName == "IMG")
				ob = ob.parentElement;
			bn = bn.split("|||");
			if (c == 1) //click
				execTool(bn[0]);
			else if (c == 2) //mouse over
				button_over(ob, bn[1]);
			else if (c == 3) //mouse out
				button_out(ob, bn[1]);
			else if (c == 4) //mouse down
				button_down(ob, bn[1]);
			else if (c == 5) //mouse up
				button_up(ob, bn[1]);
		}
	}
}
function addButtons(buttons) {
	var tbl, bn, newRow;
	document.write('<table id=c1 cellpadding=0 cellspacing=0 width=100% border=0 onclick="do_button(1)" onmouseover="do_button(2)" onmouseout="do_button(3)" onmousedown="do_button(4)" onmouseup="do_button(5)"><tr><td><table border=0 cellpadding=0 cellspacing=0 width=100%><tr><td>');
	insertHTML();
	document.write('</td></tr></table>');	
	newRow = 1;
	for (var i = 0; i < buttons.length; i++) {
		if (buttons[i] == "*") {
			if (newRow == 0) {
				document.write('</tr></table>');
				document.write('</td></tr></table>');
				newRow = 1;
			}
			continue;
		}
		if (newRow) {
			document.write('<table dir="ltr" border="0" cellpadding="0" cellspacing="0" width="1px" style="merge:5px;"><tr><td style="border:0px; padding-right:10px;padding-left:10px">');
			document.write('<table width="1px" border="0" cellpadding="0" cellspacing="0"><tr>');
			newRow = 0
		}
		if (buttons[i] == "") {
			tbl = '</tr></table><td style="border:1px ridge;padding-right:10px;padding-left:10px">';
			tbl += '<table width="1px" border="0" cellpadding="0" cellspacing="0"><tr>';
		} else if (buttons[i] == " ") {
			tbl = '<td style="padding:1px"><div class="cbtn" style="font-size:1pt">&nbsp;';
		} else {
			bn = buttons[i] + "| ";
			bn = bn.split("|");			
			tbl = '<td style="padding:1px"><div align="center" class="cbtn" tag="' + bn[0] + '|||' + bn[1] + '">';
			tbl += '<img tag="' + bn[0] + '|||' + bn[1] + '" border="0" align="absmiddle" width="18px" height="18px" src="images/toolbox/' + bn[0] + '.gif" alt="' + bn[1] + '">';
		}
		tbl += '</div></td>';		
		document.write(tbl);
	}
	if (newRow == 0) {
		document.write('</tr></table>');
		document.write('</td></tr></table>');
	}
	document.write('</td></tr></table>');
}
function showColorsWindow() {
var posX = event.screenX;
var posY = event.screenY + 5;
var screenW = screen.width;                                 // screen size
var screenH = screen.height - 20;                           // take taskbar into account
if (posX + 232 > screenW) { posX = posX - 232 - 40; }       // if mouse too far right
if (posY + 164 > screenH) { posY = posY - 164 - 80; }       // if mouse too far down
var wPosition = "dialogLeft:" +posX+ "; dialogTop:" +posY;
return showModalDialog("colors.html", "",
"dialogWidth:238px; dialogHeight: 226px; resizable: no; help: no; status: no; scroll: no; "	+ wPosition);
}
function execTool(btn) {	
if (event.altKey) {
if (btn.substr(0,4) != "key_") {
open("help.php?toolsel=" + btn, "help", "toolbar=no,scrollbars=yes,resizable=yes,width=550px,height=350px,left=50px,top=100px");
}
} else
EditorFunctions(btn);
}
function Editor_getSelText() {
var oSelect,oSelectRange;
document.toolbox.text.focus();
oSelect = document.selection;
oSelectRange = oSelect.createRange();
return oSelectRange; 
}
function Editor_addCode(tag1,tag2) {	
var oSelText = Editor_getSelText();
if (oSelText.text == "")
alert("يرجى تظليل النص أولا");
else
oSelText.text = tag1 + oSelText.text + tag2;
}
function EditorFunctions(tag) {  
if (tag.substr(0, 4) == "key_") {  // Arabic Keyboard
var oSelText = Editor_getSelText();
tag = tag.substr(4);
if (tag == "هـ") tag = "ه";
oSelText.text = tag;
}
else if (tag == "rtl" || tag == "ltr") {
document.toolbox.text.dir=tag.substr(tag);
}
else if (tag == "bold") {
Editor_addCode("[B]","[/B]");
}
else if (tag == "italic") {
Editor_addCode("[I]","[/I]");
}
else if (tag == "uline") {
Editor_addCode("[U]","[/U]");
}
else if (tag == "quote") {
Editor_addCode("[quote]","[/quote]");
}
else if (tag == "line") {
var oSelText = Editor_getSelText();
oSelText.text = "[line]";
}
else if (tag == "list") {
Editor_addCode("[list]\n","\n[/list]");
}
else if (tag == "strike") {
Editor_addCode("[S]","[/S]");
}
else if (tag == "overline") {
Editor_addCode("[overline]","[/overline]");
}
else if (tag == "left" || tag == "center" || tag == "right" || tag == "justify") {
Editor_addCode("[align="+tag.toLowerCase()+"]","[/align]");
}
else if(tag == "link")
{
	var oSelText = Editor_getSelText();
	var ob = window.prompt("أدخل الرابط","http://");
	txt=oSelText.text;
	if(ob)
	{
		if(txt=="")
			txt = window.prompt("أدخل التعليق على الرابط","");
		if (txt)
			oSelText.text = "[url="+ob+"]"+txt+"[/url]";
		else
			oSelText.text = "[url]"+ob+"[/url]";
	}
}
else if(tag == "email")
{
	var oSelText = Editor_getSelText();
	var ob = window.prompt("أدخل عنوان البريد الالكتروني","@");
	txt=oSelText.text;
	if(ob)
	{
		if(txt=="")
			txt = window.prompt("أدخل التعليق على العنوان","");
		if (txt)
			oSelText.text = "[email="+ob+"]"+txt+"[/email]";
		else
			oSelText.text = "[email]"+ob+"[/email]";
	}
}
else if (tag == "image") {
var oSelText = Editor_getSelText();
var link=((oSelText.text=="")?"http://":oSelText.text);
var ob = window.prompt("أدخل رابط الصورة:",link);
if (ob)
oSelText.text = "[img]"+ob+"[/img]";
}
else if (tag == "plain") {
var oSelText = Editor_getSelText();
if (oSelText.parentElement().name=="text") {
var temp = oSelText.text;
temp = temp.replace(/\[FLASH=([^\]]*)\]WIDTH=[0-9]{0,4} HEIGHT=[0-9]{0,4}\[\/FLASH\]/gi,"$1");
temp = temp.replace(/\[VIDEO=([^\]]*)\]WIDTH=[0-9]{0,4} HEIGHT=[0-9]{0,4}\[\/VIDEO\]/gi,"$1");
oSelText.text = temp.replace(/\[[^\]]*\]/gi,"");
}
}
else if(tag == "color")
{
	var oSelText = Editor_getSelText();
	if(oSelText.text == "")
	{
		alert("يرجى تظليل النص أولا");
		return;
	}
	var c = showColorsWindow();
	if(c)
	{ oSelText.text = "[" + tag + "=" + c + "]" + oSelText.text + "[/" + tag + "]"; }
}
else if(tag == "preview")
{}
else if (tag == "cut" || tag == "copy" || tag == "paste" || tag == "delete" || tag == "close")
{
	var oSelText = Editor_getSelText();
	oSelText.execCommand(tag);
}
}
