function getCookie(name) {
 var cookie = " " + document.cookie;
 var search = " " + name + "=";
 var setStr = null;
 var offset = 0;
 var end = 0;
 if (cookie.length > 0) {
 offset = cookie.indexOf(search);
 if (offset != -1) {
 offset += search.length;
 end = cookie.indexOf(";", offset)
 if (end == -1) {
 end = cookie.length;
 }
 setStr = unescape(cookie.substring(offset, end));
 }
 }
 return(setStr);
}

function setCookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function put2basket() {
 var goodUrl=String(document.location).split('#')[0].split('?')[0];
 goodUrl=goodUrl.replace(/www./,"");
 if ( goodTitle ) { var goodName=goodTitle }
 else {
     var goodName=document.getElementsByTagName("h1")[0].innerHTML;
     goodName=goodName.replace(/<.*?>/g, '');
 }
 var i = 1;
 var chk = false;
 var goodsUrls = getCookie("goodUrl1");
 if ( goodsUrls == "none" ) goodsUrls=false;

 while (goodsUrls) {
        if ( goodsUrls == goodUrl ) {
                goodsUrls=false;
                chk=true;
        };
        i++;
        goodsUrls=getCookie("goodUrl"+i);
	if ( goodsUrls=="none") goodsUrls=false;
 }
 //alert(document.title);

 if ( chk ) {if ( confirm("Этот товар уже есть в корзине. Перейти к оформлению заказа?") ) window.location.href='basket.html';}
 else {
        setCookie("goodUrl"+i,goodUrl,7);
        setCookie("goodName"+i,goodName,7);
        //setCookie("price"+i,price,7);
        if ( confirm("Товар добавлен в корзину. Перейти к оформлению заказа?") ) window.location.href='basket.html';
 }
}

//<div style="top: 50%; left:50%; margin:-100px -100px 0 0; width: 200px; height:200px; position:fixed">Это див!!!</div> 

function delGood(n) {
 if ( confirm("Отказаться от товара "+getCookie("goodName"+n)+"?") ) {
 	setCookie("goodUrl"+n,"none",7);
	var j=n;
	var j1=n+1;
 	var nextUrl = getCookie("goodUrl"+j1);
	if ( nextUrl == "none" ) nextUrl=false;
	while (nextUrl) {
		setCookie("price"+j,getCookie("price"+j1),7);
		setCookie("goodName"+j,getCookie("goodName"+j1),7);
		setCookie("quantity"+j,getCookie("quantity"+j1),7);
		setCookie("goodUrl"+j,getCookie("goodUrl"+j1),7);
		j++;
		j1++;
 		nextUrl = getCookie("goodUrl"+j1);
		if ( nextUrl == "none" ) nextUrl=false;
 	}
	setCookie("goodUrl"+j,"none",7);
	setCookie("quantity"+j,"1",7);
 	window.location.reload();
 }
}

var badprice = '<a title="Стоимость товара зависит от комплектации и опций.">Будет уточнено.</a>';

function totalPrice () {

 var goodUrl = getCookie("goodUrl1");
 if ( goodUrl == "none" ) goodUrl=false;

  var i = 1;
  var chkBad = false;
  totPrice=0;

  while (goodUrl) {
	thediv=document.getElementById("res"+i).innerHTML;
        if ( thediv == "" ) thediv="no";
        totPrice+=thediv*1;
	i++;
 	goodUrl = getCookie("goodUrl"+i);
 	if ( goodUrl == "none" ) goodUrl=false;
  }
  if ( totPrice > 0 ) return (totPrice);
  else return (false);
        
}

function setQuantity(n) {
        var quantity=document.forms[0].elements[n].value;
	if ( quantity == "0" ) {
		document.forms[0].elements[n].value=1;
		quantity = 1;
	};
        var price=getCookie("price"+n);
        resultPrice=price*quantity+1;
        if ( resultPrice ) {
                setCookie("quantity"+n,quantity,7);
                if ( price != 0 ) {
                        thediv=document.getElementById("res"+n);
                        thediv.innerHTML=price*quantity;
                        var totPrice=totalPrice();
                        if ( totPrice ) {
                                totPrDiv=document.getElementById("totalPrice");
                                totPrDiv.innerHTML="<b>"+totPrice+"</b>";
                        }

                }
        } 
        else document.forms[0].elements[n].value=getCookie("quantity"+n);
}

