var focused_element;

function set_focused_element ( element_name ) {
	focused_element = element_name;
}

function smilie_add ( text ) {
	var tmp = ":" + text + ":";
	insertAtCursor(document.getElementById('info')[focused_element], tmp);
}



function bold_add () {
	var bold_text = prompt("Enter the text you would like to appear in bold", "");
	var tmp = "[b]" + bold_text + "[/b]";
	
	insertAtCursor(document.getElementById('info')[focused_element], tmp);
}

function italic_add () {
	var bold_text = prompt("Enter the text you would like to appear in italics", "");
	var tmp = "[i]" + bold_text + "[/i]";
	
	insertAtCursor(document.getElementById('info')[focused_element], tmp);
}

function underline_add () {
	var bold_text = prompt("Enter the text you would like to appear underlined", "");
	var tmp = "[u]" + bold_text + "[/u]";
	
	insertAtCursor(document.getElementById('info')[focused_element], tmp);
}



function link_add() {
	var input_url = prompt("Enter the link address", "http://");
	var input_text = prompt("Enter the text for the link", "");
	var tmp = '[link href=' + input_url + ']' + input_text + ' [/link]';
	
	insertAtCursor(document.getElementById('info')[focused_element], tmp);
}

function list_add() {
	var tmp = "[list]";
	
	do {
		var input_item = prompt("Enter the list item\nLeave blank to end the list", "");
		if ( input_item != "" ) {
			tmp = tmp + '::' + input_item + '';
		}
	} while ( input_item != "" )
	
	tmp += '[/list]';
	
	insertAtCursor(document.getElementById('info')[focused_element], tmp);
}

function indent_add () {
	var tmp = "[indent /]";
	insertAtCursor(document.getElementById('info')[focused_element], tmp);
}

function add_inline_photo ( photo_info ) {
	var tmp = photo_info;
	insertAtCursor(document.getElementById('info')[focused_element], tmp);
}



function add_font(text) {
	// first we need to find out if the tag pressed is already open
	if ( text == "font color") {
		 var tmp = '[font:' + document.info.font_color.value + '][/font]';
	} else if ( text == "font size") {
		var tmp = '[font:' + document.info.font_size.value + '][/font]';
	}
	
	insertAtCursor(document.getElementById('info')[focused_element], tmp);
}


function correct_spelling(text) {
	// put the new stuff into the text box completly replacing the old stuff
	text = text.replace ( /\[sp_checker_r\]/g, "\r" );
	text = text.replace ( /\[sp_checker_n\]/g, "\n" );
	
	document.getElementById('info')[focused_element].value = text;
	document.getElementById('info')[focused_element].focus();
}

function insertAtCursor(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	
	//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);
	} else {
		myField.value += myValue;
	}
	
	myField.focus();
}



function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
