/* Links blur */
function linksblur() {
		var i,l=document.getElementsByTagName('a')
		for(i=0;i<l.length;i++)
		{
			l[i].onfocus=function(){this.blur()}
		}
}
window.onload = linksblur

/* Menu & vacancies */
function expand(a)
{
	var i, list, nod = a.parentNode.childNodes
	for(i = 0; i < (nod.length); i++)
	{
		if (nod[i].className && (nod[i].className == 'opened' || nod[i].className == 'closed'))
		{
			list = nod[i]
		}
	}
	a.parentNode.className = (a.parentNode.className != 'opened') ? 'opened':'closed';
	list.className = (list.className != 'opened') ? 'opened':'closed';
}

function expandOnRequest()
{
	linksblur()
	var a, b, c, vacancies
	if (c = getCookie('expand'))
	{
		if (a = document.getElementById(c))
		{
		a.scrollIntoView(true);
		deleteCookie('expand', './');
		}
	}
	else
	{
		if (b = document.location.hash)
		{
			if (/#(\d*)/.exec(String(b)))
			{
				a = document.getElementById(RegExp.$1)
			}
		}
	}
	vacancies = document.getElementById('vacancies').childNodes
	for (var i = 0; i < vacancies.length; i++)
	{
		if (vacancies[i].nodeName == 'LI')
		{
				if (!a || vacancies[i] != a.parentNode) {
					expand(vacancies[i].firstChild)
				}
		}
	}
}

function highlt(a)
{
	deleteCookie('expand', './');
	if (!logged)
	{
		setCookie('expand', a.id,'./');
	}
}

/* Cookies */

function getCookie(name) {
        var prefix = name + "="
        var cookieStartIndex = document.cookie.indexOf(prefix)
        if (cookieStartIndex == -1)
                return null
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length
        return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}

function setCookie(name, value, path, domain, secure) {
        var curCookie = name + "=" + escape(value) +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "")
        if ((name + "=" + escape(value)).length <= 4000)
                document.cookie = curCookie
        else
                if (confirm("Cookie превышает 4KB и будет вырезан !"))
                        document.cookie = curCookie
}

function deleteCookie(name, path, domain) {
        if (getCookie(name)) {
                document.cookie = name + "=" + 
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                "; expires=Thu, 01-Jan-70 00:00:01 GMT"
        }
}

/* Mail */

function showem(x, y, z) {
	c = arguments;
	for (b = 0; b < c.length; b++) {
        if (b == 3) continue;
		c[b] = c[b].split(',');
		for (a = 0; a < c[b].length; a++)
			c[b][a] = String.fromCharCode(c[b][a] - 666);
		c[b] = c[b].join('');
	}
	document.location.href = "mailto:" + c[0] + "@" + c[1] + "." + c[2] + "";
}

/* Forms */

	var form_messages_r = Array ("Необходимо заполнить следующие поля", "Необходимо заполнить следующее поле", "Данные отправлены", "Необходимо заполнить хотя бы одно поле", "Данные сохранены")
	var form_messages = Array ("Please complete the following fields", "Please complete the following field", "Information sent", "Please complete at least one field", "Information saved")

var err_col = '#F8E8E9';
var norm_col = '#ECEAE6';

function process_form(the_form, show_alert, info_saved, russian)
{
	if (russian)
	{
		form_messages = form_messages_r
	}
	var real_fields = 0, err = 0, empty_fields = 0, fields = 0, errmess = ""
	if (document.all || document.getElementById)
	{
		for (i = 0; i < the_form.length; ++i)
		{
			var elem = the_form.elements[i]
			var _realname = (elem.getAttribute('alt'))
			var _oneof = (elem.getAttribute('oneof'))
			var _depends = null
			if(elem.getAttribute('depends') && elem.getAttribute('depends') != null)
			{
				_depends = elem.getAttribute('depends').split(',')
				custom_field_array(_depends, elem)
			}
			if (_realname)
			{	
				real_fields++
				if (((elem.type && (elem.type=="text" || elem.type=="input" || elem.type=="textarea" || elem.type=="password" || (elem.type=="hidden" && elem.getAttribute('depends')))) || elem.options)  && elem.value == '' )
				{
					errmess += "\n\"" + _realname + "\""
					
					if (!_depends)
					{
						if(!err){elem.focus()}
						elem.style.backgroundColor = err_col
					}
					else
					{
						if(!err){document.getElementById(_depends[0]).focus()}
						for (m = 0; m < _depends.length; m++)
						{
							document.getElementById(_depends[m]).style.backgroundColor = err_col
						}
					}
					err++
				}
				else
				{
					if (!_depends)
					{
						elem.style.backgroundColor = norm_col
					}
					else
					{
						for (m = 0; m < _depends.length; m++)
						{
							document.getElementById(_depends[m]).style.backgroundColor = norm_col
						}
					}
				}
			}
			if (_oneof)
			{
				fields++
				if (elem.type && elem.value == '')
				{
					empty_fields++
				}
			}
		}
		
		if(err == 1)
		{
			alert(form_messages[1] + "\n" + errmess)
			return false
		}
		else
		{
			if(err > 1)
			{
				alert(form_messages[0] + "\n" + errmess)
				return false
			}
			if (real_fields > 0 || empty_fields != fields || (empty_fields == 0 && fields == 0))
			{
				if (show_alert)
				{
					if (info_saved)
					{
						alert(form_messages[4])
					}
					else
					{
						alert(form_messages[2])
					}
				}
				return true
			}
			else
			{
			alert(form_messages[3])
			return false
			}
		}
	}
}

function reset_form(the_form)
{
	if (document.all || document.getElementById)
	{
		for (i = 0; i < the_form.length; ++i)
		{
			var elem = the_form.elements[i]
			if (elem.getAttribute('alt'))
			{	
				if (elem.type && (elem.type=="text" || elem.type=="input" || elem.type=="textarea" || elem.type=="password") && elem.value=='')
				{
					elem.style.backgroundColor = norm_col
				}
			}
		}
	}
	return true;
}

function custom_field_array(arr, el)
{
	var result = ""
	for (j = 0; j < arr.length; j++)
	{
		obj = document.getElementById(arr[j])
		if (obj.options)
		{
			for(k = 0; k < (obj.options.length); k++)
			{
				if (obj.options[k].selected)
				{
					result += obj.options[k].value + ","
				}
			}
		}
		else
		{
			if (obj.type == "text" && obj.value != '')
			{
				result += obj.value + ","
			}
		}
	}
	if (result.lastIndexOf(",") == result.length - 1)
	{
		result = result.substring(0, result.lastIndexOf(","))
	}
	if (result)
	{
		el.value = result
	}
	else
	{
		el.value = ''
	}
}