function MarketInstrumentsHandler(search_string_value, select_instruments_value, select_all_instruments_value) {
	
	var this_copy = this;
	
	
	this.market_obj = document.getElementById('market');
	
	this.market_instruments = document.getElementById('marketInstruments');
	
	this.market_instruments_container = document.getElementById('marketInstrumentsContainer');
	
	this.user_instruments = document.getElementById('userInstruments');
	
	this.instruments_search = document.getElementById('text'); 
	
	this.market_loading_container = document.getElementById('loading');
	
	this.market_indexes_container  = document.getElementById('marketIndexes');
	
	this.market_kase_indexes_container = document.getElementById('marketKaseIndexes');
	
	this.submit_button = document.getElementById('submitButton');
	
	this.select_all_instruments = document.getElementById('selectAllInstruments');
	
	this.standart_indexes_chose = document.getElementById('standartIndexesChose');
	
	this.last_chosen_instruments = document.getElementById('lastChosenInstruments');
	
	this.last_chosen_instruments.style.display = 'none';
	
	this.last_chosen_indexes = document.getElementById('lastIndexesChose');
	
	
	
	var search_string = document.getElementById('searchString');
	
	this.search_string_value = search_string_value;
	
	this.select_instruments_value = select_instruments_value;
	
	this.select_all_instruments_value = select_all_instruments_value;
	
	search_string.value = this.search_string_value;
	
	search_string.style.color = '#B8B8B8';
	
	
	this.standart_indexes = Array();
	
	this.instruments_cache = new Array();
	
	this.indexes = new Array();
	
	this.indexes_cache = new Array();
	
	
	this.user_instruments_limit = 20;
	
	this.user_instruments_special_limit = [{NAME: 'Index_KASE', 'VALUE': 11}];
	
//	document.getElementById('userInstrumentsLimit').innerHTML = this.user_instruments_limit;
	
	
	// Устанавливаем обработчики на компоненты страницы
	
	this.setEvent(this.market_obj, 'change', function(){this_copy.setUserInstrumentsSpecialLimit(); return this_copy.marketHandler()});
	
	this.setEvent(this.submit_button, 'click', function(){return this_copy.submitHandler()});
	
	this.setEvent(this.select_all_instruments, 'click', function(){return this_copy.selectAllInstruments()});
	
	this.setEvent(this.last_chosen_instruments, 'click', function(){return this_copy.getLastChosenInstruments()});
	
	this.setEvent(search_string, 'keyup', function(){return this_copy.searchInstrument(search_string.value)});
	
	this.setEvent(search_string, 'click', function(){return this_copy.searchFieldonClick(search_string)});
	
	this.setEvent(search_string, 'blur', function(){ return this_copy.searchFieldonBlur(search_string) });
	
	this.setEvent(document.getElementById('removeAllInstruments'), 'click', function(){return this_copy.removeAllInstruments()});
	
	this.setEvent(document.getElementById('allIndexesChose'), 'click', function(){return this_copy.choseAllIndexes()});
	
	this.setEvent(this.standart_indexes_chose, 'click', function(){return this_copy.choseStandartIndexes()});
	
	this.setEvent(document.getElementById('removeIndexesChose'), 'click', function(){return this_copy.removeAllIndexes()});
	
	this.setEvent(this.last_chosen_indexes, 'click', function(){return this_copy.getLastChosenIndexes()});
	
	
	return this;

}

MarketInstrumentsHandler.prototype.setUserInstrumentsSpecialLimit = function () {

	for(var i in this.user_instruments_special_limit) {
	
		if (this.user_instruments_special_limit[i].NAME == this.market_obj.value) {
			
			this.user_instruments_limit = this.user_instruments_special_limit[i].VALUE;
			
			break;
		}
		else {
		
			this.user_instruments_limit = 20;
		}
	}
}


MarketInstrumentsHandler.prototype.process = function (market) {
	
	this.market_obj.value = market;
	
	this.marketHandler();
}

MarketInstrumentsHandler.prototype.searchFieldonClick = function (search_obj) {
	
	if (search_obj.value == this.search_string_value) {
		
		search_obj.value = "";
		search_obj.style.color = '';
	}
}

MarketInstrumentsHandler.prototype.searchFieldonBlur = function(search_obj){
	
	if (search_obj.value == "") {
		
		search_obj.value = this.search_string_value;
		search_obj.style.color = '#B8B8B8';
	}
}

