/*

JackinChat.com Message Boards JavaScript
Copyright 2004 - present

*/

function showReply() {
	document.getElementById("replyarea").style.display = "block";
}

function quote(text, author) {
	showReply();
	insertBBCode("[quote=" + author + "]\n" + text + "\n[/quote]", 0);
}

function insertImage() {
	var url = prompt('Enter the URL of the image you want to insert', 'http://');
	insertBBCode("[img]" + url + "[/img]", 0);
}

function insertLink() {
	var url = prompt('Enter the URL of the link you want to insert', 'http://');
	var lnk = prompt('Enter the text you want linked (leave blank for none)', '');
	
	if (lnk == "") {
		insertBBCode("[url]" + url + "[/url]", 0);
	} else {
		insertBBCode("[url=" + url + "]" + lnk + "[/url]", 0);
	}
}

function toggleEmoticons() {
	if (document.getElementById("emoticonarea").style.display == "block") {
		document.getElementById("emoticonarea").style.display = "none";
		document.getElementById("postarea").cols = "61";
		document.getElementById("emoticonbutton").src = "gfx/emoticonbtnopen.gif";
	} else {
		document.getElementById("emoticonarea").style.display = "block";
		document.getElementById("postarea").cols = "47";
		document.getElementById("emoticonbutton").src = "gfx/emoticonbtnclose.gif";
	}
}

function emoticon(code) {
	insertBBCode(code, 0);
}

function bbcode(code) {
	if (bbcode == "img") {
		insertImage();
	} else if (bbcode == "url") {
		insertLink();
	} else {
		insertBBCode(code, 1);
	}
}

function insertBBCode(val, t) {
	//For IE
	var postarea = document.getElementById("postarea");
	if (document.selection) {
		postarea.focus();
		sel = document.selection.createRange();
		if (t == 1) {
			sel.text = "[" + val + "]" + sel.text + "[/" + val + "]";
		} else {
			sel.text = sel.text + val;
		}
	}
  	//For Gecko
  	else if (postarea.selectionStart || postarea.selectionStart == '0') {
    	var startPos = postarea.selectionStart;
    	var endPos = postarea.selectionEnd;
    	if (t == 1) {
    		postarea.value = postarea.value.substring(0, startPos) + "[" + val + "]" + postarea.value.substring(startPos, endPos) + "[/" + val + "]" + postarea.value.substring(endPos, postarea.value.length);
		} else {
			postarea.value = postarea.value.substring(0, startPos) + val + postarea.value.substring(endPos, postarea.value.length);
		}
  	//Sorry Opera and old browsers (for now)
  	} else {
		if (t == 1) {
			postarea.value += "[" + val + "] [/" + val + "]";
		} else {
			postarea.value += val;
		}
  	}
}