// JavaScript Document

function changeLocOpt(myForm, nid) {
	var http_request = false;
	var url = "getlocation.php";
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
	  http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
	  try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (e) {
		try {
		  http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	  }
	}
	
	if (!http_request) {
	  alert('Giving up :( Cannot create an XMLHTTP instance');
	  return false;
	}
	// 定義事件處理函數為 alterContents()
	http_request.onreadystatechange = function() { genLocOpt(myForm, http_request); };
	
	// IE 6.x 和 Firefox 1.5.x 皆要 encodeURI()
	
	url = url + "?nid=" + nid + "&amp;seed=" + Date();
	http_request.open('GET', url, true);
	http_request.send(null);
}
	
function genLocOpt(myForm, http_request) {
	if (http_request.readyState == 4) {
      if (http_request.status == 200) {
        var xmldoc = http_request.responseXML;
        var nodes = xmldoc.getElementsByTagName('loc');
        var target = myForm.location;
		var selectedIndex = 0;
		
		// clear all options
		for(tmpCount=target.length - 1; tmpCount>=0; tmpCount--)
			target.options[tmpCount] = null;
		
		// add new options
		target.options[target.length] = new Option("- please select -", ''); // default option
        for(var i=0; i<nodes.length; i++) {
		  if(nodes[i].getAttribute("selected") == "1")
			  selectedIndex = i+1;
		  target.options[target.length] = new Option(nodes[i].firstChild.nodeValue, nodes[i].getAttribute("id"));
        }
		target.selectedIndex = selectedIndex;
		
      } else {
        alert('There was a problem with the request.');
      }
    }
}
