function showVendors(e,kindex){
	var elem = $('topImgVendorsShow')
	elem.setStyle('display','block');
	$('partMainMenu').style.backgroundPosition = '503px 21px';
	if ($$('div.topImgVendors')[e]){
		elem.innerHTML=$$('div.topImgVendors')[e].innerHTML;
	}
	
	var rank = Math.floor((kindex)/10);
	var myFx = new Fx.Scroll($('partMainMenuInn'));
	var num1 = 290*rank;
	var scroll = $('partMainMenuInn').getScroll();
	myFx.start(0,num1);
	oldY = num1;
	//alert(rank);
}

var expandMenu = function() {
	var elem2 = $('partMainMenuInn');
		var setWidth = (elem2.getStyle('width')=='275px') ? 825 : 275;
		var setScroll = (elem2.getStyle('width')=='275px') ? 0 : oldY;
		elem2.setStyle('width',setWidth);
		var myFx = new Fx.Scroll($('partMainMenuInn'));
		myFx.start(0,oldY);
}

window.addEvent('domready', function() {
	new SmoothScroll({duration:1000}, window);
		if ($('menuOnRedExpand')) {
			$('menuOnRedExpand').addEvent('click', function() {
				expandMenu();
				this.setStyle('display','none');
				$('menuOnRedCollapse').setStyle('display','block');
				$('menuOnRedExpandTop').setStyle('display','none');
				//alert(oldY);
			});

			$('menuOnRedExpandTop').addEvent('click', function() {
				expandMenu();
				this.setStyle('display','none');
				$('menuOnRedCollapse').setStyle('display','block');
				$('menuOnRedExpand').setStyle('display','none');
				//alert(oldY);
			});

			$('menuOnRedCollapse').addEvent('click', function() {
				expandMenu();
				this.setStyle('display','none');
				$('menuOnRedExpand').setStyle('display','block');
				$('menuOnRedExpandTop').setStyle('display','block');
				//alert(oldY);
			});
		}
});
var myRequestBasketBox = new Request({method: 'post', url: '/files/bg.basket.php',evalScripts : true,
onSuccess: function(html){
	if (html) {
		var myRequestResult = new Element('div',{'id':'myRequestResult','styles':{'padding':'20px'}});
		myRequestResult.innerHTML = html;
		//информационное сообщение
		infoBox ();
		//ответ аякс в информационное сообщение
		myRequestResult.inject($('windowFront'));
	}
}
});
var myRequestCompareBoxEasy = new Request({method: 'post', url: '/files/bg.compare.php',evalScripts : true});
var myRequestCompareBox = new Request({method: 'post', url: '/files/bg.compare.php',evalScripts : true,
onSuccess: function(html){
	var myRequestResult = new Element('div',{'id':'myRequestResult','styles':{'padding':'20px'}});
	myRequestResult.innerHTML = html;
	//информационное сообщение
	infoBox ();
	//ответ аякс в информационное сообщение
	myRequestResult.inject($('windowFront'));
}
});
var myRequestInfoBoxEasy = new Request({method: 'post', url: '/files/bg.info.php',evalScripts : true});
var myRequestInfoBox = new Request({method: 'post', url: '/files/bg.info.php',evalScripts : true,
onSuccess: function(html){
	if (html!='') {
	var myRequestResult = new Element('div',{'id':'myRequestResult','styles':{'padding':'20px'}});
	myRequestResult.innerHTML = html;
	//информационное сообщение
	infoBox ();
	//ответ аякс в информационное сообщение
	myRequestResult.inject($('windowFront'));
	}
}
});
var myRequestBasketEasy = new Request({method: 'post', url: '/files/bg.basket.php',evalScripts : true});
var myRequestBasketPerson = new Request({method: 'post', url: '/files/bg.basket.person.php',evalScripts : true});
var myRequestBasketPaymentCash = new Request({method: 'post', url: '/files/bg.basket.payment.php',evalScripts : true,
onSuccess: function(html){
	//$('ttt1').innerHTML = html;
}
});
var myRequestBasketDelivery = new Request({method: 'post', url: '/files/bg.basket.delivery.php',evalScripts : true});
var myRequestBasketDeliveryEMSCalc = new Request({method: 'post', url: '/files/bg.ems.calc.php',evalScripts : true});
var myRequestBasketComment = new Request({method: 'post', url: '/files/bg.basket.comment.php',evalScripts : true});
var myRequestBasketFinish = new Request({method: 'post', url: '/files/bg.basket.finish.php',evalScripts : true});
var myRequestAuth = new Request({method: 'post', url: '/files/bg.auth.php',evalScripts : true});
var myRequestRegistration = new Request({method: 'post', url: '/files/bg.reg.php',evalScripts : true});
var myRequestReviews = new Request({method: 'post', url: '/files/bg.reviews.php',evalScripts : true});
var myRequestRegions = new Request({method: 'get', url: '/files/bg.regions.php',evalScripts : true});
var myRequestBasketRegions = new Request({method: 'post', url: '/files/bg.basket.regions.php',evalScripts : true});
/*
var myRequestCredit = new Request({method: 'get', url: '/files/bg.credit.php',evalScripts : true,
onSuccess: function(html){
	var myRequestResult = new Element('div',{'id':'myRequestResult','styles':{'padding':'20px'}});
	myRequestResult.innerHTML = html;
	//информационное сообщение
	infoBox ();
	//ответ аякс в информационное сообщение
	myRequestResult.inject($('windowFront'));
}
});
*/
var myRequestCredit = new Request({method: 'get', url: '/files/bg.credit.php',evalScripts : true});