function checkForm() {
	var content="";
	//.indexOf('a');
	var i=1;
	var goodsUrls = getCookie("goodUrl1");
	if ( goodsUrls == "none") goodsUrls=false;
	while (goodsUrls) {
		price=getCookie("price"+i);
                goodName=getCookie("goodName"+i);
                quantity=getCookie("quantity"+i);
		totprice=price*quantity;
		content=content+goodName+", "+goodsUrls+" , цена за шт. "+price+", кол-во "+quantity+", общая стоимость "+totprice+"\n"
		i++;
		goodsUrls = getCookie("goodUrl"+i);
		if ( goodsUrls=="none") goodsUrls=false;
	}
	document.forms[0].elements[0].value=content;
	email=document.forms[0].elements[i+4].value;
	indDog=email.indexOf('@');
	chk=true;
	if (document.forms[0].elements[i+1].value == "") {chk=false; alert("Пожалуйста, представьтесь (укажите Ваше имя)");}
	if (document.forms[0].elements[i+3].value == "") {chk=false; alert("Пожалуйста, укажите Ваш контактный телефон");}
	if (( email != "" ) && (( indDog == -1 ) || (email.indexOf('.') == -1 ))) {chk=false; alert("email указан неверно");}
	if (content == "") chk=false;
	return chk;
}


function basket() {

 var goodsUrls = getCookie("goodUrl1");
 if ( goodsUrls=="none") goodsUrls=false;

 if (goodsUrls) {

        warningDiv = document.getElementById("warning");
        warningDiv.style.visibility = "hidden";
        warningDiv.style.height = "1px";
 
        var price = [];
        var goodName = [];
        var goodUrl = [];
        var quantity = [];      
        var i = 1;
        var chk = false;
        //var chk0 = true;
        var chkBad = false;

        while (goodsUrls) {
                goodUrl[i]=goodsUrls;
                price[i]=getPrice(goodsUrls);
                setCookie("price"+i,price[i],7);
                goodName[i]=getCookie("goodName"+i);
                quantity[i]=getCookie("quantity"+i);
                if ( ! quantity[i] ) {
                        quantity[i]="1";
                        setCookie("quantity"+i,"1",7);
                        //alert ("it works!");
                }
                if ( price[i] == "0" ) { price[i] = badprice; chkBad = true;}
                else chk = true;
		//chk0=false;
                i++;
                goodsUrls=getCookie("goodUrl"+i);
		if ( goodsUrls=="none") goodsUrls=false;
        }

        //alert ("it works!");

        //if ( ! chk0 ) {
	if ( chk ) {
                document.write ('<h1>Ваш заказ</h1><table width="100%"><form method="post" action="send.php" name="basketForm" onSubmit="return checkForm()"><input name="basketContent" type="hidden" value=""><tr><td width="45%"><b>Товар</b></td><td width="18%"><b>Цена</b> (руб.)</td><td width="14%"><b>Кол-во</b></td><td width="18%"><b>Итого</b> (руб.)</td><td> </td></tr>');
                for (j=1;j<i;j++) {
                        
                        prq=price[j]*quantity[j];
                        if ( ! prq ) prq="";
        
                        document.write('<tr><td><a href="'+goodUrl[j]+'" target="_blank"><font color="#833828">'+goodName[j]+'</font></a></td><td>'+price[j]+'</td><td><input type="text" size="1" maxlength="3" name="n'+j+' id="n'+j+'" value="'+quantity[j]+'" onChange="setQuantity(\''+j+'\')"></td><td><div id="res'+j+'">'+prq+'</div></td><td><div align="right"><a href="javascript://" rel="nofollow" onclick="delGood('+j+');return false;"><img src="cart_remove.png" title="Удалить товар" border=0></a></div></td></tr>'); 
                        //document.forms[0].elements[j-1].onChange="setQuantity("+j+")";
                        //setQuantity(j);
                        //alert(quantity[j]);
                }

                var totPrice=totalPrice();
                if ( totPrice ) document.write('<tr><td><b>Итого</b>:</td><td></td><td></td><td><div id="totalPrice"><b>'+totPrice+'</b></div></td></tr>');

        }
        else {
                document.write ('<h1>Ваш заказ</h1><table width="80%"><tr><td width="60%"><b>Товар</b></td><td width="20%"><b>Количество</b></td><td> </td></tr><form method="post" name="basketForm" action="send.php" onSubmit="return checkForm()"><input name="basketContent" type="hidden" value="">');
                for (j=1;j<i;j++) /*if ( goodUrl[j] != "none" )*/ {
                        document.write('<tr><td><a href="'+goodUrl[j]+'" target="_blank"><font color="#833828">'+goodName[j]+'</font></a></td><td><input type="text" size="1" maxlength="3" name="n'+j+'" id="n'+j+'" value="'+quantity[j]+'" onChange="setQuantity('+j+')"></td><td><div align="right"><a href="javascript://" rel="nofollow" onclick="delGood('+j+');return false;"><img src="cart_remove.png" title="Удалить товар" border=0></a></div></td></tr>');
                        //document.getElementById.("n"+j).onChange='setQuantity("'+j+'");';
                }
        }

        document.write ('</table><h3>Информация о покупателе</h3> <table width="96%"><tr><td width="100">Фамилия:</td><td><input type="text" size="30" name="family"></td></tr><tr><td> Имя<sup>*</sup>: </td><td><input type="text" size="30" name="name"> </td></tr><tr><td> Отчество:</td><td><input type="text" size="30" name="sname"></td></tr><tr><td>Контактный телефон<sup>*</sup>:</td> <td><input type="text" size="30" name="phone"></td></tr><tr><td> Электронная почта:</td> <td><input type="text" size="30" name="email"></td></tr><tr><td>Адрес доставки:</td><td><textarea name="address" rows="5" cols="33"></textarea></td></tr></table><p><input type="submit" value="Отправить заказ"></p></form><p><sup>*</sup>Поля, обязательные для заполнения.</p>');
	//}
	//else document.write('<big>Ваша корзина пуста.</big>');
        
 }
 else document.write('<big>Ваша корзина пуста.</big>');
}

