var isIE = (!isOpera && ua.indexOf('msie') > -1);
var selected_country_text = '';
var selected_region_text = '';
var selected_locality_text = '';
var selected_country_id = 0;
var selected_region_id = 0;
var selected_locality_id = 0;

function OnCountryChange(country_id, language)
{
	$("#region_area").html('<img src="/res/images/chocolate/ajax-loader.gif">');
	$("#locality_area").html('<input type="hidden" id="user_documents_locality" value="0">');
	var obj_country = document.getElementById("user_documents_country");
	selected_country_text = (obj_country.selectedIndex > 0 ? obj_country.options[obj_country.selectedIndex].text : '');
	selected_region_text = '';
	selected_locality_text = '';
	selected_country_id = obj_country.value;
	selected_region_id = 0;
	selected_locality_id = 0;
	if (isIE)
	{
		$.post("/res/ajax_server/get_region.php", {'country' : country_id, 'language' : language, 'ie' : 1}, fillRegion);
	}
	else
	{
		$.post("/res/ajax_server/get_region.php", {'country' : country_id, 'language' : language}, fillRegion);
	}
}

function fillRegion(data)
{
    var ArrRegins   = $('region', data);

	var fr_sel_txt = '<select class="lightgray_12px" name="region" id="user_documents_region" onchange="javascript:OnRegionChange(this.value,'+ u_lang +')">\r\n<option> - Все регионы - </option>\r\n';
	var fr_opt_txt = '';
	
    if(ArrRegins.length > 0)
    {
        for(var i=0; i<ArrRegins.length; i++)
        {
			fr_opt_txt ='';
            for( var j=0; ArrRegins[i].attributes.length; j++ )
            {
                if( ArrRegins[i].attributes[j].nodeName == 'id' )
                {
					fr_opt_txt = ArrRegins[i].attributes[j].nodeValue;
                    break;
                }
            }
			fr_sel_txt += '<option value="'+ fr_opt_txt +'">'+ArrRegins[i].childNodes[0].nodeValue +'</option>\r\n';
        }
    } else
		fr_sel_txt += '<option value="0"> На данный момент нет данных. Для добавления региона обратитесь в <span class="blue_12px">техподдержку</span>.</option>'
	fr_sel_txt += '</select>';
	
	$("#region_area").html(fr_sel_txt);
}

function OnRegionChange(region_id, language)
{
	var obj_region = document.getElementById("user_documents_region");
	selected_region_text = obj_region.options[obj_region.selectedIndex].text;;
	selected_locality_text = '';
	selected_region_id = obj_region.value;
	selected_locality_id = 0;
	$("#locality_area").html('<img src="/res/images/chocolate/ajax-loader.gif">');
	if (isIE)
	{
		$.post("/res/ajax_server/get_locality.php", {'region' : region_id, 'language' : language, 'ie' : 1}, fillLocality);
	}
	else
	{
		$.post("/res/ajax_server/get_locality.php", {'region' : region_id, 'language' : language}, fillLocality);			
	}
}

function fillLocality(data)
{
    var ArrLocality = $('locality', data);

	var fr_sel_txt = '<select class="lightgray_12px" id="user_documents_locality" name="locality" onchange="javascript:OnLocalityChange()">\r\n<option value="0"> - Все населенные пункты - </option>\r\n';
	var fr_opt_txt = '';	
	
    if(ArrLocality.length > 0)
    {
        for(var i=0; i<ArrLocality.length; i++)
        {
            for( var j=0; ArrLocality[i].attributes.length; j++ )
            {
                if( ArrLocality[i].attributes[j].nodeName == 'id' )
                {
					fr_opt_txt = ArrLocality[i].attributes[j].nodeValue;
                    break;
                }
            }
			fr_sel_txt += '<option value="'+ fr_opt_txt +'">'+ArrLocality[i].childNodes[0].nodeValue +'</option>\r\n';
        }
    }else
		fr_sel_txt += '<option value="0"> На данный момент нет данных. Для добавления населенного пункта обратитесь в техподдержку.</option>'
	fr_sel_txt += '</select>';
	
	$("#locality_area").html(fr_sel_txt);
}
function OnLocalityChange()
{
	var obj_locality = document.getElementById("user_documents_locality");
	selected_locality_text = obj_locality.options[obj_locality.selectedIndex].text;
	selected_locality_id = obj_locality.value;	
}