

function getHTTPObject()
{
	var xmlhttp;
	if(window.ActiveXObject)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			xmlhttp = false;
		}
	}
	else if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch (e)
		{

		}
	}
	return xmlhttp;
}

var http = getHTTPObject();

function ajax_add_new_category(category_name, which)
{
	if ( http.readyState == 0 || http.readyState == 4 )
	{
		http.open("GET", domain + "/library/ajax_update.php?action=add_new_category&category_title=" + category_name + "&which=" + which + "&dummy=" + new Date().getTime(), true);
		http.onreadystatechange = handle_response_add_new_category;
		http.send(null);
	}
	else
	{
		setTimeout('ajax_add_new_file_category(' + category_name + ', ' + which + ')', 500);
	}
}


function handle_response_add_new_category()
{
	if (http.readyState == 4)
	{
		var response = http.responseXML;
		if(!response || !response.documentElement)
		{
			alert ('ERROR!');
		}

		xmlRoot =  response.documentElement;

		var action_complete = response.getElementsByTagName("action_complete").item(0).firstChild.nodeValue;
		var category_id = response.getElementsByTagName("category_id").item(0).firstChild.nodeValue;
		var category_title = response.getElementsByTagName("category_title").item(0).firstChild.nodeValue;
		var what = response.getElementsByTagName("what").item(0).firstChild.nodeValue;

		//add the item to the page
		page_update_add_new_file_category(category_id, category_title, what);
	}
}

function ajax_delete_content_item(content_item_id, section_id)
{
	if ( http.readyState == 0 || http.readyState == 4 )
	{
		http.open("GET", domain + "/library/ajax_update.php?action=delete_content_item&content_item_id=" + content_item_id + "&section_id=" + section_id + "&dummy=" + new Date().getTime(), true);
		http.onreadystatechange = handle_response_delete_content_item;
		http.send(null);
	}
	else
	{
		setTimeout('ajax_delete_content_item(' + category_item_id + ', ' + section_id + ')', 500);
	}
}


function handle_response_delete_content_item()
{
	if (http.readyState == 4)
	{
		var response = http.responseXML;
		if(!response || !response.documentElement)
		{
			alert ('ERROR!');
		}

		xmlRoot =  response.documentElement;

		var action_complete = response.getElementsByTagName("action_complete").item(0).firstChild.nodeValue;
		var content_item_id = response.getElementsByTagName("content_item_id").item(0).firstChild.nodeValue;
		var section_id = response.getElementsByTagName("section_id").item(0).firstChild.nodeValue;

		//add the item to the page
		page_update_delete_content_item(content_item_id, section_id);
	}
}


function ajax_delete_file_item(file_item_id)
{
	if ( http.readyState == 0 || http.readyState == 4 )
	{
		http.open("GET", domain + "/library/ajax_update.php?action=delete_file_item&file_item_id=" + file_item_id + "&dummy=" + new Date().getTime(), true);
		http.onreadystatechange = handle_response_delete_file_item;
		http.send(null);
	}
	else
	{
		setTimeout('ajax_delete_file_item(' + category_file_id + ')', 500);
	}
}


function handle_response_delete_file_item()
{
	if (http.readyState == 4)
	{
		var response = http.responseXML;
		if(!response || !response.documentElement)
		{
			alert ('ERROR!');
		}

		xmlRoot =  response.documentElement;

		var action_complete = response.getElementsByTagName("action_complete").item(0).firstChild.nodeValue;
		var file_item_id = response.getElementsByTagName("file_item_id").item(0).firstChild.nodeValue;

		//add the item to the page
		page_update_delete_file_item(file_item_id);
	}
}

function ajax_delete_category(category_id, what)
{
	if ( http.readyState == 0 || http.readyState == 4 )
	{
		http.open("GET", domain + "/library/ajax_update.php?action=delete_category&category_id=" + category_id + "&what=" + what + "&dummy=" + new Date().getTime(), true);
		http.onreadystatechange = handle_response_delete_category_item;
		http.send(null);
	}
	else
	{
		setTimeout('ajax_delete_category(' + category_id + ', ' + what + ')', 500);
	}
}


function handle_response_delete_category_item()
{
	if (http.readyState == 4)
	{
		var response = http.responseXML;
		if(!response || !response.documentElement)
		{
			alert ('ERROR!');
		}

		xmlRoot =  response.documentElement;

		var action_complete = response.getElementsByTagName("action_complete").item(0).firstChild.nodeValue;
		var category_id = response.getElementsByTagName("category_id").item(0).firstChild.nodeValue;

		//add the item to the page
		page_update_delete_category(category_id);
	}
}
