// JavaScript Document
var xmlHttp;
window.onload = refreshBooklList;
function createXMLHttpRequest() {
	if(window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}else if(window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");    
	}
}
function refreshBooklList() {
	var type = document.getElementById("type").value;
	var url = "class2-sql.php?type=" + type + "&" +new Date().getTime();
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("GET", url);
	xmlHttp.send(null);
}
function handleStateChange() {
	if(xmlHttp.readyState == 4) {
      //alert(xmlHttp.readyState);//檢示readyState結果
      if(xmlHttp.status == 200) {
			clearBookList();
			updateBookList();
            //alert(xmlHttp.status);//檢示status結果
      }
	}
}// 清除上一次的顯示結果

function clearBookList() {
	var books = document.getElementById("books");
	while(books.childNodes.length > 0) {
		  books.removeChild(books.childNodes[0]);    
	}
		//alert("clear!!");//檢示清除元素之涵數是否執行完成
}// 以回應更新資料

function updateBookList() {    
   //var typev = document.getElementById("typev").value;
	var results = xmlHttp.responseXML.getElementsByTagName("book");  
	//alert(results.length);//顯示book元素之array長度
	var books = document.getElementById("books");        
	var option = null;    
	//alert(results.length);//顯示book元素之array長度
	for(var i = 0; i < results.length; i++) {        
  		option = document.createElement("option"); //新增books的option元素       
  		option.appendChild(document.createTextNode(results[i].firstChild.nodeValue));//附加新增books的option元素的內容<option>值</option>
		option.value=results[i].getAttribute('x');//取得book節點的屬性(x)值
  		books.appendChild(option);
		//alert(results[i].getAttribute('x'));
		//if(results[i].getAttribute('x')==typev){option.selected=true;}
    }
      //alert("update!!");//檢示更新元素是否執行完成
}