function checkQuantity () {

 var goodUrl = getCookie("goodUrl1");
 if ( goodUrl == "none") goodsUrl=false;
 var i = 1;

 while (goodUrl) {	
	quantity=getCookie ("quantity"+i);
 	if ( quantity != document.forms[0].elements[i].value ) document.forms[0].elements[i].value=quantity;
	i++;
	goodUrl = getCookie("goodUrl"+i);
	if ( goodUrl=="none") goodUrl=false;
 }
        
}

function showButtons() {
	document.write('<div style="padding-top:0px;padding-left:0px;text-align:left;"><a href="javascript://" onclick="window.open(\'sendtel.html\',\'pmw\',\'scrollbars=0,top=0,left=0,resizable=1,width=365,height=382\'); return false;" title="Заказать обратный звонок"><img src="pic/6693879_5.png" alt="Заказать обратный звонок" height="88" width="260" border="0"></a></div><div style="padding-top:0px;padding-left:70px;text-align:left"><a href="basket.html"><img src="pic/i_3.png" height="30" width="40" border="0" alt="Ваш заказ" title="Ваш заказ"></a> <a href="div_sklad.html"><img src="pic/i_7.png" height="30" width="40" border="0" title="купить диван"></a> <a href="div_sklad2.html"><img src="pic/i_5.png" height="30" width="40" border="0" title="акции и распродажи"></a> <a href="in_div_tkani.html"><img src="pic/i_2.png" height="30" width="40" border="0" title="ткани для обивки диванов"></a></div>');
}

function addButton() {
	document.write('<div align="center"><br /><a href="javascript://" rel="nofollow" onClick="put2basket();return false;"><img src="img_vira/korzin1.png" width="474" height="27" border="0" alt="Положить в корзину" title="Добавить товар в корзину" /></a></div>');
}

