var question_now	= 0;
var max_show_num	= 20;	//сколько максимально вопросов можно показать за один раз
var max_show_str	= "...";//текст, который выводится если слишком много вариантов выбора

function set_question_word ( num )
{
	//устанавливаем переменную, обозначающую вопросительное слово, которое сейчас выбранно
	if ( question_now )
	{
		document.search_form.keyword.value = document.search_form.keyword.value.substr ( a_question_words [ question_now ].length + 1, 1000 );
		document.search_form.keyword.value = a_question_words [ num ] + ' ' + document.search_form.keyword.value;
	}
	else
	{
		document.search_form.keyword.value = a_question_words [ num ] + ' ' + document.search_form.keyword.value;
	}
			
	question_now = num;
	show_variants ();
}

function show_variants ()
{
//	return false;//убрать потом
	//отображаем список полных вопросов в выпадающем списке
	var variants_div = document.getElementById ( "variants_div" );
	variants_div.style.display = 'none';
	var text = "";
	var text_length = 0;
	var is_question_word = 0;

	text = document.search_form.keyword.value;
	variants_div.innerHTML = "";
	text_length = text.length;

	is_question_word = check_question_word ( text );
	
	if ( is_question_word )
	{
		//это если сначала идет вопрос
		show_variants_by_question ( text, text_length, variants_div );
	}
	else
	{
		//это если вопрос не выбирали
		show_variants_by_letters ( text, text_length, variants_div );
	}
}

function show_variants_by_question ( text, text_length, variants_div )
{
	var first_letter = "";
	var question_in_array = "";
	var question_length = 0;
	var find_questions_num = 0;	//сколько найдено вопросов, соответствующих критерию

	if ( text_length <= a_question_words [ question_now ].length + 1 ) return true; //если после выбора вопроса ничего еще не ввели
	first_letter = text.substr ( a_question_words [ question_now ].length + 1, 1 );

	//определяем индекс первой буквы	
	for ( var i = 1; i <= a_letters.length - 1; i++ )
	{
		if ( a_letters [ i ] == first_letter )
		{
			first_letter = i;
			break;
		}
	}

	if ( !a_questions [ question_now ] [ first_letter ] ) return true;//т.е. если ввели что-то тира: "что asdf"
	find_questions_num = get_variants_by_question_num ( text, text_length, first_letter );
	
	if ( find_questions_num <= max_show_num )
	{
		for ( var i = 1; i <= a_questions [ question_now ] [ first_letter ].length - 1; i++ )
		{
			question_in_array = a_questions [ question_now ] [ first_letter ] [ i ];
			question_length = a_question_words [ question_now ].length;
			if ( ( a_question_words [ question_now ] + " " + question_in_array.substr ( 0, text_length - question_length - 1 ) ).toUpperCase () == text.toUpperCase () )
			{
				variants_div.innerHTML += '<div class="words" onmouseover="this.style.backgroundColor=\'C9E3F3\'; this.style.cursor=\'hand\'; " onmouseout="this.style.backgroundColor=\'FFF\'; this.style.cursor=\'arrow\';" onclick="set_text_field_value ( \'' + a_question_words [ question_now ] + ' ' + question_in_array + '\' )">' + a_question_words [ question_now ] + ' ' + question_in_array + "</div>";
			}
		}
	}
	else
	{
		variants_div.innerHTML += max_show_str;
	}
	if ( variants_div.innerHTML )	variants_div.style.display = 'block';
	else 							variants_div.style.display = 'none';
}

function get_variants_by_question_num ( text, text_length, first_letter )
{
	var question_in_array = "";
	var question_length = 0;
	var find_questions_num = 0;	//сколько найдено вопросов, соответствующих критерию

	for ( var i = 1; i <= a_questions [ question_now ] [ first_letter ].length - 1; i++ )
	{
		question_in_array = a_questions [ question_now ] [ first_letter ] [ i ];
		question_length = a_question_words [ question_now ].length;
		if ( ( a_question_words [ question_now ] + " " + question_in_array.substr ( 0, text_length - question_length - 1 ) ).toUpperCase () == text.toUpperCase () ) find_questions_num++;
	}
	
	return find_questions_num;
}

