function replace_bold() {
	      // code for IE
	      var textarea = document.getElementById("text");
	      if (document.selection) {
		      textarea.focus();
		      var sel = document.selection.createRange();
		      // Finally replace the value of the selected text with this new replacement one
		      sel.text = '[b]' + sel.text + '[/b]';
	      }
	      else {
		      // code for Mozilla
		      var textarea = document.getElementById("text");
		      var len = textarea.value.length;
		      var start = textarea.selectionStart;
		      var end = textarea.selectionEnd;
		      var sel = textarea.value.substring(start, end);
		      // This is the selected text
		      var replace = '[b]' + sel + '[/b]';
		      // Here we are replacing the selected text with this one
		      textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
	      }
}

function replace_italic() {
    // code for IE
    var textarea = document.getElementById("text");
    if (document.selection) {
	      textarea.focus();
	      var sel = document.selection.createRange();
	      // Finally replace the value of the selected text with this new replacement one
	      sel.text = '[i]' + sel.text + '[/i]';
    }
    else {
	      // code for Mozilla
	      var textarea = document.getElementById("text");
	      var len = textarea.value.length;
	      var start = textarea.selectionStart;
	      var end = textarea.selectionEnd;
	      var sel = textarea.value.substring(start, end);
	      // This is the selected text
	      var replace = '[i]' + sel + '[/i]';
	      // Here we are replacing the selected text with this one
	      textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
    }
}

function replace_underline() {
    // code for IE
    var textarea = document.getElementById("text");
    if (document.selection) {
	      textarea.focus();
	      var sel = document.selection.createRange();
	      // Finally replace the value of the selected text with this new replacement one
	      sel.text = '[u]' + sel.text + '[/u]';
    }
    else {
	      // code for Mozilla
	      var textarea = document.getElementById("text");
	      var len = textarea.value.length;
	      var start = textarea.selectionStart;
	      var end = textarea.selectionEnd;
	      var sel = textarea.value.substring(start, end);
	      // This is the selected text
	      var replace = '[u]' + sel + '[/u]';
	      // Here we are replacing the selected text with this one
	      textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
    }
}

function replace_color() {
	// get color
	var color=document.getElementById("forum_newpost_fontcolor").value;
    // code for IE
    var textarea = document.getElementById("text");
    if (document.selection) {
	      textarea.focus();
	      var sel = document.selection.createRange();
	      // Finally replace the value of the selected text with this new replacement one
	      sel.text = '[color='+color+']' + sel.text + '[/color]';
    }
    else {
	      // code for Mozilla
	      var textarea = document.getElementById("text");
	      var len = textarea.value.length;
	      var start = textarea.selectionStart;
	      var end = textarea.selectionEnd;
	      var sel = textarea.value.substring(start, end);
	      // This is the selected text
	      var replace = '[color='+color+']' + sel + '[/color]';
	      // Here we are replacing the selected text with this one
	      textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
    }
}

function replace_code() {
    // code for IE
    var textarea = document.getElementById("text");
    if (document.selection) {
	      textarea.focus();
	      var sel = document.selection.createRange();
	      // Finally replace the value of the selected text with this new replacement one
	      sel.text = '[code]' + sel.text + '[/code]';
    }
    else {
	      // code for Mozilla
	      var textarea = document.getElementById("text");
	      var len = textarea.value.length;
	      var start = textarea.selectionStart;
	      var end = textarea.selectionEnd;
	      var sel = textarea.value.substring(start, end);
	      // This is the selected text
	      var replace = '[code]' + sel + '[/code]';
	      // Here we are replacing the selected text with this one
	      textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
    }
}

function replace_link() {
    // code for IE
    var textarea = document.getElementById("text");
    if (document.selection) {
	      textarea.focus();
	      var sel = document.selection.createRange();
	      // Finally replace the value of the selected text with this new replacement one
	      sel.text = '[url]' + sel.text + '[/url]';
    }
    else {
	      // code for Mozilla
	      var textarea = document.getElementById("text");
	      var len = textarea.value.length;
	      var start = textarea.selectionStart;
	      var end = textarea.selectionEnd;
	      var sel = textarea.value.substring(start, end);
	      // This is the selected text
	      var replace = '[url]' + sel + '[/url]';
	      // Here we are replacing the selected text with this one
	      textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
    }
}

function replace_font() {
	// get font
	var font=document.getElementById("forum_newpost_fontname").value;
    // code for IE
    var textarea = document.getElementById("text");
    if (document.selection) {
	      textarea.focus();
	      var sel = document.selection.createRange();
	      // Finally replace the value of the selected text with this new replacement one
	      sel.text = '[font='+font+']' + sel.text + '[/font]';
    }
    else {
	      // code for Mozilla
	      var textarea = document.getElementById("text");
	      var len = textarea.value.length;
	      var start = textarea.selectionStart;
	      var end = textarea.selectionEnd;
	      var sel = textarea.value.substring(start, end);
	      // This is the selected text
	      var replace = '[font='+font+']' + sel + '[/font]';
	      // Here we are replacing the selected text with this one
	      textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
    }
}

function replace_size() {
	// get size
	var size=document.getElementById("forum_newpost_fontsize").value;
    // code for IE
    var textarea = document.getElementById("text");
    if (document.selection) {
	      textarea.focus();
	      var sel = document.selection.createRange();
	      // Finally replace the value of the selected text with this new replacement one
	      sel.text = '[size='+size+']' + sel.text + '[/size]';
    }
    else {
	      // code for Mozilla
	      var textarea = document.getElementById("text");
	      var len = textarea.value.length;
	      var start = textarea.selectionStart;
	      var end = textarea.selectionEnd;
	      var sel = textarea.value.substring(start, end);
	      // This is the selected text
	      var replace = '[size='+size+']' + sel + '[/size]';
	      // Here we are replacing the selected text with this one
	      textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
    }
}

var timer_active=0;

function getY( oElement ) {
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function getX( oElement ) {
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function show_smileys() { 
	var tmpY=document.getElementById("forum_newpost_link_smiley").offsetTop;
	var tmpX=document.getElementById("forum_newpost_link_smiley").offsetLeft;
	document.getElementById("forum_newpost_emotes_popup").style.top=tmpY+document.body.scrollTop+"px";
	document.getElementById("forum_newpost_emotes_popup").style.left=tmpX+document.body.scrollLeft+"px";
	document.getElementById("forum_newpost_emotes_popup").style.display="inline";
	enable_timer();
}

function starttimer() {
	setTimeout("close_smileys();",2000);
}

function close_smileys() {
	if (timer_active==1) {
		close_popup();
	}
	else {
		starttimer();
	}
}

function close_popup() {
	timer_active=0;
	document.getElementById('forum_newpost_emotes_popup').style.display="none";
}

function enable_timer() {
	timer_active=1;
}

function disable_timer() {
	timer_active=0;
}

function replace_smiley(emoticon) {
    // code for IE
    var textarea = document.getElementById("text");
    if (document.selection) {
	      textarea.focus();
	      var sel = document.selection.createRange();
	      // Finally replace the value of the selected text with this new replacement one
	      sel.text = sel.text + emoticon;
    }
    else {
	      // code for Mozilla
	      var textarea = document.getElementById("text");
	      var len = textarea.value.length;
	      var start = textarea.selectionStart;
	      var end = textarea.selectionEnd;
	      var sel = textarea.value.substring(start, end);
	      // This is the selected text
	      var replace = sel + emoticon;
	      // Here we are replacing the selected text with this one
	      textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
    }
}