function getPrice(address) {
	var addrArr=address.split("/");
	addr=addrArr[addrArr.length-1];
	var price1;
	switch(addr) {

	case "div_krov_f_omega.html":
		price1=25200
	break
	case "div_krov_f_akatcia.html":
		price1=29200
	break
	case "div_krov_f_galilea.html":
		price1=29200
	break
	case "div_krov_f_azalia_2.html":
		price1=35800
	break
	case "divan_kn_f_tais.html":
		price1=18800
	break
	case "divan_kn_f_titan.html":
		price1=18800
	break
	case "divan_kn_f_vanil.html":
		price1=19600
	break
	case "divan_kn_f_mustang.html":
		price1=19100
	break
	case "divan_kn_f_diona.html":
		price1=20000
	break
	case "divan_kn_f_san_remo.html":
		price1=21900
	break
	case "divan_kn_f_atlas.html":
		price1=18500
	break
	case "div_accord_f_ampir.html":
		price1=19800
	break
	case "div_accord_f_ariel.html":
		price1=20000
	break
	case "div_accord_f_avgyst.html":
		price1=17900
	break
	case "div_accord_f_neo.html":
		price1=19900
	break
	case "div_accord_f_vegas.html":
		price1=21000
	break
	case "div_accord_f_kristal.html":
		price1=17600
	break
	case "div_accord_f_astra.html":
		price1=18600
	break
	case "div_accord_f_glamur.html":
		price1=20500
	break
	case "div_accord_f_dyet.html":
		price1=19300
	break
	case "div_accord_f_zhasmin.html":
		price1=20200
	break
	case "div_accord_f_brest.html":
		price1=29100
	break
	case "div_accord_f_jeneva.html":
		price1=30500
	break
	case "div_clik_f_blesk.html":
		price1=19700
	break
	case "div_clik_f_vesna.html":
		price1=20000
	break
	case "divan_v_f_san-remo.html":
		price1=27400
	break
	case "divan_v_f_diona.html":
		price1=17100
	break
	case "divan_v_f_miranda.html":
		price1=18400
	break
	case "div_ugol_f_mira.html":
		price1=35500
	break
	case "div_ugol_f_diona.html":
		price1=26200
	break
	case "div_ugol_f_nodus.html":
		price1=61300
	break
	case "div_ugol_f_vanil.html":
		price1=22000
	break
	case "div_ugol_f_glamyr.html":
		price1=32400
	break
	case "div_ugol_f_san-remo.html":
		price1=39800
	break
	case "div_ugol_f_kalisto.html":
		price1=33600
	break
	case "div_ugol_f_agat.html":
		price1=24800
	break
	case "div_ugol_f_nimfa.html":
		price1=35600
	break
	case "div_ugol_f_sirius.html":
		price1=36000
	break
	case "div_ugol_f_oberon.html":
		price1=59100
	break
	case "div_ugol_f_adajio.html":
		price1=37100
	break
	case "div_koja_f_sirius.html":
		price1=42700
	break
	case "divan_raskl_f_oberon.html":
		price1=43700
	break
	case "div_koja_f_mira.html":
		price1=47800
	break
	case "divan_raskl_f_nodus.html":
		price1=44050
	break
	case "divan_raskl_f_viola.html":
		price1=21200
	break
	case "divan_raskl_f_akva.html":
		price1=27000
	break
	case "divan_raskl_f_persey.html":
		price1=44050
	break
	case "div_ugol_nm_moris_st.html":
		price1=31300
	break
	case "div_ugol_nm_moris_dek.html":
		price1=32000
	break
	case "div_ugol_nm_moris.html":
		price1=27950
	break
	case "div_ugol_nm_moris_lux.html":
		price1=35270
	break
	case "div_ugol_nm_moris_lux_pol.html":
		price1=39790
	break
	case "div_ugol_nm_lion_2.html":
		price1=24650
	break
	case "divan_v_nm_bristol.html":
		price1=33550
	break
	case "divan_kn_nm_debor.html":
		price1=25200
	break
	case "divan_kn_nm_finiks.html":
		price1=22400
	break
	case "div_accord_nm_luiz.html":
		price1=15650
	break
	case "divan_kn_nm_elizabet.html":
		price1=18650
	break
	case "divan_kn_a_adonis.html":
		price1=25000
	break
	case "divan_v_a_robin.html":
		price1=27800
	break
	case "div_ugol_ol_olimp.html":
		price1=41200
	break
	case "div_krov_t_995.html":
		price1=56600
	break
	case "div_krov_t_1020.html":
		price1=52650
	break
	case "div_krov_t_1031.html":
		price1=64110
	break
	case "div_krov_t_1041.html":
		price1=53450
	break
	case "div_krov_t_1007.html":
		price1=57700
	break
	case "div_krov_t_1077.html":
		price1=54650
	break
	case "div_krov_t_1100-2.html":
		price1=56750
	break
	case "div_krov_t_1101.html":
		price1=59450
	break
	case "div_krov_t_925.html":
		price1=58750
	break
	case "div_krov_t_980.html":
		price1=56500
	break
	case "div_krov_t_1033.html":
		price1=91650
	break
	case "div_accord_ml_adel1.html":
		price1=25300
	break
	case "div_accord_ml_adel2.html":
		price1=29400
	break
	case "div_accord_ml_adel4.html":
		price1=26500
	break
	case "divan_raskl_ml_ital.html":
		price1=32100
	break
	case "divan_raskl_ml_floren.html":
		price1=27500
	break
	case "divan_raskl_ml_lada.html":
		price1=25000
	break
	case "divan_raskl_ml_prezident.html":
		price1=32300
	break
	case "div_ugol_ml_konsul.html":
		price1=37800
	break
	case "div_ugol_ml_kaskad1.html":
		price1=29000
	break
	case "div_ugol_ml_olimp.html":
		price1=32900
	break
	case "div_ugol_ol_bruklin.html":
		price1=26300
	break
	case "div_ugol_ml_uyut_2.html":
		price1=23000
	break
	case "div_ugol_mkf_monplezir.html":
		price1=56900
	break
	case "div_ugol_ai_keln.html":
		price1=26210
	break
	default:
		price1=0
	}
	return price1;
}

