function setPriceFromList(prod_id, nestandartSize)
{	pr = 0;
	ds = 0;
	sh = 0;
	dl = 0;
	if (document.getElementById("value_discount["+prod_id+"]")) ds = document.getElementById("value_discount["+prod_id+"]").value;
	if (document.getElementById("shirina["+prod_id+"]"))        sh = document.getElementById("shirina["+prod_id+"]").value;
	if (document.getElementById("dlina["+prod_id+"]"))          dl = document.getElementById("dlina["+prod_id+"]").value;
	if (nestandartSize==0)
		if (price_product[prod_id]!=undefined)
			if (price_product[prod_id][sh]!=undefined)
				if (price_product[prod_id][sh][dl]==undefined) dl=200;
	if (price_product[prod_id]!=undefined && price_product[prod_id][sh]!=undefined && price_product[prod_id][sh][dl]!=undefined) pr = 1*price_product[prod_id][sh][dl];
	document.getElementById("value_price["+prod_id+"]").value    = Math.round(pr*(100-ds)/100);
	document.getElementById("text_price["+prod_id+"]").innerHTML = pr + " руб.";
	if (ds>0)
	document.getElementById("text_price_discount["+prod_id+"]").innerHTML = Math.round(pr*(100-ds)/100) + " руб.";
}

function buy(prod_id, clr, type_value, firm_value, group_value)
{
	if (clr==1)
	{
		alert("Укажите цвет и размер и нажмите кнопку «Купить».");
		document.location.href="matras.php?type="+type_value+"&firm="+firm_value+"&group="+group_value+"&product="+prod_id;
	}
	else
	{
		if (document.getElementById("value_price["+prod_id+"]").value<=0)
		{
			alert("К сожалению Вы не можете сейчас купить данный товар, т.к. для него не задана цена или заказ данного товара невозможен в данных размерах. Сообщите нам об этом по телефону.");
			return;
		}

		sh = 0;
		dl = 0;
		if (document.getElementById("shirina["+prod_id+"]")) sh = document.getElementById("shirina["+prod_id+"]").value;
		if (document.getElementById("dlina["+prod_id+"]"))   dl = document.getElementById("dlina["+prod_id+"]").value;
		document.location.href="basket.php?product_id=" + prod_id + "&shirina=" + sh + "&dlina=" + dl;
	}
}

function compare(type,id)
{
	var width  = 800;
	var height = 600;
	var left   = 20;
	var top    = 20;

	if (self.screen)
	{
		width  = screen.width  - 120;
        height = screen.height - 150;
	}

	windowName = type;
	if (windowName=='podstavka') windowName = 'krovat';
	win = window.open('compare.php?type=' + type + '&product=' + id, windowName, 'width=' + width + ',height=' + height + ', left=' + left + ', top=' + top + ', toolbar=0, location=0, directories=0, menubar=0, scrollbars=1, resizable=1, status=1, fullscreen=0');
	win.focus();
	return;
}

function setPriceFromCompare()
{	for(var i=0; a=document.getElementsByTagName("input")[i]; i++)
	{		position1 = a.name.indexOf("[");
		position2 = a.name.indexOf("]");
		name      = a.name.substring(0,position1);
		if (name!="value_price") continue;
		prod_id   = a.name.substring(position1+1,position2);
		pr = 0;
		ds = document.getElementById("value_discount["+prod_id+"]").value;
		sh = document.getElementById("shirina").value;
		dl = document.getElementById("dlina").value;
		if (price_product[prod_id]!=undefined)
			if (price_product[prod_id][sh]!=undefined)
				if (price_product[prod_id][sh][dl]==undefined) dl=200;
		if (price_product[prod_id]!=undefined && price_product[prod_id][sh]!=undefined && price_product[prod_id][sh][dl]!=undefined) pr = 1*price_product[prod_id][sh][dl];
		document.getElementById("value_price["+prod_id+"]").value    = Math.round(pr*(100-ds)/100);
		document.getElementById("text_price["+prod_id+"]").innerHTML = pr + " р.";
		if (ds>0) document.getElementById("text_price_discount["+prod_id+"]").innerHTML = Math.round(pr*(100-ds)/100) + " р.";
	}
}

function buyFromProduct()
{
	if (document.getElementById("value_price").value<=0)
	{
		alert("К сожалению Вы не можете сейчас купить данный товар, т.к. для него не задана цена или данный товар невозможен в данных размерах. Сообщите нам об этом, пожалуйста, на форуме.");
		return;
	}
	document.getElementById("form").submit();
}

