function SetCookie (name, value) {
	var largeExpDate = new Date ();
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));
	var expires = largeExpDate;
	document.cookie = name + "=" + escape (value) +"; expires=" + expires.toGMTString() +  "; path=/";
}


function getCookie(name) {
	if(document.cookie == "") return false;
	else {
		var cookieStart, cookieEnd;
		var cookieString = document.cookie;
		cookieStart = cookieString.indexOf(name+"=");
		if(cookieStart != -1) {
			cookieStart += name.length+1;
			cookieEnd = cookieString.indexOf(";", cookieStart);
			if(cookieEnd == -1) cookieEnd = cookieString.length;
			return cookieString.substring(cookieStart, cookieEnd);
		} else {
			return false;
		}
	}
}

function getshopingcart() {
	var rows = new Array();
	var shopingcart = unescape(getCookie("shopingcart"));
	if(shopingcart) {
	rows = shopingcart.split(" ");
	}
	return rows.length;
}

function emptyshopingcart() {
	SetCookie("shopingcart", "");
      SetCookie("tsumtnum","");
	return false;
}

function removeposition(id) {
	var rows = new Array();
	var shopingcart = unescape(getCookie("shopingcart"));
	var newcartstring = "";
	if(shopingcart) {
		rows = shopingcart.split(" ");
		for(i=0; i<rows.length; i++){
			if(rows[i]!=id && rows[i]!='') {
				newcartstring = newcartstring + rows[i]+" ";
			}
		}
		
	}
	if(newcartstring.length) newcartstring = newcartstring.substr(0, newcartstring.length-1);
	SetCookie("shopingcart", newcartstring);
}

function toshopingcart(id) {
	var rows = new Array();
	var shopingcart = unescape(getCookie("shopingcart"));
	
	var flag_avaible = false;
	if(shopingcart) {
		rows = shopingcart.split(" ");
		for(i=0; i<rows.length; i++){
			TryFlag=rows[i].split("|");
			if(TryFlag[0]==id) {
				flag_avaible = true;
			}
		}
		
	}
	
	if(flag_avaible==false) {
		
		if(rows && rows[0]!="false") {
			if (document.getElementById("num"+id).value!="0"){
				rows[rows.length]=id+"|"+document.getElementById("num"+id).value;
			}
			else 
			{
				rows[rows.length]=id+"|"+"1";	
				document.getElementById("num"+id).value="1";
			}
		} 
		else
		{
			if (document.getElementById("num"+id).value!="0"){
				rows[0]=id+"|"+document.getElementById("num"+id).value;
			}
			else 
			{
				rows[0]=id+"|"+"1";	
				document.getElementById("num"+id).value="1";
			}
		}
		SetCookie("shopingcart", rows.join(" "));
	}
	if(flag_avaible==false) {
		alert("Товар добавлен в корзину");
		tsum_tnum_update(id);
	}
	else alert('Вы уже добавили этот товар в корзину. Для изменения количества или удаления товара перейдите в корзину');
}
	
	
	
	
function changeshopingcart() {
	var rows = new Array();
	var shopingcart = unescape(getCookie("shopingcart"));
	var flag_avaible = false;
		
	if(shopingcart) {
		rows = shopingcart.split(" ");
		for(i=0; i<rows.length; i++){
			TryFlag=rows[i].split("|");
			if(document.getElementById("num"+TryFlag[0])!=null){
				if(document.getElementById("num"+TryFlag[0]).value=="0"){
					rows[i]=" ";
				}
				else{
            		TryFlag[1]=document.getElementById("num"+TryFlag[0]).value;
					rows[i]=TryFlag.join("|");
					
				}
			}
		}
	
	}
	replc="~ +~";
	replstr=rows.join(" ");
	finalstr=replstr.replace(replc, " ");
		SetCookie("shopingcart", finalstr);
		
//	else alert('Вы уже добавили этот товар в корзину. Для изменения количества или удаления товара перейдите в корзину');
}
function delgood(idnew)
{
document.getElementById("num"+idnew).value="0";
changeshopingcart();
window.location.reload();
}

function changecat() {
	var rows = new Array();
	var shopingcart = unescape(getCookie("shopingcart"));		
	 if(shopingcart) {
		rows = shopingcart.split(" ");
		 for(i=0; i<rows.length; i++){
			 TryFlag=rows[i].split("|");
if (TryFlag[0]!=null){		
if(document.getElementById("num"+TryFlag[0])!=null)document.getElementById("num"+TryFlag[0]).value=TryFlag[1];
}
		}
	
	}
}

function tsum_tnum_load(){
	var rows = new Array();
	var tsumtnum = unescape(getCookie("tsumtnum"));
	if(tsumtnum)
	{
		rows = tsumtnum.split("|");
		if (rows[0]=='false')
		{
		SetCookie("tsumtnum", "0|0");
		tsumtnum="0|0";
		}
	}
	else {
	tsumtnum="0|0";
	SetCookie("tsumtnum", "0|0");
	}
rows = tsumtnum.split("|");
document.getElementById('tnum').innerText=rows[0];
document.getElementById('tsum').innerText=rows[1];
}

function tsum_tnum_update(id)
{
	
	var tsumtnum = unescape(getCookie("tsumtnum"));
	if(tsumtnum)
	{
		rows = tsumtnum.split("|");
		rows[0]=rows[0]*1+document.getElementById('num'+id).value*1;
		document.getElementById('tnum').innerText=rows[0];
		roun=rows[1]*1+document.getElementById('price'+id).innerText*1*document.getElementById('num'+id).value;
		rows[1]= Math.round(100*roun)/100;
document.getElementById('tsum').innerText=rows[1];
		SetCookie("tsumtnum", rows.join("|"));
	}
}
function tsum_tnum_refresh(sm)
{
	var rows = new Array();
	var tsumtnum = unescape(getCookie("tsumtnum"));
	var rows = new Array();
	var shopingcart = unescape(getCookie("shopingcart"));
	
	if(tsumtnum)
	{
		rows = tsumtnum.split("|");
		if (rows[0]=='false')
		{
		SetCookie("tsumtnum", "0|0");
		tsumtnum="0|0";
		}
	}
	else {
	tsumtnum="0|0";
	SetCookie("tsumtnum", "0|0");
	}

		nm=0;
	if(shopingcart) {
		rows = shopingcart.split(" ");
		for(i=0; i<rows.length; i++){
			pl=rows[i].split("|");
			//window.alert(pl[1]);
			if (pl[1]!=null) nm=nm+pl[1]*1;
		}
	
rows = tsumtnum.split("|");
		rows[0]=nm;
		rows[1]=sm;
document.getElementById('tnum').innerText=rows[0];
document.getElementById('tsum').innerText=rows[1];
		SetCookie("tsumtnum", rows.join("|"));
	}
}


