var current_active,time_id=false,time_id_username=false;
// Current active is populated when a description text field gets focus.
// time_id is the id of the setTimeout that updates descriptions when a user keyups the description text field.

function delete_item(id)
{
	document.getElementById("screenshot_td_"+id).style.backgroundColor="#FFA6A6";
	
	var answer = confirm("Are you sure that you want to delete this screenshot?")
	if (answer){
		var url="/ajax/deleteitem.php?id="+id;
		
		xmlhttp=GetXmlHttpObject();
		xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4){
			request_response = xmlhttp.responseText;
		
			if ( request_response == "true" ) // If the php script returns "true", remove the file from the list
			{
				document.getElementById("screenshot_"+id).innerHTML="";
			}
			else
			{
				alert("There was an error deleting the screenshot.  ID is "+id+".");
			}
		}
	}
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	document.getElementById("screenshot_td_"+id).style.backgroundColor="";
}
function update_description(id,description)
{
	var url="/ajax/updatedescription.php?id="+id+"&description="+description;
	
	xmlhttp=GetXmlHttpObject();
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);

	time_id = false;
	document.getElementById("debug").innerHTML = "updated";
}
function update_screenshots_per_page(number)
{
	var url="/ajax/updatescreenshotsperpage.php?screenshots_per_page="+number;
		
		xmlhttp2=GetXmlHttpObject();
		xmlhttp2.onreadystatechange = function(){
			if(xmlhttp2.readyState == 4){
				request_response = xmlhttp2.responseText;
				if ( request_response == "true" ) // If the php script returns "true", remove the file from the list
				{
					showResult();
					//alert("it said i got true in return "+number+".");
				}
				else
				{
					alert("There was an error changing the number of screenshots per page, number is "+number+".");
				}
			}
		}
		xmlhttp2.open("GET",url,true);
		xmlhttp2.send(null);
}
function update_search_what(number)
{
	var url="/ajax/updatesearchwhat.php?search_what="+number;
		
		
		
		xmlhttp2=GetXmlHttpObject();
		xmlhttp2.onreadystatechange = function(){
			if(xmlhttp2.readyState == 4){
				request_response = xmlhttp2.responseText;
				if ( request_response == "true" ) // If the php script returns "true", remove the file from the list
				{
					showResult();
					//alert("it said i got true in return "+number+".");
				}
				else
				{
					alert("There was an error changing the search parameters. The search in field was "+number+".");
				}
			}
		}
		xmlhttp2.open("GET",url,true);
		xmlhttp2.send(null);
}
function update_description_timer(id,description)
{
	if ( time_id )
	{
		clearInterval(time_id);
		//document.getElementById("debug").innerHTML = "cleared";
	}
	time_id = setTimeout('update_description("'+id+'","'+description+'");',1000);
	document.getElementById("debug").innerHTML = "timeout id is: "+id+" description is: "+description;
}

function check_username_timer(username)
{
	if ( time_id_username )
	{
		clearInterval(time_id_username);
		//document.getElementById("debug").innerHTML = "cleared";
	}
	time_id_username = setTimeout('check_username("'+username+'");',200);
	//document.getElementById("debug").innerHTML = "username to check is: "+username;
}

function check_username(username)
{
	var url="/ajax/checkusername.php?username="+username;
		
		xmlhttp2=GetXmlHttpObject();
		xmlhttp2.onreadystatechange = function(){
			if(xmlhttp2.readyState == 4){
				request_response = xmlhttp2.responseText;
				if ( request_response == "false" ) // If the php script returns "false", the name is not avl
				{
					document.getElementById("usernametaken").innerHTML = "<img src='/images/errorcross.png' alt='The username "+username+" is not available' title='The username "+username+" is not available' />";
				}
				else
				{
					document.getElementById("usernametaken").innerHTML = "";
				}
			}
		}
		xmlhttp2.open("GET",url,true);
		xmlhttp2.send(null);
}


function clear_text_box(textboxinstance)
{
	if ( textboxinstance.value=="Add a description here" )
	{
		textboxinstance.value="";	
	}
}
