// <?php !! This fools phpdocumentor into parsing this file
/**
* Missus - Private Messages for Joomla
* Version: 1.1
* Author: Bárbara Irene Meclazcke
* URL:  ewriting.com.ar
* mail: aclaina@yahoo.com.ar
* FileName: editor.missus.js
* Date: 08/10/2006
* License: GNU General Public License
**/
<!--
// bbCode control by subBlue design <www.subBlue.com>
// extended for Missus by Bárbara Irene Meclazcke (aka Aclaina) <ewriting.com.ar>

//INIT VARS
var imageTag = false;
var theSelection = false;

//CHECK BROWSER & PLATFORM
var _navegador = navigator.userAgent;
//CLIENT INFO
var clientPC = _navegador.toLowerCase(); 
//BROWSER VERSION
var clientVer = parseInt( navigator.appVersion ); 
//CHECK IE
var is_ie = /msi/i.test(_navegador);
//CHECK OPERA
var is_opera = /opera/i.test(_navegador);
//CHECK GECKO COMPATIBLE (NETSCAPE, FIREFOX, CAMINO, ETC)
var is_moz = /gecko/i.test(_navegador);
//CHECK IF ANY OTHER
var is_nav = !(is_ie || is_moz);
//CHECK IF WIN OR MAC
var is_win = ( ( clientPC.indexOf( "win" ) != -1 ) || ( clientPC.indexOf( "16bit" ) != -1 ) );
var is_mac = ( clientPC.indexOf( "mac" ) != -1 );

//BBCODE TAGS
bbcode = new Array();
bbtags = new Array( '[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]', '[quote]', '[/quote]', '[code]', '[/code]', '[ul]', '[/ul]', '[ol]', '[/ol]', '[img size=150]', '[/img]', '[url]', '[/url]', '[li]', '[/li]' );
imageTag = false;

//SHOW HELP MESSAGE
function helpline( help ) 
{	document.adminForm.helpbox.value = help;
}

//RETURN ARRAY SIZE
function getarraysize( thearray ) 
{	for ( i = 0; i < thearray.length; i++ ) {
		if ( ( thearray[i] == "undefined" ) || ( thearray[i] == "" ) || ( thearray[i] == null ) )
			return i;
	}
	return thearray.length;
}

//PUSH INTO ARRAY STACK
function arraypush( thearray, value ) 
{	thearray[getarraysize( thearray )] = value;
}

//POP FROM ARRAY STACK
function arraypop( thearray ) 
{	thearraysize = getarraysize( thearray );
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

//INSERT BBCODE INTO TEXTAREA
function bbstyle( bbnumber ) 
{	var txtarea = document.adminForm.message;	
	txtarea.focus();
	donotinsert = false;
	theSelection = false;
	bblast = 0;
	//CLOSE ALL OPEN TAGS & RESET BUTTON NAMES
	if ( bbnumber == -1 ) { 
		while ( bbcode[0] ) {
			butnumber = arraypop( bbcode ) - 1;
			insertTextMissus( bbtags[butnumber + 1] );
			buttext = eval( 'document.adminForm.addbbcode' + butnumber + '.value' );
			eval( 'document.adminForm.addbbcode' + butnumber + '.value ="' + buttext.substr( 0, ( buttext.length - 1 ) ) + '"' );
		}
		imageTag = false;
		txtarea.focus();
		return;
	}
	//IF IS IE V>4 OVER WIN OR OPERA
	if ( ( ( clientVer >= 4 ) && is_ie && is_win ) || is_opera ) {
		//GET SELECTED TEXT
		theSelection = document.selection.createRange().text; 
		if ( theSelection ) { //ADD TAGS ON START & END OF SELECTION
			document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
			txtarea.focus();
			theSelection = '';
			return;
		}
	} //IF IS GECKO
	else if ( txtarea.selectionEnd && ( txtarea.selectionEnd - txtarea.selectionStart > 0 ) ) {
		mozWrap( txtarea, bbtags[bbnumber], bbtags[bbnumber+1] );
		return;
	}
	//SEARCH LAST OCURRENCE OF THE SAME TAG
	for ( i = 0; i < bbcode.length; i++ ) {
		if ( bbcode[i] == bbnumber + 1 ) {
			bblast = i;
			donotinsert = true;
		}
	}
	//CLOSE ALL EQUAL OPEN TAGS AND RESET BUTTONS NAMES
	if ( donotinsert ) {
		while ( bbcode[bblast] ) {
			butnumber = arraypop( bbcode ) - 1;
			insertTextMissus( bbtags[butnumber + 1] );
			buttext = eval( 'document.adminForm.addbbcode' + butnumber + '.value' );
			eval( 'document.adminForm.addbbcode' + butnumber + '.value ="' + buttext.substr( 0, ( buttext.length - 1 ) ) + '"' );
			imageTag = false;
		}
		txtarea.focus();
		return;
	} else { //OPEN TAG
		//CLOSE IMG TAG BEFORE ADDING NEW ONE
		if ( imageTag && ( bbnumber != 14 ) ) {
			insertTextMissus( bbtags[15] );
			lastValue = arraypop( bbcode ) - 1;
			document.adminForm.addbbcode14.value = "Img";
			imageTag = false;
		}
		//OPEN NEW TAG
		insertTextMissus( bbtags[bbnumber] );
		//STOP MORE TAGS AFTER UNCLOSED IMG TAG
		if ( ( bbnumber == 14 ) && ( imageTag == false ) ) imageTag = 1; 
		arraypush( bbcode, bbnumber + 1 );
		eval( 'document.adminForm.addbbcode' + bbnumber + '.value += "*"' );
		txtarea.focus();
		return;
	}
	storeCaret( txtarea );
}

//WRAP SELECTED TEXT FOR GECKO
//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.
//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();
}

