// JavaScript Document

/* Open XML connection */
function createXMLHttpRequest() {
	try{return new XMLHttpRequest();}
	catch(e){
		try { return new ActiveXObject('Msxml2.XMLHTTP'); }
		catch(e){
			try { return new ActiveXObject('Microsoft.XMLHTTP'); }
			catch(e){}
		}	
	}
	
	return null;
}

//-----------------------------------------------------------------

function flag_post(varThreadID, varPostID, varMessage) {
	var conf = confirm("Are you sure you want flag this post?\n\n-- " + varMessage + " --\n\nIf you continue, this post will be reported to the site administrators for review.\n");

	if(conf) {
		var xmlHttp = createXMLHttpRequest();
		xmlHttp.open('GET', "/forum/flag_message.asp?threadid=" + varThreadID + "&postid=" + varPostID, false);
		xmlHttp.send(null);
		
		alert('Your request has been sent. If this message is determined to be inappropriate, it will be removed.')
	}
}

function ra_check_message() {

	var errmsg = '';
	
	if (document.getElementById('message').value == '') {
		errmsg = errmsg + '- Please enter a message\n';
	}
	
	if (errmsg != '') {
		errmsg = 'There were some errors in this form:\n\n' + errmsg + '\nPlease correct these errors and resubmit.';
		alert(errmsg); 
	}
	
	if (errmsg == '') { 
		document.ra_forum_message.submit();
	}

}

function ra_delete_thread(id,topic) {
	var conf = confirm("Are you sure you want delete this thread?\n\n-- " + topic + " --");

	if(conf) {
		window.open("/forum/delete_thread.asp?id=" + id,"_self")
	}
}

function ra_delete_message_first(id,message) {
	var conf = confirm("Are you sure you want delete this message?\n\n-- " + message + " --\n\nSince this is the first message in this thread, the entire thread will be deleted.");

	if(conf) {
		window.open("/forum/delete_message.asp?id=" + id,"_self")
	}
}

function ra_delete_message(id,message) {
	var conf = confirm("Are you sure you want delete this message?\n\n-- " + message + " --");

	if(conf) {
		window.open("/forum/delete_message.asp?id=" + id,"_self")
	}
}

function ra_lock_thread(id,topic) {
	var conf = confirm("Are you sure you want lock this thread?\n\n-- " + topic + " --");

	if(conf) {
		window.open("/forum/lock_thread.asp?id=" + id,"_self")
	}
}

function ra_unlock_thread(id,topic) {
	var conf = confirm("Are you sure you want unlock this thread?\n\n-- " + topic + " --");

	if(conf) {
		window.open("/forum/unlock_thread.asp?id=" + id,"_self")
	}
}

//-----------------------------------------------------------------

function textCounter( field, countfield, maxlimit ) {
	countfield.value = maxlimit - field.value.length - 1;
	
	if ( field.value.length >= maxlimit ) {
		field.value = field.value.substring( 0, maxlimit-1 );
		alert( 'Message can only contain ' + maxlimit + ' characters.' );
		return false;
	}
}

var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder)
{
	if (window.event&&event.srcElement.value.length>=maxlength)
		return false
	else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
		var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
	if (pressedkey.test(String.fromCharCode(e.which)))
		e.stopPropagation()
	}
}

function countlimit(maxlength,e,placeholder)
{
	var theform=eval(placeholder)
	var lengthleft=maxlength-theform.value.length
	var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
	if (window.event||e.target&&e.target==eval(placeholder))
	{
		if (lengthleft<0)
		theform.value=theform.value.substring(0,maxlength)
		placeholderobj.innerHTML=lengthleft
	}
}

function displaylimit(thename, theid, thelimit)
{
	var theform=theid!=""? document.getElementById(theid) : thename
	var limit_text='<span id="'+theform.toString()+'">'+thelimit+'</span> characters remaining'
	if (document.all||ns6)
		document.write(limit_text)
	if (document.all){
		eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
		eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
	}
	else if (ns6){
		document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
		document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true); 
	}
}

//-----------------------------------------------------------------

function applyTag(obj, tag)
{
	wrapText(obj, '<'+tag+'>', '</'+tag+'>');
};

function wrapText(obj, beginTag, endTag)
{
	if(typeof obj.selectionStart == 'number')
	{
		// Mozilla, Opera, and other browsers
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;
		
		obj.value = obj.value.substring(0, start) + beginTag + obj.value.substring(start, end) + endTag + obj.value.substring(end, obj.value.length);
	}
	else if(document.selection)
	{
		// Internet Explorer

		// make sure it's the textarea's selection
		obj.focus();
		var range = document.selection.createRange();
		if(range.parentElement() != obj) return false;

	    if(typeof range.text == 'string')
	        document.selection.createRange().text = beginTag + range.text + endTag;
	}
	else
		obj.value += text;
		
};