function regionSelected() {
	if (document.getElementById("state") != null) {
		document.getElementById("state").style.display = "none";
	}
	getCountryList();
}

function countrySelected() {
	getStateList();
}

function stateSelected() {
	//display Homes
}

function getCountryList() {
	new Ajax.Request('/country_listing.php', { 
		method:'post',
		parameters: {region_id:document.formDetails.region_select[document.formDetails.region_select.selectedIndex].value}, 
	  	onSuccess: function(transport) {
	  	  
		  var json = transport.responseText.evalJSON();
		  buildCountrySelect(json);
		}
	  });
}

function getStateList() {
	new Ajax.Request('/state_listing.php', { 
		method:'post',
		parameters: {country_id:document.formDetails.country_select[document.formDetails.country_select.selectedIndex].value},
	  	onSuccess: function(transport) {
	  	  
		  var json = transport.responseText.evalJSON();
		  buildStateSelect(json);
		}
	  });
}

function buildCountrySelect(jsonResponse) {
	document.formDetails.country_select.options.length = 0;
	
	document.formDetails.country_select[0] = new Option("Select Country", 0, false);
	
	for (var i=0; i<jsonResponse.data.length; i++) {
		document.formDetails.country_select[i+1]=new Option(jsonResponse.data[i].name, jsonResponse.data[i].id, false);
	}
	if (document.getElementById("country") != null) {
		document.getElementById("country").style.display = "block";
	}
}

function buildStateSelect(jsonResponse) {
	document.formDetails.state_select.options.length = 0;
	document.formDetails.state_select[0] = new Option("Select State", 0, false);
	
	for (var i=0; i<jsonResponse.data.length; i++) {
		document.formDetails.state_select[i+1]=new Option(jsonResponse.data[i].name, jsonResponse.data[i].id, false);
	}
	
	if (document.getElementById("state") != null) {
		document.getElementById("state").style.display = "block";
	}
}

function regionSelected2() {
	if (document.getElementById("state") != null) {
		document.getElementById("state").style.display = "none";
	}
	getCountryList2();
}

function countrySelected2() {
	getStateList2();
}

function stateSelected2() {
	//display Homes
}

function getCountryList2() {
	new Ajax.Request('country_listing.php', { 
		method:'post',
		parameters: {region_id:document.formDetails.region_select2[document.formDetails.region_select2.selectedIndex].value}, 
	  	onSuccess: function(transport) {
	  	  
		  var json = transport.responseText.evalJSON();
		  buildCountrySelect2(json);
		}
	  });
}

function getStateList2() {
	new Ajax.Request('state_listing.php', { 
		method:'post',
		parameters: {country_id:document.formDetails.country_select2[document.formDetails.country_select2.selectedIndex].value},
	  	onSuccess: function(transport) {
	  	  
		  var json = transport.responseText.evalJSON();
		  buildStateSelect2(json);
		}
	  });
}

function buildCountrySelect2(jsonResponse) {
	document.formDetails.country_select2.options.length = 0;
	
	document.formDetails.country_select2[0] = new Option("Select Country", 0, false);
	
	for (var i=0; i<jsonResponse.data.length; i++) {
		document.formDetails.country_select2[i+1]=new Option(jsonResponse.data[i].name, jsonResponse.data[i].id, false);
	}
	if (document.getElementById("country") != null) {
		document.getElementById("country").style.display = "block";
	}
}

function buildStateSelect2(jsonResponse) {
	document.formDetails.state_select2.options.length = 0;
	document.formDetails.state_select2[0] = new Option("Select State", 0, false);
	
	for (var i=0; i<jsonResponse.data.length; i++) {
		document.formDetails.state_select2[i+1]=new Option(jsonResponse.data[i].name, jsonResponse.data[i].id, false);
	}
	
	if (document.getElementById("state") != null) {
		document.getElementById("state").style.display = "block";
	}
}

