function populateLocation (countryVal, stateVal, countyVal, cityVal, url) {
	if (!url) return;
	setLocation (countryVal, stateVal, countyVal, cityVal, url, false);
}

function setLocation (countryVal, stateVal, countyVal, cityVal, url, force) {
	if (!url) return;

	//if country is mandatory but not provided, take it from form
	if (countryVal==-1) countryVal = getLocationValue("countryId");

	if (countryVal==-1 && stateVal==-1 && countyVal == -1 && cityVal == -1) return;

	url += "?countryId=" + countryVal + "&stateId=" + stateVal + "&countyId=" + countyVal + "&cityId=" + cityVal;
       if (force) url += "&populate=Y";
	var cityFrameDoc = getIframeDoc( getIframeById('citySelector') );
	if (cityFrameDoc && cityFrameDoc.location) {
		cityFrameDoc.location.href=url;
	}
}


function getLocation(type, url) {
	if (!url) return;
	var countryVal = getLocationValue("countryId");
	var countyVal = getLocationValue("countyId");
	var stateVal = getLocationValue("stateId");
	var cityVal = getLocationValue("cityId");

	if (type == "country") {
		url += "?countryId=" + countryVal + "&stateId=-1&countyId=-1&cityId=-1";
	} else if (type == "state") {
		url += "?countryId=" + countryVal + "&stateId=" + stateVal + "&countyId=-1&cityId=-1";
	} else if (type == "county") {
		url += "?countryId=" + countryVal + "&stateId=" + stateVal + "&countyId=" + countyVal + "&cityId=-1";
	} else {
		url += "?countryId=" + countryVal + "&stateId=" + stateVal + "&countyId=" + countyVal + "&cityId=" + cityVal;
	}

	var cityFrameDoc = getIframeDoc( getIframeById('citySelector') );
	if (cityFrameDoc && cityFrameDoc.location) {
		cityFrameDoc.location.href=url;
	}
}

function getLocationValue(id) {
	var elm = element(id);
	var val = "-1";
	if (elm && elm.options) {
		val = elm.options[elm.options.selectedIndex].value;

	} 
	return val;
}
