	function clickDirectPrint()
	{
		ddp=document.getElementById("div_direct_print");
		if(g_direct_print)
		{
			g_direct_print=false;
			ddp.style.background="#FF0000";
		}
		else
		{
			ddp.style.background="#00FF00";
			g_direct_print=true;
		}
	}
	function getCharPart(string)
	{
		ret="";
		for(i=0;i<string.length;i++)
		{
			if((string.charCodeAt(i)>=65)&&(string.charCodeAt(i)<=90))
				ret+=string.charAt(i);
			if((string.charCodeAt(i)>=97)&&(string.charCodeAt(i)<=122))
				ret+=string.charAt(i);
			if(string.charCodeAt(i)==32)
				ret+=string.charAt(i);
			if(string.charAt(i)=="/")
				ret+=string.charAt(i);
				
		}
		return ret;
	}
	function getNumberPart(string)
	{
		ret="";
		for(i=0;i<string.length;i++)
		{
			if((string.charCodeAt(i)>=48)&&(string.charCodeAt(i)<=57))
				ret+=string.charAt(i);
			if(string.charCodeAt(i)==32)
				ret+=string.charAt(i);
		}
		return ret;
	}

	function show()
	{
		objid=document.getElementById("obj_id");
		
		//find('','dagoi');
	
		window.open('?man_id='+g_man_id+'&obj_id='+objid.value,'_self');
	}
	function expand_cat(div_id)
	{
		if(g_lastdiv)
		{
			g_lastdiv.innerHTML="";
			g_lastdiv.style.visibility="hidden";
		}
		divid=document.getElementById(div_id);
		if(g_lastdiv==divid)
			return;
		divid.style.visibility="visible";
		divid.innerHTML=(strArray[div_id]);
		g_lastdiv=divid;
	}
	function refreshObj()
	{
		alert('a');
	}
	function getURL(url,dataExtra,functionHandler) 
	{
		if (window.XMLHttpRequest) 
		{ // Non-IE browsers & New IE
			req = new XMLHttpRequest();
			req.onreadystatechange = functionHandler;
			req.dataExtra=dataExtra;
			
			try 
			{
				req.open("GET", url, true);
			} 
			catch (e) 
			{
				alert(e);
			}
			req.send(null);
		} 
		else if (window.ActiveXObject) 
		{ // OLD IE
			req = new ActiveXObject("Microsoft.XMLHTTP");
			
			if (req) 
			{
				req.dataExtra=dataExtra;
				req.onreadystatechange = functionHandler;
				req.open("GET", url, true);
				req.send();
			}
		}
	}
	function onaddreturn()
	{
		if (req.readyState == 4) 
		{ // Complete
			if (req.status == 200) 
			{ // OK response
				if(req.dataExtra=="them")
				{
					alert(req.responseText);
					
					if(!isNaN(req.responseText))
					{
						showMarkGoodsAdded(Number(req.responseText));
						
					}
				}
				else
				{
					g_innerhtml_divcontent=req.responseText;
					
					showContent();
			} 	}
			else 
			{
			}
		}
	}
	function find(goods_name,param)
	{
		objid=document.getElementById("obj_id");
		if(objid.value<=0)
		{
			g_innerhtml_divcontent="";
			showContent();
			return;
		}
		categoryid=document.getElementById("goods_category");
		if(param=="nhom")
			goods_name=categoryid.value;

		getURL('?page=res_find&goods_name='+goods_name+'&param='+param+'&obj_id='+objid.value+'&category_id='+categoryid.value,param,onaddreturn);
	}
	function add(goods_id,quantity)
	{
		objid=document.getElementById("obj_id");
		getURL('?page=res_add&goods_id='+goods_id+'&obj_id='+objid.value+'&quantity='+quantity+'&print='+g_direct_print,"them",onaddreturn);		
	}
	function remove(order_id)
	{
		objid=document.getElementById("obj_id");
		getURL('?page=res_add&order_id='+order_id+'&obj_id='+objid.value+"&param=loai"+'&print='+g_direct_print,"loai",onaddreturn);		
	}
	function showMarkGoodsAdded(goods_id)
	{
		var divname="divgoodsindex"+goods_id;
		divindex=document.getElementById(divname);
		divindex.innerHTML="<B>OK</B>";
	}

	function submitUpdate(f)
	{
		f.man_id.value=g_man_id;
		
		f.submit();
		return TRUE;
	}
	
	function showContent()
	{
		divcontent=document.getElementById("div_content");
		divcontent.innerHTML=(g_innerhtml_divcontent);
		
	}
	function getGoodsIdFromIndex(goods_index)
	{
		var divid="divgoodsindex";
		var alldivs=document.getElementsByTagName("div");
		var divcmd=document.getElementById("div_cmd");
		var goods_id=0;
		for(i=0;i<alldivs.length;i++)
		{
			if(alldivs[i].id.substr(0,divid.length)==divid)
			{
				goods_id=alldivs[i].id.substr(divid.length,alldivs[i].id.length-divid.length);
				if(alldivs[i].innerHTML==goods_index)
				{
					return goods_id;
				}
			}
		}
	}
	function selectRow(goods_id)
	{
		if(g_last_row!=null)
		{
			g_last_row.style.background="#FFFFFF";
			g_last_row=null;
		}
		var rowid=document.getElementById("tr"+goods_id);
		if(rowid==null)
			return;
		rowid.style.background="#FF00FF";
		g_last_row=rowid;
	}
	function addGoodsByEnter(goods_index,goods_quantity)
	{
		var goods_id=getGoodsIdFromIndex(goods_index);
		if(goods_id<=0)
			return;
		add(goods_id,goods_quantity);
	}
	function selObj(npart)
	{
		objid=document.getElementById("obj_id");
		obj_id.selectedIndex=npart;
		show();
	}
	function findGoods(param,key_code)
	{
		if(param=="")
			return;

		if((key_code==13)&&(param.charAt(0)=="/"))
		{
			var cpart=getCharPart(param);
			var npart=getNumberPart(param);
			if((cpart=='/b')&&!isNaN(npart))
				selObj(npart);
			return;
		}
		var arvals=param.split(g_find_split);
		if(arvals.length<1)
			return;
			
		var goods_name=arvals[0];
		var goods_index=0;
		var goods_quantity=1;
		if(arvals.length>1)
			goods_index=arvals[1];
		if(arvals.length>2)
			goods_quantity=arvals[2];
		if(isNaN(goods_quantity))
			goods_quantity=1;
	
		if(goods_index>0)
		{
			goods_id=getGoodsIdFromIndex(goods_index);
			selectRow(goods_id);
		}
		//Enter
		if(key_code==13)
		{
			if(!isNaN(goods_index))
				addGoodsByEnter(goods_index,goods_quantity);
			return;
		}
		if(goods_name.substr(0,1)==" ")
			return;
		//neu den phan chon index thi khong tim hang nua
		if(goods_index==0)
			find(goods_name,'tim');
	}
	function keyboard(key)
	{
		divcmd=document.getElementById("div_cmd");
		val=divcmd.innerHTML;
		if((key.length==1)&&(key!="/"))
		{
			val+=key;
			divcmd.innerHTML=val;
			//neu la lenh thi bo qua khong tim tru khi nhan phim enter - xu ly phia duoi
			if(val.substr(0,1)!="/")
				findGoods(val,key);
		}
		if(key=="db")
		{
			val=val.substr(0,val.length-1);
			divcmd.innerHTML=val;
			findGoods(val,key);
		}
		if(key=="sp")
		{
			divcmd.innerHTML=val+" ";
			findGoods(val,key);
		}
		if(key=="do")
		{
			divcmd.innerHTML=val+".";
			findGoods(val,key);
		}
		if(key=="bs")
		{
			divcmd.innerHTML=val+"/";
			findGoods(val,key);
		}
		if(key=="et")
		{
			findGoods(val,13);
			divcmd.innerHTML="";
		}
		if(key=="goithem")
		{
			find('','goithem');
		}
		if(key=="dagoi")
		{
			find('','dagoi');
		}
	}