var infoBox = function () {
	var title = (arguments[0]) ? arguments[0] : '';
	var text = (arguments[1]) ? arguments[1] : '';
	var width = (arguments[2]) ? arguments[2] : 600;
	var height = (arguments[3]) ? arguments[3] : 400;
	//затемнить экран

	var windowBackground = new Element('div',{'id': 'windowBackground','styles': {'opacity':'0','background':'gray','position':'absolute','left':'0','top':'0','width':window.getWidth(),'height':window.getScrollHeight(),'z-index':'10000'},
		'events': {'click': function(){infoBoxClose();}}
	});
	windowBackground.inject($('page'));
	new Fx.Tween(windowBackground,{duration:50}).start('opacity', 0.4, 0.6);
	//выдать окно
	var lWidth = width;
	var lHeight = height;
	var lCenterWidth = window.getWidth()/2;
	var lCenterHeight = window.getHeight()/2;
	var lLeft = lCenterWidth-lWidth/2;
	var lTop = window.getScrollTop()+lCenterHeight-lHeight/2;
	var windowFront = new Element('div',{'id': 'windowFront','styles': {'opacity':'0','background':'white','position':'absolute','left':lLeft,'top':lTop,'width':lWidth,'height':lHeight+'px','overflow':'auto','z-index':'10001'}});
	var infoBoxClose1 = new Element('div',{'id':'infoBoxClose','styles':{'padding':'10px 10px','background':'#333333','color':'white','text-align':'right','text-transform':'uppercase','font-size':'10px'}});
	infoBoxClose1.innerHTML = "<a href='javascript:void(0)' onclick='infoBoxClose()' style='color:white;font-weight:bold;text-decoration:none'>X</a>";
	windowFront.inject($('page'));
	new Fx.Tween(windowFront,{duration:'long'}).start('opacity', 0.3, 1);
	//отобразить заголовок
	var windowInn = new Element('div',{'id':'windowInn','styles':{'padding':'20px'}});
	infoBoxClose1.inject(windowFront);
	windowInn.inject(windowFront);
	if (title) {
		var windowTitle = new Element('h1',{'styles':{}});
		windowTitle.innerHTML = title;
		windowTitle.inject(windowInn);
	}

	//отобразить текстовое содержание
	if (text!='') {
		var windowText = new Element('div',{'styles':{}});
		windowText.innerHTML = text;
		windowText.inject(windowInn);
	}
};

function infoBoxClose() {
	new Fx.Tween($('windowBackground'),{duration:'long'}).start('opacity', 0.9, 0);
	var myFunction = function(){
		if ($('windowFront')) $('windowFront').destroy();
		if ($('windowBackground')) $('windowBackground').destroy();
	};
	myFunction.delay(500);
}

