// This javascript was originally written by Alex King: www.alexking.org
// Modifications noted below were made by Jennifer of scriptygoddess.com :
// Removed all but the "most popular" html tags used in comments
// with the express purpose of including it in the comments form on blogs
// These changes were made on 6/10/2004
// 
// For the FULL version of Alex's script (which has ALOT MORE functionality!
// go here: 
// http://www.alexking.org/blog/2004/06/03/js-quicktags-under-lgpl/
//
// JS QuickTags version 1.0
//
// Copyright (c) 2002-2004 Alex King
// http://www.alexking.org/
//
// Licensed under the LGPL license
// http://www.gnu.org/copyleft/lesser.html
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
// **********************************************************************
//
// This JavaScript will insert the tags below at the cursor position in IE and 
// Gecko-based browsers (Mozilla, Camino, Firefox, Netscape). For browsers that 
// do not support inserting at the cursor position (Safari, OmniWeb) it appends
// the tags to the end of the content.
//
// The variable 'edCanvas' must be defined as the <textarea> element you want 
// to be editing in. See the accompanying 'index.html' page for an example.

var edButtons = new Array();
var edLinks = new Array();
var edOpenTags = new Array();

function edButton(id, display, tagStart, tagEnd, open) {
	this.id = id;				// used to name the toolbar button
	this.display = display;		// label on button
	this.tagStart = tagStart; 	// open tag
	this.tagEnd = tagEnd;		// close tag
	this.open = open;			// set to -1 if tag does not need to be closed
}

edButtons[edButtons.length] = new edButton('ed_bold'
                                          ,'BOLD'
                                          ,'<strong>'
                                          ,'</strong>'
                                          );

edButtons[edButtons.length] = new edButton('ed_italic'
                                          ,'ITALIC'
                                          ,'<em>'
                                          ,'</em>'
                                          );

edButtons[edButtons.length] = new edButton('ed_link'
                                          ,'WWW'
                                          ,''
                                          ,'</a>'
                                          ); // special case

edButtons[edButtons.length] = new edButton('ed_quote'
                                          ,'BLOCKQUOTE'
                                          ,'<blockquote>'
                                          ,'</blockquote>'
                                          );



//----- Start iG:Syntax Hiliter Plugin's Quick Tags -----

edButtons[edButtons.length] =
new edButton('ed_shAS'
,'ActionScript'
,'[as]'
,'[/as]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shASP'
,'ASP'
,'[asp]'
,'[/asp]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shC'
,'C'
,'[c]'
,'[/c]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shCPP'
,'C++'
,'[cpp]'
,'[/cpp]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shCSHARP'
,'C#'
,'[csharp]'
,'[/csharp]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shCSS'
,'CSS'
,'[css]'
,'[/css]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shDELPHI'
,'DELPHI'
,'[delphi]'
,'[/delphi]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shHTML'
,'HTML'
,'[html]'
,'[/html]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shJAVA'
,'JAVA'
,'[java]'
,'[/java]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shJS'
,'JavaScript'
,'[js]'
,'[/js]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shMySQL'
,'MySQL'
,'[mysql]'
,'[/mysql]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shPERL'
,'PERL'
,'[perl]'
,'[/perl]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shPHP'
,'PHP'
,'[php]'
,'[/php]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shPYTHON'
,'PYTHON'
,'[python]'
,'[/python]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shRuby'
,'RUBY'
,'[ruby]'
,'[/ruby]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shSMARTY'
,'SMARTY'
,'[smarty]'
,'[/smarty]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shSQL'
,'SQL'
,'[sql]'
,'[/sql]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shVB'
,'Visual Basic'
,'[vb]'
,'[/vb]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shVBNET'
,'VB.NET'
,'[vbnet]'
,'[/vbnet]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shXML'
,'XML'
,'[xml]'
,'[/xml]'
,''
);

