function ChangeLoc() {
	var mythis=this;
	this.country="up_country";
	this.region="up_region";
	this.city_search="up_city";
	this.city_result="city_result";
	this.city_id="up_city_id";
	this.city_display="up_city_display";
	$(this.region).onchange=function() { $(mythis.city_search).value=""; };
	$(this.country).onchange=function() { mythis.upSelChange(); };
	$(this.country).onkeypress=function() { clearTimeout(mythis.timerUp); mythis.timerUp=setTimeout(function() { mythis.upSelChange();},1000) };
	$(this.city_search).onkeypress=function() { mythis.autoComplete(); };
	$(this.city_search).onkeydown=function() { mythis.autoComplete(); };
}
ChangeLoc.prototype = {
	autoComplete: function() {
		var mythis = this;
		clearTimeout(this.timer);
		this.timer=setTimeout ( function () { mythis.getCity() }, 500);	
		if ( this.canBeBlank )
		{
			$(this.city_id).value="";
			$(this.city_display).innerHTML="Select a city";
		}
	},
	getCity: function() {
		var mythis=this;
		$(this.city_result).innerHTML='<p><img src="/images/loading.gif" /> Loading...</p>';
		this.citySel=null;
		new Ajax("/default/ajax/getCity", { onComplete: function(data){ mythis.AnswerCity(data); }, method: 'post', data: { "region_id": $(mythis.region).value , "city": $(mythis.city_search).value } }).request();
	},
	AnswerCity: function(data) {
		var mythis = this;
		data=Json.evaluate(data);
		ul=document.createElement('ul');
		for ( var i = 0; i<data.length; i++  )
		{
			li=document.createElement('li');
			ul.appendChild(li);
			li.innerHTML=data[i].name_accent;
			li.title=data[i].id;
			li.onclick=function() { mythis.liCitySel(this); };
		}
		$(this.city_result).innerHTML="";
		$(this.city_result).appendChild(ul);
	},
	liCitySel: function(obj) {
		this.citySel=obj;
		$(this.city_id).value=obj.title;
		$(this.city_display).innerHTML=obj.innerHTML;
	},
	upSelChange: function() {				
		var mythis=this;
		if ( $(this.country).value != "" ) {
			$(this.region).options.length=0; 
			$(this.region+"_loading").style.display="block";
			$(this.city_search).value="";
			$(this.city_result).innerHTML="";
			var Req = new Json.Remote(
				"/default/ajax/upSelChange?country=" + $(this.country).value, 
				{onComplete: function(data){ mythis.AnswerChange(data); } } 
			).send();
		}
	},
	AnswerChange: function(data) {
		if ( data["region"] && data["region"].length ) {
			if ( this.canBeBlank )
					this.SelectAdd($(this.region),"","");
			for ( var i = 0; i<data["region"].length; i++  )
				this.SelectAdd($(this.region),data["region"][i].name,data["region"][i].id);
		}
		$(this.region+"_loading").style.display="none";
	},
	SelectAdd: function(obj,text,value) {
		var option = document.createElement("option");
		option.text = text;
		option.value = value;
		try {
			obj.add(option,null);
		} catch(ex) {
			obj.add(option);
		}
	}
}
