var country_area_town3=function(country_id, area_id, town_id, relement_id, c_value_sign, a_value_id,  t_value_name, country_areas,focus_id ){
var urlGetArea= '/ajax/get_areas2.php';
var urlGetTown= '/ajax/get_towns3.php';
//var urlGetCountry= '/ajax/get_countris2.php';
//var urlProgress="/distance/ajax/progress.gif";
//var progress=$("<img src='"+urlProgress+"'>");
var set_by_click=false;
var disable =function(element){ element.attr("disabled","disabled");} ; 
var enable =function(element){ element.removeAttr("disabled");  };
var object_select_country =$("#"+country_id); 
var object_select_area = $("#"+area_id); 
var object_input_town = $("#"+town_id); 

var dom_select_country =document.getElementById(country_id); 
var dom_select_area = document.getElementById(area_id); 
var dom_input_town = document.getElementById(town_id); 





function addOption (oListbox, text, value, isDefaultSelected, isSelected)
{/*
  var oOption = document.createElement("option");
  oOption.appendChild(document.createTextNode(text));
  oOption.setAttribute("value", value);

  if (isDefaultSelected) oOption.defaultSelected = true;
  else if (isSelected) oOption.selected = true;

  oListbox.appendChild(oOption);*/
	oListbox.options[oListbox.options.length] = new Option(text,value);
}



function selectElement(select_id,value){
	 
	var aElement=document.getElementById(select_id);
	 for (var i=0; i < aElement.options.length; i++)
	  {
	     if (aElement.options[i].value==value)
		 aElement.options[i].selected=true;
	  }
} 


var refresh_country= function(c_sign,a_id,dis){
	/*
	//$("#"+country_id+" option").remove();
	object_select_country.empty();
	disable(object_select_country);
	//object_select_country.append($('<option ></option>').attr("value",""));
	addOption(dom_select_country,"","");
	$.getJSON(urlGetCountry,
			{'hash':Math.random()			
			},
			function(data){
				$.each(data.countris, function(i,country){	
				//object_select_country.append($('<option >'+country.country_name+'</option>').attr("value",country.country_sign));
					addOption(dom_select_country,country.country_name,country.country_sign);
				});
				//alert("");
				if(c_id!=''&& c_id!=null)
				//	$("#"+country_id+" option[value=" + c_id +"]").attr("selected","selected") ;
					object_select_country.val(c_id); 
					//selectElement(country_id,c_id);
					
				if(c_id!=''&& c_id!=null) {refresh_areas(c_id, a_id );}
				if(!dis) enable(object_select_country);
			});
			*/
	if(c_sign==''|| c_sign==null) disable(object_select_area);
	//disable(object_select_country);
	//object_select_country.val(c_id); 
	//object_select_area.val(a_id); 
	//if(c_id!=''&& c_id!=null) {refresh_areas(c_id, a_id );}
	//if(!dis) enable(object_select_country);
	};
var refresh_areas= function(c_sign, a_id){
	
	disable(object_select_area);
	//$("#"+area_id+" option").remove();
	object_select_area.empty();
	//object_select_area.append($('<option ></option>').attr("value",""));
	addOption(dom_select_area,"","");
	addOption(dom_select_area,"любая обл.","");
	var count=0;
	
	/*
	$.getJSON(urlGetArea,
			{ 'country_sign':c_id ,  'hash':Math.random()},
			function(data){
				
				$.each(data.countris, function(i,country){	
					count++;
					//object_select_area.append($('<option >'+country.area_name+'</option>').attr("value",country.area_id));
					addOption(dom_select_area,country.area_name,country.area_id);	
				});
				
				if (count>0){//alert(count);
					enable(object_select_area);}
				
				//alert(a_id);
				
				if (a_id!=''&& a_id!=null){
				//$("#"+area_id+" option[value=" + a_id +"]").attr("selected","selected") ;
					//alert($("#"+area_id+" option[value=" + a_id +"]").text());
				
					object_select_area.val(a_id); 	
					//selectElement(area_id,a_id);
				}
			
				
			});
	
	*/
	if (c_sign!=''&& c_sign!=null){
	
		for (var i in country_areas[c_sign]) {
		count++;
		addOption(dom_select_area, country_areas[c_sign][i], i);	
		}
	}
	if (count>0){
		enable(object_select_area);}
	if (a_id!=''&& a_id!=null){
		object_select_area.val(a_id); 	
	}
	
	};
var refresh_town= function(town_name,target,e){
	if (town_name!=null&&town_name!=''){
	object_input_town.addClass("loading");
	disable(object_select_country);	
	disable(object_select_area);
	$("#"+area_id+" option").remove();
	
	//$("#"+country_id+" option[value='']").attr("selected","selected") ;
	object_select_country.val(''); 	
	//selectElement(country_id,"");
	var count=0;
	
	$.getJSON(urlGetTown,
				{ 'town':town_name,  'hash':Math.random()},
				function(data){
					
					if(data!=null){
					c_sign=data.country_sign;
					if(c_sign!=''&& c_sign!=null){
						//$("#"+country_id+" option[value=" + c_id +"]").attr("selected","selected") ;
						object_select_country.val(c_sign);
						//selectElement(country_id,c_id);
					}
					
					if(c_sign!=''&& c_sign!=null) 
						refresh_areas(c_sign,data.area_id);
					
					
					move_focuse(target,e);
					}
					
					enable(object_select_country);
					object_input_town.removeClass("loading");	
					
					if(data==null){move_focuse(object_select_country,e);};
					
				});
	}
	else{
		move_focuse(object_input_town,e);
	}
		
		
};
var getInputTownValue= function (){
	var out= object_input_town.attr("value");
	return out;
}; 
var setInputTownValue=function (value){   object_input_town.attr( "value", value);}; 
var setValue=function (element,value){ element.attr( "value", value);};

//disable(object_select_country);
//disable(object_select_area);
refresh_country(c_value_sign,a_value_id,false);

setInputTownValue(t_value_name);

 
var _change_country=function (e) {
	var val=object_select_country.val();
	if(val!=null)
	//if ($("#"+country_id+" option:selected").val()!=null)
	refresh_areas(val,'');
};
var _change_town=function (e) {
	refresh_town(getInputTownValue(),$('#'+focus_id),e);
	return false;
};


function move_focuse(target, e){
	target.focus();
	if (e!=null){
		e.returnValue = false; // for IE
		if (e.preventDefault) e.preventDefault(); // for Mozilla
	}
}

var _keydown=function (e) {
	if (e.keyCode == 9 ) {
		refresh_town(getInputTownValue(),$('#'+focus_id),e);
		//if(focus_id!=undefined){
		//	move_focuse(focus_id, e);
		//}
	}
};




/*
var _keydown=function (e) {
	if(e.keyCode ==13){
	
	};
};*/
rElement=$("#"+relement_id);
rElement.bind("click",  _change_town);

object_select_country.bind("change",_change_country);
object_input_town.bind("keydown",  _keydown);
//object_input_town.bind("keyup",  _keyup);
//object_input_town.bind("keydown",  _change_town);


};