MarketInstrumentsHandler.prototype.submitHandler = function () {

	var market_cookie = '{"INSTRUMENTS": { ';
	
	
	for (var instrument in this.instruments_cache) {
		
		if (this.instruments_cache[instrument]['BOX'].checked) market_cookie += '"' + instrument + '"' + ': 1, ' ;
	}
	
	
	market_cookie = market_cookie.substring(0, market_cookie.length - 2);
	
	market_cookie += ' }, ';
	
	market_cookie += '  "INDEXES": { ';
	
	
	for (var index in this.indexes_cache) {
		
		if (this.indexes_cache[index].checked) market_cookie += '"' + index + '"' + ': 1, ' ;
	}
	
	
	market_cookie = market_cookie.substring(0, market_cookie.length - 2);
	
	market_cookie += ' } }';
	
	
	this.createCookie(this.market_obj.value, market_cookie, 360);
	
	document.forms['contentRequstForm'].submit();
}

MarketInstrumentsHandler.prototype.searchInstrument = function (text) {
	
	for (var instrument in this.instruments_cache) {
	
		if (instrument.toLowerCase().indexOf(text.toLowerCase()) == -1 ) {
			
			this.instruments_cache[instrument]['CONTAINER'].style.display = 'none';
		}
		else {
			
			this.instruments_cache[instrument]['CONTAINER'].style.display = 'block';
		}
	}
}

MarketInstrumentsHandler.prototype.emptyCacheInstruments = function () {
	
	for (var instrument in this.instruments_cache) {
		
		this.instruments_cache[instrument]['BOX'].onclick = null;
		this.instruments_cache[instrument]['BOX'] = null;
		this.instruments_cache[instrument]['CONTAINER'] = null;
	}
	
	this.instruments_cache = new Array();
}

MarketInstrumentsHandler.prototype.selectAllInstruments = function () {
	
	var to_select_instruments_count = this.user_instruments_limit - this.user_instruments.options.length;
	
	var selected_count = 0;
	
	for (var instrument in this.instruments_cache) {

		if (!this.instruments_cache[instrument]['BOX'].checked && this.instruments_cache[instrument]['CONTAINER'].style.display == 'block') {

			this.instruments_cache[instrument]['BOX'].checked = true;
			
			this.instrumentHandler(this.instruments_cache[instrument]['BOX']);
			
			selected_count ++;
		}
		
		if (selected_count == to_select_instruments_count) break;
	}
	
	this.instrumentsCheckCount();
}

MarketInstrumentsHandler.prototype.removeAllInstruments = function () {
	
	for (var instrument in this.instruments_cache) {
		
		if (this.instruments_cache[instrument]['BOX'].checked) {
			
			this.instruments_cache[instrument]['BOX'].removeAttribute('checked');
			
			this.instruments_cache[instrument]['BOX'].checked = false;
			
			this.instrumentHandler(this.instruments_cache[instrument]['BOX']);
		}
		
		if (this.user_instruments.options.length == 0) break;
	}
	
	this.instrumentsCheckCount();
}

MarketInstrumentsHandler.prototype.getLastChosenInstruments = function () {
	
	var json = this.getJSONcookie(this.market_obj.value);
	
	this.user_instruments.innerHTML = '';
	
	for (var instrument in this.instruments_cache) {

		if (json['INSTRUMENTS'][instrument]) {
			
			this.instruments_cache[instrument]['BOX'].checked = true;
			
			this.instruments_cache[instrument]['CONTAINER'].style.display = 'block';
		}
		else {
			
			this.instruments_cache[instrument]['BOX'].checked = false;
		}
		
		this.instrumentHandler(this.instruments_cache[instrument]['BOX']);
	}
	
	this.instrumentsCheckCount();
	
}

MarketInstrumentsHandler.prototype.setEvent = function (element, event_type, handler) {
	
	var listener_fn = handler;
	
	if (element.attachEvent){
		
		listener_fn = function() { handler(window.event) }
		
		element.attachEvent ('on' + event_type, handler);
	}

	if (element.addEventListener){
		
		element.addEventListener (event_type, handler, false);
	}
}

MarketInstrumentsHandler.prototype.setInstrumentCache = function (key, container_obj, option) {
	
	if (this.instruments_cache[key] == undefined) this.instruments_cache[key] = new Array(); 
	
	if (option) this.instruments_cache[key]['BOX'] = container_obj;
	else this.instruments_cache[key]['CONTAINER']  = container_obj;
}

