    

//path of images
var path="";
// show prompt or not 1 or 0
var prompt_bbcode = 1 ;
var cpaletc= 5 ;
var wbg= "#fff";
var wcolor= "#000";
// lang of the editor
var l_undo="Undo";
var l_redo="Redo";
var l_b="Bold";
var l_u="Underline";
var l_i="Italic";
var l_remove="Remove text";
var l_p="Color palette";
var l_link="Link";
var l_link_p= 1 ;
var url_enter_desc = "Enter the Text of the URL :";
var l_unlink="Unlink";var l_image="Insert image";
var l_jr="justify Right";var l_jl="justify Left";
var l_jc="justify Center";
var l_ol="Insert ordered list";
var l_ul="Insert unordered list";
var l_quote="Quote";
var l_code="Code";
var l_phpcode="PHP code";
var l_rf="Remove format";
var l_out="Outdent";
var l_ind="Indent";
var l_size="Size";
var l_font="Font";
var l_para="paragraph";
var l_ex="Expand editor area";
var l_con="Contract editor area";
var url_enter="Enter a URL for the selection :";
var image_enter="Enter Image URL :";
var fontsarr=['Arial','Courier','Tahoma','Verdana','Impact'];
var direction="rtl";
var l_exp="expressions";
var l_s="strike";
var l_hr="Horizontal rule";
var l_sub="Subscript";
var l_sup="Superscript";
var com_content="<?php echo sdasdas; ?>";
var l_sent =['hello','fine'];

var almsamim = {};
// objects
var comm, bbcode;
// vars
var isIE, isOpera, isWebKit,ua,Editor;
// Browsers check
ua = navigator.userAgent;
almsamim.isOpera = isOpera = window['opera'] && opera.buildNumber;
almsamim.isWebKit = isWebKit = /WebKit/.test(ua);
almsamim.isIE = isIE = !isWebKit && !isOpera && (/MSIE/gi).test(ua) && (/Explorer/gi).test(navigator.appName);

// ----------- almsamim plug-ins object -----------//
almsamim.plugins = {};

