function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return xmlHttp;
}

function showForm(element_id,element)
{
	element_id = "add_item_" + element_id;
	
	if(document.getElementById(element_id).style.display == 'block')
	{
		element.innerHTML = '[Edit]';
		element.style.fontWeight = 'normal';
		document.getElementById(element_id).style.display = 'none';
	}
	else
	{
		element.innerHTML = '[Hide]';
		element.style.fontWeight = 'bold';
		document.getElementById(element_id).style.display = 'block';
	}
}

function showUpdateForm(element_id,element)
{
	element_id = "update_item_" + element_id;

	if(document.getElementById(element_id).style.display == 'block')
	{
		element.innerHTML = '[Edit]';
		element.style.fontWeight = 'normal';
		document.getElementById(element_id).style.display = 'none';
	}
	else
	{
		element.innerHTML = '[Hide]';
		element.style.fontWeight = 'bold';
		document.getElementById(element_id).style.display = 'block';
	}
}

function delete_category_item(path,element_id,cat_id)
{
	div = document.getElementById("block_cat_" + cat_id);		
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	
	var url= path;

	div.style.position	= 'relative';
	div.style.top		= '40%';
	div.style.left		= '100px';
	div.innerHTML = "<img src='http://www.webb4you.com/medesk/images/loading.gif'>";
	xmlHttp.onreadystatechange= function()
	{	
	  	if (xmlHttp.readyState==4)
  		{ 
			div.style.position	= 'relative';
			div.style.top		= '0px';
			div.style.left		= '0px';
		    div.innerHTML = xmlHttp.responseText;
  		}
	}	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function delete_category(path)
{
	div = document.getElementById("category_container");		
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	
	var url= path;

	div.style.textAlign = 'center';
	div.style.position	= 'relative';
	div.style.top		= '100px';
	div.innerHTML 		= "<img src='http://www.webb4you.com/medesk/images/loading.gif'>";
	xmlHttp.onreadystatechange= function()
	{	
	  	if (xmlHttp.readyState==4)
  		{ 
			div.style.textAlign = 'left';
			div.style.position	= 'relative';
			div.style.top		= '0px';
		    div.innerHTML 		= xmlHttp.responseText;
  		}
	}	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function showEditTitleForm(element_id,cat_id,value,path)
{
	var form = "<span><input id='c_title" + element_id + "' name='c_title' style='width:100px' type='text' value='" + value + "'></span>";
	form = form + "<span><input class='btnNormal' type='button' value='Save' onClick='save_category_title(\"" + element_id + "\"," + cat_id + ")'/></span>";
	form = form + "<span><input id='c_path" + element_id + "' type='hidden' value='"+ path +"'/></span>";
	
	document.getElementById("title_" + element_id).innerHTML = form;
	
}

function save_category_title(element_id,cat_id,pos)
{	
	value = document.getElementById("c_title" + element_id).value;
	div = document.getElementById("block_cat_" + cat_id);
	
	if(value == "")
	{
		alert("Category Title cannot be black.");
		return false;
	}
	site_url = document.getElementById("c_path" + element_id).value;
	
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	
	var url= site_url + "user/ajax_save_category/" + cat_id + "/" + pos + "/" + escape(value);
	
	div.innerHTML = "<img src='http://www.webb4you.com/medesk/images/loading.gif'>";
	xmlHttp.onreadystatechange= function()
	{	
	  	if (xmlHttp.readyState==4)
  		{
		    div.innerHTML = xmlHttp.responseText;
  		}
	}	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function add_new_category(path)
{
	value = document.getElementById("new_cat_name").value;
	div = document.getElementById("category_container");
	
	if(value == "")
	{
		alert("Name cannot be blank");
		return;
	}
	
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	
	var url= path + "/" + escape(value);
	
	div.style.textAlign = 'center';
	div.style.position	= 'relative';
	div.style.top		= '100px';
	div.innerHTML = "<img src='http://www.webb4you.com/medesk/images/loading.gif'>";
	
	xmlHttp.onreadystatechange= function()
	{	
	  	if (xmlHttp.readyState==4)
  		{
			div.style.textAlign = 'left';
			div.style.position	= 'relative';
			div.style.top		= '0px';
		    div.innerHTML 		= xmlHttp.responseText;
  		}
	}	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function moveCategory(path)
{	
	div = document.getElementById("category_container");	
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	
	var url= path;
	
	div.style.textAlign = 'center';
	div.style.position	= 'relative';
	div.style.top		= '100px';
	div.innerHTML = "<img src='http://www.webb4you.com/medesk/images/loading.gif'>";
	
	xmlHttp.onreadystatechange= function()
	{	
	  	if (xmlHttp.readyState==4)
  		{	
			div.style.textAlign = 'left';
			div.style.position	= 'relative';
			div.style.top		= '0px';
		    div.innerHTML 		= xmlHttp.responseText;
  		}
	}	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function toggle_edit_page_controls(path,value)
{
	div = document.getElementById("category_container"); 
	
	if(value == 1)
	{
		document.getElementById("user_edit_page").innerTEXT = 'Hide Edit Controls';
	}
	else
	{
		document.getElementById("user_edit_page").innerTEXT = 'Show Edit Controls';
	}
	
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	
	var url= path + "/" + value;
	
	div.style.textAlign = 'center';
	div.style.position	= 'relative';
	div.style.top		= '100px';
	div.innerHTML = "<img src='http://www.webb4you.com/medesk/images/loading.gif'>";
	
	xmlHttp.onreadystatechange= function()
	{	
	  	if (xmlHttp.readyState==4)
  		{
			div.style.textAlign = 'left';
			div.style.position	= 'relative';
			div.style.top		= '0px';
		    div.innerHTML 		= xmlHttp.responseText;
  		}
	}	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function update_form(element_id,value)
{
	element = "add_form_content_" + element_id;
	if(value == 'link')
	{
		var form_content = "<div class='formText' style='width:100px'>Link Title:</div><input type='text' name='link_name'>";
		form_content = form_content + "<br>";
		form_content = form_content + "<div class='formText' style='width:100px'>Link URL:</div><input type='text' name='link_url'>";
		
		document.getElementById(element).innerHTML = form_content;
	}
	else
	{
		var form_content = "<textarea name='item_content' cols='40' rows='8'></textarea>";
		document.getElementById(element).innerHTML = form_content;
	}
}