function showCompanyForm(id,check) {
	//return alert(id+' '+check)
	//var d = $('CompanyTable').getStyle('display');
	if (id==0 && check==true) {
		$('CompanyTable').setStyle('display','none');
	} else if (id==1 && check==true) {
		$('CompanyTable').setStyle('display','table');
	}
}

var paymentInfo = function() {
	var t = arguments[0];
	var z = arguments[1];
	myRequestBasketPaymentCash.send('variant='+t);
	if (z==1) return;

	$$('div.mBlockItemsToggle').setStyle('display','none');
	$$('div.mBlockItemsToggle').setStyle('opacity','0');
	var el = $('mBlockItemsToggle_'+t);
	el.setStyle('display','block');
	new Fx.Tween(el,{duration:'long'}).start('opacity', 0.5, 1);

};

var deliveryInfo = function() {
	var t = arguments[0];
	var s = arguments[1];

	if (s!=1) {
		$$('div.mBlockItemsToggle').setStyle('display','none');
		$$('div.mBlockItemsToggle').setStyle('opacity','0');
		var el = $('mBlockItemsToggle_'+t);
		el.setStyle('display','block');
		new Fx.Tween(el,{duration:'long'}).start('opacity', 0.5, 1);
	}
	if (s==2) {
		return myRequestBasketFinish.send('finish=1');
	}

	myRequestBasketDelivery.send('variant='+t+'&s='+s);

};

function openImg(id,iter) {
	var img=new Image();
	img.src = id;
	$('idImg').src = img.src;
	$$('div.itemImgs').setStyle('border','1px solid #ffffff');
	$('itemImgs'+iter).setStyle('border','1px solid gray');
}

var myMessage = function() {
	var id = arguments[0];
	var mess = arguments[1];
	var color = (arguments[2]) ? arguments[2] : 'red';
	$(id).setStyle('opacity',0);
	$(id).setStyle('font-size','13px');
	$(id).innerHTML = mess;
	new Fx.Tween($(id),{duration:'long'}).start('opacity', 0.5, 1);
	new Fx.Tween($(id),{duration:'long'}).start('font-size', 16);
	new Fx.Morph($(id),{duration:'long'}).set({'color': color,'font-weight': 'bold'});
};

function tradeChange(seln) {
	numer=seln.trade.selectedIndex;
	location.href='?category='+seln.trade.options[numer].value;
}
function vendorChange(seln,category) {
	numer=seln.vendor.selectedIndex;
	var myRequest = new Request({method: 'post', url: '/files/bg.compare.php',evalScripts : true
	/*,
		onSuccess: function(html){
			var myRequestResult = new Element('div',{'id':'myRequestResult','styles':{'padding':'20px'}});
			myRequestResult.innerHTML = html;
			//информационное сообщение
			infoBox ();
			//ответ аякс в информационное сообщение
			myRequestResult.inject($('windowFront'));
	}*/
	});
	myRequest.send('category='+category+'&vendor='+seln.vendor.options[numer].value+'&modelsadd=1');
}
function modelChange(seln,compareId) {
	var numer=seln.model.selectedIndex;
	var myid = seln.model.options[numer].value;
	myRequestCompareBox.send('category='+compareId+'&id='+myid+'&redirect=1');
}

function modelChange2(seln,compareId) {
	var numer=seln.model.selectedIndex;
	var myid = seln.model.options[numer].value;
	myRequestCompareBoxEasy.send('category='+compareId+'&id='+myid+'&refresh=1');
}


function emptyField(obj) {
	if (obj.value=='поиск') {
		obj.value = '';
	}
	obj.setStyle('color','black');
}

var openDivEasy = function() {
	/*
	var addprice = (parseFloat($('deliveryPriceTop').innerHTML)+parseFloat($('paymentPriceTop').innerHTML)+parseFloat('<?=$summ?>')).toFixed(0);
	$('summary').innerHTML = addprice;
	*/
	var elClass = arguments[0];
	var el = $(arguments[1]);
	
	$$('div.'+elClass).setStyle('display','none');
	
	var status = (el.getStyle('display')=='none') ? 'block' : 'none';
	el.setStyle('opacity',0);
	el.setStyle('display',status);
	new Fx.Tween(el,{duration:'long'}).start('opacity', 0.5, 1);

}