// ----------- almsamim bbcode object -----------//
almsamim.bbcode = bbcode = {
//print the editor
_Print : function(){
	// Toolbar icons
	document.writeln('<style type="text/css">.toolbar{align:left;background-image: url("' + path +'images/editor/toolbar_background.gif");}.button, .toolbar{padding: 2px;}.oover{background-image: url("' + path +'images/editor/toolbar_button_background.gif");padding: 2px;}select { font-family: Arial, Helvetica, sans-serif; font-size: 11pt;padding:0;margin:0;}</style><div class="toolbar">');
	// Undo
	document.writeln('<img src="' + path +'images/editor/arrow_undo.png" onClick="comm._command(\'Undo\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_undo + '" />');
	//Redo
	document.writeln('<img src="' + path +'images/editor/arrow_redo.png" onClick="comm._command(\'Redo\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_redo + '" />');
	// Bold
	document.writeln('<img src="' + path +'images/editor/text_bold.png" onClick="comm._command(\'bold\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_b + '" />');
	// italic
	document.writeln('<img src="' + path +'images/editor/text_italic.png" onClick="comm._command(\'italic\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_i + '" />');
	// Underline
	document.writeln('<img src="' + path +'images/editor/text_underline.png" onClick="comm._command(\'underline\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_u + '" />');

	// color palette
	document.writeln('<img src="' + path +'images/editor/palette.png" id="forecolor" onClick="toggle_visibility(\'colour_palette\');" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_p + '" />');
	// link
	document.writeln('<img src="' + path +'images/editor/world_link.png" onClick="comm._url('+ l_link_p +')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_link + '" />');
	// unlink
	document.writeln('<img src="' + path +'images/editor/world_delete.png" onClick="comm._command(\'Unlink\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_unlink + '" />');
	//insert image
	document.writeln('<img src="' + path +'images/editor/photo.png" onClick="comm._image()" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_image + '" />');
	// align left
	document.writeln('<img src="' + path +'images/editor/text_align_left.png" onClick="comm._command(\'justifyleft\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_jl + '" />');
	// align center
	document.writeln('<img src="' + path +'images/editor/text_align_center.png" onClick="comm._command(\'justifycenter\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_jc + '" />');
	// align right
	document.writeln('<img src="' + path +'images/editor/text_align_right.png" onClick="comm._command(\'justifyright\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_jr + '" />');
	

	// ordered list
	document.writeln('<img src="' + path +'images/editor/text_list_numbers.png" onClick="comm._command(\'InsertOrderedlist\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_ol + '" />');
	// unordered list
	document.writeln('<img src="' + path +'images/editor/text_list_bullets.png" onClick="comm._command(\'InsertUnorderedlist\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_ul + '" />');
	// quote
	document.writeln('<img src="' + path +'images/editor/quote.png" onClick="comm._HTML(\'[quote]\',\'[/quote]\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_quote + '" />');
	//code
	document.writeln('<img src="' + path +'images/editor/page_white_code.png" onClick="comm._HTML(\'[CODE]\',\'[/CODE]\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_code + '" />');
	// PHP code
	document.writeln('<img src="' + path +'images/editor/page_white_php.png" onClick="comm._HTML(\'[PHP]\',\'[/PHP]\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_phpcode + '" />');
	// remove format
	document.writeln('<img src="' + path +'images/editor/delete.png" onClick="comm._command(\'removeformat\')" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_rf + '" />');

	// expand editor
	document.writeln('<img src="' + path +'images/editor/arrow_down.png" onClick="textbox_resize(100);" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)" title="'+ l_ex + '" />');
	// cotract editor
	document.writeln('<img src="' + path +'images/editor/arrow_up.png" onClick="textbox_resize(-100);" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)"  title="'+ l_con + '" />');
	//  switch bbcode /HTML VIEW
	document.writeln('<img src="' + path +'images/editor/switch.gif" onClick="comm._toggle();" class="button" onmouseover="overIcon(this)" onmouseout="outIcon(this)"  title="Switch BBcode/HTML" />');
	//Size menu
	document.writeln('</div><div class="toolbar"><select unselectable="on" id="fontsize" onchange="comm._select(this.id);"><option value="Size">' + l_size +'</option><option value="1">Tiny</option><option value="2">Small</option><option value="3">Normal</option><option value="4">Large</option><option value="5">Huge</option></select>');
	//font menu
	document.writeln('<select id="fontname" onchange="comm._select(this.id);"><option value="' + l_font + '">' + l_font + '</option>');
	for (i=0;i<fontsarr.length;i++)
		document.writeln('<option value="' + fontsarr[i] + '">' + fontsarr[i] + '</option>');
		document.writeln('</select>');

	// Heading menu
	document.writeln('<select id="formatblock" onchange="comm._select(this.id);"><option value="<p>">Normal</option><option value="<p>">' + l_para + '</option><option value="<h1>">Heading 1 <H1></option><option value="<h2>">Heading 2 <H2></option><option value="<h3>">Heading 3 <H3></option><option value="<h4>">Heading 4 <H4></option><option value="<h5>">Heading 5 <H5></option><option value="<h6>">Heading 6 <H6></option></select></div>');
	document.writeln('<div id="colour_palette" style="display:none;">');
	comm._palette('h', 15, 10);
	document.writeln('</div>');
	document.writeln('<iframe id="box" width="100%" scrolling="auto" height="200" style="display:block;margin-top:5px;border:0px solid;background:#fff;" frameborder="0"></iframe>');
	document.writeln('<textarea id="box_text" style="display:none;height: 13em;width:100%;" class="inputbox" tabindex="3" rows="13" cols="74" onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);"></textarea>');
	bbcode.Start();
},
// turning the editor on
Start  : function (){
		Editor = document.getElementById('box').contentWindow.document;
		//writing iframe content and style of the editor
		var iframeContent;
		iframeContent  = '<html xmlns="http://www.w3.org/1999/xhtml" dir="' + direction + '" lang="en-gb" xml:lang="en-gb">\n';
		iframeContent += '<head><style type="text/css">body{font-family:Arial,Sans-Serif,Tahoma;font-size:12pt; FONT-WEIGHT: bold; background:'+ wbg +';color:' + wcolor + ';margin:2px;padding:2px;}p {margin:0;padding:0;min-height: 1em; } *+html p { min-height: auto; }div {margin:0;padding:0;}</style></head><body>';
		iframeContent += '</body>';
		iframeContent += '</html>';
		Editor.open();
		Editor.write(iframeContent);
		Editor.close();
		Editor.designMode = "on";
		// disable CSS in Geko ,IE and opera  
		try {
			// Try new Gecko method
			Editor.execCommand("styleWithCSS", 0, false);
		} catch (e) {
			// Use old method
			try {Editor.execCommand("useCSS", 0, true);} catch (e) {};
		}
},

// convert BBcode to HTML code
_BBcodetoHTML : function (a)
{
    function r(re, str) {
            a = a.replace(re, str);
    };
	r(/\n+(\[\/list\])/gi,'[/list]');
	r(/\[list\]\n+/gi,'[list]');
	r(/\[list=1\]\n+/gi,'[list=1]');
	r(/\[list=a\]\n+/gi,'[list=a]');
	r(/\n+\[\/tr\]/gi,'[/tr]');
	r(/\n+\[tr\]/gi,'[tr]');
	r(/\n+\[td\]/gi,'[td]');
	r(/\n+\[\/td\]/gi,'[/td]');
	r(/\n+\[\/table\]/gi,'[/table]');
	r(/\[\/table\]$/gi,"[/table]\n");
	r(/&/g,'&amp;');
	r(/</g,'&lt;');
	r(/>/g,'&gt;');
	r(/  /g,'&nbsp;&nbsp;');
	r(/\t/g,'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');	
	r(/\n/g,'<br>');
	r(/\[hr\]/gi,'<hr />');
	r(/\[\/hr]/gi,'');
	r(/\[table\]/gi,'<table style="width: 100%; padding: 0px; border: none; border: 1px solid #789DB3;">');
	r(/\[\/table\]/gi,'</table>');
	r(/\[(\/|)tr\]/gi,'<$1tr>');
	r(/\[(\/|)td\]/gi,'<$1td style="font-size: 20px; border: 1px solid #789DB3; background-color: #F4F4F4">');
	r(/\[(sub|sup|strike|s|b|i|u)\]/gi,'<$1>');
	r(/\[\/(sub|sup|strike|s|b|i|u)\]/gi,'</$1>');
	r(/\[font=(.*?)\]/gi,'<font face="$1">');
	r(/\[color=(.*?)\]/gi,'<font color="$1">');
	r(/\[size=(.*?)\]/gi,'<font size="$1">');
	r(/\[\/(font|color|size)\]/gi,'</font>');
	r(/\[(center|left|right|justify)\]/gi,'<div align="$1">');
	r(/\[\/(center|left|right|justify)\]/gi,'</div>');
	r(/\[url=(.*?)\]/gi,'<a href="$1">');
	r(/\[url\](.*?)\[\/url\]/gi,'<a href="$1">$1[/url]');
	r(/\[\/url\]/gi,'</a>');
	r(/\[img\](.*?)\[\/img\]/gi,'<img src="$1">');
	var b=a.match(/\[(list|list=1|list=a)\]/gi);
	r(/\[list=1\]/gi,'<ol>');
	r(/\[list=a\]/gi,'<ol style="list-style-type: lower-alpha">');
	r(/\[list\]/gi,'<ul>');
	r(/\[\*\]/gi,'<li>');
	r(/<br[^>]*><li>/gi,'<li>');
	r(/<br[^>]*> <li>/gi,'<li>');
	r(/<br[^>]*><\/li>/gi,'</li>');
	r(/\[h([1-6])?\]/gi,"<h$1>");
	r(/\[\/h([1-6])?\]/gi,"</h$1>");
	if(b){for(var i=0;i<b.length;i++){if(b[i].toLowerCase()=="[list]"){r(/\[\/list\]/i,'</ul>');}else if(b[i].toLowerCase()=="[list=1]"||b[i].toLowerCase()=="[list=a]"){r(/\[\/list\]/i,'</ol>');}}}
	if(isOpera){r(/<\/table>/gi,'</tr></table>');r(/<\/tr>/gi,'</td></tr>');}	
	if(isOpera||isIE){r(/<li>/gi,'</li><li>');r(/<\/(ol|ul)>/gi,'</li></$1>');}
	return a;
},

// erase white spaces from the text inside the editor
_erase : function (a)
{
	if(typeof a!="string")return a;
	var b=a;
	var c=b.substring(0,1);
	while(c==" ")
	{
		b=b.substring(1,b.length);
		c=b.substring(0,1)
	}
	c=b.substring(b.length-1,b.length);
	while(c==" ")
	{
		b=b.substring(0,b.length-1);
		c=b.substring(b.length-1,b.length)
	}
	while(b.indexOf("  ")!=-1)
	{
		b=b.substring(0,b.indexOf("  "))+b.substring(b.indexOf("  ")+1,b.length)
	}
	return b ;
},

// convert HTML to bb when submit bbcode
_HTMLtoBBcode : function (a) {
	    function r(re, str) {
	            a = a.replace(re, str);
	    };
        if (isIE) {
            r(/<\/li>/gi, "");
            r(/<li>/gi, "[*]");
        }
        r(/<div><\/div>/gi, "");
        r(/<br[^>]*>/gi, "<br>");
        r(/[\n\r]/gi, "");
        r(/<script>(.*?)<\/script>/gi, "");
        r(/<script.*?>(.*?)<\/script>/gi, "");
		// remove style tag with anything within
        r(/<style>(.*?)<\/style>/gi, "");
        r(/<style.*?>(.*?)<\/style>/gi, "");
		// remove tags <w:> of MS word
        r(/<w:.*?>(.*?)<\/w:.*?>/gi, "");
        r(/<(abbr|acronym|applet|area|base|basefont|bdo|bgSound|big|body|button|caption|center|cite|code|col|colGroup|comment|custom|dd|del|dfn|dir|dl|dt|embed|fieldSet|frame|frameSet|head|html|ins|isIndex|kbd|label|legend|link|listing|map|marquee|menu|meta|noBR|noFrames|noScript|optGroup|option|param|plainText|pre|q|rt|ruby|samp|small|tBody|tFoot|tHead|title|tt|wbr|xml|xmp|th|script|form|input|iframe|object|select|textarea)(.*?)>/gi, "");
        r(/<\/(abbr|acronym|applet|area|base|basefont|bdo|bgSound|big|body|button|caption|center|cite|code|col|colGroup|comment|custom|dd|del|dfn|dir|dl|dt|embed|fieldSet|frame|frameSet|head|html|ins|isIndex|kbd|label|legend|link|listing|map|marquee|menu|meta|noBR|noFrames|noScript|optGroup|option|param|plainText|pre|q|rt|ruby|samp|small|tBody|tFoot|tHead|title|tt|wbr|xml|xmp|th|script|form|iframe|object|select|textarea)(.*?)>/gi, "");
        r(/\xA0/gi, " ");
        r(/<br[^>]*><\/div>/gi, "</div>");
        r(/<br[^>]*>/gi, "\n");
        r(/<hr[^>]*>/gi, "[hr][/hr]");
        r(/<\/hr>/gi, "");
        r(/<(ul|ol)><\/li>/gi, "<$1>");
        r(/  /gi, " ");
        r(/<p([^>]*)>/gi, "<div$1>");
        r(/<\/p([^>]*)>/gi, "</div$1>\n");
        r(/\t/g, "     ");
        r(/\n /g, "\n");
        r(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi, "[url=$1]$2[/url]");
        r(/<h([1-6])([^>]*)>/gi, "[h$1]");
        r(/<\/h([1-6])([^>]*)>/gi, "[/h$1]");
        var b = a.split("<");
        var c = new Array;
        var e = 0;
        if (b.length > 1) {
            for (var i = 0; i < b.length; i++) {
                if (i > 0) {
                    b[i] = "<" + b[i];
                }
                var f = b[i];
                if (f.match(/<(div|span|font|strong|b|u|i|em|var|address|h1|h2|h3|h4|h5|h6|blockquote|img|ol|ul|li|a|strike|s|sub|sup|hr|table|tr|td)( ([^>]{1,}.*?)){0,1}( {0,1}){0,1}>/i)) {
                    var g = RegExp.$1;
                    var h = RegExp.$3;
                    if (h.toLowerCase().indexOf("style=") != -1 && h.toLowerCase().indexOf("font-family:") != -1 && h.toLowerCase().indexOf("face=") != -1) {
                        h = h.replace(/face="(.*?)"/gi, "");
                    } else if (h.toLowerCase().indexOf("style=") != -1 &&  h.toLowerCase().indexOf("color:") != -1 && h.toLowerCase().indexOf("color=") != -1) {
                        h = h.replace(/color="(.*?)"/gi, "");
                    }
                    h = h.replace(/(color=|size=|face=|style=)/gi, "|$1");
                    h = h.replace(/('|")/g, "");
                    h = h.replace(/ \|/g, "|");
                    var j = h.split("|");
                    var k = new Array;
                    if (j != null) {
                        for (var z = 0; z < j.length; z++) {
                            var l = j[z].split("=");
                            k[l[0].toLowerCase()] = j[z].replace(l[0].toLowerCase() + "=", "");
                        }
                    }
                    var m = "";
                    var g = g.toLowerCase();
                    if (g == "strike" || g == "s") {
                        if (k.style) {
                            m = "[s]" + this._process(g, k);
                        } else {
                            m = "[s]";
                        }
                    } else if (g == "sub") {
                        if (k.style) {
                            m = "[sub]" + this._process(g, k);
                        } else {
                            m = "[sub]";
                        }
                    } else if (g == "sup") {
                        if (k.style) {
                            m = "[sup]" + this._process(g, k);
                        } else {
                            m = "[sup]";
                        }
                    } else if (g == "li") {
                        if (k.style) {
                            m = "[*]" + this._process(g, k);
                        } else {
                            m = "[*]";
                        }
                    } else if (g == "strong" || g == "b") {
                        if (k.style) {
                            if (k.style.toLowerCase().indexOf("font-weight: bold") != -1 ||  k.style.toLowerCase().indexOf("font-weight: 700") != -1) {
                                m = this._process(g, k);
                            } else {
                                m = "[b]" + this._process(g, k);
                            }
                        } else {
                            m = "[b]";
                        }
                    } else if (g == "em" ||
                        g == "i" || g == "var" || g == "address") {
                        if (k.style) {
                            if (k.style.toLowerCase().indexOf("font-style: italic") != -1) {
                                m = this._process(g, k);
                            } else {
                                m = "[i]" + this._process(g, k);
                            }
                        } else {
                            m = "[i]";
                        }
                    } else if (g == "u") {
                        if (k.style) {
                            if (k.style.toLowerCase().indexOf("text-decoration: underline") != -1) {
                                m = this._process(g, k);
                            } else {
                                m = "[u]" + this._process(g, k);
                            }
                        } else {
                            m = "[u]";
                        }
                    } else if (g == "ol") {
                        if (k.style) {
                            m = this._process(g, k);
                            if (m.indexOf("[list=a]") == -1) {
                                m += "[list=1]";
                            }
                        } else if (k.align) {
                            m = "[" + k.align.toUpperCase() + "]" + "[list=1]";
                        } else {
                            m = "[list=1]";
                        }
                    } else if (g == "ul") {
                        if (k.style) {
                            m = this._process(g, k) + "[list]";
                        } else if (k.align) {
                            m = "[" + k.align.toUpperCase() + "]" + "[list=1]";
                        } else {
                            m = "[list]";
                        }
                    } else if (g == "font" || g == "h1" || g == "h2" || g == "h3" || g == "h4" || g == "h5" || g == "h6") {
                        if (j.length > 0) {
                            for (var r in k) {
                                if (r == "color") {
                                    m += "[color=" + k.color + "]";
                                } else if (r == "size") {
                                    if (isNaN(parseInt(k.size))) {
                                        k.size = 2;
                                    }
                                    m += "[size=" + k.size + "]";
                                } else if (r == "face") {
                                    m += "[font=" + k.face + "]";
                                } else if (r == "style") {
                                    m += this._process(g, k);
                                }
                            }
                        }
                    } else if (g == "div" || g == "span") {
                        if (k.style) {
                            m = this._process(g, k);
                        } else if (k.align) {
                            m = "[" + k.align.toUpperCase() + "]";
                        } else {
                            m = "[ALMSAMIM]";
                        }
                    } else if (g == "img") {
                        if (isWebKit) {
                            f = f.replace(/<img(.*?)src="(.*?)">/gi, "[IMG]$2[/IMG]");
                        } else {
                            f.match(/<img(.*?)src="(.*?)"(.*?)>/gi);
                            var s = RegExp.$2;
                            s = s.replace("./", "");
                            if (s.toLowerCase().substr(0, 7) != "http://") {
                                var t = document.URL;
                                t = t.replace("http://", "");
                                var u = t.split("/");
                                var v = "http://";
                                for (var d = 0; d < u.length; d++) {
                                    if (d < u.length - 1) {
                                        v += u[d] + "/";
                                    }
                                }
                                f = f.replace(/\<img(.*?)src="(.*?)"(.*?)>/gi, "[IMG]" + v + s + "[/IMG]");
                            } else {
                                f = f.replace(/<img(.*?)src="(.*?)"(.*?)>/gi, "[IMG]$2[/IMG]");
                            }
                        }
                    } else if (g == "table") {
                        m = "[table]";
                    } else if (g == "tr") {
                        m = "[tr]";
                    } else if (g == "td") {
                        m = "[td]";
                    }
                    b[i] = f.replace(/(<([^>]+)>)/, m);
                    if (g != "img") {
                        c[e] = m;
                        e++;
                    }
                } else if (f.match(/<\/(div|span|font|strong|b|u|i|em|var|address|h1|h2|h3|h4|h5|h6|blockquote|ol|ul|li|a|strike|s|sub|sup|table|tr|td)>/i)) {
                    e--;
                    var w = c.pop();
                    if (w != null) {
                        var x = "";
                        var A = w;
                        A = A.replace(/=(.*?)\]/g, "]");
                        A = A.replace(/\]/g, "],");
                        A = A.replace(/\[(.*?)\]/g, "[/$1]");
                        var B = A.split(",");
                        B.reverse();
                        for (var y = 0; y < B.length; y++) {
                            x += B[y];
                        }
                        x = x.replace(/\[\/\*\]/gi, "");
                        b[i] = b[i].replace(/(<([^>]+)>)/, x);
                    } else {
                        b[i] = b[i].replace(/(<([^>]+)>)/, "");
                    }
                }
            }
            var C = b.join("");
        } else {
            var C = a;
        }
		function r2(re, str) {
	            C = C.replace(re, str);
	    };
        r2(/<[^>]*>/g, "");
        r2(/&lt;/g, "<");
        r2(/&gt;/g, ">");
        r2(/&nbsp;/g, " ");
        r2(/&amp;/g, "&");
        r2(/     /g, "\t");
        r2(/\[ALMSAMIM\]/g, "\n");
        r2(/\[ALMSAMIM\]\n+/g, "\n");
        r2(/\[\/ALMSAMIM\]\n+/g, "\n");
        r2(/\[\/ALMSAMIM\]/g, "\n");
        r2(/\[\*\]/gi, "\n[*]");
        r2(/\n\n\[\*\]/gi, "\n[*]");
        r2(/\[color=#.\w*\]\[\/color\]/gi, "");
        r2(/\[size=\d\]\[\/size\]/gi, "");
        r2(/\[b\]\[\/b\]/gi, "");
        r2(/\[u\]\[\/u\]/gi, "");
        r2(/\[i\]\[\/i\]/gi, "");
        r2(/\[left\]\[\/left\]/gi, "");
        r2(/\[center\]\[\/center\]/gi, "");
        r2(/\[right\]\[\/right\]/gi, "");
        r2(/\[url\]\[\/url\]/gi, "");
        r2(/\[s\]\[\/s\]/gi, "");
        r2(/\[sub\]\[\/sub\]/gi, "");
        r2(/\[sup\]\[\/sup\]/gi, "");
        r2(/\[img\]\[\/img\]/gi, "");
        r2(/^\n+/, "");
        r2(/\n+$/, "");
        var D = C.match(/\[table\]/gi);
        var E = C.match(/\[\/table\]/gi);
        if (D && E) {
            if (D.length > E.length) {
                C += "[/table]";
            }
        } else if (D && !E) {
            C += "[/table]";
        }
        r2(/\[\/tr\]/gi, "\n[/tr]");
        r2(/\[tr\]/gi, "\n[tr]");
        r2(/\[td\]/gi, "\n[td]");
        r2(/\[\/table\]/gi, "\n[/table]");
        r2(/\[\/table\]$/gi, "[/table]\n");
        r2(/\[table\]\n+/gi, "[table]");
        r2(/\n+\[td\]/gi, "[td]");
        r2(/\n+\[\/table\]/gi, "[/table]");
        r2(/\n+\[\/td\]/gi, "[/td]");
        r2(/\n+\[tr\]/gi, "[tr]");
        r2(/\n+\[\/tr\]/gi, "[/tr]");
        return C;
},

// processing HTML code to BBcode
_process : function (a, b) {
        var c = "";
        var d = b.style.split(";");
        for (var j = 0; j < d.length; j++) {
            if (d[j] != "" && d[j] != null) {
                var e = d[j].split(":");
                var f = e[0].toLowerCase().replace(/  /g, "");
                f = f.replace(/style=/gi, "");
                var g = e[1];
				// removing white spaces from g
				if (g){
				g=g.replace(/^ +| +$/g,"");
				}
                if (f == "vertical-align" && g == "sub") {
                    c += "[sub]";
                } else if (f == "vertical-align" && g == "super") {
                    c += "[sup]";
                } else if (f == "list-style-type" && g == "lower-alpha") {
                    c += "[list=a]";
                } else if (f == "text-align") {
				g=g.replace(/^ +| +$/g,"");
                    g = g.toUpperCase();
                    c += "[" + g + "]";
                } else if (f == "font-weight") {
                    if (g.toUpperCase() == "BOLD" || g.toUpperCase() == "700") {
                        c += "[b]";
                    }
                } else if (f == "font-style") {
                    if (g.toUpperCase() == "ITALIC") {
                        c += "[i]";
                    }
                } else if (f == "font-family") {
                    c += "[font=" + g + "]";
                } else if (f == "font-size") {
                    if (g == "8pt" || g == "9pt" || g == "x-small") {
                        c += "[size=1]";
                    } else if (g == "10pt" || g == "11pt" || g == "small") {
                        c += "[size=2]";
                    } else if (g == "12pt" || g == "13pt" || g == "medium") {
                        c += "[size=3]";
                    } else if (parseInt(g) >= 14 && parseInt(g) < 18 || g == "large") {
                        c += "[size=4]";
                    } else if (parseInt(g) >= 18 && parseInt(g) < 24 || g == "x-large") {
                        c += "[size=5]";
                    } else if (parseInt(g) >= 24 && parseInt(g) < 36 || g == "xx-large") {
                        c += "[size=6]";
                    } else if (parseInt(g) >= 36 || g == "-webkit-xxx-large") {
                        c += "[size=7]";
                    }
                } else if (f == "text-decoration") {
                    if (g.toUpperCase() == "UNDERLINE") {
                        c += "[u]";
                    } else if (g.toUpperCase() == "LINE-THROUGH") {
                        c += "[s]";
                    }
                } else if (f == "color") {
                    if (g.indexOf("#") == -1) {
					if (g.match(/rgb\((.*?)\)/gi)){
					var h = this._toHex(g);
					}else{
                       var h = g; }
					   } else {
                        var h = g;
                    }
                    c += "[color=" + h + "]";
                }
            }
        }
        return c;
},
//converting rgb(..) to Hex
_toHex : function (a)
	{
	a=a.replace(/rgb\((.*?)\)/gi,"$1");
	a=a.replace(/ /,"");
	var c=a.split(",");
	var r=parseInt(c[0]).toString(16);
	var g=parseInt(c[1]).toString(16);
	var b=parseInt(c[2]).toString(16);
	if(r.length==1)r="0"+r;
	if(g.length==1)g="0"+g;
	if(b.length==1)b="0"+b;
	return"#"+r+g+b
}
};

// ----------- almsamim command object -----------//
almsamim.comm = comm = {
_viewmode : 1 ,
_focus : function ()
{
var editor = document.getElementById('box');
editor.contentWindow.focus();
},
// the execCommand function	
_command : function (command)
{
	if (this._viewmode == 1){
	if (command == 'justifycenter' && isWebKit){
	this._focus();
	this._HTML('<div align="center">','</div>','');
	this._focus();
	}else if (command == 'justifyleft' && isWebKit){
	this._focus();
	this._HTML('<div align="left">','</div>','');
	this._focus();
	}else if (command == 'justifyright' && isWebKit){
	this._focus();
	this._HTML('<div align="right">','</div>','');
	this._focus();
	}else{
	this._focus();
	document.getElementById('box').contentWindow.document.queryCommandEnabled(command);
	document.getElementById('box').contentWindow.document.execCommand(command, false, null);
	this._focus();
}
}else {
if (command == 'bold'){
bbfontstyle('[b]','[/b]');
}else if (command == 'italic'){
bbfontstyle('[i]','[/i]');
}else if (command == 'underline'){
bbfontstyle('[u]','[/u]');
}else if (command == 'StrikeThrough'){
bbfontstyle('[s]','[/s]');
}else if (command == 'InsertHorizontalRule'){
bbfontstyle('[hr]','[/hr]');
}else if (command == 'subscript'){
bbfontstyle('[sub]','[/sub]');
}else if (command == 'superscript'){
bbfontstyle('[sup]','[/sup]');
}else if (command == 'justifyleft'){
bbfontstyle('[left]','[/left]');
}else if (command == 'justifyright'){
bbfontstyle('[right]','[/right]');
}else if (command == 'justifycenter'){
bbfontstyle('[center]','[/center]');
}else if (command == 'InsertOrderedlist'){
bbfontstyle('[list=1]','[/list]');
}else if (command == 'InsertUnorderedlist'){
bbfontstyle('[list]','[/list]');
}else {
alert("You must disable BBcode Mode to use this feature");

}
}
},

// create URL of selection through prompt
_url : function (prompturl) 
{
	if (this._viewmode == 1){
	if (prompturl == 1){
		var szURL = prompt(url_enter, "http://");
		var tURL = prompt(url_enter_desc, "");
		this._HTML(' ',' ','<a href ="'+ szURL +'">'+ tURL +'</a>');
	}else{ 
		var szURL = prompt(url_enter, "http://");
		if ((szURL != null) && (szURL != "")) {
			var editor = document.getElementById('box');
			this._focus();
			Editor.queryCommandEnabled("CreateLink");
			Editor.execCommand("CreateLink", false,szURL);
			this._focus();	
		  }
	  }
	  }else {
	  if (prompturl == 1){
		var szURL = prompt(url_enter, "http://");
		var tURL = prompt(url_enter_desc, "");
bbfontstyle('[url=' + szURL +']' + tURL ,'[/url]');
}else{
bbfontstyle('[url=]','[/url]');
}
}
},
// select font size,font-family,heading
_select : function (selectname)
{
	if (this._viewmode == 1){
  var cursel = document.getElementById(selectname).selectedIndex;
  if (cursel != 0) {
    var selected = document.getElementById(selectname).options[cursel].value;
    var editor = document.getElementById('box');
	this._focus();
	Editor.queryCommandEnabled(selectname);
	Editor.execCommand(selectname, false, selected);
	this._focus();
    document.getElementById(selectname).selectedIndex = 0;
  }
  document.getElementById("box").contentWindow.focus();
  	  }else {
alert("You must disable BBcode Mode to use this feature");
}
},
// creating table in the editor
_table : function ()
{
if (this._viewmode == 1){
		var p=prompt("Rows Number :","");
		var q=prompt("Columns Number :","");
		if(p!=null&&q!=null&&!isNaN(p)&&!isNaN(q))
			{
			var r='<table style="width: 100%; padding: 0px; border: none; border: 1px solid #789DB3;">';
			var t="";
			for(irow=0;irow<p;irow++)
				{
				t+="<tr>";
				for(icol=0;icol<q;icol++)
					{
					t+='<td style="font-size: 20px; border: none;border: 1px solid #789DB3; background-color: #F4F4F4;">&nbsp;</td>'
				}
				t+="</tr>"
			}
			r+=t+"</table><br>";
	var editor = document.getElementById('box');
	this._focus();
    this._HTML('','',r);
	this._focus();
    }
	  	  }else {
alert("You must disable BBcode Mode to use this feature");
}
},
//alternative to insertHTML with all browsers to insert bbcode OR html
_HTML : function (tag,tagend,a)
	{
	if (this._viewmode == 1){
	var c;
	var u="";
	var d;
	tagend = tagend.replace("=","");
	if(isIE) {
		d=document.getElementById('box').contentWindow;
		var f=d.document.selection;
		if(f!=null)
			{
			rng=f.createRange();
			u=rng.htmlText;
		}
	} else if (window.getSelection) {
		d=document.getElementById('box').contentWindow;
		d.focus();
		var f=d.getSelection();
		if(f!=""&&f.rangeCount>0)
			{
			rng=f.getRangeAt(f.rangeCount-1).cloneContents();
			var g=d.document.createElement('div');
			g.appendChild(rng);
			u=g.innerHTML;
		}
	}
	if(a){
	var cc = tag+a+tagend;
	}else if(u){
	if(tag=="[code]"||tag=="[code=php]")
	{
			u=u.replace(/[\n\r]/ig,'');
			u=u.replace(/<(br|p|div|li).*?>/ig,"[BR/]");
			u=u.replace(/<\/(p|div).*?>/ig,"");
			u=u.replace(/(<([^>]+)>)/ig,"");
			u=u.replace(/\[BR\/\]/ig,"<br>");
	}
	var cc = tag+u+tagend;	
	}else {
	if(tag=="[code]"||tag=="[code=php]"||tag=="[quote]"){
	var cc = tag+tagend;
	}else{
		if (prompt_bbcode == 1){
		if(tag == "[youtube]"){
			var a = prompt("Enter Youtube Video URL :", "http://www.youtube.com/watch?v=");
				if(a){
				var cc = tag+a+tagend;
				}else{
				var cc = tag+tagend;
			}
		}else if(tag == "[googlevideo]"){
			var a = prompt("Enter Youtube Video URL :", "http://video.google.com/videoplay?docid=");
				if(a){
				var cc = tag+a+tagend;
				}else{
				var cc = tag+tagend;
			}
		}else if(tag == '<div align="center">' || tag == '<div align="right">'  || tag == '<div align="left">' ){
		var cc ='';
		}else {
				var a = prompt("Enter Value :", "");
				if(a){
				var cc = tag+a+tagend;
				}else{
				var cc = tag+tagend;
			}
		}
		}else{
		var cc = tag+tagend;
		}
	}
	}
	if(isIE)
		{
		d.document.execCommand("removeformat",false,"");
		d.focus();
		rng.pasteHTML(cc);
		d.focus();
	}
	else if(isWebKit)
		{
		c=document.getElementById('box').contentWindow;
		c.focus();
		u=u.replace(/</g,"[OPEN]");
		u=u.replace(/>/g,"[CLOSE]");
		c.document.execCommand('insertHTML',false,cc);
		var e=document.getElementById('box').contentWindow.document.body.innerHTML;
		u=u.replace(/\[OPEN\]/g,'<');
		u=u.replace(/\[CLOSE\]/g,'>');
		u=u.replace(/[\n\r]/ig,'');
		document.getElementById('box').contentWindow.document.body.innerHTML=e;
		c.focus();
	}
	else
		{
		c=document.getElementById('box').contentWindow;
		c.focus();
		c.document.execCommand("removeformat",false,"");
		c.document.execCommand('insertHTML',false,cc);
		c.document.execCommand("removeformat",false,"");
		c.focus();
	}
	  	  }else {
		  if(a){
bbfontstyle(tag + a,tagend);
}else{
bbfontstyle(tag ,tagend);
}
}
},
// insert image in the editor
_image : function () 
{
	if (this._viewmode == 1){
	imagePath = prompt(image_enter, 'http://');
	if ((imagePath != null) && (imagePath != "")) {
		editor = document.getElementById('box');
		this._focus();
		Editor.queryCommandEnabled("InsertImage");
		Editor.execCommand("InsertImage", false, imagePath);
		this._focus();	
		}
		  	  }else {
bbfontstyle('[img]','[/img]');
}
},
//colour EXECCOMMAND
_colour : function (colour) 
{
	if (this._viewmode == 1){
	var editor = document.getElementById('box');
	this._focus();
	Editor.queryCommandEnabled('forecolor');
	Editor.execCommand('forecolor', false, colour);
	this._focus();
	  	  }else {
bbfontstyle('[color=' + colour +']','[/color]');
}
},
//toggle BBcode HTML
_toggle : function (colour) 
{
		var Editor = document.getElementById('box').contentWindow.document;
if(this._viewmode == 1)
{
	var source = document.getElementById('box_text');
	var editor = document.getElementById('box');
	editor.style.display = 'none';
	source.style.display = 'block';
	var ceditor = bbcode._HTMLtoBBcode(bbcode._erase(Editor.body.innerHTML));
	source.value = ceditor;
	this._viewmode = 2; // Code
}
else
{
	var source = document.getElementById('box_text');
	var editor = document.getElementById('box');
	editor.style.display = 'block';
	source.style.display = 'none';
	//writing iframe content and style of the editor
	var iframeContent;
	iframeContent  = '<html xmlns="http://www.w3.org/1999/xhtml" dir="' + direction + '" lang="en-gb" xml:lang="en-gb">\n';
	iframeContent += '<head><style type="text/css">body{font-family:Verdana,Arial,Sans-Serif,Tahoma;font-size:12px;background:'+ wbg +';color:' + wcolor + ';margin:2px;padding:2px;}p {margin:0;padding:0;min-height: 1em; } *+html p { min-height: auto; }div {margin:0;padding:0;}</style></head><body>';	
	if (source.value !== ""){
	var v = source.value;
	iframeContent +=  bbcode._BBcodetoHTML(v) ;
	}
	iframeContent += '</body>';
	iframeContent += '</html>';
	Editor.open();
	Editor.write(iframeContent);
	Editor.close();
	this._viewmode = 1; // WYSIWYG
}
},
// print colour palette in a table [phpBB3]
_palette : function (dir, width, height)
{
	var r = 0, g = 0, b = 0;
	var numberlist = new Array(6);
	var color = '';
	numberlist[0] = '00';
	numberlist[1] = '40';
	numberlist[2] = '80';
	numberlist[3] = 'BF';
	numberlist[4] = 'FF';
	document.writeln('<table cellspacing="1" cellpadding="0" border="0">');
	for (r = 0; r < 5; r++)
	{
		if (dir == 'h')
		{
			document.writeln('<tr>');
		}

		for (g = 0; g < 4; g++)
		{
			if (dir == 'v')
			{
				document.writeln('<tr>');
			}
			for (b = 0; b < cpaletc; b++)
			{
				color = String(numberlist[r]) + String(numberlist[g]) + String(numberlist[b]);
				document.write('<td bgcolor="#' + color + '" style="width: ' + width + 'px; height: ' + height + 'px;">');
				document.write('<a href="#" onClick="comm._colour(\'#' + color + '\'); return false;"><img src="' + path + 'images/editor/spacer.gif" width="' + width + '" height="' + height + '" alt="#' + color + '" title="#' + color + '" /></a>');
				document.writeln('</td>');
			}
			if (dir == 'v')
			{
				document.writeln('</tr>');
			}
		}
		if (dir == 'h')
		{
			document.writeln('</tr>');
		}
	}
	document.writeln('</table>');
}
};

//-------  golbal functions -------- //

// gettext into textarea html or bbcode
function Gettext(value,bb) {
		if (bb){
  	 var text = document.getElementById(value);
	 if (comm._viewmode == 2){
	 	var source = document.getElementById('box_text');
	text.value = source.value;
	 comm._viewmode = 1; // WYSIWYG
		 }else {
		 Editor = document.getElementById('box').contentWindow.document;
		 var ceditor = bbcode._HTMLtoBBcode(bbcode._erase(Editor.body.innerHTML));
		 text.value = ceditor;
		 }
		 }else{
		 var text = document.getElementById(value);
 		 Editor = document.getElementById('box').contentWindow.document;
		 var ceditor = Editor.body.innerHTML;
		 text.value = ceditor;
		 }
};
// increase and decrease size of the iframe
function textbox_resize(pix)
{
	var box			= document.getElementById('box');
	var new_height	= (parseInt(box.style.height) ? parseInt(box.style.height) : 200) + pix;
	if (new_height > 0)
	{
		box.style.height = new_height + 'px';
	}
	return false;
};
//visibility of the color pallette
function toggle_visibility(id) 
{
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
};
// adding quote in topic revision
function addquote(post_id, username){
	var message_name = 'message_' + post_id;
	divarea = document.getElementById(message_name).innerHTML;
	comm._HTML('[quote="' + username + '"]','[/quote]',divarea);
};
// add smiley through path as image
function AddSmileyIcon(imagePath,theme){
	// removing dot from URL
		 if (comm._viewmode == 1){
	imagePath = imagePath.replace(/.\/images\/smilies/gi,"/images/smilies");
		comm._focus();
		comm._HTML('','','<img src="'+ imagePath +'" />');
		comm._focus();
		}else{
		bbfontstyle(' ' + theme,' ');
		}
};
//Function to hover button icon
function overIcon(iconItem){
	iconItem.className='oover';
};
//Function to moving off button icon
function outIcon(iconItem){
	iconItem.className='button';
};


/**
* Caret Position object
*/
function caretPosition()
{
	var start = null;
	var end = null;
}


/**
* Get the caret position in an textarea
*/
function getCaretPosition(txtarea)
{
	var caretPos = new caretPosition();
	
	// simple Gecko/Opera way
	if(txtarea.selectionStart || txtarea.selectionStart == 0)
	{
		caretPos.start = txtarea.selectionStart;
		caretPos.end = txtarea.selectionEnd;
	}
	// dirty and slow IE way
	else if(document.selection)
	{
	
		// get current selection
		var range = document.selection.createRange();

		// a new selection of the whole textarea
		var range_all = document.body.createTextRange();
		range_all.moveToElementText(txtarea);
		
		// calculate selection start point by moving beginning of range_all to beginning of range
		var sel_start;
		for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++)
		{		
			range_all.moveStart('character', 1);
		}
	
		txtarea.sel_start = sel_start;
	
		// we ignore the end value for IE, this is already dirty enough and we don't need it
		caretPos.start = txtarea.sel_start;
		caretPos.end = txtarea.sel_start;			
	}

	return caretPos;
}

/**
* Apply bbcodes
*/
function bbfontstyle(bbopen, bbclose)
{
	theSelection = false;

	var textarea = document.getElementById('box_text');

	textarea.focus();

  if (isIE)
   {
      // Get text selection
      theSelection = document.selection.createRange().text;
      var sel = document.selection.createRange();

      if (theSelection)
      {
         // Add tags around selection
         document.selection.createRange().text = bbopen + theSelection + bbclose;
         textarea.focus();
         sel.moveStart('character', bbopen.length);
         sel.moveEnd('character', theSelection.length);
         sel.select();
         theSelection = '';
         return;
      }
   }
	else if (textarea.selectionEnd && (textarea.selectionEnd - textarea.selectionStart > 0))
	{
		mozWrap(textarea, bbopen, bbclose);
		textarea.focus();
		theSelection = '';
		return;
	}
	
	//The new position for the cursor after adding the bbcode
	var caret_pos = getCaretPosition(textarea).start;
	var new_pos = caret_pos + bbopen.length;	
	
	// Open tag
	insert_text(bbopen + bbclose);

	// Center the cursor when we don't have a selection
	// Gecko and proper browsers
	if (!isNaN(textarea.selectionStart))
	{
		textarea.selectionStart = new_pos;
		textarea.selectionEnd = new_pos;
	}	
	// IE
	else if (document.selection)
	{
		var range = textarea.createTextRange(); 
		range.move("character", new_pos); 
		range.select();
		storeCaret(textarea);
	}

	textarea.focus();
	return;
};
function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	var scrollTop = txtarea.scrollTop;

	if (selEnd == 1 || selEnd == 2) 
	{
		selEnd = selLength;
	}

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);

	txtarea.value = s1 + open + s2 + close + s3;
    txtarea.selectionStart = selStart + open.length;
    txtarea.selectionEnd = selEnd + open.length;
	txtarea.focus();
	txtarea.scrollTop = scrollTop;

	return;
};
function insert_text(text, spaces, popup)
{
	var textarea;
	
	if (!popup) 
	{
		textarea = document.getElementById('box_text');
	} 
	else 
	{
		textarea = opener.document.getElementById('box_text');
	}
	if (spaces) 
	{
		text = ' ' + text + ' ';
	}
	
	if (!isNaN(textarea.selectionStart))
	{
		var sel_start = textarea.selectionStart;
		var sel_end = textarea.selectionEnd;

		mozWrap(textarea, text, '')
		textarea.selectionStart = sel_start + text.length;
		textarea.selectionEnd = sel_end + text.length;
	}
	else if (textarea.createTextRange && textarea.caretPos)
	{
		if (baseHeight != textarea.caretPos.boundingHeight) 
		{
			textarea.focus();
			storeCaret(textarea);
		}

		var caret_pos = textarea.caretPos;
		caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
	}
	else
	{
		textarea.value = textarea.value + text;
	}
	if (!popup) 
	{
		textarea.focus();
	}
}

function storeCaret(textEl)
{
	if (textEl.createTextRange)
	{
		textEl.caretPos = document.selection.createRange().duplicate();
	}
}