﻿// bbCode control by
// subBlue design
// www.subBlue.com

// Startup variables
var imageTag = false;
var theSelection = false;

// Check for Browser & Platform for PC & IE specific bits
// More contents from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav  = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));

var is_win   = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac    = (clientPC.indexOf("mac")!=-1);


// Helpline messages
e_help = "إبراز نص: <span dir='ltr'>[e]نص[/e]</span>";
i_help = "نص مائل: <span dir='ltr'>[i]نص[/i]</span>";
b_help = "نص غامق: <span dir='ltr'>[b]نص[/b]</span>";
u_help = "نص مسطر: <span dir='ltr'>[u]نص[/u]</span>";
k_help = "عنوان: <span dir='ltr'>[t]عنوان[/t]</span>";
q_help = "آية قرآنية: <span dir='ltr'>[q]آية قرآنية[/q]</span>";
h_help = "حديث شريف: <span dir='ltr'>[h]حديث شريف[/h]</span>";
l_help = "إضافة نقطة: <span dir='ltr'>[*]نص[/*]</span>";
ac_help = "توسيط النص: <span dir='ltr'>[c]نص[/c]</span>";
al_help = "محاذاة النص لليسار: <span dir='ltr'>[l]نص[/l]</span>";
ar_help = "محاذاة النص لليمين: <span dir='ltr'>[r]نص[/r]</span>";
aj_help = "ضبط محاذاة النص لليمين ولليسار: <span dir='ltr'>[j]نص[/j]</span>";
pr_help = "اضف صورة إلى اليمين: <span dir='ltr'>[imgr]http://image_url[/img]</span>";
p_help = "اضف صورة: <span dir='ltr'>[img]رابط الصورة[/img]</span>";
fl_help = "اضف فلاش: <span dir='ltr'>[fl]رابط الفلاش[/fl]</span>";
rp_help = "اضف مقطع real player: <span dir='ltr'>[rp]رابط المقطع[/rp]</span>";
mp_help = "اضف مقطع media player: <span dir='ltr'>[mp]رابط المقطع[/mp]</span>";
pl_help = "اضف صورة إلى اليسار: <span dir='ltr'>[imgl]رابط الصورة[/img]</span>";
w_help = "اضف رابط: <span dir='ltr'>[url]الرابط[/url] أو [url=الرابط]عنوان صفحة[/url]</span>";
m_help = "بريد إلكتروني: <span dir='ltr'>[email]العنوان[/email] أو [email=العنوان[صاحب البريد[/email]</span>";
a_help = "اغلق جميع اشارات seCode المفتوحة";
s_help = "لون الخط: <span dir='ltr'>[color=red]النص[/color]</span>";
f_help = "حجم الخط: <span dir='ltr'>[size=x-small]النص[/size]</span>";
x_help = "إدراج هامش: ([1])";
fo_help = "نوع الخط:<span dir='ltr'>[font=arial]النص[/font]</span>";
te_help = "لإضافة جمل مختصرة";

// Define the bbCode tags
bbcode = new Array();
bbtags = new Array(	'[e]','[/e]','[i]',
					'[/i]','[u]','[/u]',
					'[t]','[/t]','[q]',
					'[/q]','[*]','[/*]',
					'[c]','[/c]','[img]',
					'[/img]','[url]','[/url]',
					'[h]','[/h]','[email]',
					'[/email]','[imgr]','[/img]',
					'[imgl]','[/img]','([',
					'])','[r]','[/r]',
					'[l]','[/l]','[rp]',
					'[/rp]','[mp]','[/mp]',
					'[fl]','[/fl]','[/b]',
					'[b]','[j]','[/j]');
imageTag = false;

// Shows the help messages in the helpline window
function helpline(help) {
	document.getElementById("helpbox").innerHTML  = eval(help + "_help");
}


// 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 checkForm() {

	formErrors = false;

	if (document.post.content.value.length < 2) {
		formErrors = "عليك ادخال نص للرسالة عند ادخال موضوع جديد";
	}

	if (formErrors) {
		alert(formErrors);
		return false;
	} else {
		bbstyle(-1);
		//formObj.preview.disabled = true;
		//formObj.submit.disabled = true;
		return true;
	}
}

function emoticon(text) {
	text = ' ' + text + ' ';
	if (document.post.content.createTextRange && document.post.content.caretPos) {
		var caretPos = document.post.content.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		document.post.content.focus();
	} else {
	document.post.content.value  += text;
	document.post.content.focus();
	}
}

function bbfontstyle(bbopen, bbclose) {
	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			document.post.content.value += bbopen + bbclose;
			document.post.content.focus();
			return;
		}
		document.selection.createRange().text = bbopen + theSelection + bbclose;
		document.post.content.focus();
		return;
	} else {
		document.post.content.value += bbopen + bbclose;
		document.post.content.focus();
		return;
	}
	storeCaret(document.post.message);
}


function bbstyle(bbnumber) {

	donotinsert = false;
	theSelection = false;
	bblast = 0;

	if (bbnumber == -1) { // Close all open tags & default button names
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;
			document.post.content.value += bbtags[butnumber + 1];
			buttext = eval('document.post.addbbcode' + butnumber + '.value');
			eval('document.post.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		imageTag = false; // All tags are closed including image tags :D
		document.post.content.focus();
		return;
	}

	if ((clientVer >= 4) && is_ie && is_win)
		theSelection = document.selection.createRange().text; // Get text selection

	if (theSelection) {
		// Add tags around selection
		document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
		document.post.content.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;
				document.post.content.value += bbtags[butnumber + 1];
				buttext = eval('document.post.addbbcode' + butnumber + '.value');
				eval('document.post.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				imageTag = false;
			}
			document.post.content.focus();
			return;
	} else { // Open tags

		if (imageTag && (bbnumber != 14)) {		// Close image tag before adding another
			document.post.content.value += bbtags[15];
			lastValue = arraypop(bbcode) - 1;	// Remove the close image tag from the list
			document.post.addbbcode14.value = "Img";	// Return button back to normal state
			imageTag = false;
		}

		// Open tag
		//document.post.content.value += bbtags[bbnumber];
		if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		//arraypush(bbcode,bbnumber+1);
		eval('document.post.addbbcode'+bbnumber+'.value += "*"');
		document.post.content.focus();
		return;
	}
	storeCaret(document.post.message);
}

// Insert at Claret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

