var geo_url = "/scripts/suggest.php";
var geo_prefix = "";
var country_field = "";
var region_field = "";
var city_field = "";
var geo_do_not_submit_mf = false;

function getSelectionId(text, li) {
	$(text.id+"_id").value=li.id;
	switch (text.id){
		case (geo_prefix + "country"): { region_completer.url=geo_url+"?id="+li.id; break;}
		case (geo_prefix + "region"): { city_completer.url=geo_url+"?id="+li.id; break;}
		case (geo_prefix + "city"): { 
									if	(text.value == "Москва"){
										$(geo_prefix + "metro").disabled = false;
									}else{
										$(geo_prefix + "metro").disabled = true;
									}
									break;
									}

		default: break;
	}
}

function catchDiff(e, obj){
	if(e.keyCode==Event.KEY_RETURN){
			geo_do_not_submit_mf = true;
			e.stop;
			country_completer.hide(); 
			region_completer.hide(); 
			city_completer.hide(); 
			return false;
	}
	if(e.keyCode==Event.KEY_TAB){
			country_completer.hide(); 
			region_completer.hide(); 
			city_completer.hide(); 
			return;
	}
	if ( (e.shiftKey && (e.keyCode==16 || e.keyCode==9)) || (e.altKey && e.keyCode==17) || (e.ctrlKey  && e.keyCode==18 )) return;
	
//	alert(obj.id + " " + geo_prefix + "country");
	switch (obj.id){
		case (geo_prefix + "country"): { 
			if	(country_field != obj.value){
				$(geo_prefix + "region").value="";
				$(geo_prefix + "city").value="";
				$(geo_prefix + "country_id").value="";
				$(geo_prefix + "region_id").value="";
				$(geo_prefix + "city_id").value="";
				if	(obj.value == ""){
					$(geo_prefix + "region").disabled = true;
				}else{
					$(geo_prefix + "region").disabled = false;
				}
				$(geo_prefix + "city").disabled = true;
				region_completer.url=geo_url; 
				city_completer.url=geo_url; 
			}
			country_field = obj.value;
			break;
		}
		case (geo_prefix + "region"): { 
			if	(region_field != obj.value){
				$(geo_prefix + "city").value="";
				$(geo_prefix + "region_id").value="";
				$(geo_prefix + "city_id").value="";
				if	(obj.value == ""){
					$(geo_prefix + "city").disabled = true;
				}else{
					$(geo_prefix + "city").disabled = false;
				}
				city_completer.url=geo_url; 
			}
			region_field = $(geo_prefix + "region").value;
			break;
		}
		case (geo_prefix + "city"): { 
			if	(region_field != $(geo_prefix + "region").value){
				$(geo_prefix + "city_id").value="";
			}
			city_field = $(geo_prefix + "city").value;
			break;
		}
		default: break;
	}
}

function checkKey(e, self){
	var e = e || window.event;
	if ( (e.shiftKey && (e.keyCode==16 || e.keyCode==9)) || (e.altKey && e.keyCode==17) || (e.ctrlKey  && e.keyCode==18 )) return;
	switch (e.keyCode){
		case Event.KEY_RETURN:
			geo_do_not_submit_mf = true;
			e.stop;
			country_completer.hide(); 
			region_completer.hide(); 
			city_completer.hide();
			return false;
		case Event.KEY_TAB:
		case Event.KEY_ESC:
		case Event.KEY_LEFT:
		case Event.KEY_RIGHT:
		case Event.KEY_UP:
		case Event.KEY_DOWN:
			break;
		default: {
			switch (self.id){
				case (geo_prefix + "country"): { 
					$(geo_prefix + "region").value="";
					$(geo_prefix + "city").value="";
					$(geo_prefix + "country_id").value="";
					$(geo_prefix + "region_id").value="";
					$(geo_prefix + "city_id").value="";
					region_completer.url=geo_url; 
					city_completer.url=geo_url; 
					break;
				}
				case (geo_prefix + "region"): { 
					$(geo_prefix + "city").value="";
					$(geo_prefix + "region_id").value="";
					$(geo_prefix + "city_id").value="";
					city_completer.url=geo_url; 
					break;
				}
				case (geo_prefix + "city"): { 
					$(geo_prefix + "city_id").value="";
					break;
				}
				default: break;
			}
		}
	}
}

function geoSetPrefix(prefix){
	geo_prefix = prefix;
	country_field = $(geo_prefix + "country").value;
	region_field = $(geo_prefix + "region").value;
	city_field = $(geo_prefix + "city").value;
	if($(geo_prefix + "country").value == "") $(geo_prefix + "region").disabled = true;
	if($(geo_prefix + "region").value == "") $(geo_prefix + "city").disabled = true;
	if($(geo_prefix + "metro") && $(geo_prefix + "city").value != "Москва" ) $(geo_prefix + "metro").disabled = true;

	country_completer.options.parameters = "";
	country_completer.initialize( $(prefix + "country"), country_completer.update, country_completer.url, country_completer.options)
	region_completer.options.parameters = "";
	region_completer.initialize( $(prefix + "region"), region_completer.update, region_completer.url, region_completer.options)
	city_completer.options.parameters = "";
	city_completer.initialize( $(prefix + "city"), city_completer.update, city_completer.url, city_completer.options)
	
}
function monitor(){
	$('c_id').innerHTML = $('country_id').value;
	$('c_url').innerHTML = country_completer.url;
	$('r_id').innerHTML = $('region_id').value;
	$('r_url').innerHTML = region_completer.url;
	$('ci_id').innerHTML = $('city_id').value;
	$('ci_url').innerHTML = city_completer.url;
}