function manageFiles(id, type)
{	
	if(type == undefined)
		var popup = window.open("/files-manager/" + id, "ManageFiles","menubar=0,resizable=0,status=0,location=0,directories=0,toolbar=0,width=400"); 
	else
		var popup = window.open("/files-manager/" + id + "/" + type, "ManageFiles","menubar=0,resizable=0,status=0,location=0,directories=0,toolbar=0,width=600"); 
	//var x = (window.innerWidth / 2) - 100;
	try{
		var x = (getWindowWidth() / 2) - 100;
	}catch(ex)
	{
		var x = 300;
	}
	popup.moveTo(x,300);
	popup.focus();
}
function downloadFile(id, lines)
{
	var height = 310 + (lines * 50); 
	//var x = (window.innerWidth / 2) - 100;
	try{
		var x = (getWindowWidth() / 2) - 200;
		var popup = window.open("/files/" + id, "files",("menubar=0,resizable=1,status=0,location=0,directories=0,toolbar=0,width=600,height=" + parseInt(height))); 
	}catch(ex)
	{
		var x = 300;
	}
	popup.moveTo(parseInt(x),300);
	popup.focus();
}
function report(type,id)
{
	try{
		var popup = window.open("/report/" + type + "/" + id + "/" + encodeURI(window.location), "Abuse Report", "menubar=0,resizable=1,status=0,location=0,directories=0,toolbar=0,width=400,height=300"); 
		popup.focus();
	}catch(ex)
	{
	}
}
function deleteItem(id, method)
{
	if(method == undefined)
	{
		document.getElementById(id).innerHTML = '';
	}else{
		document.getElementById(id).style.display = 'none';
	}
}
function viewDate(month, year)
{
	window.location = "/calendar/" + year + "/" + month;
}
function checkUsername(obj)
{
	ajaxRequest("table=check-username&username=" + obj.value, update);
	function update(text)
	{
		if(text == "bad")
		{
			obj.focus();
			obj.className = "bad";
		}
		else if(text == "taken")
		{
			obj.className = "bad";
			alert("Username already taken.");
			obj.focus();
		}
		else
		{
			obj.className = "happy";
			return true;
		}
		
	}
}


function ajaxRequest(query, state_function)
{
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{// code for IE7, Firefox, Opera, etc.
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if ( xmlhttp != null )
	{
		xmlhttp.onreadystatechange=state_Change;
		xmlhttp.open("GET","/ajax.php?"+query,true);
		xmlhttp.send(null);
		return true;
	}
	else
	{
	  alert("Your browser does not support AJAX. Which is really quite odd in the reality of it, you're either using outdated technology or you're quite paranoid.");
	  return false;
	}
	function state_Change()
	{
		if (xmlhttp.readyState==4)
		{// 4 = "loaded"
			if (xmlhttp.status==200)
			{// 200 = "OK"
				state_function(xmlhttp.responseText);
				return true
			}
			else
			{
				alert("Error: " + xmlhttp.statusText);
				return false;
			}
		}
	}

}
function ajaxPostRequest(query, params, state_function)
{
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{// code for IE7, Firefox, Opera, etc.
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if ( xmlhttp != null )
	{
		xmlhttp.onreadystatechange=state_Change;
		xmlhttp.open("POST","/ajax.php?" + query,true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.send(params);
		return true;
	}
	else
	{
	  alert("Your browser does not support AJAX. Which is really quite odd in the reality of it, you're either using outdated technology or you're quite paranoid.");
	  return false;
	}
	function state_Change()
	{
		if (xmlhttp.readyState==4)
		{// 4 = "loaded"
			if (xmlhttp.status==200)
			{// 200 = "OK"
				state_function(xmlhttp.responseText);
				return true
			}
			else
			{
				alert("Error: " + xmlhttp.statusText);
				return false;
			}
		}
	}
}
function showColorPicker()
{
	document.getElementById('colors').style.display = 'block';
}
function pickColor(id,color)
{
	document.getElementById(id).value = color;
	document.getElementById('colors').style.display = 'none';
	
}
function appendBB(start, end, id, content)
{
	if(content == undefined)
	{
		var obj = document.getElementById(id);
		var selectedText = (obj.value).substring(obj.selectionStart,obj.selectionEnd);  
		var text = document.getElementById(id).innerHTML;
		text = obj.value.replace(selectedText, start + selectedText + end);
		document.getElementById(id).value = text;
	}else{
		var text = start + content + end;
		document.getElementById(id).value = document.getElementById(id).value + text;
	}
		
}

function insertURL(id)
{
	var url = prompt("Please paste your Web URL Here: \nExample: http://www.google.com");
	if(url != null)
		appendBB('[url]','[/url]',id,url);
}
function insertImage(id)
{
	var url = prompt("Please paste your Image URL Here: \nExample: http://www.robostangs.com/sponsors/gm.jpg");
	if(url != null)
		appendBB('[img]','[/img]',id,url);
}
function insertYoutube(id)
{
	var url = prompt("Please paste your Youtube Video ID Here: \nExample: 3PuHGKnboNY \nFrom http://www.youtube.com/watch?v=3PuHGKnboNY");
	if(url != null)
	{
		appendBB('[yt]','[/yt]',id,url);
	}
}
function closeObj(id)
{
	
	var obj = document.getElementById(id);
	obj.style.display = 'none';
	obj.style.innerHTML = '';
	return;
}
function deleteObj(id)
{
	var obj = document.getElementById(id);
	obj.parentNode.removeChild(obj);
}
function insertSmiley(smiley,id)
{
	document.getElementById(id).value = document.getElementById(id).value + smiley;
}
function getWindowWidth()
{
	var x = 0;
	x = document.body.clientWidth;
	return x;
}
