



// B----- working with cookies -----
function setCookie(cookieName, cookieContent, cookieExpireTime){
    if(cookieExpireTime>0){
        var expDate=new Date()
        expDate.setTime(expDate.getTime()+cookieExpireTime*1000*60*60)
        var expires=expDate.toGMTString()
//        document.cookie=cookieName+"="+escape(cookieContent)+"; path="+escape('/')+"; expires="+expires
        document.cookie=cookieName+"="+cookieContent+"; path="+escape('/')+"; expires="+expires
    }else{
//        document.cookie=cookieName+"="+escape(cookieContent)+"; path="+escape('/')+"";
        document.cookie=cookieName+"="+cookieContent+"; path="+escape('/')+"";
    }
}

function getCookie(cookieName){
    var ourCookie=document.cookie
    if(!ourCookie || ourCookie=="")return ""
    ourCookie=ourCookie.split(";")
    var i=0
    var Cookie
    while(i<ourCookie.length){
        Cookie=ourCookie[i].split("=")[0]
        if(Cookie.charAt(0)==" ")
                Cookie=Cookie.substring(1)
        if(Cookie==cookieName){
            return unescape(ourCookie[i].split("=")[1])
        }
        i++
    }
    return ""
}

// E----- working with cookies -----





// This function shows the shopping cart table in the shopping cart page.

function Cart () {
//	document.writeln("<br>"+document.cookie+"<br>");
	document.writeln ('<div class=price><br><table cellpadding=0 cellspacing=1 width=95% align=center><tr class=head><td>Товар</td><td>Код и наименование</td><td>Розничная цена</td><td>Кол-во</td><td>Удалить</td></tr>');
	
	position = 0;
	
	index = document.cookie.indexOf ("Cart");
	cookie_begin = (document.cookie.indexOf ("=", index) + 1);
	cookie_end = document.cookie.indexOf (";", index);
	
	if (cookie_end == -1) cookie_end = document.cookie.length;
	
	if (cookie_end <= 7) document.writeln ('<TR><TD colspan=5 align=center>Вы еще не выбрали товар для заказа!</TD></TR>');
	
	cookie = getCookie("Cart");
	
	for (var i = 0; i <= cookie.length; i++) {
		
		if (cookie.substring (i, i+1) == '[') {
			position_begin = i+1;
			} else if (cookie.substring (i, i+1) == ']') {
			price1 = (eval (cookie.substring (position_begin, i)));
			position_end = i;
			position = position+1;
			
			document.writeln ('<tr><td rowspan=2><img src=img/'+code+'.jpg width=175 height=80></td><TD align=center height=15>'+code+'<input type=hidden value="'+code+'" name=code['+position+']></TD><TD align="center" width=60 rowspan=2>'+price1+'<input type=hidden value="'+price1+'" name=price['+position+']></TD><TD width=60 rowspan=2 align=center><input type=text size=6 name=quant['+position+']></TD><td rowspan=2><A href="javascript:Remove ('+position+')"><img src=images/nocart.gif border=0 alt="Удалить позицию"></A></td></tr><tr><TD valign=top>'+name+'<input type=hidden value="'+name+'" name=name['+position+']></TD></TR>');
			
			} else if (cookie.substring (i, i+1) == '|') {
			code = cookie.substring (position_begin, i);
			position_begin = i+1;
			} else if (cookie.substring (i, i+1) == '#') {
			name = cookie.substring (position_begin, i);
			position_begin = i+1;
		}
		
	}

	
	document.writeln ('<input type=hidden name=totalpos value='+position+'></TABLE></div>');

}


// This function adds a code to the shopping cart.
//--- old
function Add_o (code, name, price1) { 
	index = this.document.cookie.indexOf ("Cart");
	cookie_begin = (this.document.cookie.indexOf ("=", index) + 1);
	cookie_end = this.document.cookie.indexOf (";", index);
	
	if (cookie_end == -1) cookie_end = this.document.cookie.length;
	var tmp_ = this.document.cookie.substring (cookie_begin, cookie_end)+"["+code+"|"+name+"#"+price1+"]";
	//this.document.cookie = "test=1;test1=2;Carts="+tmp_;
	this.document.cookie = "Cart="+escape(tmp_)+"; expires=Sat, 10 May 2008 11:21:13 GMT";
	//this.document.cookie = "Cart=zzzzzzz; expires=Sat, 10 May 2008 11:21:13 GMT";
//	alert("Cookie="+this.document.cookie);
//	alert("tmp_="+tmp_);
//	history.go(-1);
//(new Date((new Date()).getTime() + hours*3600000)).toGMTString()
	
}

function Add (code, name, price1) {
	var m_cookie = getCookie("Cart");
	m_cookie += "["+code+"|"+name+"#"+price1+"]";
	setCookie("Cart",m_cookie,0);
}

// This function adds a code to the shopping cart.
//--- new
/*
function Add (code, cat_name) { 
	index = document.cookie.indexOf ("Cart");
	cookie_begin = (document.cookie.indexOf ("=", index) + 1);
	cookie_end = document.cookie.indexOf (";", index);
	
	if (cookie_end == -1) cookie_end = document.cookie.length;
	
	document.cookie = "Cart="+document.cookie.substring (cookie_begin, cookie_end)+"["+code+"|"+cat_name+"]";
//	history.go(-1);
}
*/

// This function removes a code from the shopping cart.

function Remove (remove_position) {

	var headposs;
	var m_head;
	headposs= document.cookie.indexOf ("Cart");
//alert(remove_position);	
	newcart = null;
	if (headposs>0)
		m_head = document.cookie.substring(0,headposs);
	else
		m_head = null;
//alert(document.cookie);
//alert(cookie);
//alert(document.cookie.substring(0,headposs));

	position = 0;
	
	for (var i = 0; i <= cookie.length; i++) {
		
		if (cookie.substring (i, i+1) == '[') {
			position_begin = i+1;
//			alert("pos_beg "+position_begin);
		} 
		else{
			if (cookie.substring (i, i+1) == ']') {
				position_end = i;
//				alert("pos_end "+position_end);
				position = position + 1;
//				alert("pos "+position);
				if (position != remove_position){
					 newcart = newcart+'['+cookie.substring (position_begin, position_end)+']';
//				         alert(newcart);
				}
			}
		}
	}
	
//	alert(m_head+"Cart="+newcart);
	setCookie("Cart",-1,0);
	setCookie("Cart",newcart);
//	document.cookie=m_head+"Cart="+newcart;
//	alert(document.cookie);
	document.location = "blank.html";
	
}


// This function fixes the price1

function Correct (price1) {
	
	if (price1 <= 0.99) {
		left = '0';
		} else {
		left = parseInt (price1);
	}
	
	right = parseInt ((price1+.0008-left)*100);
	if (eval (right) <= 9) right='0'+right;
	
	price1 = left + '.' + right;
	
	return (price1);
	
}

