// Start Poem

// **********************************
// poem_Script v4.0.3 free software
// الرجاء عدم إزالة وصف الحقوق
// Please don't remove the copyright note
// هذا الاسكريبت بواسطة علوي باعقيل
// By Alawi Hussain Ba3qeel, http://www.rayaheen.net
// **********************************

document.write("<table border=1 width=1px cellpadding=0 cellspacing=0 style=\"visibility:hidden;position:absolute\"><tr><td id=\"poem_CheckTextWidth\" nowrap>TESTING</td></tr></table>");
xx = document.getElementsByTagName("TD");
var poem_CheckTextWidth = xx[0];

function poem_getValue(t, p) {
	var r = new RegExp("([ \']|^)" + t + "[ ]*=[ ]*\'[^\']*\'", "i");
	var m = p.match(r);
	if (!m) {
	  r = new RegExp("([ \']|^)" + t + "[ ]*=[ ]*[^\' ]*([ ]|$)", "i");
	  m = p.match(r);
	  if (!m)
		return "";
	}
	m = m[0];
	var v = "";
	while (m!=v) {
		v = m;
		m = m.replace(/[\'\";]/g, "");
		m = m.replace(/&#/g, "&^#");
	}
	m = m.replace(/^[ ]/, "");
	m = m.replace(/[ ]$/, "");
	var i = m.search("=");
	return m.substring(i + 1).replace(/[\(\)\&\[\]\;]/g, "");
}

function poem_setArg(arg) {
	var par, poem_FontSize2;

	par = poem_getValue("font", arg) + ",,,";  //comas = number of arrays - 1
	par = par.split(",");
	poem_FontName = par[0];
	poem_FontSize2 = par[1];

	if (poem_FontSize2 < 1 || poem_FontSize2 > 7)
		poem_FontSize2 = 4;
	poem_FontColor = par[2];
	poem_FontBold = "normal";
	poem_FontItalic = "normal";
	if (par[3]=="b")
		poem_FontBold = "bold";
	else if (par[3]=="i")
		poem_FontItalic = "italic";
	else if (par[3]=="bi") {
		poem_FontBold = "bold";
		poem_FontItalic = "italic";
	}

	par = poem_getValue("border", arg) + ",,";
	par = par.split(",");
	poem_BorderStyle = par[0];
	poem_BorderSize = par[1];
	poem_BorderColor = par[2];

	poem_Shadow = (poem_BorderStyle!="none");

	par = poem_getValue("num", arg) + ",";
	par = par.split(",");
	poem_Serial = "0" + par[0];
	poem_Serial = poem_Serial.replace(/^([0-9]+).*/g, "$1");
	if (poem_Serial<0) poem_Serial = 0;
	poem_SerialColor = par[1];

	par = poem_getValue("type", arg);
	poem_Type = par;

	par = poem_getValue("align", arg);
	poem_Align = par;

	par = poem_getValue("brimage", arg);
	poem_BrImage = par;
	if (poem_BrImage) {
		if (poem_BrImage.search(/^http:\/\//i)==-1)
			poem_BrImage = "http://" + poem_BrImage;
		poem_BrImage = "<img border=0 src=\"" + poem_BrImage + "\">";
	}
	else
		poem_BrImage = "-";

	par = poem_getValue("bkcolor", arg);
	poem_BackColor = par;

	par = poem_getValue("bkimage", arg);
	poem_BackImage = par;
	if (poem_BackImage.search(/^http:\/\//i)==-1)
		poem_BackImage = "http://" + poem_BackImage;

	//////////// set style ///////////

	var facesize = new Array("8","10","12","14","16","18","20");
	poem_FontSize = facesize[poem_FontSize2 - 1];
	poem_CheckTextWidth.style.fontFamily = poem_FontName;
	poem_CheckTextWidth.style.fontSize = poem_FontSize + "pt";
	poem_CheckTextWidth.style.fontWeight = poem_FontBold;
	poem_CheckTextWidth.style.fontStyle = poem_FontItalic;

	///////////////////////////////////
}

function doPoem(preview) {

	var maxwidth, maxnumwidth, pre, w, i, HTML, t, span, poem_value, serial, poem_PaddingMiddle, poem_PaddingSide,sp;
	var padding = 15, poem_PrevHTML = "";


	function showpoem_Line(preview, html) {
		if (preview)
			poem_PrevHTML += html;
		else
			document.write(html);
		
	}


	function showSerial() {
		if (maxnumwidth) {
			HTML += "<table border=0 cellpadding=0 cellspacing=0 style=\"border-collapse:collapse;position:absolute;width:" + maxnumwidth + "px;font-family:" + poem_FontName + ";font-size:" + poem_FontSize + "px;font-weight:" + poem_FontBold + ";font-style:normal;text-decoration:none;color:" + poem_SerialColor + "\"><tr><td><div align=left style=\"padding-left:" + padding + "px\">" + serial + " </div></td></tr></table>";
		}
	}



	if (preview) {
		pre = poem_Preview;
		poem_PrevHTML = "";
		poem_value = "لغة الكلامِ كما رأيتَ على فمي = خجلى ولولا الحبُّ لم أتكلـمِ";
	}
	else {
		pre = document.getElementsByTagName("DIV");
		pre = pre[pre.length - 1];
		pre.style.display = "none";
		poem_value = pre.innerHTML;
	}

	arg = pre.title;
	arg = arg.replace(/\"/g, "'");

	poem_setArg(arg);

	pre.title = "";
	pre.innerHTML = "";

	//remove all bb code tag
	poem_value = poem_value.replace(/\[[a-z]+[^]]*]/gi, "");
	poem_value = poem_value.replace(/\[\/[a-z]+]/gi, "");

	// allow some html tags
	poem_value = poem_value.replace(/<@/i, "<");
	poem_value = poem_value.replace(/<span style="background-color:[ ]*([^> ]*)">/gi, "<@span style’background-color:$1>");
	poem_value = poem_value.replace(/<\/span>/gi, "<@/span>");
	poem_value = poem_value.replace(/<font color=([^> ]*)>/gi, "<@font color’$1>");
	poem_value = poem_value.replace(/<\/font>/gi, "<@/font>");
	poem_value = poem_value.replace(/<(s|b|i|u|sup|sub|br)>/gi, "<@$1>");
	poem_value = poem_value.replace(/<\/(s|b|i|u|sup|sub|br)>/gi, "<@/$1>");
	poem_value = poem_value.replace(/<[^@][^>]*>/g, "");
	poem_value = poem_value.replace(/<@/g, "<");
	poem_value = poem_value.replace(/=/g, "‘")
	poem_value = poem_value.replace(/’/g, "=");
	// *******************
	poem_value = poem_value.replace(/[\t\n]/g, "");
	poem_value = poem_value.replace(/<br \/>|<br>/gi, "\r");
	poem_value = poem_value.replace(/[\r]*/, "\r");
	poem_value = poem_value.replace(/^[\r ]*/, "");
	poem_value = poem_value.replace(/[\r ]*$/, "");
	poem_value = poem_value.replace(/([!a-zA-Z])[ ]*‘[ ]*([!a-zA-Z])/g, "$1+$2");



	w = poem_value;
	w = w.replace(/[ ]*‘[ ]*/g, "<br>");
	w = w.replace(/\r/g, "<br>");

	poem_CheckTextWidth.innerHTML = w;
	maxwidth = poem_CheckTextWidth.clientWidth;
	poem_PaddingMiddle = maxwidth / 4;
	poem_PaddingSide = maxwidth / 2;
	maxwidth += 0 + padding;
	w = "&nbsp;999";

	poem_CheckTextWidth.style.fontStyle = "normal";
	poem_CheckTextWidth.innerHTML = w;

	if (poem_Serial==0)
		maxnumwidth = 0;
	else
		maxnumwidth = poem_CheckTextWidth.clientWidth;

	span = "<img border=0 src=\"clear.gif\" height=1px width=" + maxwidth + "px>";

	showpoem_Line(preview, "<div dir=rtl align=" + poem_Align + ">");

	if (poem_Shadow) {
		showpoem_Line(preview, "<table border=0 cellpadding=0 cellspacing=0 bordercolor=#111111><tr>");
		showpoem_Line(preview, "<td height=100% style=\"padding-top:10px\"><div style=\"height:100%;width:5px;background-color:black;font-size:1pt;filter:alpha(opacity=15);-moz-opacity:0.15;opacity:0.15\"></div></td>");
		showpoem_Line(preview, "<td valign=top>");
	}

	showpoem_Line(preview, "<table bgcolor=\"" + poem_BackColor + "\" background=\"" + poem_BackImage + "\" cellpadding=0 cellspacing=0 style=\"background-position:top right;border:" + poem_BorderSize + "px " + poem_BorderStyle + " " + poem_BorderColor + ";font-family:" + poem_FontName + ";font-size:" + poem_FontSize + "pt;font-weight:" + poem_FontBold + ";font-style:" + poem_FontItalic + ";color:" + poem_FontColor + "\"><tr>");
//	showpoem_Line(preview, "<td style=\"padding:" + padding  + "px\">");
	showpoem_Line(preview, "<td style=\"padding:" + padding  + "px;font-size:16pt;font-weight:bold;font-family:Traditional Arabic\">");

	serial = poem_Serial - 1;
	poem_value = poem_value.split("\r");
	if (poem_FontItalic=="italic")
		sp = "&nbsp;";
	else
		sp = "";

	for (i=0; i<poem_value.length; i++) {
		t = poem_value[i];
		HTML="";
		if (t!="-")
			serial++;
		if (t.indexOf("‘")!=-1) {
			t = t.split("‘");
			if (poem_Type==1) {
				HTML += "<div style=\"width:" + (maxwidth + poem_PaddingSide + maxnumwidth) + "px\">";

				showSerial();

				HTML += "<div style=\"text-align:justify;padding-right:" + maxnumwidth + "px;padding-left:" + poem_PaddingSide + "px\">" + sp + t[0] + span + "</div>";
				HTML += "<div style=\"text-align:justify;padding-right:" + (poem_PaddingSide + maxnumwidth) + "px\">" + t[1] + span + "</div>";
				HTML += "</div>";

			}
			else if (poem_Type==2) {
				HTML += "<div style=\"width:" + (2 * maxwidth + maxnumwidth) + "px\">";

				showSerial();
				HTML += "<div style=\"text-align:justify;padding-right:" + maxnumwidth + "px;padding-left:" + maxwidth + "px\">" + sp + t[0] + span + "</div>";
				HTML += "<div style=\"text-align:justify;padding-right:" + (maxwidth + maxnumwidth) + "px\">" + t[1] + span + "</div>";
				HTML += "</div>";
			}
			else if (poem_Type==3) {
				HTML += "<div style=\"width:" + (maxwidth + maxnumwidth) + "px\">";
				showSerial();
				HTML += "<div style=\"text-align:justify;padding-right:" + maxnumwidth + "px\">" + sp + t[0] + span + "</div>";
				HTML += "<div style=\"text-align:justify;padding-right:" + maxnumwidth + "px\">" + sp + t[1] + span + "</div>";
				HTML += "</div>";
			}
			else {
				HTML += "<div style=\"width:" + (2 * maxwidth + poem_PaddingMiddle + maxnumwidth) + "px\">";
				showSerial();
				HTML += "<div style=\"text-align:justify;position:absolute;margin-right:" + maxnumwidth + "px;width:" + maxwidth + "px\">" + sp + t[0] + span + "</div>";
				HTML += "<div style=\"text-align:justify;padding-right:" + (poem_PaddingMiddle + maxwidth + maxnumwidth) + "px\">" + t[1] + span + "</div>";
				HTML += "</div>";
			}
		}
		else {
			if (t=="-")
				HTML += "<div align=center style=\"padding-right:" + maxnumwidth + "px\">" + poem_BrImage + "</div>";
			else {
				showSerial();
				HTML += "<div align=center style=\"padding-right:" + maxnumwidth + "px\"><div style=\"text-align:justify;width:" + maxwidth + "px\">" + sp + t + span + "</div></div>";
			}
		}
		
		showpoem_Line(preview, HTML);
			
	}
	showpoem_Line(preview, "</td></tr></table>");


	if (poem_Shadow) {
	    showpoem_Line(preview, "</td></tr>");
  		showpoem_Line(preview, "<tr><td rowspan=3 colspan=2 style=\"padding-left:10px\"><div style=\"width:100%;height:5px;background-color:black;font-size:1px;filter:alpha(opacity=15);-moz-opacity:0.15;opacity:0.15;font-size:1px\"></div></td></tr>");
		showpoem_Line(preview, "</table>");
	}

	showpoem_Line(preview, "</div>");


	if (preview)
		pre.innerHTML = poem_PrevHTML;


}

// End Poem

var buttons = new Array();

var btn_backcolor_over, btn_backcolor_down, btn_bordercolor;
var toolbox_background,toolbox_backcolor;
var btn_obj = "";

function button_over(eButton, w) {
	if (w == "qkeys") {
		eButton.parentElement.background = bburl + "images/toolbox/key_2.gif";
		eButton.style.color = "white";
	} else {
		vbform.toolboxbar.style.color = statusColor;
		vbform.toolboxbar.value = w;
		doButton(btn_backcolor_over, 1, eButton);
	}
}

function button_out(eButton, w) {
	if (w == "qkeys") {
		eButton.parentElement.background = "transparent";
		eButton.style.color = "black";
	} else {
		vbform.toolboxbar.style.color = tipColor;
		vbform.toolboxbar.value = tip;
		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 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(bburl + "magic_toolbox.php?todo=color", "",
		"dialogWidth:238px; dialogHeight: 195px; resizable: no; help: no; status: no; scroll: no; "	+ wPosition);
}

function execTool(btn) {	
	if (event.altKey) {
		if (btn.substr(0,6) != "qkeys_") {			
			open(bburl + "magic_toolbox.php?todo=help&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.vbform.message.focus();
    oSelect = document.selection;
    oSelectRange = oSelect.createRange();
    return oSelectRange; 
}

function Editor_addCode(tag1,tag2) {	
  var oSelText = Editor_getSelText();
  if (oSelText.text == "")
     alert(vbphrase["mtool_highlight_first"]);
  else
     oSelText.text = tag1 + oSelText.text + tag2;     
}

function EditorFunctions(tag) {
  if (tag.substr(0, 6) == "qkeys_") {  // Arabic Keyboard
	var oSelText = Editor_getSelText();
	tag = tag.substr(6);
	if (tag == "هـ") tag = "ه";
	oSelText.text = tag;
  }
  else if (tag == "left" || tag == "center" || tag == "right" || tag == "justify") {
    Editor_addCode("[align="+tag.toLowerCase()+"]","[/align]");
  }
  else if (tag == "line") {
	var oSelText = Editor_getSelText();
    oSelText.text = "[line]";
  }
  else if (tag == "poem") {
	var oSelText = Editor_getSelText();
	if (oSelText.text == "") {
		alert(vbphrase["mtool_poem_type"]);
		return;
	}
    oSelText.text = "[poem]" + oSelText.text + "[/poem]"
  }
  else if (tag=="text") {
	var oSelText = Editor_getSelText();
	if (oSelText.text == "") {
		alert(vbphrase["mtool_highlight_first"]);
		return;
	}
    oSelText = showModalDialog(bburl + "magic_toolbox.php?todo=text",oSelText,"help:no; center:yes; status:no; dialogHeight:400px; dialogWidth:700px");
  }
  else if (tag == "plain") {
	var oSelText = Editor_getSelText();
    if (oSelText.parentElement().name=="message") {
	  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 == "keyb") {	  
	  if (keyboardTable.style.display == "none") {		  
		  event.srcElement.src = bburl + "images/toolbox/keybx.gif";
		  keyboardTable.style.display = "inline";
		  event.srcElement.alt = "" + vbphrase["mtool_hide_keyboard"] + "";
	  }
	  else {		  
		  event.srcElement.src = bburl + "images/toolbox/keyb.gif";
		  keyboardTable.style.display = "none";
		  event.srcElement.alt = "" + vbphrase["mtool_keyboard"] + "";
	  }
  }
  else if (tag == "preview") {
	  var c = document.vbform.message.value.replace(/^[\r\n\t ]*|[\r\n\t ]*$/gi, "").length;
	  if (c < minchars)
		  alert(vbphrase["mtool_message_too_short"] + " " + minchars);
	  else if (c > maxchars)
		  alert(vbphrase["mtool_message_too_long"] + " " + maxchars);
	  else
		  open(bburl + "magic_toolbox.php?todo=preview", "postpreview");
  }
}