var myRequestBasketOrders = new Request({method: 'post', url: '/files/bg.basket.orders.php',evalScripts : true});

var deliveryPrice = function() {
	
	var el = arguments[0];
	var dest = arguments[1];
	//alert(el);
	myRequestBasketOrders.send('action=ems&to='+dest+'&el='+el);
}

var paymentPrice = function() {
	$('paymentPriceTop').set('text',0);
	var action = arguments[0];
	var item = arguments[1];
	myRequestBasketOrders.send('action='+action+'&id='+item);
}

//Картинка
function myUgadai() {
	var tmp = $('myImage').getProperty('rel')+1;
	$('myImage').setProperty('rel',tmp);
	img = new Image();
	img.src = '/imca/ugadai.png?'+tmp;
	$('myImage').src = img.src;
	 $('ImgText').focus();
}
function tradeChangeCredit(seln) {
	var numer=seln.trade.selectedIndex;
	var myid = seln.trade.options[numer].value
	//GetBg('GET','/bg_credit.php','compareId='+myid+'&fl=5');
	//alert(myid);
	myRequestCredit.send('compareId='+myid+'&fl=5');
}
function vendorChangeChedit(seln,compareId) {
	numer=seln.vendor.selectedIndex;
	//GetBg('GET','/bg_credit.php','compareId='+compareId+'&tradeId='+seln.vendor.options[numer].value);
	//alert(compareId);
	//alert(seln.vendor.options[numer].value);
	myRequestCredit.send('compareId='+compareId+'&tradeId='+seln.vendor.options[numer].value);
}
function modelChangeCredit(seln,compareId) {
	numer=seln.model.selectedIndex;
	//GetBg('GET','/bg_credit.php','modelId='+seln.model.options[numer].value+'&fl=2');
	myRequestCredit.send('modelId='+seln.model.options[numer].value+'&fl=2&compareId='+compareId);
}
function emptyOptions(id) {
	var sel = document.getElementById(id);
	while (sel.options.length) {
		sel.options[0] = null;
	}
}
function tradeItemIdDelete(id) {
	var tradeItem = 'tradeItem_'+id;
	//alert(tradeItem);
	$(tradeItem).dispose();
}
function tradeItemIdInsert(id,name,price,url) {
	//alert('tradeItem_'+id);
	var myTradeItem = new Element('div', {id: 'tradeItem_'+id});
	var myTradeHref = new Element('a', {'href': url});
	myTradeHref.innerHTML = name;
	myTradeHref.inject(myTradeItem)
	myTradeItem.innerHTML += ' '+price+' Р. '+'<a href="javascript:tradeItemIdDelete(\''+id+'\')" class="tradeItemsHref">X</a>';
	myTradeItem.innerHTML += '<input type="hidden" name="tradeItem_'+id+'" value="'+name+' : '+price+' Р."/>';
	myTradeItem.inject($('tradeItems'));
}
var regionMode = function() {
	var regform = arguments[0];
	//alert(regform)
	myRequestBasketRegions.send('regform='+regform);
}

var sendRevPhone = function() {
	if ($('phonePref').value=="" || $('phonePref').value=="" || $('phonePref').value=="") {
		return messageItem('revPhoneMsg','Номер телефона не отправлен: имя, префикс телефона и телефон - должны быть заполненными.','red',14);
	}
	$('revPhoneForm').setStyle('display','none');
	messageItem('revPhoneMsg','Ваш номер телефона успешно отправлен!<br/>Мы перезвоним Вам в течение 15 минут*<br><br>*При заказе обратного звонка в рабочие дни с 10.00 до 19.00 по Московскому времени.','green',14);
	myRequestInfoBox.send('action=revcall&sendPhone='+$('phoneRev').value+'&sendPref='+$('phonePref').value+'&uName='+$('name1phone').value+'&myloc='+location.href);
}

