var Ajax_states = false;
var Ajax_counties = false;
var Ajax_cities = false;
var Ajax_zipcodes = false;

function AjaxRequest(){
	var Ajax = false;
	if(window.XMLHttpRequest){
		Ajax = new XMLHttpRequest();			
	}else if(window.ActiveXObject){
		try{
			Ajax = new ActiveXObject("Msxml2.XMLHTTP");				
		}catch(e){
			try{
				Ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e) { }
		}
	}
	return Ajax;
}

//==================================================================
// STATES
//==================================================================
function build_combo_states(state_sg){
	
	Ajax_states = new AjaxRequest();		
	if(!Ajax_states){
		window.alert("Error inicializing Ajax!");
		return;
	}
	Ajax_states.onreadystatechange = build_combo_states_ajax;
	Ajax_states.open('GET', 'build_combo_states.php?state_sg='+state_sg, true);	
	Ajax_states.send(null);	
}
function build_combo_states_ajax(){	
	var ss = document.getElementById("state_sg");
	if(Ajax_states.readyState == 4){
		if(Ajax_states.status == 200 ){
			var html_response = Ajax_states.responseText;
			ss.innerHTML = html_response;
		}
		else{
			window.alert("Server error!");
		}
	}
}

//==================================================================
// COUNTIES
//==================================================================
function build_combo_counties(state_sg, county_name){
	Ajax_counties = new AjaxRequest();		
	if(!Ajax_counties){
		alert("Error inicializing Ajax!");
		return;
	}
	Ajax_counties.onreadystatechange = build_combo_counties_ajax;
	Ajax_counties.open('GET', 'build_combo_counties.php?state_sg='+state_sg+'&county_name='+county_name, true);
	Ajax_counties.send(null);
}
function build_combo_counties_ajax(){	
	var ss = document.getElementById("county_name");
	if(Ajax_counties.readyState == 4){
		if(Ajax_counties.status == 200 ){
			var html_response = Ajax_counties.responseText;
			ss.innerHTML = html_response;
		}
		else{
			alert("Server error!");
		}
	}
}

//==================================================================
// CITIES
//==================================================================
function build_combo_cities(state_sg, county_name, city){
	Ajax_cities = new AjaxRequest();		
	if(!Ajax_cities){
		alert("Error inicializing Ajax!");
		return;
	}
	Ajax_cities.onreadystatechange = build_combo_cities_ajax;
	Ajax_cities.open('GET', 'build_combo_cities.php?state_sg='+state_sg+'&county_name='+county_name+'&city='+city, true);
	Ajax_cities.send(null);
}
function build_combo_cities_ajax(){	
	var ss = document.getElementById("city");
	if(Ajax_cities.readyState == 4){
		if(Ajax_cities.status == 200 ){
			var html_response = Ajax_cities.responseText;
			ss.innerHTML = html_response;
		}
		else{
			alert("Server error!");
		}
	}
}

//==================================================================
// ZIPCODES
//==================================================================
function build_combo_zipcodes(state_sg, county_name, city, zipcode){
	Ajax_zipcodes = new AjaxRequest();		
	if(!Ajax_zipcodes){
		alert("Error inicializing Ajax!");
		return;
	}
	Ajax_zipcodes.onreadystatechange = build_combo_zipcodes_ajax;
	Ajax_zipcodes.open('GET', 'build_combo_zipcodes.php?state_sg='+state_sg+'&county_name='+county_name+'&city='+city+'&zipcode='+zipcode, true);
	Ajax_zipcodes.send(null);
}
function build_combo_zipcodes_ajax(){	
	var ss = document.getElementById("combo_zipcodes");
	if(Ajax_zipcodes.readyState == 4){
		if(Ajax_zipcodes.status == 200 ){
			var html_response = Ajax_zipcodes.responseText;
			ss.innerHTML = html_response;
		}
		else{
			alert("Server error!");
		}
	}
}
//==================================================================
// REQUEST PHOTO
//==================================================================
function request_property_photo(lat, long, elementId){
	Ajax_property_photo = new AjaxRequest();		
	if(!Ajax_property_photo){
		alert("Error inicializing Ajax!");
		return;
	}
	Ajax_property_photo.onreadystatechange = function (){
		//window.alert(elementId);
		divId = elementId;
		request_property_photo_ajax(divId);	
	};
	window.alert('request_property_photo.php?lat='+lat+'&long='+long);
	Ajax_property_photo.open('GET', 'request_property_photo.php?lat='+lat+'&long='+long, true);
	Ajax_property_photo.send(null);
}


/*function request_property_photo(lat, long, elementId){
	Ajax_property_photo = new AjaxRequest();		
	if(!Ajax_property_photo){
		alert("Error inicializing Ajax!");
		return;
	}
	Ajax_property_photo.onreadystatechange = request_property_photo_ajax(elementId);
	//window.alert('request_property_photo.php?lat='+lat+'&long='+long);
	Ajax_property_photo.open('GET', 'request_property_photo.php?lat='+lat+'&long='+long, true);
	Ajax_property_photo.send(null);
}*/
function request_property_photo_ajax(divId){	
	//window.alert(elementId);
	elementId = divId;
	var ss = document.getElementById(elementId);
	//window.alert(Ajax_property_photo.readyState);
	if(Ajax_property_photo.readyState == 4){
		//window.alert("a");
		if(Ajax_property_photo.status == 200 ){
			var html_response = Ajax_property_photo.responseText;
			window.alert(html_response);
			ss.innerHTML = html_response;
		}
		else{
			alert("Server error!");
		}
	}
}