//var Browser=(function x(){})[-5]=='x'?'FF3':(function x(){})[-6]=='x'?'FF2':/a/[-1]=='a'?'FF':'\v'=='v'?'IE':/a/.__proto__=='//'?'Saf':/s/.test(/a/.toString)?'Chr':/^function \(/.test([].sort)?'Op':'Unknown';
var Browser = Prototype.Browser;

if (Prototype.Browser.IE6 = Prototype.Browser.IE &&
    parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6) {
    
    Browser = 'IE6';
}
else if (Prototype.Browser.IE7 = Prototype.Browser.IE &&
         parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7) {
    
    Browser = 'IE7';
}
else if (Prototype.Browser.IE8 = Prototype.Browser.IE &&
        !Prototype.Browser.IE6 && !Prototype.Browser.IE7) {
            
    Browser = 'IE8';
}
else if (Prototype.Browser.Opera) {
    Browser = 'Opera';
}
else if (Prototype.Browser.WebKit) {
    Browser = 'Chrome';
}
else if (Prototype.Browser.Gecko) {
    Browser = 'Firefox';
}

function Show(fname, width, height) {
	if (screen.height<=550 && screen.width<=700) { 
		document.location = fname;
	} else {
		if(!height) height = 600;
		if(!width) width = 800;		
		newHeight = height + 20;
		var left = parseInt((screen.availWidth / 2) - (width / 2));
    	var top = parseInt((screen.availHeight / 2) - (height / 2));
		PopupWin = window.open(fname, "_blank", "height="+newHeight+",width="+width+",menubar=no,status=no,titlebar=no,toolbar=no,location=no,resizable=no,scrollbars=yes,left="+left+",top="+top+",screenX="+left+",screenY="+top); }
	return false;
}

function checkStrSearch() {
	var srch = $F('srch');
   if((srch.length) < 3) {
        alert('Строка поиска должна быть не менее 3 символов');
        return false;
    } else return true;
}

function checkStrSearchPage() {
	var srch = $F('srch_page');
   if((srch.length) < 3) {
        alert('Строка поиска должна быть не менее 3 символов');
        return false;
    } else return true;
}

function checkFForm(){
	var elem = getElem('login');
	if (elem.value.length < 4){
		alert('Логин/e-mail должен быть не меньше 3 символов');
		return false;
	}
	else return true;
}

function checkSubscrForm(email) {
	if (checkEmail(email.value)) return true;
	else {
		alert('Вы ввели некорректный E-mail.');
		return false;
	}
}

function checkRequestForm(str) {
	if (str.value.length > 0) return true;
	else {
		alert('Поле "Общая стоимость" обязательно для заполнения');
		return false;
	}
}

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 checkFeedbackForm() {
	var required = {
		"name"		:	"Вы не ввели Ваше ФИО",
		"org"		:	"Вы не ввели оганизацию",
		"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 checkSubscrForm() {
	var required = {
		"name_subscr"		:	"Вы не ввели Ваше имя",
		"email_subscr"		:	"Не введен или введен некорректный e-mail"
	};

	for (i in required) {
		var el = document.getElementById(i);
		if(el.id == 'email_subscr' && el.value && !checkEmail(el.value))
		{
			alert(required['email_subscr']);
			el.focus();
			return false;
		};
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function checkFeedbackFormFeedback() {
	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.id == 'msg' && el.value && el.value.length < 10 )
		{
			alert('Длина Вашего сообщения должна быть не менее 10 символов');
			el.focus();
			return false;
		};
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function checkFormOrder() {
	var required = {
		"name"		:	"Вы не заполнили поле \"Контактное лицо\"",
		"email"		:	"Не введен или введен некорректный e-mail",
		"tel"		:	"Вы не ввели телефон"
	};

	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;
		}
		
		var msg = document.getElementById('msg');
		if(msg.value && msg.value.length < 10 ) {
			alert('Длина Вашего примечания должна быть не менее 10 символов');
			msg.focus();
			return false;
		}
	}
	return true;
}

function checkRegistationForm() {
	var required = {
		"name"		:	"Вы не ввели Ваше ФИО",
		"tel"		:	"Не введен телефон",
		"email"		:	"Не введен или введен некорректный e-mail",
		"login_reg"	:	"Не введен логин",
		"password_reg"	:	"Не введен пароль",
		"img_code"	:	"Вы не ввели контрольный номер либо ввели его неверно"
	}

	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.id == 'login' && el.value && el.value.length < 4 ) {
			alert('Длинна логина должна быть не менее 4 символов');
			el.focus();
			return false;
		}
		
		if(el.id == 'password' && el.value && el.value.length < 4 ) {
			alert('Длинна пароля должна быть не менее 4 символов');
			el.focus();
			return false;
		}
		
		if(el.id == 'img_code' && el.value && !isFinite(el.value)) {
			alert(required['img_code']);
			el.focus();
			return false;
		}
		
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	
	var password = document.getElementById('password_reg');
	var confirm_password = document.getElementById('confirm_password');
	
	if(password.value != confirm_password.value)
	{
		alert('Пароли не совпадают');
		password.focus();
		password.value = '';
		confirm_password.value = '';
		return false;
	};

	return true;
}