MarketInstrumentsHandler.prototype.showIndexes = function () {
	
	if (this.indexes_pattern == 'Index_KASE') {
		
		this.market_all_indexes.style.display = 'none';
		this.market_kase_indexes.style.display = 'block';
	}
	else {
		
		this.market_all_indexes.style.display = 'block';
		this.market_kase_indexes.style.display = 'none';
	}	
}

MarketInstrumentsHandler.prototype.hideIndexes = function () {
		
	this.market_all_indexes.style.display = 'none';
	this.market_kase_indexes.style.display = 'none';
}

MarketInstrumentsHandler.prototype.doLetLastChose = function () {
	
	var the_rules = new Array();
		
	if (document.styleSheets[0].cssRules) the_rules = document.styleSheets[0].cssRules;
		
	else if (document.styleSheets[0].rules) the_rules = document.styleSheets[0].rules;
	
	
	if (this.readCookie(this.market_obj.value) != null) {
		
		this.last_chosen_instruments.style.display = 'inline';
		this.last_chosen_indexes.style.display = 'inline';
	}
	else {
		
		this.last_chosen_instruments.style.display = 'none';
		this.last_chosen_indexes.style.display = 'none';
	}
}

// Метод обработчик выбора рынка. Подгружает инструменты рынка
MarketInstrumentsHandler.prototype.marketHandler = function () {
	
//	this.indexes_pattern = 'All'; 
	this.indexes_pattern = this.market_obj.value;
	this.standart_indexes_chose.style.display = 'inline';
	
	var this_copy = this;
	
	this.market_obj.setAttribute('disabled', true);
	
	this.market_instruments_container.style.display = 'none';
	
	this.market_indexes_container.style.display = 'none';
	
	this.market_indexes_container.innerHTML = '';
	
	this.market_loading_container.style.display = 'inline';
	
	this.market_instruments.innerHTML = '';
	
	this.user_instruments.innerHTML = '';
    
	this.emptyCacheInstruments();
	
	$.getJSON( "/trades/instruments/" + this_copy.market_obj.value + "/ajax", function (content) { return this_copy.onMarketInstrumentsLoad(content); } );
	
	this.doLetLastChose();
}

MarketInstrumentsHandler.prototype.choseAllIndexes = function () {
	
	for (var index in this.indexes_cache) {
		
		this.indexes_cache[index].checked = true;
	}
}

MarketInstrumentsHandler.prototype.choseStandartIndexes = function () {
	
	for (var index in this.indexes_cache) {
		
		this.indexes_cache[index].checked = false;
		
		for (var j in this.standart_indexes) {
			
			if (this.standart_indexes[j] == index) this.indexes_cache[index].checked = true;
		}
	}
}

MarketInstrumentsHandler.prototype.removeAllIndexes = function () {
	
	for (var index in this.indexes_cache) {
		
		this.indexes_cache[index].checked = false;
	}
}

MarketInstrumentsHandler.prototype.getLastChosenIndexes = function () {
	
	var json = this.getJSONcookie(this.market_obj.value);
	
	for (var index in this.indexes_cache) {
		
		if (json['INDEXES'][index]) {
			
			this.indexes_cache[index].checked = true;	
		}
		else {
			
			this.indexes_cache[index].checked = false;
		}
	}
}

MarketInstrumentsHandler.prototype.buildIndexes = function () {

	// Содзаем новый массив стандартных полей
	
	this.standart_indexes = new Array();
	
	for (var index in this.indexes[this.indexes_pattern]) {
		
		// Если Индекс является стандартным, добавляем его в соответствующий массив
		
		if (this.indexes[this.indexes_pattern][index][1] == 'true') {
			
			this.standart_indexes.push(index);
		}
		
		// Создаем контейнер для индекса
		
		var index_container = document.createElement("div");
		
		index_container.style.marginBottom = '5px';
			
		
		// Создаем checkbox индекса
		
		var index_checkbox = document.createElement("input");
				
		index_checkbox.setAttribute('type', 'checkbox');
				
		index_checkbox.setAttribute('name', "ind[]");
				
		index_checkbox.setAttribute('value', index);
				
		
		// Запоминаем индекс в кэше
		
		this.setIndexCache(index, index_checkbox);
		
		
		// Даем индексу имя
		
		var index_name = document.createTextNode(this.indexes[this.indexes_pattern][index][0]);
		
		
		// Создаем контейнер для названия индекса
		
		var index_label = document.createElement('label');
				
		index_label.setAttribute('for', 'int' + index);
					
		
		// Выводим индекс на страницу
		
		this.market_indexes_container.appendChild(index_container);
		
		index_container.appendChild(index_checkbox);
				
		index_container.appendChild(index_label);		
					
		index_label.appendChild(index_name);
		
		
		// Удаляем созданные объекты из памяти
		
		index_container = null;
		
		index_label = null;
		
		index_name = null;
	}
}

