var xmlHttp;
var ca_code;
var sc_code;
var pr_code;
var am_code;
var to_code;


function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}

///////////////////////////////////////////////////////////////////////////////

function UserChange(){
ca_code=document.getElementById("ca_code").value;
var url="load.php?ca_code="+ca_code;
createXMLHttpRequest();
xmlHttp.onreadystatechange=handleUserChange;
xmlHttp.open("GET",url,false);
xmlHttp.send(null);
}

function handleUserChange(){
	if(xmlHttp.readyState==4){
		if(xmlHttp.status==200){
		updateUserList();
		}
	}
}

function updateUserList(){
clearUserList();
sc_code=document.getElementById("sc_code");
var result=xmlHttp.responseText;
var option=null;

c=result.split("|");
option = document.createElement("option");
option.value="";
option.text = "Select Sub Category";
sc_code.add(option);

for(var i=0;i<c.length-1;i++){
cc=c[i].split(":");
option = document.createElement("option");
option.value=cc[0];
option.text = cc[1];
sc_code.add(option);
}
}


function clearUserList(){
sc_code=document.getElementById("sc_code");
for(i=sc_code.length-1;i>=0;i--){
	sc_code.options.remove(i);
}
}

/////////////////////////////////////////////////////////////

function UserChange2(){
pr_code=document.getElementById("pr_code").value;
var url="load2.php?pr_code="+pr_code;
createXMLHttpRequest();
xmlHttp.onreadystatechange=handleUserChange2;
xmlHttp.open("GET",url,false);
xmlHttp.send(null);
}

function handleUserChange2(){
	if(xmlHttp.readyState==4){
		if(xmlHttp.status==200){
		updateUserList2();
		}
	}
}

function updateUserList2(){
clearUserList2();
am_code=document.getElementById("am_code");
var result=xmlHttp.responseText;
var option=null;

c=result.split("|");
option = document.createElement("option");
option.value="";
option.text = "Select Amphur";
am_code.add(option);

for(var i=0;i<c.length-1;i++){
cc=c[i].split(":");
option = document.createElement("option");
option.value=cc[0];
option.text = cc[1];
am_code.add(option);
}
}


function clearUserList2(){
am_code=document.getElementById("am_code");
for(i=am_code.length-1;i>=0;i--){
	am_code.options.remove(i);
}
}

//////////////////////////////////////////////////////////////////////


function UserChange3(){
am_code=document.getElementById("am_code").value;
var url="load31.php?am_code="+am_code;
createXMLHttpRequest();
xmlHttp.onreadystatechange=handleUserChange3;
xmlHttp.open("GET",url,false);
xmlHttp.send(null);
}

function handleUserChange3(){
	if(xmlHttp.readyState==4){
		if(xmlHttp.status==200){
		updateUserList3();
		}
	}
}

function updateUserList3(){
clearUserList3();
to_code=document.getElementById("to_code");
var result=xmlHttp.responseText;
var option=null;

c=result.split("|");
option = document.createElement("option");
option.value="";
option.text = "Select Tombon";
to_code.add(option);

for(var i=0;i<c.length-1;i++){
cc=c[i].split(":");
option = document.createElement("option");
option.value=cc[0];
option.text = cc[1];
to_code.add(option);
}
}


function clearUserList3(){
to_code=document.getElementById("to_code");
for(i=to_code.length-1;i>=0;i--){
	to_code.options.remove(i);
}
}

//////////////////////////////////////////////////////////////////////



