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()
{
	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='';
	
    var phone1 = document.getElementById('countryCode');

    if (phone1 && (typeof(phoneCode) != 'undefined') &&
        (typeof(phoneCode[country]) != 'undefined') && (phoneCode[country] != '+')) {
        phone1.value = phoneCode[country];
    } else if (!this.firstLoad) {
        phone1.value = '';
    }

    this.firstLoad = false;
	
	if (country == 0 || (country !=155 && country !=196) || 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);
		 }
		 document.getElementById(this.region_id).style.display = 'none';
		 return;
	}
  if (navigator.userAgent.indexOf("MSIE") != -1 || document.getElementById(this.region_id).tagName != "TR")
  {
	document.getElementById(this.region_id).style.display = 'block';
  }
  else
  {	
	  document.getElementById(this.region_id).style.display = 'table-row';
  }
	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.firstLoad = true;
	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)
{
	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; 
	    }, 
	    failure : function(o) 
	    { 
	    		filials.innerHTML = "<strong>Connection failed!</strong>"; 
	    } 
	} 
	
	var conn = YAHOO.util.Connect.asyncRequest("GET", url+"&t=" + new Date().getTime(), callback); 
}