/* Text Editor by Alex Kalinin
Last update: 14.02.2009
*/

function addButton(tx, btn)
{
var b = btn;
var tags = ["b", "u", "i", "br", "q" ,"nbsp", "center","right", ];
var tag = "";
var current = tx.value;
switch(btn){
 case "Bold": tag = tags[0]; break;
 case "Underline": tag = tags[1]; break;
 case "Italic": tag = tags[2]; break;
 case "Break": tag = tags[3]; break;
 case "Quote": tag = tags[4]; break;
 case "Indent": tag = tags[5]; break;
 case "Center": tag = tags[6]; break;
 case "Right": tag = tags[7]; break;
}

this.tag(tx, tag);

}

// insert text
function insert(tx, text) {
    if (document.selection) {
      // ie
      tx.focus();
      var sel = document.selection.createRange();
      sel.text = text;
    } else if (tx.selectionStart || tx.selectionStart == '0') {
       // mozilla
       var startPos = tx.selectionStart;
       var endPos = tx.selectionEnd;
       tx.value = tx.value.substring(0, startPos) + text + tx.value.substring(endPos, tx.value.length);
    } else {
       tx.value += text;
    }
    tx.focus();
}


function tag (tx, tg) {
    var uni = false;
	var one_tag = "";
    var start_tag = "[" + tg + "]";
    var end_tag = "[\/" + tg + "]";
	
    if(tg == "br" || tg == "nbsp"){
	end_tag = "";
	}
 
    if (document.selection) {
      // ie
      tx.focus();
      var selected = document.selection.createRange().text;
      var sel = document.selection.createRange();
      sel.text = start_tag + selected + end_tag;
    } else if (tx.selectionStart || tx.selectionStart == '0') {
      // mozilla
      var startPos = tx.selectionStart;
      var endPos = tx.selectionEnd;
      var text = start_tag + tx.value.substring(startPos, endPos) + end_tag;
	  tx.value = tx.value.substring(0, startPos) + text + tx.value.substring(endPos, tx.value.length);
	} else {
      tx.value += start_tag + end_tag;
    }
    tx.focus();
}


function addLink(tx, btn)
{
var link = "";
var title = "";
var text = "";
//Hyperlink
if(btn == "Link"){
link=prompt("URL of Link","http://");
if(link){
title=prompt("Name of Link",link);
text = '[a href="'+link+'"]'+title+'[/a]';
this.insert(tx, text); }
}
//Email
if(btn == "Email")
{
link=prompt("Email: ","");
if(link){
title=prompt("Name of Link",link);
text = '[a mailto='+link+']'+title+'[/a]';
this.insert(tx, text); }
}
//Image
if(btn == "Image")
{
link = prompt("Image link: ", 'http://');
if(link){
text = '[img src='+ link + ' width=200 height=100]';
this.insert(tx, text); }
}
//Video
if(btn == "Video")
{
link = prompt("Video link", "http://");
if(link){
text = '[video type="application/x-shockwave-flash" width="350" height="250" src="' + link +'"]';
this.insert(tx, text); }
}


}

function addbr(tx, e){
//Automatic <br> creation when key pressed
var characterCode
	 if(e && e.which){
	 e = e
	 characterCode = e.which
	 }
	 else{
	 e = event
	 characterCode = e.keyCode
	 }
	 
	 //if Enter
	 if(characterCode == 35 || characterCode == 40){  //13 - Enter, 35 - End, 40 - down key
	  text = "[br]"; //new line
	  this.insert(tx, text);
	 }

}