function checkAuthorizationForm() {
	var required = {
		"login_form"		:	"Вы не ввели логин",
		"password_form"		:	"Не введен пароль"
	};

	for (i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}

	return true;
}

function checkAuthorizationPage() {
	var required = {
		"login_page"		:	"Вы не ввели логин",
		"password_page"		:	"Не введен пароль"
	};

	for (i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}

	return true;
}

function checkPassProfileForm() {
	var required = {
		"old_password"		:	"Вы не ввели текущий пароль",
		"new_password"		:	"Вы не ввели новый пароль",
		"confirm_password"	:	"Вы не подтверждение нового пароля"
	};

	for (i in required) {
		var el = document.getElementById(i);

		if(el.id == 'new_password' && el.value && el.value.length < 4 )
		{
			alert('Длинна пароля должна быть не менее 4 символов');
			el.focus();
			return false;
		};
		
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	
	var new_password = document.getElementById('new_password');
	var confirm_password = document.getElementById('confirm_password');
	
	if(new_password.value != confirm_password.value)
	{
		alert('Не верное подтверждение пароля');
		confirm_password.focus();
		confirm_password.value = '';
		return false;
	}
	
	return true;
}

function checkDataProfileForm() {
	var required = {
		"name"		:	"Вы не ввели Ваше ФИО",
		"tel"		:	"Не введен телефон"
	};

	for (i in required) {
		var el = document.getElementById(i);
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}

	return true;
}

function checkForgetPassForm() {
	var required = {
		"login_forget"	:	"Не введен логин",
		"email"			:	"Не введен или введен некорректный e-mail"
	};

	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 checkForm_query() {
	var required = {
		"name_query"		:	"Вы не ввели Ваше ФИО",
		"email_query"		:	"Не введен или введен некорректный e-mail",
		"tel_query"			:	"Не введен конткатный телефон"
	};

	for (i in required) {
		var el = document.getElementById(i);
		if(el.id == 'email_query' && el.value && !checkEmail(el.value))
		{
			alert(required['email_query']);
			el.focus();
			return false;
		};
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function showPassEditForm (){
	var form = document.getElementById('form_open');
	if(form.style.display == 'block') {
		form.style.display = 'none';
	} else {
		form.style.display = 'block';
		location.href = '#_form';
	}
}

function foggetTableDescriptionObject (id) {
	var elem = $('' + id);
	

	if(Element.visible(elem)) {
		Element.hide(elem);
	} else {
		Element.show(elem);
	}
}

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 checkUrl(url) {
//	pattern = /https?:\/\/(?:[-_A-Z0-9]*)\.[A-Z0-9]{2,5}(?:\/[-A-Z0-9+&@#/%=~_|]*)?/i;
//	return url.match(pattern);
}

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 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 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 setHttp(link) {
	if(link && link.substr(0, 7) != 'http://') link = 'http://' + link;
	return link;
}

function dump(elem) {
	var str="";
	for(var key in elem) {
		str += key + ": " + elem[key] + "\n<br>";
	}
	document.write(str);
}

function sendFilterForm() {
	var form = document.filter;
	
	var formFields = new Array('country', 'region', 'period', 'count');
	var toAdd = new Array();
	var url = location.href;
	
	for(var key in formFields) {
		var field = formFields[key];
		var elem = eval('form.'+field);
		if(elem.nodeName == 'SELECT') {
			var value = elem.options[elem.selectedIndex].value;
		} else if(elem.nodeName == 'INPUT') var value = elem.value;
		
		var _delimiter = url.indexOf('?') > -1 ? '&' : '?';

		if(url.indexOf(field+'=') == -1) {
			url += _delimiter + field+'='+value;
		} else {
			var RegEx = new RegExp(field+'=[0-9a-z]*', 'i');
			url = url.replace(RegEx, field+'='+value);
		}
	}
	
	location.href = url;
}

function preloadImg(src) {
	if (document.images) {
		var image = new Image();
		image.src = src;
	}
}

if (navigator.appName.toLowerCase()=='opera')
{
	var a = document.createElement('link');
	a.href = 'css/opera.css';
	a.media = 'all';
	a.type = 'text/css';
	a.rel = 'stylesheet';
	document.appendChild(a);
}

function addtoBasket(itemId, F) {
	if ( auth == 'false' ) {
		if (confirm('Вы не можете добавить товар в корзину, так как Вы не авторизованы. Хотите авторизоваться?')) {
			location.href = reg + 'authorization/';
		}
		return false;
	}
	
	$('popup_div').update('Пожалуйста, подождите...');
	$('popup_div').show();

	var options = {
		method: 'get',

		onSuccess: function(transport) {
			var serverResponse = transport.responseText;
			var data = serverResponse.split('||');
			
			
			$('basket_block').update('<img class="important" src="' + site_url + 'img/important2.gif" class="title" /><p><b>Товаров:</b> ' + data[0] + ' <br /><b>На сумму:</b> ' + data[1] + ' руб.</p> <form method="POST" action="' + basket + '"><input class="button3" style="position: absolute; top: 54px; left: 90px;" type="image" src="' + site_url + 'img/buttons/in_basket.gif" />');
			$('popup_div').update('Товар успешно добавлен в Вашу корзину');
			window.setTimeout(function() {
				$('popup_div').hide();
			}, 3000);
			OperationsOpenDivs(itemId, data, F)
		}
	};
	
	var url = basket + 'add' + itemId;
	new Ajax.Request(url, options);
}

function OperationsOpenDivs(itemId, data, F) {
	$('float_div_summ').update('В корзине ' + data[2] + ' шт.');
	if (F) {
		if (F == 'sale') {
			$('float_sale_' + itemId + '_summ').innerHTML = data[2];
		}
		
		if (F == 'sale_basket') {
			$('float_sale_' + itemId + '_summ').innerHTML = data[2];
			$('allFormCount_' + itemId).innerHTML = '<input type="text" name="' + itemId + '" size="6" value="' + data[2] + '" maxlength="6"> шт.<br />';
		}
		
		if (F == 'offer') {
			$('allFormCount_' + itemId).innerHTML = '<input type="text" name="' + itemId + '" size="6" value="' + data[2] + '" maxlength="6"> шт.<br />';
		}
		
		if (F == 'basket') {
			$('allFormCount_' + itemId).innerHTML = '<input type="text" name="' + itemId + '" size="6" value="' + data[2] + '" maxlength="6"> шт.<br />';
		}
		
		
	}
	
	if ( $('in_b' + itemId) && $('in_b' + itemId).style.display == 'none' ) $('in_b' + itemId).style.display = 'inline';
	
	$('allCount_' + itemId).innerHTML = data[2];
	$('float_sale_' + itemId + '_summ').innerHTML = data[2];
}

function Confirmation(text) {
	if (confirm(text)) {
		return true;
	} else return false;
}


function _showOverlay() {
	
	hideSelectBoxes();
	
	var overlay = $('overlay');
	overlay.className = '__overlay';
	var arrayPageSize = getPageSize();	
	overlay.style.height = arrayPageSize[1] + "px";
	overlay.show();
}

function _hideOverlay() {
	$('overlay').hide();
	showSelectBoxes();
}

function openDiv(id, e, F) {
	
	_showOverlay();
	
	var div = $( 'help' );
	
	var header 		= $('float_' + id + '_header').innerHTML;
	var image 		= $('float_' + id + '_image').innerHTML;
	var content 	= $('float_' + id + '_content').innerHTML;
	var price 		= $('float_' + id + '_price').innerHTML;
	var article 	= $('float_' + id + '_article').innerHTML;
	var dop_text 	= $('dop_text').innerHTML;
	if(F != 'basket'){
		var fotoZoom 	= $('float_' + id + '_fotoZoom').innerHTML;
	}else{
		//var producer_name = $('float_' + id + '_producer_name').innerHTML;
		//var producer_logo = $('float_' + id + '_producer_logo').innerHTML;
	};
	var PrintLink 	= $('float_' + id + '_PrintLink').innerHTML;
	var summ 		= $('allCount_' + id ).innerHTML;
	if (summ) summ 	= 'В корзине ' + summ + ' шт.';


	$('float_div_header').update(header);
	$('float_div_image').update(image);
	$('float_div_content').update(content);
	$('float_div_price').update(price);
	$('float_div_article').update(article);
	$('dop_text_insert').update(dop_text);
//	if(fotoZoom)	  	
	$('float_div_fotoZoom').update(fotoZoom);
	//if(producer_name) 	$('float_div_producer_name').update(producer_name);
	//if(producer_logo) 	$('float_div_producer_logo').update(producer_logo);
	if(PrintLink) 		$('float_div_PrintLink').update(PrintLink);	
	if(summ) 			$('float_div_summ').update(summ);
	
	if ($('add')) {
		$('add').onclick = function() {
			addtoBasket(id, F); 
			return false;
		}
	}
	
	div.show();
	var pageSizes = getPageSize();
	var hCenter = (pageSizes[0]-div.offsetWidth) / 2;
	hCenter -= 40;
	div.style.left = hCenter+"px";


	/*
	if(Browser == 'Chr'){
		div.style.top = document.documentElement.scrollTop+80+"px";
	}else if(Browser == 'IE'){
		var IE6 = true; //@cc_on || @_jscript_version < 5.7 @;
		if(IE6){
			// если указан doctype то вместо document.body.scrollTop надо юзать document.documentElement.scrollTop
			div.style.top = document.documentElement.scrollTop; //+80+"px";
		}else{
			div.style.top = document.documentElement.scrollTop+5+"px";
		}
	}else if(Browser == 'FF3' || Browser == 'FF2'){
		div.style.top = document.body.scrollTop+80+"px";
	}else if(Browser == 'Op'){
		div.style.top = document.body.scrollTop+80+"px";
	}
	*/
    /*
	switch (Browser)
	{
		case 'Chr':
			//div.style.top = document.body.scrollTop+80+"px";
			break;
		case 'IE':
//			var IE6 = navigator.userAgent.toLowerCase().indexOf(’msie 6?) != -1;
//			var IE6 = /msie|MSIE 6/.test(navigator.userAgent);
			var ie /*@cc_on = ScriptEngineMinorVersion() @* /;
			if(IE6){
			// если указан doctype то вместо document.body.scrollTop надо юзать document.documentElement.scrollTop
				div.style.top = document.documentElement.scrollTop+80+"px";
			}
			else {
				div.style.top = document.body.scrollTop+40+"px";
			}
			break;
		case 'FF3':
		case 'FF2':
			div.style.top = document.body.scrollTop+80+"px";
			break;
		case 'Op':
			div.style.top = document.body.scrollTop+80+"px";
			break;
		default:
			//div.style.top = document.body.scrollTop+40+"px";
            div.style.top = 80 + 'px';
	}
    */
    
    switch (Browser) {
        case 'IE6':
            div.style.top = document.documentElement.scrollTop + 80 + 'px';
        case 'IE7':
        case 'IE8':
            div.style.top = 40 + 'px';
            break;
            
        case 'Opera':
        case 'Firefox':
            div.style.top = document.body.scrollTop + 80 + 'px';
            break;
            
        case 'Chrome':
            div.style.top = 80 + 'px';
            break;
    }

	new Effect.Appear(div, {duration: 0.5});
}

function closeGoodDiv() {
	new Effect.Fade($('help'), {duration: 0.5});
	_hideOverlay();
}

function openDivForSale(id, F) {
	
	var div = $( 'help' );
	
	var header = $('float_sale_' + id + '_header').innerHTML,
	image = $('float_sale_' + id + '_image').innerHTML,
	content = $('float_sale_' + id + '_content').innerHTML,
	price = $('float_sale_' + id + '_price').innerHTML,
	producer_name = $('float_sale_' + id + '_producer_name').innerHTML,
	producer_logo = $('float_sale_' + id + '_producer_logo').innerHTML,
	article = $('float_sale_' + id + '_article').innerHTML,
	summ = $('float_sale_' + id + '_summ').innerHTML;
	
	
	if (summ) {
		var summ = 'В корзине ' + summ + ' шт.';
	}
	
	$('float_div_header').update(header);
	$('float_div_image').update(image);
	$('float_div_content').update(content);
	$('float_div_price').update(price);
	//$('float_div_producer_name').update(producer_name);
	$('float_div_producer_logo').update(producer_logo);
	$('float_div_article').update(article);
	//$('float_div_summ').update(summ);
	
	if ($('add')) $('add').onclick = function() {
		addtoBasket(id, F); 
		return false;
	}
	div.toggle();
	//new Effect.Appear(div, {duration: 0.5});
}


var isDOM = document.getElementById //DOM1 browser 
var isO   = isO5 = window.opera && isDOM; //Opera 5+
var isO6  = isO && window.print //Opera 6+
var isO7  = isO && document.readyState //Opera 7+
var isIE  = document.all && document.all.item && !isO //Microsoft Internet Explorer 4+
var isIE5 = isIE && isDOM //MSIE 5+
var isMZ  = isDOM && (navigator.appName=="Netscape")