var cs = {
	url:'/practice/service.php?',
	c_select:null,
	s_select:null,
	http:null,
	init:function(country_id,state_id) {
		var c_select = document.getElementById(country_id);
		var s_select = document.getElementById(state_id);
		if (c_select) {
			this.c_select = c_select;
			this.s_select = s_select;
			c_select.onchange = function() { cs.onchange(this.id,this.value); }
			if(!s_select) {
				var div = document.createElement('div');
			}

		}
	},
	onchange:function(id,country_code) {
		if (this.s_select) {
			this.http = createRequestObject();

			// synchronous call
			this.http.open('get',this.url+'type=state&ccode='+country_code, false);
			this.http.send(null);
			cs.populate(id,eval(this.http.responseText));

		}
	},
	populate:function(id,data) {
		var c_split = id.split("_");
		if (c_split.length > 1) {
			var s_tmp = this.s_select.id.split("_");
			this.s_select = document.getElementById(c_split[0]+"_"+s_tmp[s_tmp.length-1]);
		}
		else if (this.s_select.id.split("_").length > 1)
			this.s_select = document.getElementById(this.s_select.id.split("_")[1]);
		if(this.s_select) {

			// clean any old states
			while(this.s_select.hasChildNodes()){
				this.s_select.removeChild(this.s_select.lastChild);
			}
			var other = document.createElement('option');
			other.value = '';
			other.appendChild(document.createTextNode('All Regions'));
			this.s_select.appendChild(other);

			for (var i = 0; i <data.length; i++) {
				var option = document.createElement('option');
				option.value = data[i];
				option.appendChild(document.createTextNode(data[i]));
				this.s_select.appendChild(option);
			}

		}
	}

}
