var lastQuery="";
var lastRun;
var wasApost=true;

function newXHR() {
	var xmlHttp;
	try
	{
		xmlHttpObj=new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			xmlHttpObj=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttpObj=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser cannot use AJAX, please turn on javascript or upgrade to one that does.");
				return false;
			}
		}
	}
	return xmlHttpObj;
}

function addtag(textareaid,bbtag) {
 	if (document.selection) {
 		document.getElementById(textareaid).focus();
		document.selection.createRange().text = "["+bbtag+"]"+document.selection.createRange().text+"[/"+bbtag+"]";
		document.getElementById(textareaid).focus();
	} else {
		var sstart = document.getElementById(textareaid).selectionStart;
		var ssend = document.getElementById(textareaid).selectionEnd;
		document.getElementById(textareaid).value=document.getElementById(textareaid).value.substring(0, sstart)+"["+bbtag+"]"+document.getElementById(textareaid).value.substring(sstart, ssend)+"[/"+bbtag+"]"+document.getElementById(textareaid).value.substring(ssend);
		document.getElementById(textareaid).setSelectionRange(sstart+bbtag.length+2, ssend+bbtag.length+2);
		document.getElementById(textareaid).focus();
	}
}

function addtext(textareaid, texts) {
 	if (document.selection) { 		
 		document.getElementById(textareaid).focus();
		document.selection.createRange().text = texts;
		document.getElementById(textareaid).focus();
	} else {
		var sstart = document.getElementById(textareaid).selectionStart;
		var ssend = document.getElementById(textareaid).selectionEnd;
		document.getElementById(textareaid).value=document.getElementById(textareaid).value.substring(0, sstart)+texts+document.getElementById(textareaid).value.substring(ssend);
		document.getElementById(textareaid).setSelectionRange(sstart, sstart+texts.length);
		document.getElementById(textareaid).focus();
	}
}

function addurl(textareaid) {
 	if (document.selection) {
 		document.getElementById(textareaid).focus();
 		var url = prompt("Enter URL");
 		if (!url) return;
		document.selection.createRange().text = "[URL="+url+"]"+document.selection.createRange().text+"[/URL]";
		document.getElementById(textareaid).focus();
	} else {
		var sstart = document.getElementById(textareaid).selectionStart;
		var ssend = document.getElementById(textareaid).selectionEnd;
 		var url = prompt("Enter URL","http://");
 		if (!url) return;	
		document.getElementById(textareaid).value=document.getElementById(textareaid).value.substring(0, sstart)+"[URL="+url+"]"+document.getElementById(textareaid).value.substring(sstart, ssend)+"[/URL]"+document.getElementById(textareaid).value.substring(ssend);
		document.getElementById(textareaid).setSelectionRange(sstart+url.length+6, ssend+url.length+6);
		document.getElementById(textareaid).focus();
	}
}

function linkpost(textareaid) {
 	if (document.selection) {
 		document.getElementById(textareaid).focus();
 		var url = prompt("Enter post ID");
 		if (!url) return;
		document.selection.createRange().text = "[POST="+url+"]"+document.selection.createRange().text+"[/POST]";
		document.getElementById(textareaid).focus();
	} else {
		var sstart = document.getElementById(textareaid).selectionStart;
		var ssend = document.getElementById(textareaid).selectionEnd;
 		var url = prompt("Enter post ID");
 		if (!url) return;	
		document.getElementById(textareaid).value=document.getElementById(textareaid).value.substring(0, sstart)+"[POST="+url+"]"+document.getElementById(textareaid).value.substring(sstart, ssend)+"[/POST]"+document.getElementById(textareaid).value.substring(ssend);
		document.getElementById(textareaid).setSelectionRange(sstart+url.length+7, ssend+url.length+7);
		document.getElementById(textareaid).focus();
	}
}

function linkpage(textareaid) {
 	if (document.selection) {
 		document.getElementById(textareaid).focus();
 		var url = prompt("Enter page ID");
 		if (!url) return;
		document.selection.createRange().text = "[PAGE="+url+"]"+document.selection.createRange().text+"[/PAGE]";
		document.getElementById(textareaid).focus();
	} else {
		var sstart = document.getElementById(textareaid).selectionStart;
		var ssend = document.getElementById(textareaid).selectionEnd;
 		var url = prompt("Enter page ID");
 		if (!url) return;	
		document.getElementById(textareaid).value=document.getElementById(textareaid).value.substring(0, sstart)+"[PAGE="+url+"]"+document.getElementById(textareaid).value.substring(sstart, ssend)+"[/PAGE]"+document.getElementById(textareaid).value.substring(ssend);
		document.getElementById(textareaid).setSelectionRange(sstart+url.length+7, ssend+url.length+7);
		document.getElementById(textareaid).focus();
	}
}

function searchattachments(query,is_post) {
	lastQuery=query;
	wasApost=is_post;
	clearTimeout(lastRun);
	lastRun=setTimeout("runQuery()",500);
}

function runQuery() {
	var searchbox=document.getElementById("attachment_search_text");
	var resultsbox=document.getElementById("attachment_search_report");
	var userid=document.getElementById("userid").value;
	var userkey=document.getElementById("userkey").value;
	/*
	* query
	* true -> use search box value
	* false -> get recent 5 attachments
	* anything else -> use custom value
	*/
	if(lastQuery===true) {
		var searchstring=searchbox.value;
		resultsbox.innerHTML="Searching...";
	}
	else if(lastQuery===false) {
		var searchstring=false;
		resultsbox.innerHTML="Getting recent attachments...";
	}
	else {
		var searchstring=lastQuery;
		searchbox.value=lastQuery;
		resultsbox.innerHTML="Searching...";
	}

	var serverQuery=newXHR();
	
	//What to do.. what to do...
	serverQuery.onreadystatechange=function() {
		//Wait till completed
		if(serverQuery.readyState  == 4)
		{
			//Check for error
			if(serverQuery.status  != 200) {
				resultsbox.innerHTML="Error " + serverQuery.status;
			}
			//Deal with the response
			else {
				resultsbox.innerHTML=serverQuery.responseText;
			}
		}
	}
		if(searchstring===false)
			var qString="recent=5";
		else
			var qString="q="+encodeURIComponent(searchstring);
		
		//Auth information
		qString+="&uid="+userid+"&key="+userkey;
		
		//Post or page
		if(wasApost)
			qString+="&type=post";
		else
			qString+="&type=page";
		
		//Send off to the server and hope all goes well
		serverQuery.open("POST", "search.php", true); 
		serverQuery.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
		serverQuery.send(qString);
}