//函數：在 IE 中建立 XMLHttpRequest 物件，避免不同瀏覽器的差異性
if (typeof XMLHttpRequest == "undefined" && window.ActiveXObject) {
	function XMLHttpRequest() {
		var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
		for (var i=0; i < arrSignatures.length; i++) {
			try {
				var oRequest = new ActiveXObject(arrSignatures[i]);
				return oRequest;
			} catch (oError) {
				//ignore
			}
		}
		throw new Error("MSXML is not installed on your system.");
	}
}

//函數：將參數加入到 URL 尾端，以便讓 GET 使用
function addURLParam(sURL, sParamName, sParamValue) {
	sURL += (sURL.indexOf("?") == -1 ? "?" : "&");
	sURL += encodeURIComponent(sParamName) + "=" + encodeURIComponent(sParamValue);
	return sURL;
}
//-----------------------------------------------------------------------
//
//分別取得兩個選單 的reference
var oCateList = document.getElementById('gstand');
var oGameList = document.getElementById('groupamt');
var oCateListZero = document.getElementById('gstand');

//後端傳回 JSON 資料的路徑
var fileURL = "group_amount.php";
var fileUrlZero = "group_zeroamt.php";

//用來儲存 JSON 的全域變數
var json,json_1,json_2;


//一開始時先將第二個選單停用
oGameList.disabled = false;

//第一個選單的 onchange 事件，用來產生第二個選單的內容
oCateList.onchange = function() {
	
	//alert(document.getElementById('standardId').value);
	//如果選擇的是第一個選項，第二個選單只顯示"請選擇"，並且無法使用。
	if(this.selectedIndex == 0) {
		oGameList.options.length = 0;
		oGameList.options[0] = new Option("請選擇數量",0);
		//oGameList.options[0].selected = true;
		oGameList.disabled = false;
	} else {
	//如果有選擇其他選項，送出 Ajax 跟後端要求第二個選單的資料。
		//產生要求資料的 url，加入第一個選單所選擇的選項值作為參數
		
		sURL = addURLParam(fileURL,"stand_id",this.options[this.selectedIndex].value);
		
		//建立 XMLHttpRequest 物件，並且送要求
		var oRequest = new XMLHttpRequest();
		oRequest.open("post", sURL, true);
		//接收資料的 callback 函數
		oRequest.onreadystatechange = function() {
			if(oRequest.readyState == 4) {
				//接收到資料後，就將第一個選單啟用
				oCateList.disabled = false;

				oGameList.options.length = 0;
				oGameList.options[0] = new Option("請選擇數量",0);
				//oGameList.options[0].selected = true;
				oGameList.disabled = false;

				if(oRequest.status == 200 ){
					//接受資料成功，可以從 oRequest.responseText 取得傳回的資料
					//轉換為 JSON 的格式

					json = "json = " + oRequest.responseText;
					json = eval(json);
					//產生第二個選單的選項
					oGameList.options.length = 1;
					for(var i=0,j=json.length;i<j;i++) {
						oGameList.options[i+1] = new Option(json[i].text,json[i].value);
					}
					
					zeroshow();
					//將第二個選單啟用
					oGameList.disabled = false;
					
				} else {
					//接收資料失敗，可以從 oRequest.statusText 取得錯誤狀態資訊
					//將錯誤資訊顯示在第二個選單中
					oGameList.options[0] = new Option(oRequest.statusText,0);
				}
			}
		}
		//選擇選項後先將第一個選單停用，避免重複送出要求
		//oCateList.disabled = true;
		//送出 Ajax 要求
		oRequest.send(null);
	}
}
function zeroshow() {
	//如果選擇的是第一個選項，第二個選單只顯示"請選擇"，並且無法使用。
	sURLZero = addURLParam(fileUrlZero,"standa_id",document.getElementById('gstand').options[oCateListZero.selectedIndex].value);
	var oRequestZero = new XMLHttpRequest();
	oRequestZero.open("post", sURLZero, true);
	
	oRequestZero.onreadystatechange = function() {
		if(oRequestZero.readyState == 4) {
			if(oRequestZero.status == 200 ){
			jsonZero = oRequestZero.responseText;
			document.getElementById('zeroshow').innerHTML = jsonZero;
			}else{
				document.getElementById("amtShow").style.display = "block";
			}
		}
	}
	//送出 Ajax 要求
	oRequestZero.send(null);
}