edButtons[edButtons.length] =
new edButton('ed_shCode'
,'CODE'
,'[code]'
,'[/code]'
,''
);
//----- End iG:Syntax Hiliter Plugin's Quick Tags -----
function edShowButton(button, i) {
	if (button.id == 'ed_link') {
		document.write('<input type="button" id="' + button.id + '" class="ed_button" onclick="edInsertLink(edCanvas, ' + i + ');" value="' + button.display + '" />');
	}
	else {
		document.write('<input type="button" id="' + button.id + '" class="ed_button" onclick="edInsertTag(edCanvas, ' + i + ');" value="' + button.display + '" />');
	}
}

function edAddTag(button) {
	if (edButtons[button].tagEnd != '') {
		edOpenTags[edOpenTags.length] = button;
		document.getElementById(edButtons[button].id).value = '/' + document.getElementById(edButtons[button].id).value;
	}
}

function edRemoveTag(button) {
	for (i = 0; i < edOpenTags.length; i++) {
		if (edOpenTags[i] == button) {
			edOpenTags.splice(i, 1);
			document.getElementById(edButtons[button].id).value = 		document.getElementById(edButtons[button].id).value.replace('/', '');
		}
	}
}

function edCheckOpenTags(button) {
	var tag = 0;
	for (i = 0; i < edOpenTags.length; i++) {
		if (edOpenTags[i] == button) {
			tag++;
		}
	}
	if (tag > 0) {
		return true; // tag found
	}
	else {
		return false; // tag not found
	}
}	

function edCloseAllTags() {
	var count = edOpenTags.length;
	for (o = 0; o < count; o++) {
		edInsertTag(edCanvas, edOpenTags[edOpenTags.length - 1]);
	}
}

function edToolbar() {
	document.write('<div id="ed_toolbar">');
	for (i = 0; i < edButtons.length; i++) {
		edShowButton(edButtons[i], i);
	}
	document.write('<input type="button" id="ed_close" class="ed_button" onclick="edCloseAllTags();" value="close" />');
	document.write('</div>');
}

// insertion code

function edInsertTag(myField, i) {
	//IE support
	if (document.selection) {
		myField.focus();
	    sel = document.selection.createRange();
		if (sel.text.length > 0) {
			sel.text = edButtons[i].tagStart + sel.text + edButtons[i].tagEnd;
		}
		else {
			if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
				sel.text = edButtons[i].tagStart;
				edAddTag(i);
			}
			else {
				sel.text = edButtons[i].tagEnd;
				edRemoveTag(i);
			}
		}
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var cursorPos = endPos;
		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos)
			              + edButtons[i].tagStart
			              + myField.value.substring(startPos, endPos) 
			              + edButtons[i].tagEnd
			              + myField.value.substring(endPos, myField.value.length);
			cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
		}
		else {
			if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
				myField.value = myField.value.substring(0, startPos) 
				              + edButtons[i].tagStart
				              + myField.value.substring(endPos, myField.value.length);
				edAddTag(i);
				cursorPos = startPos + edButtons[i].tagStart.length;
			}
			else {
				myField.value = myField.value.substring(0, startPos) 
				              + edButtons[i].tagEnd
				              + myField.value.substring(endPos, myField.value.length);
				edRemoveTag(i);
				cursorPos = startPos + edButtons[i].tagEnd.length;
			}
		}
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
	}
	else {
		if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {
			myField.value += edButtons[i].tagStart;
			edAddTag(i);
		}
		else {
			myField.value += edButtons[i].tagEnd;
			edRemoveTag(i);
		}
		myField.focus();
	}
}

function edInsertContent(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		              + myValue 
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
	} else {
		myField.value += myValue;
		myField.focus();
	}
}

function edInsertLink(myField, i, defaultValue) {
	if (!defaultValue) {
		defaultValue = 'http://';
	}
	if (!edCheckOpenTags(i)) {
		var URL = prompt('Ingresa la URL' ,defaultValue);
		if (URL) {
			edButtons[i].tagStart = '<a href="' + URL + '">';
			edInsertTag(myField, i);
		}
	}
	else {
		edInsertTag(myField, i);
	}
}

