/* 	These two functions are a hack for the bug in IE7 that
	doesnt handle return false correctly in onclick events.
	I made these, wow!										*/

function myfalse (el) {
	if ( navigator.appVersion.match("MSIE 7") ) {
		el.href = "javascript:;";
	}
	return false;
}
function myconfirm (msg,el) {
	if ( navigator.appVersion.match("MSIE 7") ) {
		if ( !confirm(msg) ) {
			if ( !el.oldhref ) { el.oldhref = el.href; }
			el.href = "javascript:;";
		}
		else {
			if ( el.oldhref ) { el.href = el.oldhref; }
		}
	}
	else {
		return confirm(msg);
	}
}


function fileChange(oObj) /* Function shamelessly stolen from CSM - Sorry >:-( */
{
	sFile = oObj.value.split('\\');
	if (sFile.length > 0)
	{
		sFile = sFile[sFile.length - 1];
	}
	else
	{
		sFile = oObj.value;
	}
	document.getElementById('file_span').innerHTML = sFile;
}

/* In fact, this function is stolen from CSM too, along with all the other bbcode ones!
   I mean they just did such a good job of this coding thing, why do it again? :D */

function doTag(sTag, sName) 
{
	var oTextArea = document.getElementById(sName);

	if (document.selection)
	{
		sSelection = document.selection.createRange().text;
		if (sSelection)
		{
			document.selection.createRange().text = '[' + sTag + ']' + sSelection + '[/' + sTag + ']';
			oTextArea.focus();
			return;
		}
	}
	else if (oTextArea.selectionEnd && (oTextArea.selectionEnd - oTextArea.selectionStart > 0))
	{
		mozWrap(oTextArea, '[' + sTag + ']', '[/' + sTag + ']');
		return;
	}

	alert('Please select some text to use this button.');
}

function mozWrap(oTextArea, open, close)
{
	var selLength = oTextArea.textLength;
	var selStart = oTextArea.selectionStart;
	var selEnd = oTextArea.selectionEnd;
	if (selEnd == 1 || selEnd == 2)
		selEnd = selLength;

	var s1 = (oTextArea.value).substring(0,selStart);
	var s2 = (oTextArea.value).substring(selStart, selEnd);
	var s3 = (oTextArea.value).substring(selEnd, selLength);
	oTextArea.value = s1 + open + s2 + close + s3;
	return;
}

var oSelection = null;
var sSelPre = null;
var sSelPost = null;

function getSel(oTextArea)
{
	if (oTextArea.selectionEnd && (oTextArea.selectionEnd - oTextArea.selectionStart > 0))
	{
		var selLength = oTextArea.textLength;
		var selStart = oTextArea.selectionStart;
		var selEnd = oTextArea.selectionEnd;
		if (selEnd == 1 || selEnd == 2)
			selEnd = selLength;

		sSelPre = (oTextArea.value).substring(0,selStart);
		sSelPost = (oTextArea.value).substring(selEnd, selLength);

		return (oTextArea.value).substring(selStart, selEnd);
	}
	else if (document.selection)
	{
		oSelection = document.selection.createRange();
		if (oSelection.text)
			return oSelection.text;
		else
			oSelection = null;
	}

	return '';
}

function showDiv (div) { document.getElementById(div).style.display = 'block'; }
function hideDiv (div) { document.getElementById(div).style.display = 'none'; }
function toggleVis (div,type) {
	if ( !type || type == "id" ) { el = document.getElementById(div); }	else { el = div; }
	if ( el.style.display == "none" ) { el.style.display = 'block'; } else { el.style.display = 'none'; }
}
function hideDivO (ob) { ob.style.display = 'none'; }

function getPos(oTarget)
{
	var oPos = {
		x: 0,
		y: 0
	};
	while (oTarget != document.body && oTarget != null)
	{
		oPos.y += oTarget.offsetTop;
		oPos.x += oTarget.offsetLeft;
		oTarget = oTarget.offsetParent;
	}
	return oPos;
}
var http = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");

function editCom (id,button) {
	var oPos = {
		x: mousex,
		y: mousey
	}
	var popupdiv = document.getElementById('editcomdiv'); // div holding everything.
	popupdiv.style.display = 'none';
	texta = document.getElementById('edit_comment'); // the textarea
	texta.value = "Retrieving comment " + id + "...";
	texta.disabled = true;
	document.getElementById('edit_comment_id').value = id; // the hidden input with comment id.
    http.open('post', '/tools/getcom.php5?id='+id, true); // get the comment
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.onreadystatechange = function () {
		if(http.readyState == 4){				// when response comes back
      		var response = http.responseText;	// set the value of
			texta.disabled = false;
	      	texta.value = response;				// the text area to it
   		}
	}
    http.send('ie=crap'); // do the request
	popupdiv.style.left = (oPos.x-310) + 'px'; // position the
	popupdiv.style.top = (oPos.y-110) + 'px'; // container div
	popupdiv.style.position = 'absolute'; 
	popupdiv.style.display = 'block'; // show the div
}

function delCom (id) {
	divi = document.getElementById('delcomdiv');
}

