
function reverseMenu(id){
	var div1 = document.getElementById('manufactures_menu');
	var div2 = document.getElementById('category_menu');
	if(id != 2){
		SetCookie('menu_state', '1');
		div1.style.display = 'none';
		div2.style.display = 'block'
	}else{
		SetCookie('menu_state', '2');
		div2.style.display = 'none';
		div1.style.display = 'block'
	}
}

function SetCookie(sName, sValue){
	document.cookie = sName + "=" + escape(sValue) + ";expires=30 Dec 2999 23:59:59 UTC; path=/";
}

function showSubMenu(id){
	var td = document.getElementById('td'+id);
	var div = document.getElementById('show'+id);
	if(div.style.display == 'none'){
		td.className = 'pod_up';
		div.style.display = 'block';
 	}else{
 		td.className = 'pod_down';
 		div.style.display = 'none';
		
 	}
}

function checkForgetForm(){
	var elem = getElem('email_main');
	
	if (!checkEmail(elem.value)) {
		 alert('Введенный E-mail некорректен');
		 return false;
	}
	 else return true;
}


function changeImgSrcInCart(id){
	var elem = $(id);
	if (elem.src.indexOf('_back.gif')) {
		elem.src = elem.src.replace('_back.gif', '');
	} else {
		elem.src = elem.src.replace('.gif', '_back.gif');
	}
	
}

function checkPhisicRegForm(){
	var required = {
		"pfio"		:	"Вы не ввели ваше ФИО",
		"pcity"		:	"Вы не ввели ваш город",
		"pphone"	:	"Вы не ввели ваш телефон",
		"pemail"	:	"Вы не ввели ваш E-mail",
		"ppassword"	:	"Вы не ввели пароль",
		"ppassword_rep"	:	"Вы не ввели подтверждение пароля"
	};
	for (var i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function checkPhisicEditForm(){
	var required = {
		"pfio"		:	"Вы не ввели ваше ФИО",
		"pcity"		:	"Вы не ввели ваш город",
		"pphone"	:	"Вы не ввели ваш телефон",
		"pemail"	:	"Вы не ввели ваш E-mail"
	};
	for (var i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}


function checkLegalRegForm(){
	var required = {
		"lfio"		:	"Вы не ввели ваше ФИО",
		"lphone"	:	"Вы не ввели ваш телефон",
		"lcity"		:	"Вы не ввели ваш город",
		"lemail"	:	"Вы не ввели ваш E-mail",
		"lpassword"	:	"Вы не ввели пароль",
		"lpassword_rep"	:	"Вы не ввели подтверждение пароля"
	};
	for (var i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function checkLegalEditForm(){
	var required = {
		"lfio"		:	"Вы не ввели ваше ФИО",
		"lphone"	:	"Вы не ввели ваш телефон",
		"lcity"		:	"Вы не ввели ваш город",
		"lemail"	:	"Вы не ввели ваш E-mail"
	};
	for (var i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}


function checkPasswordForm(form){
	var required = {
		"current_password"		:	"Вы не ввели текущий пароль",
		"password_ty"				:	"Вы не ввели новый пароль",
		"password_r"			:	"Вы не ввели новый пароль (повторно)"
	};
	
	for (var i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

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, event) {
	if(((num >= 48) && (num <= 57)) || num == 37 || num == 8)
	{
		return event.keyCode;
	}
	
	if(((chara >= 48) && (chara <= 57)) || chara == 37 || chara == 8) {
		return event.charCode;
	} else {
		return false;
	}
}

function checkFeedbackForm() {
	var required = {
		"name"		:	"Вы не ввели Ваше ФИО",
		"msg"		:	"Вы не ввели сообщение"
	};
	for (var i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

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 changeTitle( elem ){
	if (elem.title = 'Раскрыть') {
		elem.title = 'Скрыть';
		elem.alt = '-';
	}
	else {
		elem.title = 'Раскрыть';
		elem.alt = '+';
	}
}

function changeImg(img_name, image) {
	if (image.className == 'std')
	{
		image.src = site_url+'buttons/'+img_name+'_hover.gif';
		image.height = 81;
		image.className = '';
	}
	else
	{
		image.src = site_url+'buttons/'+img_name+'.gif';
		image.height = 65;
		image.className = 'std';
	}
}


function changeBg(elem)
{
	if(elem.className=='hover') elem.className='';
	else if (elem.className=='') elem.className='hover'
}

function redirect(elem) {
	var url = base_url + 'catalog/';
	if(elem.value != '') url += 'manufacture'+elem.value+'/';
	
	location.href = url;
}

function changePage(elem) {
	var limit = elem.value;
	var loc = location.href ;
	var pos = loc.lastIndexOf('limit');
	if ( pos > -1) loc = loc.replace(/limit[0-9]+/, 'limit'+limit);
	else {
		if(loc.indexOf('?') > -1) {
			loc = loc.substr(0, loc.indexOf('?')) + 'limit' + limit + '/' + loc.substr(loc.indexOf('?'));
		}
		else loc = loc + 'limit'+limit+'/';
	}
	loc = loc.replace(/page[0-9]+/i, 'page1');
	
	location = loc;
}

function replaceInUrl(what, value) {
	var loc = location.href ;
	var pos = loc.lastIndexOf(what);
	
	var regex = new RegExp(what+"([0-9]+)?");
	
	if ( pos > -1) loc = loc.replace(regex, what + value);
	else {
		if(loc.indexOf('?') > -1) loc += '&' + what + value;
		else loc += '?' + what + value;
	}
	return loc;
}

function changeInStockFlag(checkbox) {
	var url = self_url;
	
	var key;
	
	if(checkbox.checked) {
		key = 'only-in-stock';
	} else {
		key = 'out-of-stock';
	}
	
	var isGET = location.href.indexOf('?') > -1;
	if(!isGET) {
		if(key == 'out-of-stock') {
			url = url.replace('only-in-stock/', '');
		} else {
			url = url.replace('out-of-stock/', '');
		}
		url += key + '/';
	} else {
		url = location.href;
		if(key == 'out-of-stock') {
			url = url.replace('&only-in-stock', '');
		} else {
			url = url.replace('&out-of-stock', '');
		}
		url += '&' + key;
	}
	
	
	location.href = url;
}

function changeSort(sort, order, changeSort) {
	var loc;
	if(changeSort) {
		if(order == 0) {
			order = 1;
		} else {
			order = 0;
		}
		loc = replaceInUrl('orderBy=', order);
	} else {
		loc = replaceInUrl('sortBy=', sort);
	}
	//alert(loc);
	location = loc;
}


function getPageSizes () {
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
}

function allClose(){
	var list = document.getElementById("menu").getElementsByTagName("ul");
	for(var i=0;i<list.length;i++){
		list[i].style.display = "none";
	}
}
function openMenu(node){
    var subMenu = node.parentNode.getElementsByTagName("ul")[0];
    if(subMenu.style.display == "none")
    {
          subMenu.style.display = "block";
          subMenu.parentNode.setAttribute("class",subMenu.parentNode.getAttribute("class") + " opened");
    }
    else
    {
          subMenu.style.display = "none"
          subMenu.parentNode.setAttribute("class",subMenu.parentNode.getAttribute("class").replace(" opened",""));
    }
}