MarketInstrumentsHandler.prototype.setIndexCache = function (index_value, index_obj) {
	
	this.indexes_cache[index_value] = index_obj;
}

MarketInstrumentsHandler.prototype.onMarketInstrumentsLoad = function(content){
			
	var this_copy = this;
	
	var market_instruments_count = 0;
	
	for (var i = 0; i < content.length; i++) {
		
		// Создаем контейнер для инструмента
			
		var instrument_container = document.createElement("div");
				
		instrument_container.style.display = 'block';
				
		instrument_container.setAttribute('margin-bottom', '5px');
				
		instrument_container.setAttribute('id', content[i].VALUE);
				
		instrument_container.setAttribute('value', content[i].VALUE);
				

		// Создаем checkbox инструмента
			
		var instrument_checkbox = document.createElement("input");
				
		instrument_checkbox.setAttribute('type', 'checkbox');
				
		instrument_checkbox.setAttribute('name', "int[]");
				
		instrument_checkbox.setAttribute('value', content[i].VALUE);
				
		instrument_checkbox.setAttribute('id', "int" + content[i].VALUE);
		

		// Устанавливаем обработчик на выбор инструмента

		instrument_checkbox.onclick = function() {  
			
			// Провепяем контрольную сумму
			
			this_copy.instrumentHandler(this);
			
			
			// Делаем инструмент выбранным
			
			this_copy.instrumentsCheckCount(this);
		}
				

		// Запоминаем инструмент в кэше
		
		this.setInstrumentCache(content[i].VALUE, instrument_container, false);
		
		this.setInstrumentCache(content[i].VALUE, instrument_checkbox, true);
		

		// Создаем контейнер для название инструмента
			
		var instrument_label = document.createElement('label');
				
		instrument_label.setAttribute('for', 'int' + content[i].VALUE);
				

		// Даем инструменту имя
			
		var instrument_name = document.createTextNode(content[i].VALUE);
		

		this.market_instruments.appendChild(instrument_container);
			
		instrument_container.appendChild(instrument_checkbox);
		
		instrument_container.appendChild(instrument_label);
		
		instrument_label.appendChild(instrument_name);	
		
		
		// Удаляем созданные объекты из памяти
		
		instrument_container = null;
		
		instrument_checkbox = null;
			
		instrument_label = null;	
		
		instrument_name = null;	
		
		market_instruments_count ++;
	}

	this.market_obj.removeAttribute('disabled');
	
	this.market_instruments_container.style.display = 'block';
	
	this.market_indexes_container.style.display = 'block';
			
	this.market_loading_container.style.display = 'none';
	
	
	if (market_instruments_count > this.user_instruments_limit) this.select_all_instruments.innerHTML = this.select_instruments_value + ' ' + this.user_instruments_limit;
	
	else this.select_all_instruments.innerHTML = this.select_all_instruments_value;
	
	
	// Выводим рыночные индексы
	
	this.buildIndexes();
}

// Метод обработчик выбора инструмента
MarketInstrumentsHandler.prototype.instrumentHandler = function (_instrument) {

	var instrument = _instrument.value;
	
	var user_instrument_id = '_' + instrument;

	if (_instrument.checked) {
		
		this.user_instruments.options[this.user_instruments.options.length] = new Option(instrument, instrument);
		
		this.user_instruments.options[this.user_instruments.options.length - 1].setAttribute('id', user_instrument_id);
	}
	
	else {
		
		var del_instrument = document.getElementById(user_instrument_id);
			
		if (del_instrument) this.user_instruments.removeChild(del_instrument);
	}
}

