function updateSelect(id, response) {
    var select = document.getElementById(id);
    select.innerHTML = '<>';
    var newOption;
//    newOption.text = ' ';
//    newOption.value = '';
//    select.options.add(newOption);

    for (var i in response.options) {
        newOption = document.createElement("OPTION");
        newOption.text = response.options[i].text;
        newOption.value = response.options[i].value;
        select.options.add(newOption);
    }

    newOption = document.createElement("OPTION");
    newOption.text = ' --- Другой ---';
    newOption.value = '!!!other!!!';
    select.options.add(newOption);
}
function citySetInput () {
    $('#cityInput').val('');
    $('#cityInput').css('display','inline');
    $('#citySelect').css('display','none');
}

function citySetSelect() {
    $('#citySelect').css('display','inline');
    $('#cityInput').css('display','none');
    $('#cityInput').val($('#citySelect').val());
}

function dg_AddOption(element, value, text)
{
    newOption = document.createElement("OPTION");
    newOption.text = text;
    newOption.value = value;
    this.form_el.elements[element].options.add(newOption, this.form_el.elements[element].length);
}

function dg_ForceUpdate()
{
    $('#city_tr').css('display','none');
    //document.getElementById('city_tr').style.display = 'none';
  
    LoadFilials(1,2,3,4);
    this.form_el = eval('document.'+this.form_n);
    var country = this.form_el.elements[this.country_el].options[this.form_el.elements[this.country_el].selectedIndex].value;
    document.getElementById(this.filials_el).innerHTML='';
	
    if (country == 0 || (country !=155 && country !=196 && country != 98) || this.lang != 'ru')
    {
        this.form_el.elements[this.region_el].disabled = true;
        this.form_el.elements[this.region_el].selectedIndex=0;
        if (this.cur_el != null)
        {
            this.cur_el.options[0].text = '';
            this.cur_el = null;
            this.region_sel_el = null;
            self.clearTimeout(this.to);
        }
        citySetInput();
        $('#city_tr').attr('style','display:table-row');
        document.getElementById(this.region_id).style.display = 'none';
        return;
    }
	
    document.getElementById(this.region_id).style.display = 'block';

    for (i =  this.form_el.elements[this.region_el].options.length - 1; i >= 1 ; i--)
    {
        this.form_el.elements[this.region_el].remove(i);
    }

    this.form_el.elements[this.region_el].disabled = true;
    frames[this.iframe_el].location.href = "?action=get_region&countryid=" + country+'&t='+new Date().getTime();
    this.CheckDataLoaded();
}

function dg_CheckDataLoaded()
{
    self.clearTimeout(this.to);

    iframe=document.getElementById(this.iframe_el);
	
    if (this.cur_el == null)
    {
        this.cur_el =  this.form_el.elements[this.region_el];
        this.tmp = this.cur_el.options[0].text;
		
        this.cur_el.options[0].text = "Please wait";
        this.to = window.setTimeout(this.obj_name + ".CheckDataLoaded()", 500);
        return;
    }
	
    this.cur_el.options[0].text += '.';

    var ifr = (iframe.contentDocument) ? iframe.contentDocument : ((iframe.contentWindow) ? iframe.contentWindow.document : null);

    if(ifr.getElementById(this.iframe_data_end))
    {
        var str=ifr.getElementById(this.iframe_data).innerHTML.split(",");
        var  sel 	= 0;
        var  y 		= 0;
        for (i = 0; i < str.length; i+=2)
        {
            if(this.region_sel_el!=null && parseInt(this.region_sel_el) == parseInt(str[i]))sel=y+1;
            this.AddOption(this.region_el, str[i], str[i+1]);
            y++;
        }
		
        this.form_el.elements[this.region_el].selectedIndex = sel;
        this.form_el.elements[this.region_el].disabled = false;
		
        this.cur_el.options[0].text = this.tmp;
        this.cur_el = null;
    }
    else
    {
        this.to = window.setTimeout(this.obj_name + ".CheckDataLoaded()", 500);
    }
}

function CDynamicGeo(obj_name, form_n, iframe_el, lang, country_el, region_el, region_sel_el)
{
    this.to = -1;
    this.cur_el = null;
    this.obj_name = obj_name;
    this.iframe_el = iframe_el;
    this.iframe_data_end = 'end';
    this.iframe_data = 'data';
    this.country_el = country_el;
    this.region_el = region_el;
    this.region_sel_el = region_sel_el;
    this.region_id = 'region';
    this.form_el = null;
    this.form_n = form_n;
    this.filials_el = 'filials';
    this.lang = lang;
    this.AddOption = dg_AddOption;
    this.ForceUpdate = dg_ForceUpdate;
    this.CheckDataLoaded = dg_CheckDataLoaded;
}

function LoadFilials(regionid, brokerid, selectedId)
{
    var adses = document.getElementById("adsid");

    if(typeof(LoadFilials.additionalFilials) == 'undefined')
    {
        LoadFilials.additionalFilials = new Array();
    }

    if(arguments.length > 3)
    {
        for(var j=0; j < LoadFilials.additionalFilials.length; j++)
        {
            //adses.options.remove(LoadFilials.additionalFilials[j]);
            adses.options[LoadFilials.additionalFilials[0]] = null;
        }
        LoadFilials.additionalFilials = new Array();
         
        return;
    }



    if(parseInt(brokerid) == 2)
    {
        return;
    }
    var url	= '?action=get_filial&regionid='+regionid;
    var filials  	= document.getElementById("filials");


    filials.innerHTML =  '<div align="center" style="padding-top:10px;"><strong>Loading...</strong></div>';

    var callback =
    {
        success : function(o)
        {
            obj = eval ('(' + o.responseText + ')');
            filials.innerHTML = obj.content;
			
            /*            if(typeof(obj.city) != 'undefined' && obj.city == 1)
            {
                $('#city_tr').css('display','table-row');
            //document.getElementById('city_tr').style.display = 'block';
            }
            else
            {
                $('#city_tr').css('display','none');
            //document.getElementById('city_tr').style.display = 'none';
            }*/

            for(var j=0; j < LoadFilials.additionalFilials.length; j++)
            {
                adses.options[LoadFilials.additionalFilials[0]] = null;
            }
            LoadFilials.additionalFilials = new Array();

            $('#city_tr').attr('style','display:table-row');
            if (obj.cities==null) citySetInput();
            else {
                updateSelect('citySelect',obj.cities);
                citySetSelect();
            }

            if(typeof(obj.additionalBranches) == 'object')
            {
                var newOption;
                       
                var selectedIndex = 0;
                if (adses){
                    for(var i=0; i < obj.additionalBranches.length; i++)
                    {
                        newOption = document.createElement("OPTION");
                        newOption.text = obj.additionalBranches[i].name;
                        newOption.value = obj.additionalBranches[i].id;
                        if(obj.additionalBranches[i].id == selectedId)
                        {
                            selectedIndex = adses.length;
                        }
                        adses.options.add(newOption, adses.length);
                        LoadFilials.additionalFilials.push(adses.length - 1);
                    }
                    if(selectedIndex && selectedId)
                    {
                        adses.options.selectedIndex = selectedIndex;
                    }
                }
            }
        },
        failure : function(o)
        {
            filials.innerHTML = "<strong>Connection failed!</strong>";
        } 
    }
	
    var conn = YAHOO.util.Connect.asyncRequest("GET", url+"&t=" + new Date().getTime(), callback);
}