/*************************************************************************/
//catgory picker
/*************************************************************************/
// JavaScript Document
var lowest_level=0;
var hiddren_file_id="the_cat_id";//id of the hidden file which stores the category id from the category picker
var category_data_url="";//file name of the php code
var cat_picker_container_id="catgory_pickers";//id of the container of the category picker
//str_cat_ids, ids of the category from Grandpa to Grandchild
//my_id id of the editting category, so it won not be shown on the list
function auto_generate_cats(str_cat_ids,php_url,my_id)
{	
	if (category_data_url=="")	category_data_url=php_url;
	var arr_cat_ids=str_cat_ids.split(",");
	if (arr_cat_ids.length==1)//exception for root category
		showCat_list(0,0,my_id);
	else
	{
		for(var i=0;i<(arr_cat_ids.length-1);i++)
		{//dont know why  but if i call the function directly, wired thing happen

			setTimeout("showCat_list("+arr_cat_ids[i]+","+arr_cat_ids[i+1]+","+my_id+")",i*500);	
			
		}
	}
	document.getElementById(hiddren_file_id).value=arr_cat_ids[arr_cat_ids.length-1];
}
function selected_cat(cat_id_index,cat_name,obj_select,my_id)
{	
	var cat_id=obj_select.options[cat_id_index].value;
	document.getElementById(hiddren_file_id).value=cat_id;				
	if (cat_id_index)
	{	
		var cur_level=parseInt(obj_select.id);
		for (var cnt=lowest_level;cnt>cur_level;cnt--)
		{
			var temp_child=document.getElementById("category_picker_cat_list_level_"+cnt);
			if (temp_child)
			{		
				document.getElementById(cat_picker_container_id).removeChild(temp_child);			
				lowest_level-=1;
			}
		}
		showCat_list(cat_id,0,my_id);
		var picker_parent=document.getElementById(cat_picker_container_id);
	}
}
/************************************** 
ajax 
**************************************/
var xmlHttp
//cat_id,id of the category,selected:which category is selected
//php_url or the php page, only need for the first time
function showCat_list(parent_cat_id,selected,my_id)
{
	if (category_data_url=="")	category_data_url=php_url;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	} 
	var url=category_data_url;
	url=url+"?parent_cat_id="+parent_cat_id;
	url=url+"&level="+(lowest_level+1);
	url=url+"&my_id="+my_id;
	url=url+"&y="+Math.random();
	//alert(url);
	if (selected!=0)	url=url+"&selected_id="+selected;
	xmlHttp.onreadystatechange=stateChanged ;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function stateChanged() 
{ 
	if ((xmlHttp.readyState==4 || xmlHttp.readyState=="complete") && xmlHttp.responseText)
 	{
		lowest_level+=1;
 		var new_child_div = document.createElement("div");
		new_child_div.style.float="left";
		new_child_div.id="category_picker_cat_list_level_"+lowest_level;
		new_child_div.innerHTML=xmlHttp.responseText;
 		document.getElementById(cat_picker_container_id).appendChild(new_child_div);
 	} 
}

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)
		{
			try 
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX, please try with another browser or on different computer!");
			}
		}
	}
	return xmlHttp;
}
/**************************************************************************/
//end of ajax
/**************************************************************************/
/*********************************************************************/
//end of category picker
/*********************************************************************/