MarketInstrumentsHandler.prototype.emptyUserInstrument = function (instrument, user_instrument_id) {
	
	var del_instrument = document.getElementById(user_instrument_id);
			
	if (del_instrument) this.user_instruments.removeChild(del_instrument);
		
	this.eraseCookie(instrument);
}

MarketInstrumentsHandler.prototype.createCookie = function (name, value, days) {
	
	if (days) {
		
		var date = new Date();
		
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	
	document.cookie = name + "=" + value + expires + "; path=/";
}

MarketInstrumentsHandler.prototype.readCookie = function(name) {
	
	var nameEQ = name + "=";
	
	var ca = document.cookie.split(';');
	
	for(var i = 0; i < ca.length; i ++) {
		
		var c = ca[i];
		
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	
	return null;
}

MarketInstrumentsHandler.prototype.getJSONcookie = function(name) {
	
	var json = this.readCookie(name);
	
	return eval('(' + json + ')');
}

MarketInstrumentsHandler.prototype.eraseCookie = function eraseCookie(name) {
	
	this.createCookie(name, '', -1);
}

MarketInstrumentsHandler.prototype.instrumentsCheckCount = function () {

	if (this.user_instruments.options.length >= this.user_instruments_limit) {
	
		for (var instrument in this.instruments_cache) {

			if (!this.instruments_cache[instrument]['BOX'].checked) 
			this.instruments_cache[instrument]['BOX'].setAttribute('disabled', 'disabled');
		}
		
		this.market_instruments_blocked = true;
	}
	else if (this.market_instruments_blocked && this.user_instruments.options.length <= this.user_instruments_limit) {
		;
		for (var instrument in this.instruments_cache) {

			if (this.instruments_cache[instrument]['BOX'].disabled) 
			this.instruments_cache[instrument]['BOX'].removeAttribute('disabled');
		}
		
		this.market_instruments_blocked = false;
	}
}

MarketInstrumentsHandler.prototype.toggleMarketFields = function (option) {
	
	var attr = null;
	
	
	if (this.market_obj.value == 'Index_KASE') attr = 'Kase';
	
	else attr = 'All';
	
	
	for (var i = 0; i < document.forms[0]['ind[]'].length; i++) {

		if (document.forms[0]['ind[]'][i].getAttribute('type') == 'checkbox' && document.forms[0]['ind[]'][i].getAttribute('attr') == attr) {
			
			document.forms[0]['ind[]'][i].checked = option;
		}
	}
	
	
	return false;
}

MarketInstrumentsHandler.prototype.setIndex = function (market, key, value, standart) {

	if (this.indexes[market] == undefined) this.indexes[market] = new Array();
	
	if (this.indexes[market][key] == undefined) this.indexes[market][key] = new Array();
	
	this.indexes[market][key] = new Array(value, standart);
}

MarketInstrumentsHandler.prototype.distruct = function () {
	
	this.emptyCacheInstruments();
	
	this.emptyCacheIndexes();
	
	this.market_obj = null;
	
	this.select_all_instruments = null;
	
	this.last_chosen_instruments = null;
	
	this.market_loading_container = null;
	
	this.market_instruments_container = null;
	
	
/*	
	document.getElementById('market') = null
	
	document.getElementById('marketInstruments').expandoProperty  = null;
	
//	this.market_instruments = null;
	
	this.market_instruments_container = null;
	
	this.instruments_search = null; 
	
	this.market_loading_container = null;
	
	this.market_indexes_container  = null;
	
	this.market_kase_indexes_container = null;
	
	this.submit_button = null;
	
	this.select_all_instruments = null;
	
	this.standart_indexes_chose = null;
	
	this.last_chosen_instruments = null;
	
	this.last_chosen_indexes = null;
	*/
}

MarketInstrumentsHandler.prototype.emptyCacheIndexes = function () {
	
	for (var index in this.indexes_cache) this.indexes_cache[index] = null;
}


MarketInstrumentsHandler.prototype.checkForSubmit = function () {
	
	var instruments_selected = false;
	
	var indexes_selected = 0;
	
	if (this.user_instruments.innerHTML != '') instruments_selected = true;
	
	for (var index in this.indexes_cache) if (this.indexes_cache[index].checked) { indexes_selected = true; break; }
	
	
	if (instruments_selected && indexes_selected > 0)  this.submit_button.removeAttribute('disabled');
	
	else if (this.submit_button.getAttribute('disabled') == undefined) this.submit_button.setAttribute('disabled', 'true');
}