function popGoss (id,button,e) {
	//var oPos = getPos(button);
	//var oPos = mousePos(e);
	var oPos = {
		x: mousex,
		y: mousey
	}
	var popupdiv = document.getElementById('gossip_add'); // div holding everything.
	popupdiv.style.display = 'none';
	document.getElementById('goss_add_id').value = id; // the hidden input with comment id.
	if ( id == "new" ) {
		popupdiv.style.left = (oPos.x-100) + 'px'; // position the
		popupdiv.style.top = (oPos.y-10) + 'px'; // container div
		document.getElementById('goss_add_button').value = 'Submit';
		document.getElementById('goss_add_title').value = '';
		document.getElementById('goss_add_text').value = '';
	}
	else {
		textt = document.getElementById('goss_add_title'); // the title
		textt.value = "Retreiving gossip post " + id + "...";
		textt.disabled = true;
		texta = document.getElementById('goss_add_text'); // the textarea
		texta.value = "Retrieving gossip post " + id + "...";
		texta.disabled = true;
		document.getElementById('goss_add_button').value = 'Edit';
    	http.open('post', '/tools/getgoss.php5?id='+id, true); // get the comment
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    http.onreadystatechange = function () {
			if(http.readyState == 4){				// when response comes back
    	  		var response = http.responseText;	// set the value of
				response = response.split(String.fromCharCode(27));
				texta.disabled = false;
				textt.disabled = false;
				textt.value = response[0];
	      		texta.value = response[1];				// the text area to it
	   		}
		}
	    http.send('ie=crap'); // do the request
		popupdiv.style.left = (oPos.x-310) + 'px'; // position the
		popupdiv.style.top = (oPos.y-110) + 'px'; // container div
	}
	popupdiv.style.position = 'absolute'; 
	popupdiv.style.display = 'block'; // show the div
}

function spawndiv (id,x,y,w,h,title,source) {
	if ( !document.getElementById(id) ) {
		thediv = document.createElement("div");
		thediv.className = 'popupdiv';
		thediv.id = id;
		thediv_header = document.createElement("div");
		thediv_header.className = 'popupdiv_head';
		thediv_header.onmousedown = function(){grab(this.parentNode);}
		thediv_close = document.createElement("a");
		thediv_close.href = '#';
		thediv_close.onclick = function(){hideDivO(this.parentNode.parentNode); return myfalse(this);}
		thediv_close.style.styleFloat = 'right';
		thediv_close.style.cssFloat = 'right';
		thediv_close.appendChild(document.createTextNode('Close'));
		thediv_contentdiv = document.createElement("div");
		thediv_contentdiv.id = id+'_content';
		thediv_contentdiv.style.width = '100%'; //(parseInt(w)-5)+'px';
		thediv_contentdiv.style.height = (parseInt(h)-20)+'px';
		thediv_contentdiv.style.overflow = 'auto';
		thediv_contentdiv_id = id+'_content';
		thediv_contentdiv.appendChild(document.createTextNode('Loading...'));
		thediv_header.appendChild(thediv_close);
		thediv_header.appendChild(document.createTextNode(title));
		thediv.appendChild(thediv_header);
		thediv.appendChild(thediv_contentdiv);
		document.body.appendChild(thediv);
	}
	thediv.style.width = w+'px';
	thediv.style.height = h+'px';
	thediv.style.left = x+'px';
	thediv.style.top = y+'px';
	thediv.style.position = 'absolute';
	thediv.style.display = 'block';
    http.open('post', source, true); // get the comment
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = function () {
		if(http.readyState == 4){				// when response comes back
			document.getElementById(thediv_contentdiv_id).innerHTML = http.responseText;
	  	}
	}
	http.send('ie=crap'); // do the request
}

function show_bbcode () {
	spawndiv('bbcode_div',mousex,mousey-100,400,300,'BB Code List','/tools/bbcode.php');
}

function addoption() {
	var pollopdiv = document.getElementById('options');
	if ( numoptions < 10 ) {
		numoptions = numoptions + 1;
		poll_para = document.createElement("p");
		poll_lab = document.createElement("label");
		poll_input = document.createElement("input");
		poll_input.type = "text";
		poll_input.name = "add_poll_option["+numoptions+"]";
		poll_input.className = "default_form_input";
		poll_input.style.width = "250px";
		poll_lab.appendChild(document.createTextNode("Option: "));
		poll_lab.appendChild(poll_input);
		poll_para.appendChild(poll_lab);
		pollopdiv.appendChild(poll_para);
		//pollopdiv.innerHTML += '<p><label>Option: <input type="text" name="add_poll_option['+numoptions+']" class="default_form_input" style="width: 250px;" /></label></p>';
	}
	else {
		alert("Come on! Don't you think 10 options is enough?");
	}
}

function setdiv (div,url,params,loadim,errmsg) {
	setdiv_div = div;
	div.innerHTML = '<div style="text-align: center;"><img src="/images/loaders/'+loadim+'" alt="Loading..." title="Loading..." /></div>';
	lo = setTimeout(function(){
		div.innerHTML = '<div style="text-align: center;"><img src="/images/icons/error.png" alt="Error" title="Error" /> '+errmsg+'</div>';
	}, 10000)  
    http.open('post', url, true); // get the comment
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = function () {
		if(http.readyState == 4){				// when response comes back
			clearTimeout(lo)
			setdiv_div.innerHTML = http.responseText;
	  	}
	}
	http.send(params);
}

function ajaxSearch () {
	var para = 'search='+encodeURI(document.getElementById('searchinput').value)+'';
	setdiv(document.getElementById('search_results'),'/tools/search.php5',para,'squares_circle.gif','Search failed.');
}