// JavaScript Document
var Tim;
var menu = new Array(1,2,3);
function preloadImg(src) {
	if (document.images) {
		var image = new Image();
		image.src = src;
		images.push(image);
	}
}

function showClientName(elem, e){
	 var div = document.getElementById('file'+elem.id);
	 //var top = document.body.scrollTop;
	 //if(navigator.appName.indexOf("Microsoft") > -1) e = event;
	 //div.style.left = (e.clientX ? e.clientX : e.pageX) + "px";
	 //div.style.top = top + (e.clientY ? e.clientY : e.pageY) + (10) + "px";
	 //div.style.left = e.pageX + "px";
	 //alert(elem.pageY);
	 //div.style.top = top + e.pageY + (10) + "px";
	 div.style.display = 'block';
	
	 elem.onmouseout = function() {
	 	div.style.display = 'none';
	}
}

function cheakFileForm(){
	var required = {
		"name"		:	"Вы не ввели название файла",
//		"opis"		:	"Вы не ввели описание файла",
		'file'		:	'Вы не выбрали файл'
	};

	for (i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function checkAutentifiationForm() {
	var required = {
		"login"		:	"Вы не ввели логин",
		"pass"		:	"Вы не ввели пароль"
	};

	for (i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function showSubMenu(id, exist_sub){
	var elem1 = document.getElementById(id);
	var elem2 = document.getElementById('block'+id);
	if(exist_sub == 1){
		menu.each(function(s) {
		  if(s == id) $continue;
		  else{
		  	var elem = document.getElementById(s);
		  	if(elem.className == 'block'+s+'_hover') hideSubMenu(s);
		  } 
		});
		elem1.className = 'block'+id+'_hover';
		elem2.style.display = 'block';
		
	}
	else{
		elem1.className = 'block'+id+'_active';
		
	}	
	//elem1.onmouseover = function() {window.clearTimeout(Tim)};
	elem1.onmouseout = function () {
		Tim = window.setTimeout(function() {
				hideSubMenu(id);
			}, 50);
		};
	window.clearTimeout(Tim);
}

function hideSubMenu(id){
	var elem1 = document.getElementById(id);
	var elem2 = document.getElementById('block'+id);
	elem2.style.display = 'none';
	elem1.className = 'block'+id;
}

function replaceClass(id){
	var elem1 = document.getElementById(id);
	elem1.className = 'block'+id+'_active';
}

function showAnsver(id, elem){
	var div = document.getElementById(id);
	if(elem.id == 0){
		div.style.display = 'block';
		elem.id = 1;
	}
	else{
		div.style.display = 'none';
		elem.id = 0;
	}
}

function showform(elem){
	var div = document.getElementById('form_file');
	if(div.style.display == 'none') {
		div.style.display = 'block';
		elem.innerHTML = '<h2 style="padding-top: 5px;">Скрыть форму</h2>';
		return true;
	}
	if(div.style.display == 'block') {
		div.style.display = 'none';
		elem.innerHTML = '<h2 style="padding-top: 5px;">Закачать файл</h2>';
		return true;
	}
}

function checkFaqForm() {
	var required = {
		"fio"		:	"Вы не ввели Ваше имя",
		"qw"		:	"Вы не ввели вопрос",
		"code"		:	"Вы не ввели контрольный номер либо ввели его неверно"
	};
	for (var i in required) {
		var el = document.getElementById(i);
		if(el.id == 'code' && el.value && !isFinite(el.value))
		{
			alert(required['code']);
			el.focus();
			return false;
		};
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function checkAdminForm() {
	var required = {
		"topic"		:	"Вы не ввели тему сообщения",
		"text"		:	"Вы не ввели текст сообщения"
	};
	for (var i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function checkTendersForm() {
	var required = {
		"org"		:	"Вы не ввели организацию",
		"tel"		:	"Вы не ввели телефон",
		"face"		:	"Вы не ввели контактное лицо",
		"post"		:	"Вы не ввели должность",
		"prim"		:	"Вы не ввели примечание"
	};
	for (var i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function checkSubscribeForm(){
	var email = document.getElementById('email');
	if(!checkEmail(email.value)){
			alert('Не введен или введен некорректный e-mail');
			email.focus();
			return false;
		}
	else return true;
}

function checkFormVakancy(){
	var required = {
		"fio"		:	"Вы не ввели Ваше ФИО",
		"tel"		:	"Вы не ввели телефон",
		"email"		:	"Не введен или введен некорректный e-mail",
		"prim"		:	"Вы не ввели примечание",
		"userfile"	:	"Вы не оставили резюме"
	};
	

	for (i in required) {
		var el = document.getElementById(i);
		if(el.id == 'email' && el.value && !checkEmail(el.value)){
			alert(required['email']);
			el.focus();
			return false;
		};
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function checkEmail(email) {
	if(!email) return email;
	if(!email.match(/^[\w\.\-]+@[\w\.\-]+\.[a-zA-Z]{2,}$/i, "")) return false;
	return true;
}

function checkFeedbackForm(){
	var required = {
		"name"		:	"Вы не ввели Ваше ФИО",
		"email"		:	"Не введен или введен некорректный e-mail",
		"msg"		:	"Вы не ввели сообщение"
	};

	for (i in required) {
		var el = document.getElementById(i);
		if(el.id == 'email' && el.value && !checkEmail(el.value)){
			alert(required['email']);
			el.focus();
			return false;
		};
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}


function checkStrSearch() {
	var str = document.getElementById('srch');
	if((str.value.length) < 3) {
		alert('Длина поисковой фразы должна быть больше 3 символов');
		str.focus();
		return false;
	} 
	else return true;
}