//INSERT FONT SIZE & COLOR
function bbfontstyle( bbopen, bbclose ) 
{	var txtarea = document.adminForm.message;
	//IF IS IE V>4 OVER WIN OR OPERA
	if ( ( ( clientVer >= 4 ) && is_ie && is_win ) || is_opera ) {
		theSelection = document.selection.createRange().text;
		if ( !theSelection ) {
			insertTextMissus( bbopen );
			insertTextMissus( ' ' + bbclose );
			txtarea.focus();
			return;
		}
		document.selection.createRange().text = bbopen + theSelection + bbclose;
		txtarea.focus();
		return;
	} //IF GECKO
	else if ( txtarea.selectionEnd && ( txtarea.selectionEnd - txtarea.selectionStart > 0 ) ) {
		mozWrap( txtarea, bbopen, ' ' + bbclose );
		return;
	} //IF OTHER
	else {		
		insertTextMissus( bbopen );
		insertTextMissus( ' ' + bbclose );
		txtarea.focus();
	}
	storeCaret( txtarea );
}

//INSERT SMILIES
function emo( smilie )
{	insertTextMissus( smilie );
	document.adminForm.message.focus();
}

//INSERT TEXT AT CURSOR IN MESSAGE AREA, DEPENDING ON BROWSER ^_^
//FROM OPERA V>8, INSERTING BEHAVIOUR IS ALIKE IE OR MOZILLA, BOTH ARE VALID
function insertTextMissus( insert ) 
{	var txtarea = document.adminForm.message;	
	//IF IS IE V>4 OVER WIN OR OPERA
	if ( ( ( clientVer >= 4 ) && is_ie && is_win ) || is_opera ) {
		insertIE( txtarea, insert );
		return;
	} //IF GECKO
	else if ( is_moz ) {
		insertGecko( txtarea, insert );
		return;
	} //IF OTHER
	else {
		insertOther( txtarea, insert );		
		return;
	}
	storeCaret( txtarea );
}

//INSERT IN IE
//BASED FROM http://www.pepemolina.com/editor/index.html
function insertIE( txtarea, insert )
{	txtarea.focus();
	if ( txtarea.createTextRange ) {
		mytext = document.selection.createRange().text;
		if ( txtarea.createTextRange )
			txtarea.posi = document.selection.createRange().duplicate();
		with ( txtarea ) {
			var actuar = ( posi.text == "" );
			posi.text = insert;
			if ( !actuar )
				posi.moveStart( "character", -insert.length );
			posi.select();
		}
	}
	return;
}

//INSERT IN GECKO
//BASED FROM http://www.pepemolina.com/editor/index.html
function insertGecko( txtarea, insert )
{	var Start = txtarea.selectionStart;
	var End = txtarea.selectionEnd;
	var inicio = txtarea.value.substr( 0, Start );
	var fin = txtarea.value.substr( End, txtarea.value.length );
	txtarea.value = inicio + insert + fin;
	if ( Start == End ) {
		txtarea.selectionStart = inicio.length + insert.length;
		txtarea.selectionEnd = txtarea.selectionStart;
	} else {
		txtarea.selectionStart = inicio.length;
		txtarea.selectionEnd = inicio.length + insert.length;
	}
	txtarea.focus();
	return;
}

//INSERT IN OTHERS UNKNOWN BROWSERS
//BASED FROM http://www.pepemolina.com/editor/index.html
function insertOther( txtarea, insert )
{	txtarea.value += insert;
	txtarea.focus();
	return;
}