// function for ajax
function makeRequest(url, parameter, method, object_response, execute_after){
	var http_request = false;
	if (window.XMLHttpRequest){ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType){
			http_request.overrideMimeType('text/html');
		}
	}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('Cannot create XMLHTTP instance');
		return false;
	}	
	http_request.onreadystatechange = function () {
		if(http_request.readyState==4){
			if(http_request.status==200){
				if (document.getElementById(object_response) == null) alert('There was a problem with the object response.');
				else { 
					result = http_request.responseText;
					document.getElementById(object_response).innerHTML = result;
					if (execute_after!='') eval(execute_after);
				}
				
			}else{
				alert('This file does not exist');
			}
		}
	}
	if (method=='GET' || method=='get') {
		http_request.open(method, url+'?'+parameter, true);
		http_request.send(null);
	}
	else {
		http_request.open(method, url, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", parameter.length);
		http_request.setRequestHeader("Connection", "close");		
		http_request.send(parameter);
	}
}

function searchCounties (st) {
	var temp = st.split(" ");
	var state_sigla = temp[1];
	var state_name 	= temp[0];
	
	document.getElementById('div_counties').innerHTML = "<select><option>Searching....</option></select>";
	makeRequest('/request-dropdown.php','state_name='+state_name,'GET','div_counties','document.getElementById(\'div_cities\').innerHTML = "<select id=\'drop_cities\'><option>Select a County First</option></select>";');
}

function searchCities (st,cn) {
	var temp = st.split(" ");
	var state_sigla = temp[1];
	var state_name 	= temp[0];
	
	
	if (cn!=null && cn!="") {
		temp = cn.split(" ");
		var county_name = temp[0];
		var fips = temp[1];
		document.getElementById('div_cities').innerHTML = "<select><option>Searching....</option></select>";
		makeRequest('/request-dropdown.php','state_name='+state_name+'&county_name='+county_name,'GET','div_cities','');
	} else {
		document.getElementById('div_cities').innerHTML = "<select id='drop_cities'><option>Select a County First</option></select>";
	}
}

function goto() {
	var temp = getSelectedValue (document.getElementById('drop_states'));
	if (temp == '' || temp ==null || temp=='Select a State') {
		alert('Select a state first');
	} else {
		temp = temp.split(" ");
		var state_sigla = temp[1];
		var state_name 	= temp[0];
			
		temp = getSelectedValue (document.getElementById('drop_counties'));
		if (temp == '' || temp ==null || temp=='Select a State First' || temp=='All') {
			window.location = '/lists/'+state_name+'.html';
		} else {
			temp = temp.split(" ");
			var county_name = temp[0];
			var fips = temp[1];
			
			var city_name = getSelectedValue (document.getElementById('drop_cities'));
			if (city_name == '' || city_name ==null ) {
				window.location = '/lists/'+state_sigla.toLowerCase()+'-'+county_name.toLowerCase()+'.html';
			} else {
				window.location = '/lists/'+state_sigla.toLowerCase()+'-'+fips +'-'+city_name.toLowerCase()+'.html';			
			}			
		}		
	}

}


function getSelectedValue (objSelect) {
	for (i=0;i<objSelect.length;i++) {
		if (objSelect[i].selected==true) {
			return objSelect[i].value;
		}
	}
}