// ______________________________ PREVIEW ______________________________
var popupPreview = null;
var popupAreaId = '';

function previewPopup(areaId)
{
	popupAreaId = areaId;
	if (popupPreview == null || popupPreview.closed)
	{
		popupPreview = window.open('popup_preview.html', '', 'menubar=no, toolbar=no, statusbar=yes, status=yes, scrollbars=yes, personalbar=no, locationbar=yes, defaultStatus=yes, width=1024, height=768');
	}
	else
	{
		popupPreview.focus();
		popupPreview.preview();
	}
}

function getBBcode()
{
	return document.getElementById(popupAreaId).value;
}

// ______________________________ BBCODE ______________________________
// Script adapted from phpBB code

// Check for Browser & Platform for PC & IE specific bits
// More details 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_moz = 0;

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);

function addBalise(areaId, beginBalise, endBalise)
{
	var txtarea = document.getElementById(areaId);

	txtarea.focus();
	var theSelection = false;
	if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text; // Get text selection
		// Add tags around selection
		document.selection.createRange().text = beginBalise + theSelection + endBalise;
		txtarea.focus();
		theSelection = '';
		return;
	}
	else
	{
		// Add tags around selection
		mozWrap(txtarea, beginBalise, endBalise);
		return;
	}
	
	alert('Coucou');
	storeCaret(txtarea);
}

// From http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	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;
	return;
}

// 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();
}

