var obj;

function JS_selectDepartment(host, fk_value){

	this.obj = document.getElementById('subcategory_id');

	resetSelect(this.obj, 1);

	// check if object exisits.
	if(typeof this.obj != 'object') return false;
	//alert(obj.name);
	url  = host+'department.php?department_id=' + fk_value;
	//alert(url);
	if(fk_value > 0) loadResult(url,'');

}

function resetSelect(obj, startIndex)
{
	if(startIndex == null) starIndex = 1;
	while (obj.options.length > startIndex)
	{
		deleteIndex = obj.options.length-1;
		obj.options[deleteIndex] = null;
	}
}

function loadXMLDoc(url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		try{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send();
		}
	}
}


function processReqChange() {
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here
			response  = req.responseXML.documentElement;
			if(response) {
				var result = new Array();
				for(i=0; i < response.getElementsByTagName('id').length; i++){
					result[i] = {'id': response.getElementsByTagName('id')[i].firstChild.data, 'name': response.getElementsByTagName('name')[i].firstChild.data};
				}
				loadResult('',result);
			}
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}

function loadResult(url, result){
	if (result != ''){
		// Response mode
		for(i=0; i < result.length; i++){
			if(result[i].name!=' ')
				this.obj.options[this.obj.options.length]  = new Option(result[i].name,result[i].id);
		}
	} else {
		// Input mode
		return (loadXMLDoc(url));
	}
}

