/*
================================================================================
Официальный WEB-сайт Совета национальной безопасноси и обороны Украины
WWW.RAINBOW.GOV.UA
--------------------------------------------------------------------------------
Разработчик - Андрей Головко | www.golovko.net (С)
--------------------------------------------------------------------------------

Глобальные функции
(Языковые ссылки, Главное меню...)

================================================================================
*/
/*
===========================================================
 Установка языковых ссылок
===========================================================
*/
function LangLinks(LANGS)
{
	var langs = !LANGS ? "" : LANGS;
	url = document.location.pathname;
	url = url.replace("/ru", "");
	url = url.replace("/en", "");
	var ua = document.getElementById("lang_link_ua");
	var ru = document.getElementById("lang_link_ru");
	var en = document.getElementById("lang_link_en");
	ua.href = langs.indexOf("ua") != -1 ? url : "/";
	//ru.href = langs.indexOf("ru") != -1 ? "/ru" + url : "/ru/";
	//en.href = langs.indexOf("en") != -1 ? "/en" + url : "/en/";	
}

/*
===========================================================
 Размер Интро-текста
===========================================================
*/
function IntroSize()
{
	var w = WindowAvailWidth();
	if(w >= 1080)
	{ 
		return;
	}
	var intro = document.getElementById("intro");
	if(intro) intro.style.fontSize = '10px';
}

/*
===========================================================
 Главное меню...
===========================================================
*/
function MainMenu(alias)
{
	if(alias == '(none)') return;
	
	var cat = RegExp(/^news([0-9]+)$/i).exec(alias);
	if(cat != null && cat[1] > 0)
	{
		cat = cat[1];
		var filter = 'href="/news/?cat='+cat+'"';
		var link = false;
		var dl = document.getElementById("mainmenu");
		for(var i=0; i < dl.childNodes.length; i++)
		{
			if(dl.childNodes[i].nodeName == "DT")
			{
				alias = dl.childNodes[i].id
				if(dl.childNodes[i].innerHTML.indexOf(filter) != -1)
				{
					link = true;
					break;
				}
			}
			if(dl.childNodes[i].nodeName == "DD" && dl.childNodes[i].innerHTML.indexOf(filter) != -1)
			{
				link = true;
				break;
			}
		}
		if(link != true)
		{
			return;
		}
	}

	var dt = document.getElementById(alias);
	var menu = document.getElementById("menu_" + alias);
	if(!dt || !menu) return;
	menu.style.display = menu.style.display == "none" ? "block" : "none";
	dt.className = menu.style.display == "none" ? "" : "active";
	return (false);
}

/*
===========================================================
 Провека поискового запроса
===========================================================
*/
function ValidateSearchForm(form, lang, holder_text)
{
	if(form.search_string.disabled == true)
	{
		return(true);
	}
	
	if(form.search_string.value == holder_text) 
	{
		if(lang == 'en') alert("Enter searching query!");
		if(lang == 'ru') alert("Введите поисковый запрос!");
		if(lang == 'ua') alert("Введіть пошуковий запит!");
		return(false);
	}
	if(!ValidateSearchQuery(form.search_string.value, lang)) 
	{
		return(false);
	}	
	return(true);
}
function ValidateSearchQuery(value, lang)
{
	if(!RegExp(/\S{4,}/).test(value)) 
	{
		if(lang == 'en') alert("Invalid searching query!\nA word must be more 3th characters.");
		if(lang == 'ru') alert("Некорректный поисковый запрос!\nСлово должно быть более 3-х символов.");
		if(lang == 'ua') alert("Некоректний пошуковий запит!\nСлово повинно бути більш 3-х символів.");
		return(false);
	}	
	return(true);
}
function ValidateDocsSearch(form, lang)
{
	if(form.words.value != '' && form.words.value != ' ' && !ValidateSearchQuery(form.words.value, lang))
	{
		return(false);
	}
	return(true);
}

function SearchLogic(form, value)
{
	form.search_string.disabled = value == 'without' ? true : false;
}
/*
===========================================================
 Размеры области окна
===========================================================
*/
function WindowAvailWidth()
{
	var width = window.innerWidth;
	if(width == undefined || width === 0 || isNaN(width))
	{
		width = window.document.documentElement.clientWidth;
	}
	if(width == undefined || width === 0 || isNaN(width))
	{
		width = window.document.body.clientWidth;
	}
	return width;
}
function WindowAvailHeight()
{
	var height = window.innerHeight;
	if(height == undefined || height === 0 || isNaN(height))
	{
		height = window.document.documentElement.clientWidth;
	}
	if(height == undefined || height === 0 || isNaN(height))
	{
		height = window.document.body.clientHeight;
	}
	return height;
}

/*
===========================================================
 Функция для плейсхолдера
===========================================================
*/
function InputPlaceHolder(field_id, holder_text)
{
	field = document.getElementById(field_id);
	if(!field) 
	{
		return;
	}
	field.onfocus = function()
	{
		if(field.value.length && field.value == holder_text) 
		{
			field.value = '';
			field.style.color = 'black';
		}
	}
	field.onblur = function()
	{
		if(!field.value.length) 
		{
			field.value = holder_text;
			field.style.color = 'gray';
		}
	}
	if(!field.value.length) 
	{
		field.onblur();
	}
	if(!field.getAttribute('placeholder')) 
	{
		field.setAttribute( 'placeholder', holder_text);
	}
}