getMenuURL="/loadMenu.php";

function str2unicode(str)
{
	var strRtn="";
	var separator="z";	//file:用z作分隔符

	for (var i=0;i<str.length;i++)
	{
		strRtn+=str.charCodeAt(i);
		if (i<str.length-1) strRtn+=separator;
	}
	return strRtn;
}

function escChar(str)
{
	str=str.replace("(--[--)",'<')
	str=str.replace("(--[--)",'<')
	str=str.replace("(--[--)",'<')
	str=str.replace("(--[--)",'<')
	str=str.replace('(--]--)','>')
	str=str.replace('(--]--)','>')
	str=str.replace('(--]--)','>')
	str=str.replace('(--]--)','>')

 	str=str.replace('(--]--)','>')
	str=str.replace('(--]--)','>')
	str=str.replace('(--]--)','>')
	str=str.replace('(--]--)','>')
	str=str.replace('(--]--)','>')
	str=str.replace('(--]--)','>')
	str=str.replace('(--]--)','>')
	str=str.replace('(--]--)','>')
	
	return str
}
function loadMenu(menu,menuName,selectedOption,defaultOptionName)//初始化第一個關聯菜單
{
	var url=getMenuURL+"?menuName="+menuName;
	if(defaultOptionName)
		url+='&defaultOptionName='+str2unicode(defaultOptionName);
	//document.write("<br>"+url);

	var HttpSend = new ActiveXObject("Microsoft.XMLHttp");//創建xmlHttpSend對象，用于收發數據到服務器短
	var DomMenu = new ActiveXObject("Microsoft.XMLDom");//創建xmlDomMenu對象，用于分析、獲取服務器段傳回的xml中的數據


	HttpSend.open("GET",url,false);
	HttpSend.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");//如果需要 get/post 中文參數到服務器，必須出現次行，否則服務器端出現編碼錯誤。
	HttpSend.send();//發送查詢信息
	DomMenu.async=false //設置爲同步方式獲取數據

	
	if(!DomMenu.loadXML(HttpSend.responseText))
	{
		alert("菜單裝載發生錯誤\n"+HttpSend.responseText);
		return false;
	}

	value= DomMenu.getElementsByTagName("value");
	text= DomMenu.getElementsByTagName("text");


	menuIdx=0
	// 如果該菜單沒有初始項
	if( defaultOptionName!='' && value[0].text!='' )
	{
		tempoption=new Option(defaultOptionName,'');
		menu.options[0]=tempoption;
		menuIdx++;
	}
	
	for(var key=0;key<text.length;key++)//裝載新獲取的數據到下級菜單
	{
		
		

		value[key].text=escChar(value[key].text);
		tempoption=new Option(text[key].text,value[key].text);
		menu.options[menuIdx]=tempoption;
		
		//document.write(selectedOption+"=="+value[key].text+"<br>");
		if(selectedOption==value[key].text)
			menu.options[menuIdx].selected=true;
		
		menuIdx++
	}	
}

function loadMenu_in_form(theform)
{
	for(var idx=0;idx<theform.length;idx++)
	{
		if( theform[idx].nodeName!='SELECT' || !theform[idx].loadMenu )
			continue;
		
		if(!theform[idx].defaultOptionName)
			theform[idx].defaultOptionName='';

		loadMenu(theform[idx],theform[idx].loadMenu,theform[idx].selectedOption,theform[idx].defaultOptionName)
		
	}
}
