//globals
var first = "county"; //id of first SELECT
var second = "town"; //id of second SELECT
var third = "category"; //id of third SELECT

function sendRequest1(url,params,HttpMethod) {
	if(!HttpMethod) { //check if http method is defined, if not, set it to GET    
		HttpMethod="GET"; 
	}

// initialize request object 
	req=null; 
	if(window.XMLHttpRequest){
		req=new XMLHttpRequest; //mozilla/safari 
	} else if(window.ActiveXObject){
		req=new ActiveXObject("Microsoft.XMLHTTP"); //internet explorer
	}

//define callback handler
	if(req) {
		req.onreadystatechange=onReadyState1;
		req.open(HttpMethod,url,true);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req.send(params);
	}
}


function sendRequest2(url,params,HttpMethod) {
	if(!HttpMethod) { //check if http method is defined, if not, set it to GET    
		HttpMethod="GET"; 
	}

// initialize request object 
	req2=null;
	if(window.XMLHttpRequest){
		req2=new XMLHttpRequest; //mozilla/safari 
	} else if(window.ActiveXObject){
		req2=new ActiveXObject("Microsoft.XMLHTTP"); //internet explorer
	}

//define callback handler
	if(req2) {
		req2.onreadystatechange=onReadyState2;
		req2.open(HttpMethod,url,true);
		req2.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req2.send(params);
	}
}

function onReadyState1() {
	var ready=req.readyState;
	var data=null;
	if(ready==4){ //check ready state
		data=req.responseText; //read response data
		var items = data.split(',');
		var length = items.length;
		for(var i = 0; i < length; i++) {
			var childEl = document.createElement('option'); //create option
			var El = document.getElementById(second);
			El.appendChild(childEl); //then append it to the second dropdown list
			childEl.value = items[i];
			childEl.innerHTML = items[i];
		}
	}
}

function onReadyState2() {
	var ready2=req2.readyState;
	var data2=null;
	if(ready2==4){ //check ready state
		data2=req2.responseText; //read response data
		var items = data2.split(',');
		var length = items.length;
		for(var i = 0; i < length; i++) {
			var childE2 = document.createElement('option'); //create option
			var E2 = document.getElementById(third);
			E2.appendChild(childE2); //then append it to the second dropdown list
			childE2.value = items[i];
			childE2.innerHTML = items[i];
		}
	}
}

function clicked() {
	var el = document.getElementById(first);
	var ob2=document.getElementById(second);
	var selected = el.selectedIndex;
	
	while(ob2.hasChildNodes()) { //removes items from dropdown if some already exist
		ob2.removeChild(ob2.firstChild);
	}
	if(selected!= 0) { //if they choose something other than the first select-->"Select topic first"
		sendRequest1('plannersGetTown1.php?county='+el.options[selected].value);
		ob2.disabled=0;
	} else { //otherwise add the Select Topic First option and disable it
		sendRequest1('plannersGetTown2.php');
		ob2.disabled=0;
	}
}

function clickedV(searchBy) {
	if(searchBy == 'county'){ var e2 = document.getElementById(first); }
	if(searchBy == 'town'){ var e2 = document.getElementById(second); }

	var ob3=document.getElementById(third);
	var selected = e2.selectedIndex;
	
	while(ob3.hasChildNodes()) { //removes items from dropdown if some already exist
		ob3.removeChild(ob3.firstChild);
	}
	if(selected!= 0) { //if they choose something other than the first select-->"Select topic first"
		sendRequest2('plannersGetCat1.php?'+searchBy+'='+e2.options[selected].value);
		ob3.disabled=0;
	} else { //otherwise add the Select Topic First option and disable it
		sendRequest2('plannersGetCat2.php');
		ob3.disabled=0;
	}
}