
var addressService = {
serviceURL  : "geo",	
txtCountry  : "COUNTRY", 	
txtRegion   : "REGION",
txtProvince : "PROVINCE",
txtCity     : "CITY",
getRegions  : function(countryValue) {
   if( "-" == countryValue ) return;
   var bindArgs = {
		url: this.serviceURL,
	    error: function(type, data, evt){
	   		alert("An error occurred.");
		},
    	load: function(type, data, evt){
			addressService.fillSelectById( "region", data, false );
			addressService.fillSelectById( "province", addressService.txtProvince, true);
			addressService.fillSelectById( "city", addressService.txtCity, true);
    	},
    	mimetype: "text/html",
    	method:   "post",
    	content: { 
    		level: "regions",
    		country: countryValue    		
    	}
   };
   dojo.io.bind(bindArgs);   	 
},
getProvinces : function(regionValue) {
	if( "-" == regionValue ) return;
	var bindArgs = {
		url: this.serviceURL + "!provinces",
	    error: function(type, data, evt){
			alert("An error occurred.");
		},
    	load: function(type, data, evt){ 			
			addressService.fillSelectById( "province", data, false );
			addressService.fillSelectById( "city", addressService.txtCity, true);
    	},
    	mimetype: "text/html",
    	method:   "post",
    	content: { 
    		level: "provinces",
    		region: regionValue 
    	}
   };
   dojo.io.bind(bindArgs);
},
getCities : function(provinceValue) {
	if( "-" == provinceValue ) return;
	var bindArgs = {
		url: this.serviceURL + "!cities",
	    error: function(type, data, evt){
			alert("An error occurred.");
		},
    	load: function(type, data, evt){
			addressService.fillSelectById( "city", data, false );
    	},
    	mimetype: "text/html",
    	method:   "post",
    	content: {
    		level: "cities",
    		province: provinceValue 
    	}
   };
   dojo.io.bind(bindArgs);
},
replaceSelectHTML: function(selectHTML, newContent) {
	var start = selectHTML.toLowerCase().indexOf("<option");
	var newSelectHTML = selectHTML.substring(0, start) + newContent + "</select>";
	return newSelectHTML;
},
fillSelectById: function(selId, newContent, disabled) {
	var div = document.getElementById("div_"+selId);
	if( disabled ) {
		newContent = "<option value=\"-\" selected=\"selected\">" + newContent + " </option>";
	} 
	if( div ) {
		div.innerHTML = this.replaceSelectHTML(div.innerHTML, newContent);
		var el = document.getElementById(selId);
		if( el ) el.disabled = disabled;
	}
}
};


