var current_active,time_id=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 chose_a_cat(){
		if(document.form1.txtCategory.value!="0")
		{
			document.form1.txtNewCategory.value="";	
		}
}

function adding_a_cat(){
		if(document.form1.txtNewCategory.value!="")
		{
			document.form1.txtCategory.options[0].selected=true	;
		}
}

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
	  // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}// JavaScript Document


function progress(id)
{
		document.form1.fileupload.disabled='true'; 
		document.form1.txtTitle.disabled='true'; 
		//document.form1.txtDescription.disabled='true'; 
		

		var url="ajax/uploadprogress.php?id="+id;
		xmlhttp=GetXmlHttpObject();
		xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 4){
					//alert("yes it was");
					request_response = xmlhttp.responseText;
					document.getElementById("uploadspan").innerHTML="" + request_response;
				}
		}
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
}


function delete_item(id)
{
	
	var answer = confirm("Are you sure that you want to delete this video?")
	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("video_"+id).innerHTML="";
			}
			else
			{
				alert("There was an error deleting the screenshot.  ID is "+id+".");
			}
		}
	}
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}

}

function delete_category(id)
{
	
	var answer = confirm("Are you sure that you want to delete this category and all of the videos in it?")
	if (answer){
		var url="ajax/deletecategory.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
			{
				window.location.reload();
			}
			else
			{
				alert("There was an error deleting the category.  ID is "+id+".");
			}
		}
	}
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}

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 clear_text_box(textboxinstance)
{
	if ( textboxinstance.value=="Add a description here" )
	{
		textboxinstance.value="";	
	}
}
