function NameSelector()
{
	
	 // Создать окно, если его нет или оно пропало 
	this.popup = function(from){
		var href = '';
		
		this.init(from);
		
		
		href = "/firms/?f_country=136&do_search=1";
		
	 if (!this.frame)
	 {
		
		// Прикидываемся шлангом на случай, если не сработает попап
		this.selectName(0,0);
		
		if (!(this.window && !this.window.closed))
		{	
			if (window.open)
			{
				this.window = window.open(href, "popup_choose_Name", "scrollbars=yes,resizable=yes,status=no,width=750,height=500,left=" + (screen.width-750)/2 + ",top=" + (screen.height-500)/2);
			}
			else
			{
			}
			
			if (!this.window || ("undefined" == this.window))
			{
				d_log_server('popup', 'cannot open window!');
				alert("Не удается открыть всплывающее окно со списком!"
					+ "\nЧтобы решить эту проблему, Вы можете попробовать:"
					+ "\n1. Нажать клавишу Ctrl и, не отпуская её, щелкнуть по ссылке выбора учебного заведения."
					+ "\n2. Отключить блокировку всплывающих окон в вашем браузере."
					+ "\n3. Использовать другой браузер для заполения этой формы."
				);
				return false;
			}
		}
		else
		{
			this.window.focus();
		}
	}
	else
	{
		if (this.frame && this.frame.src)
		{
			findPos();
			this.frame.src = href + '&frame=1';
			this.show(this.frame);
			this.show('txt_block'); 
		}
		
	}
	}
		
	this.findParentContainer = function(link_element)
	{
		return document;
	}
	
	 //from - любое место вызова из tr-ки 
	this.init = function(from)
	{
		if (navigator.userAgent.indexOf('MSIE 5.0') == -1)
		{
			this.frame = document.getElementById("inst_iframe");
		}
		 // Контейнер в котором находится ссылка, чтобы найти прочие элементы
		this.container = this.findParentContainer(from);
		
		// Контейнер ссылки 
		this.link_container = this.findChildElementByAttribute(this.container, 'className', 'title', 'span');
		
		this.parent_id = this.findChildElementByAttribute(this.container, 'name', 'parent_id', 'input');
		
		this.org_name_input_element = this.findChildElementByAttribute(this.container, 'name', 'org_name_choose', 'input');
		this.org_name_short_input_element = this.findChildElementByAttribute(this.container, 'name', 'org_name_short_choose', 'input');

		this.org_name_input_title = this.findChildElementByAttribute(this.container, 'className', 'org_name_title', 'span');
		this.org_name_short_input_title = this.findChildElementByAttribute(this.container, 'className', 'org_name_short_title', 'span');
		
		this.parent_name_element = this.findChildElementByAttribute(this.container, 'className', 'parent_org_name', 'span');
		this.parent_name_short_element = this.findChildElementByAttribute(this.container, 'className', 'parent_org_name_short', 'span');
		
		this.parent_name_header = this.findChildElementByAttribute(this.container, 'className', 'parent_org_name_row', 'span');
		this.parent_name_short_header = this.findChildElementByAttribute(this.container, 'className', 'parent_org_name_short_row', 'span');
		
		this.parent_name_input = this.findChildElementByAttribute(this.container, 'name', 'parent_org_name', 'input');
		this.parent_name_short_input = this.findChildElementByAttribute(this.container, 'name', 'parent_org_name_short', 'input');
		
		 // Ссылка "Выбрать компанию" 
		this.link_element = this.findChildElementByAttribute(this.container, 'className', 'choose_company_link_second', 'a');
		
		this.second_link_parent = this.findChildElementByAttribute(this.container, 'className', 'choose_cmp', 'div');
	}
	
	this.empty = function () {};
	
	 // Выбрать 
	this.selectName = function(full_name, short_name, parent_id)
	{
		this.hand_mode = false;
		this.link_element.innerHTML = 'Изменить';
		if (parent_id && parent_id > 0)
		{
			this.parent_id.value = parent_id;
		}
		if (this.frame)
		{
			window.onscroll = this.empty;
			window.onresize = this.empty;
			top.document.body.style.overflow = "auto";
		}
		if (full_name && full_name == -1)
		{
			this.hide(top.document.getElementById('txt_block'));
			return;
		}
		
		this.show(this.second_link_parent);
		
		if (0 == full_name)
		{ // Ручками или не нашли
			this.hand_mode = true;
			switch_elements_by_id_tag('mdept_switch', 'span', true);
			switch_elements_by_id_tag('jur_switch', 'span', true);
			this.parent_id.value = -1;
		
			this.show(this.parent_name_element);
			this.show(this.parent_name_short_element);

			this.hide(this.org_name_input_title);
			this.hide(this.org_name_short_input_title);

			this.show(this.parent_name_header, 'block');
			this.show(this.parent_name_short_header, 'block');

			this.show(this.org_name_input_element);
			this.show(this.org_name_short_input_element);

			this.hide(this.link_container);
			
			this.org_name_input_element.value = '';
			this.org_name_short_input_element.value = '';
			this.parent_name_input.value = '';
			this.parent_name_short_input.value = '';
			
			if (this.frame)
			{
				this.hide(top.document.getElementById('txt_block'));
				this.parent_name_input.focus();
				return;
			}

			window.focus();
			this.parent_name_input.focus();
		}
		else
		{
			switch_elements_by_id_tag('jur_switch', 'span', false);
			this.hide(this.link_container);
			this.parent_name_input.value = full_name;
			this.parent_name_short_input.value = short_name;
			this.org_name_input_title.innerHTML = '<b>' + full_name + '</b><br>';
			this.org_name_short_input_title.innerHTML = '<b>' + short_name + '</b>';
			
			this.show(this.org_name_input_title);
			this.show(this.org_name_short_input_title, 'block');
			this.show(this.org_name_input_element);
			this.show(this.org_name_short_input_element);
			this.org_name_input_element.focus();


			this.hide(this.parent_name_header);
			this.hide(this.parent_name_short_header);
			this.hide(this.parent_name_element);
			this.hide(this.parent_name_short_element);

			if (this.frame)
			{
				this.hide(top.document.getElementById('txt_block'));
			}
		}
	}
	/*
	 // Установить контейнер из которого был вызов 
	this.findParentContainer = function(link_container)
	{
		var parent = link_container.parentNode;
		while (parent.tagName.toLowerCase().indexOf('table') != 0)
		{
			parent = parent.parentNode;
		}
		
		return parent;
	}*/
	
	 // Это кандидат в общие функции 
	this.findChildElementByAttribute = function(from, attribute, value, tag)
	{
		var found = false;
		
		var node_list = Array();
		
		if (!tag)
		{
			tag = '*';
		}
		 // Для IE 5 вообще-то звездочку нельзя использовать 
		node_list = from.getElementsByTagName(tag)
		
		var length = node_list.length;
		
		for (var i = 0; i < length; i++)
		{
			if (node_list[i][attribute] 
					&& node_list[i][attribute] == value
			)
			{
				return node_list[i];
			}
		}
		
		return false;
	}
	
	this.show = function(element, display_type)
	{
		if (!display_type)
		{
			var display_type = '';
		}
		if (typeof element == 'string')
		element = document.getElementById(element);
		element.style.display = display_type;
	}
	
	this.hide = function(element)
	{
		if (typeof element == 'string')
		element = document.getElementById(element);
		element.style.display = 'none';
	}

	this.selectTown = function(from)
	{
		this.init(from);
		this.popup(from);
	}
	
	this.remove = function(from)
	{
		this.init(from);
		
		this.container.parentNode.parentNode.removeChild(this.container.parentNode);
	}
}

NameSelector.getInstance = function()
{
	if (!NameSelector.instance)
	{
		NameSelector.instance = new NameSelector();
	}
	
	return NameSelector.instance;
}