function choose_tkan()
{
	var myform  = document.getElementById("form");
	var value = 1;
	for(i = 0; i<myform['tkan'].length; i++)
	{
		if(myform['tkan'][i].checked)
		{
			value = myform['tkan'][i].value;
			break;
		}
	}
	return value;
}

function setPriceFromProduct(type, st_tkan, tkan_replace, size, nestandartSize)
{	pr = 0;

	if ((type=="matras" || type=="namatrasnik" || type=="podstavka" || type=="krovat") && nestandartSize==0)
	{		document.getElementById("nest_shirina").style.display = "none";
		document.getElementById("nest_dlina").style.display   = "none";

		sh = document.getElementById("shirina").value;
		dl = document.getElementById("dlina").value;

		if (document.getElementById("shirina").value==0)
		{
			document.getElementById("nest_shirina").style.display="";
			if (document.getElementById("nest_shirina").value==0) {document.getElementById("nest_shirina").value=80};
			sh = document.getElementById("nest_shirina").value;
		}
		else document.getElementById("nest_shirina").value=0;

		if (document.getElementById("dlina").value==0)
		{
			document.getElementById("nest_dlina").style.display="";
			if (document.getElementById("nest_dlina").value==0) {document.getElementById("nest_dlina").value=200};
			dl = document.getElementById("nest_dlina").value;
		}
		else document.getElementById("nest_dlina").value=0;

		Nestandart = 1;
		if (dl!=190 && dl!=195 && dl!=200)
		{
			if (type=="podstavka" || type=="krovat") Nestandart = 1.3;
			else                                     Nestandart = 1.1;
		}
		if (sh!=70 && sh!=80 && sh!=90 && sh!=120 && sh!=140 && sh!=160 && sh!=180 && sh!=200)
		{
			if (type=="podstavka" || type=="krovat") Nestandart = 1.3;
			else                                     Nestandart = 1.1;
		}

		if (sh<=70) sh=70;
		else if (sh<=80) sh=80;
		else if (sh<=90) sh=90;
		else if (sh<=120) sh=120;
		else if (sh<=140) sh=140;
		else if (sh<=160) sh=160;
		else if (sh<=180) sh=180;
		else         sh=200;

		ZamenaTkani = 0;
		DopTkan     = 0;

		if (tkan_replace>0)
		{			tk = choose_tkan();
			if (st_tkan!=tk)
			{
				if (price_tkan[tk]!=undefined &&  price_tkan[tk][st_tkan]!=undefined &&  price_tkan[tk][st_tkan][sh]!=undefined) ZamenaTkani = 1*price_tkan[tk][st_tkan][sh];
			}

			if (type=="matras")
			{
				for(sch_dop=1; sch_dop<=kol_dop; sch_dop++)
				{
					dop_tk = document.getElementById("dop_tkan"+sch_dop).value;
					if (document.getElementById("dop_tkan"+sch_dop).checked==true)
					{						if (price_tkan[dop_tk]!=undefined && price_tkan[dop_tk][tk]!=undefined && price_tkan[dop_tk][tk][sh]!=undefined) DopTkan = DopTkan + 1*price_tkan[dop_tk][tk][sh];
					}
				}
			}
		}

		if (price_product[sh]!=undefined)
		{
			if (price_product[sh][dl]==undefined) dl=200;
			if (price_product[sh][dl]!=undefined) pr = (1*price_product[sh][dl]+1*ZamenaTkani+1*DopTkan)*Nestandart;
		}
	}
	else if (((type=="shkaf" || type=="podushka") && size==1) || ((type=="matras" || type=="namatrasnik" || type=="podstavka" || type=="krovat") && nestandartSize==1))
	{
		sh = 0;
		dl = 0;
		if (document.getElementById("shirina")) sh = document.getElementById("shirina").value;
		if (document.getElementById("dlina"))   dl = document.getElementById("dlina").value;
		if (price_product[sh]!=undefined && price_product[sh][dl]!=undefined) pr = price_product[sh][dl];
	}
	else
	{
		pr = price_product[0][0];
	}

	pr = Math.round(pr);

	ds = document.getElementById("value_discount").value;
	document.getElementById("value_price").value    = Math.round(pr*(100-ds)/100);
	document.getElementById("text_price").innerHTML = pr + " руб.";

	if (ds>0) document.getElementById("text_price_discount").innerHTML = Math.round(pr*(100-ds)/100) + " руб.";
}

function changeRow(script, nstr)
{
	document.location.href= script + "?changeRow=" + nstr + "&newKolvo=" + document.getElementById("kolvo["+nstr+"]").value;
}
