function createXML()
{
var xmlhttp=false;
if (window.XMLHttpRequest)
	{
  	xmlhttp=new XMLHttpRequest();		
	}
	else{
		try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  		}
		catch (E){
			xmlhttp = false;
		}
	}	
}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
	xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp; 
}

function send_query (data_processor,method,query)
{
	//alert("URL = : " + data_processor + query);	
	//in_ajax = true;
	xmlhttp=createXML();
	xmlhttp.open(method, data_processor);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
		
		xmlhttp.onreadystatechange =function()
		{
		//alert("here rs = " + xmlhttp.readyState + " status = " + xmlhttp.status);
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			
		//	alert(xmlhttp.responseText);
			process_xml_response(xmlhttp);
			
		}
		}	
	xmlhttp.send(query);
}

function process_xml_response(xml){
	var caller = xml.getResponseHeader("ajax_script_name");
	var output = xml.responseText;
	var action = xml.getResponseHeader("action");
	var id = xml.getResponseHeader("id");
	var call_back = xml.getResponseHeader("ajax_callback");
	
	//alert("id = " + id);
	
	//alert("caller = " + caller + " data = " + output);
	//alert("Callback = " + call_back);
	
	if(caller == "departments"){
		set_departments(output);
		return;
		}
		
	alert("invalid response from server: " + output);
	}
	
// xGetElementById r2, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xGetElementById(e)
{
  if(typeof(e)=='string') {
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
  }
  return e;
}


function get_departments(region_sel){
	var region_id = region_sel.options[region_sel.selectedIndex].value;
	//alert("region id = " + region_id);
	send_query("/get_departments.php","POST","region_id=" + region_id);
	}
	
function set_departments(data){
	//alert("data = " + data);
	dep_td = xGetElementById("department_sel");
	dep_td.innerHTML = data;
	}