function show_variants_by_letters ( text, text_length, variants_div )
{
	var first_letter = "";
	var question_in_array = "";
	var find_questions_num = 0;	//сколько найдено вопросов, соответствующих критерию

	if ( text_length <= 3 ) return true; //если слишком мало букв
	first_letter = text.substr ( 0, 1 );

	//определяем индекс первой буквы	
	for ( var i = 1; i <= a_letters.length - 1; i++ )
	{
		if ( a_letters [ i ] == first_letter )
		{
			first_letter = i;
			break;
		}
	}

	find_questions_num = get_variants_by_letters_num ( text, text_length, first_letter );

	if ( find_questions_num <= max_show_num )
	{
		for ( var i = 1; i <= a_questions.length - 1; i++ )
		{
			if ( a_questions [ i ].length < 1 ) continue; //если какой-то из элементов массива не задан

			 for ( var j = 1; j <= a_questions [ i ] [ first_letter ].length - 1; j++ )
			 {
			 	question_in_array = a_questions [ i ] [ first_letter ] [ j ];
				if ( question_in_array.substr ( 0, text_length ).toUpperCase () == text.toUpperCase () )
				{
					variants_div.innerHTML += '<div class="words" onmouseover="this.style.backgroundColor=\'C9E3F3\'; this.style.cursor=\'hand\'; " onmouseout="this.style.backgroundColor=\'FFF\'; this.style.cursor=\'arrow\';" onclick="set_text_field_value ( \'' + question_in_array + '\' )">' + question_in_array + "</div>";
				}
			 }
		}
	}
	else
	{
		variants_div.innerHTML += max_show_str;
	}
	if ( variants_div.innerHTML )	variants_div.style.display = 'block';
	else 							variants_div.style.display = 'none';
}

function get_variants_by_letters_num ( text, text_length, first_letter )
{
	var find_questions_num = 0;
	for ( var i = 1; i <= a_questions.length - 1; i++ )
	{
		if ( a_questions [ i ].length < 1 ) continue; //если какой-то из элементов массива не задан

		 for ( var j = 1; j <= a_questions [ i ] [ first_letter ].length - 1; j++ )
		 {
		 	question_in_array = a_questions [ i ] [ first_letter ] [ j ];
			if ( question_in_array.substr ( 0, text_length ).toUpperCase () == text.toUpperCase () ) find_questions_num++;
		 }
	}
	return find_questions_num;
}

function set_text_field_value ( text )
{
	var variants_div = document.getElementById ( "variants_div" );

	document.search_form.keyword.value = text;
	variants_div.style.display = "none";
}

//проверяем есть вопросительное слово в тексте
function check_question_word ( text )
{
	var question_word = "";
	for ( var i = 1; i <= a_question_words.length - 1; i++ )
	{
		question_word = a_question_words [ i ];
		if ( text.substr ( 0, question_word.length ).toUpperCase () == question_word.toUpperCase () )
		{
			if ( i == 2 && text.substr ( 3, 1 ) != " " ) continue; //чтобы пропустить "как..."
			question_now = i;
			return true;
		}
	}
	return false;
}


//images
var word_image_question_off = "img/search/question_word.gif";
var word_image_question_on = "img/search/question_word_on.gif";

//action script
var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
function search_keywords_list_DoFSCommand(command, args) {
	var search_keywords_listObj = isInternetExplorer ? document.all.search_keywords_list : document.search_keywords_list;
	set_question_word ( args );
}

if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<script language=\"VBScript\"\>\n');
	document.write('On Error Resume Next\n');
	document.write('Sub search_keywords_list_FSCommand(ByVal command, ByVal args)\n');
	document.write('	Call search_keywords_list_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('</script\>\n');
}

function check_search_form ()
{
	var str = document.search_form.keyword.value.trim ();
	if ( str.length <= 3 )
	{
		alert ( 'Пожалуйста, введите более подробный вопрос для поиска.' );
		document.search_form.keyword.focus ();
		return false;
	}
}
