function auto_maximize_width_height(elem)
{
	var out = "";
	var item = "";
	var maxheight = 0;
	var maxwidth = 0;
	
	div = document.getElementById(elem);
	childs = div.childNodes;
	for(key in childs) {
		item = childs[key];
		if(item.offsetHeight > maxheight) {
			maxheight = item.offsetHeight;
		}
		if(item.offsetWidth > maxwidth) {
			maxwidth = item.offsetWidth;
		}
	}
	for(key in childs) {
		item = childs[key];
		if (item.nodeName != 'DIV') continue;
		item.style.height = maxheight+1+'px';
		item.style.width = maxwidth+1+'px';
	}
}
function Show(fname, width, height) {
	if (screen.height<=550 && screen.width<=700) { 
		document.location = fname;
	} else {
		if(!height) height = 800;
		if(!width) width = 600;		
		newHeight = height + 20;
		PopupWin = window.open(fname, "_blank", "height="+newHeight+",width="+width+",menubar=no,status=no,titlebar=no,toolbar=no,location=no,resizable=no,scrollbars=no"); }
	return false;
}

function checkStrSearch(str) {
if((str.length) < 3) {
		  alert('Строка поиска должна быть не менее 3 символов');
		  return false;
	} else return true;
}
function trim(str) {
	while (str.substr(0, 1) == ' ') {str = str.substr(1);};
	while (str.substr(str.length - 1, 1) == ' ') {str = str.substr(0, str.length - 1);};
	return str;
};

function checkForm() {
	input = document.getElementById('str');
	input.value = trim(input.value);

	if(input.value == default_value) input.value = ''; 
	fill = checkStrSearch(input.value);
	if(!fill && input.value == '') {
		input.value = default_value;
		return false;
	} else if(input.value != '' && !fill) return false;

};

function isInt(num, chara)
{
	if(((num >= 48) && (num <= 57)) || num == 37 || num == 8)
	{
		event.keyCode = num;
		return event.keyCode;
	}
	if(((chara >= 48) && (chara <= 57)) || chara == 37 || chara == 8)
	{
		event.charCode = chara;
		return event.charCode;
	}
	else
	{
		return false;
	}
}

function checkFeedbackForm() {
	var required = {
		"name"		:	"Вы не ввели Ваше ФИО",
		"phone"		:	"Вы не ввели номер телефона",
		"msg"		:	"Вы не ввели сообщение"
	};
	for (var i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function preloadImg(src) {
	if (document.images) {
		var image = new Image();
		image.src = src;
		images.push(image);
	}
}

function auto_form_check(required) {
	if(!required) return true;
	for (var i in required) {
		var el = document.getElementById(i);
		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 showHiddenElem(id) {
	if(!document.getElementById(id)) {
		return false;
	}
	elem = document.getElementById(id);
	if(elem.style.display == 'block') elem.style.display = 'none';
	else elem.style.display = 'block';
}

function getElem(id) {
	return document.getElementById(id);
}

function preloadImages() {
	var d = document; 
	
	if(d.images) {
		var i, a = preloadImages.arguments;
		for(i = 0; i < a.length; i++) {
			d.preloadedImages[a[i]] = new Image;
			d.preloadedImages[a[i]].src = a[i];
		}
	}
}

function menu(element)
{
	var parent;
	parent = element.parentNode;

	if(parent.className == 'item') parent.className = 'item hover';
	else parent.className = 'item';
}

function changeImage(id, src)
{
	document.getElementById("img"+id).src = src;
}




function isInt(num, chara)
{
	if(((num >= 48) && (num <= 57)) || num == 37 || num == 8)
	{
		event.keyCode = num;
		return event.keyCode;
	}
	if(((chara >= 48) && (chara <= 57)) || chara == 37 || chara == 8)
	{
		event.charCode = chara;
		return event.charCode;
	}
	else
	{
		return false;
	}
}
function changeClass(elem)
{
	elem = document.getElementById('td_'+elem);
	if(elem.className=='item') elem.className = 'hover';
	else if (elem.className =='hover') elem.className = 'item';
}

function check(ch) {
	
	name = document.getElementById('name').value;
	email = document.getElementById('email').value;
	if(ch == 'fd'){
		msg = document.getElementById('msg').value;
		phone = document.getElementById('phone').value;
	}
	else question = document.getElementById('question').value;
	
	if(name.length == 0){
		alert('Имя не введено!');
		return false;
	}
	if(question.length == 0){
		alert('Вопрос не введен!');
		return false;
	}
	if(phone.length == 0){
		alert('Телефон не введён!');
		return false;
	}
	if(msg.length == 0){
		alert('Сообщение не введно!');
		return false;
	}
	
	if(ch == 'faq'){
		if(!checkEmail(email)){
			alert('Введите корректный E-mail!');
			return false;
		}
		if(0 == document.getElementById('__ec_s').value.length)
		{
			alert('Код подтверждения не указан!')
			return false;
		}
	}
	else{
		if(email){
			if(!checkEmail(email)){
				alert('Введите корректный E-mail!');
				return false;
			}
		}
	}
}

function showSubMenu(id)
{
	document.getElementById('submenu_'+id).style.display='block';
}
function hideSubMenu(id)
{
	document.getElementById('submenu_'+id).style.display='none';
}
