//(new Image()).src = '/images/uploading.gif';  // preload the uploading image
//var loadingImage = "<img src='/images/uploading.gif' />";
CheckAjax()
var action = "";

function loadCities(p,a){ 
  // replace the city dropdown box with the loading image
  //writeObject('divCity', loadingImage);
	
	// disable the dropdown while loading new cities
	document.getElementById('ddlCity').disabled = true;
	action = a;
	
	// disable the corresponding ddl while loading cities
	if(action == "citysections") {
		document.getElementById('ddlCitySections').disabled = true;
	}
	else {
		document.getElementById('ddlHousingType').disabled = true;
	}
	
  // get the associated cities
	var url="/ajax/getCities.php";
	url+="?pID="+p;
	url+="&load="+a;
	url+="&sid="+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.onreadystatechange=cityStateChanged;
	xmlHttp.send(null);
}

// stateChanged(): If the state is 4 or complete display a message to the user
function cityStateChanged() { 
	if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		// write the corresponding cities
		var response = xmlHttp.responseText;
		xmlHttp.close;
		writeObject("divCity",response);
		
		// initialize the CitySection or HousingType ddl
		if(action == "citysections") {
			loadCitySections();
		}
		else {
			loadHousing();
		}
		
	}
}


function loadCitySections() {
	// get the associated housing types
	var url="/ajax/getCitySections.php";
	url+="?pID="+document.getElementById('ddlProvince').value;
	url+="&cID="+document.getElementById('ddlCity').value;
	url+="&sid="+Math.random();
	xmlHttp = GetXmlHttpObject();
	xmlHttp.open("GET",url,true);
	xmlHttp.onreadystatechange=csStateChanged;
	xmlHttp.send(null);
}

// stateChanged(): If the state is 4 or complete display a message to the user
function csStateChanged() { 
 	if (xmlHttp.readyState==4 && xmlHttp.status==200) { 
  	// write the corresponding city sections
  	var response = xmlHttp.responseText;
		xmlHttp.close;
		writeObject("divCitySections",response);
  }
}


function loadHousing() {
  // replace the housingType dropdown box with the loading image
	//writeObject('divHousingType', loadingImage);
	// get the associated housing types
	var url="/ajax/getHousingType.php";
	url+="?pID="+document.getElementById('ddlProvince').value;
	url+="&cID="+document.getElementById('ddlCity').value;
	url+="&sid="+Math.random();
	xmlHttp = GetXmlHttpObject();
	xmlHttp.open("GET",url,true);
	xmlHttp.onreadystatechange=htStateChanged;
	xmlHttp.send(null);
}

// stateChanged(): If the state is 4 or complete display a message to the user
function htStateChanged() { 
 if (xmlHttp.readyState==4 && xmlHttp.status==200) { 
  	// write the corresponding HousingTypes
  	var response = xmlHttp.responseText;
		xmlHttp.close;
		writeObject("divHousingType",response);
  }
}