var sendOrder1Click = function() {
	var tmpkeyurl = 'http://www.telegorod.ru'+arguments[0];
	if ($('phonePref').value=="" || $('phonePref').value=="" || $('phonePref').value=="") {
		return messageItem('revPhoneMsg','Заказ не отправлен: имя, префикс телефона и телефон - должны быть заполненными.','red',14);
	}
	$('revPhoneForm').setStyle('display','none');
	messageItem('revPhoneMsg','<h1>Спасибо!</h1>Ваш заказ успешно отправлен!<br/>Мы перезвоним Вам в течение 15 минут*<br><br>*в рабочие дни с 10.00 до 19.00 по Московскому времени.','green',14);
	myRequestInfoBox.send('action=revcall&order=1&sendPhone='+$('phoneRev').value+'&sendPref='+$('phonePref').value+'&uName='+$('name1phone').value+'&myloc='+tmpkeyurl);
}

var afterPhonePref = function() {
	var digit = arguments[0];
	var strco = digit.length;
	if (strco==3) {
		$('txt_tel').focus();
	}
}
var afterPhonePrefCall = function() {
	var digit = arguments[0];
	var strco = digit.length;
	if (strco==3) {
		$('phoneRev').focus();
	}
}

var emptyPriceInfo = function() {
	//alert(1);
	/*
	if ($$('div.newredtext')) {
		alert($$('div.newredtext'));
		$$('span.newredtext').setStyle('text-decoration','underline');
		return;
	}
	*/
}

var addBonus = function() {
	if (!$('myBonuses')) {
		$('bottomsumm').innerHTML = $('summary').innerHTML;
		return;
	}
	var mybonuses = $('myBonuses').innerHTML;
	//alert(mybonuses);
	var ischeck = $('bonusIsp').checked;
	//alert(ischeck);
	$('paymentPriceBonus').innerHTML = (ischeck==false) ? 0 : mybonuses;
	
	var newsumm = parseInt(($('summary').innerHTML).replace(/\s/g,''))-parseInt($('myBonuses').innerHTML);
	//$('summary2').innerHTML = newsumm;
	$('summary2').innerHTML = number_format(newsumm, 0, '.', ' ');
	
	if (ischeck==false) {
		$('bonusCell').setStyle('opacity',0.3);
		$('bottomsumm').innerHTML = $('summary').innerHTML;
	} else {
		$('bonusCell').setStyle('opacity',1);
		$('bottomsumm').innerHTML = $('summary2').innerHTML;
	}
	//alert(1);
}

function number_format( number, decimals, dec_point, thousands_sep ) {	// Format a number with grouped thousands
	// 
	// +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +	 bugfix by: Michael White (http://crestidg.com)

	var i, j, kw, kd, km, minus = "";

	if(number < 0){
		minus = "-";
		number = number*-1;
	}
	
	// input sanitation & defaults
	if( isNaN(decimals = Math.abs(decimals)) ){
		decimals = 2;
	}
	if( dec_point == undefined ){
		dec_point = ",";
	}
	if( thousands_sep == undefined ){
		thousands_sep = ".";
	}

	i = parseInt(number = (+number || 0).toFixed(decimals)) + "";

	if( (j = i.length) > 3 ){
		j = j % 3;
	} else{
		j = 0;
	}

	km = (j ? i.substr(0, j) + thousands_sep : "");
	kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
	//kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
	kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");


	return minus + km + kw + kd;
}
var sendBasketPerson = function() {
	var name1 = $('txt_name_req').value;
	var pref1 = $('txt_telp_req').value;
	var phone1 = $('txt_tel').value;

	myRequestInfoBoxEasy.send('action=basketPerson&name='+name1+'&pref='+pref1+'&phone='+phone1+'');
}

//Вывод системных сообщений
var messageItem = function() {
	var el = $(arguments[0]);
	var mess = arguments[1];
	var color = (arguments[2]) ? arguments[2] : 'red';
	var size = (arguments[3]) ? arguments[3] : '13';
	el.setStyle('opacity',0);
	el.setStyle('font-size','13px');
	el.set('html',mess);
	new Fx.Tween(el,{duration:'long'}).start('opacity', 0.5, 1);
	new Fx.Tween(el,{duration:'long'}).start('font-size', size);
	$(el).setStyle('background','#eaeaea');
	new Fx.Morph(el,{duration:'long'}).set({'color': color});
}
