var timer;
var actToPut;
var unitToPut;

function do_putToCart(id, type){	
	hideDivMsg();
	
	document.getElementById('aid_ccont').style.display = 'block';
	document.getElementById('aid_back').style.display = 'none';
	
	if(type == 1)
		{
			if(	detectIE6())
				{
					document.getElementById('atc_inf_div').style.left = getAbsolutePosition(document.getElementById('p' + id)).x  - 295 + 'px';
					document.getElementById('atc_inf_div').style.top = getAbsolutePosition(document.getElementById('p' + id)).y  - 200 + 'px';
				}
			else
				{
					document.getElementById('atc_inf_div').style.left = getAbsolutePosition(document.getElementById('p' + id)).x - 25 + 'px';
					document.getElementById('atc_inf_div').style.top = getAbsolutePosition(document.getElementById('p' + id)).y  - 200 + 'px';
				}
		}
	else if(type == 2)
		{		
			document.getElementById('atc_inf_div').style.left = getAbsolutePosition(document.getElementById('p' + id).parentNode.parentNode.parentNode).x + 25 + 'px';
			document.getElementById('atc_inf_div').style.top = getAbsolutePosition(document.getElementById('p' + id).parentNode.parentNode.parentNode).y  - 80 + 'px';
		}
	var str = document.getElementById('art_'+id).innerHTML;
	
	document.getElementById('aid_pr').innerHTML = str.substring(0, str.length-2);
	
	unitToPut = document.getElementById('units_'+id).innerHTML;
	document.getElementById('aid_units').innerHTML = unitToPut;
	document.getElementById('aid_units2').innerHTML = unitToPut;
	
	document.getElementById('aid_count').value = '1';
	
	document.getElementById('atc_inf_div').style.display = 'block';
		
	document.getElementById('aid_count').focus();
	document.getElementById('aid_count').select();
	actToPut = id;
	if(unitToPut == 'шт') unitToPut = 1; else unitToPut = 2;
}

function do_putToCartBreak()
	{
		document.getElementById('atc_inf_div').style.display = 'none';
	}

function do_putToCartEnd(){
	var count = document.getElementById('aid_count').value;
	document.getElementById('aid_back').innerHTML = '<img src="#" id="aid_img" style="margin-top: 60px;">';
	document.getElementById('aid_img').src = pic_load.src;
	document.getElementById('aid_ccont').style.display = 'none';
	document.getElementById('aid_back').style.display = 'block';
	x_PutToCart(actToPut, count, do_putToCart_callback);
}

function do_putToCart_callback(result){	
	document.getElementById('aid_back').innerHTML = '<br><br><br><span style="font-style: italic; font-size: 13pt; color: #DA140B;">Товар добавлен<br>в&nbsp;корзину</span>';
	document.getElementById('cart').innerHTML = unescape(result['cart']);
	timer = window.setTimeout("hideDivMsg()", 2000);
}

function CountMinus(){
	var now_count = document.getElementById('aid_count').value;
	if(unitToPut == 2)
		{
			now_count = (now_count*1-0.1*1); 
			now_count = Math.floor(now_count)+Math.round(((now_count)-Math.floor(now_count))*10)/10;
		}
	else now_count--;
	//now_count--;
	if(now_count<=0) now_count = 0;
	if(now_count>999) now_count = 999;
	if(isNaN(now_count)) now_count = 1;
	document.getElementById('aid_count').value = now_count;
}

function CountPlus(){
	var now_count = document.getElementById('aid_count').value;
	if(unitToPut == 2) 
		{
			now_count =  (now_count*1 + 0.1*1); 
			now_count = Math.floor(now_count)+Math.round(((now_count)-Math.floor(now_count))*10)/10;
		}
	else now_count++;
	//now_count++;
	if(now_count<=0) now_count = 1;
	if(now_count>999) now_count = 999;	
	if(isNaN(now_count)) now_count = 1;
	document.getElementById('aid_count').value = now_count;
}

function hideDivMsg(){	
	if (timer) window.clearTimeout(timer);
	document.getElementById('atc_inf_div').style.display = 'none';
}

function do_putToSCart(id, el){
	x_PutToSCart(id, do_putToSCart_callback);
}

function do_putToSCart_callback(result){

	document.getElementById('scart').innerHTML = unescape(result['scart']);
	document.getElementById('s'+unescape(result['elem'])).innerHTML = 'Добавлен к&nbsp;сравнению';	
	document.getElementById('s'+unescape(result['elem'])).style.border = '0px';
	document.getElementById('s'+unescape(result['elem'])).style.cursor = 'text';
	document.getElementById('s'+unescape(result['elem'])).style.color = '#F6871F';
	document.getElementById('s'+unescape(result['elem'])).title = '';	
}		

function do_showCartInfo(){

	document.getElementById('cart_info').style.display = 'block';
	document.getElementById('cart_info_content').innerHTML = '<br><br><center><img src="/images/bg_l.gif"></center>';
	x_ShowCartInfo(do_showCartInfo_callback);	
}

function do_showCartInfo_callback(result){

	document.getElementById('cart_info_content').innerHTML = unescape(result);
}

function CloseCartInfo(){

	document.getElementById('cart_info').style.display = 'none';
}

function doPic(imgName){
		document.getElementById('m_img').src = spacer.src;
		setTimeout('doPicEnd("'+imgName+'")', 100);						
	}
				
function doPicEnd(imgName){
		imgOn = ("/img/catalog/" + imgName);
		document.getElementById('m_img').src = imgOn;
	}
				
function do_ShowPrice(id){x_ShowPrice(id, do_ShowPrice_callback);}

function do_ShowPrice_callback(result){		
	if(result['count']>0)
		{
			for(var counter=0; counter<result['count']; counter++){
				document.getElementById('art_'+result['id'][counter]).innerHTML = result['price'][counter]+' Р';
				}
		}
	}

function getAbsolutePosition(el) {
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent) {
		var tmp = getAbsolutePosition(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) 
		{
			limitField.value = limitField.value.substring(0, limitNum);
		}
	else 
		{
			limitCount.value = limitNum - limitField.value.length;
	    }
	}	
	
function detectIE6()
	{
		var browser = navigator.appName;
		if (browser == "Microsoft Internet Explorer")
			{
				var b_version = navigator.appVersion;
				var re = /\MSIE\s+(\d\.\d\b)/;
				var res = b_version.match(re);
				
				if (res[1] <= 6)
					{
						return true; 
					}
			}	
		return false;
	}