var cookie_name='optina_lavka';
var path="path=/;";
var NaN = 0;

function addCookie (sName, sValue) {
   expiredays = 1;  
   var todayDate = new Date();
   todayDate.setTime(todayDate.getTime() + expiredays * 24 * 60 *60 *1000);
   document.cookie = sName + "=" + sValue + "; expires=" +todayDate.toGMTString() + ";" + path;
} 

function readCookie (sName)  
{
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
	var aCrumb = aCookie[i].split("=");
	if (sName == aCrumb[0]) 
	return unescape(aCrumb[1]);
  }
  return null;
}

function ToBasket(sValue, delKey)
{
	var sr=readCookie(cookie_name);
	if(sValue != null) {
		if(sr == null) sr = sValue;
		else sr = sr + '_' + sValue;
	}
	if(sr!=null) {
		var key, val, a_sr = sr.split('_');
		var a1 = new Array();
		var a2 = new Array();
		var a_sum = new Array();
		for(i=0; i < a_sr.length; i++) {
			val = a_sr[i];
			a_sr2 = val.split('|');
			key = a_sr2[0];
			if(key != delKey && key != 'undefined') {
				if(a2[key] == null) { a1[a1.length] = key; }
				a_sum[key] = a_sr2[3];
				a2[key] = a_sr[i];
//				alert(a1.length.toString(10) + '\nkey = ' + key + '\nvalue = ' + val);
			}
		}
		var sValue = '';
		var sum = 0;
		for(i=0; i < a1.length; i++) {
			key = a1[i];
			if(i==0) { sValue += a2[key]; }
			else { sValue += '_' + a2[key]; }
			eval("sum += " + a_sum[key]);
		}
		addCookie(cookie_name, sValue);
		if(sum > 0) { document.getElementById("basket").innerHTML = ": " + sum.toString(10) + " р"; }
		else { document.getElementById("basket").innerHTML = ""; }
		return a2;
	}
}

function checkBasket(s)
{
	if (s=='' && document.getElementById("basket").innerHTML == '') {
		alert('Ваша корзина пуста');
		return false;
	}
	return true; 
}

function to_buy(obj)
{
	var s = 0;
	if(obj.sale_count.value != null && obj.sale_price.value != null) {
		if(obj.sale_count.value == '' || obj.sale_count.value == '0') { obj.sale_count.value = '1'; }
		eval("s = " + obj.sale_count.value + " * " + obj.sale_price.value); 
		if(s > 0) {
			var sc = obj.element_id.value;
			sc += "|" + obj.sale_count.value;
			sc += "|" + obj.sale_price.value;
			sc += "|" + s.toString(10);
			ToBasket(sc, null);
		}
	}
}

function save_new_price(obj)
{
  	arr = showModalDialog("/admin/save_price.php?element_id=" + obj.element_id.value + "&property_id=1&property_value=" + obj.sale_new_price.value, null, "dialogWidth:460px; dialogHeight:150px");
	if (arr != null)
	{	
		obj.sale_price.value = arr["price"];
		var a_all = obj.all;
		a_all[1].innerHTML = arr["price"];
	}
}

function ShowWin(url) {
var x, y, cx, cy;
x = 800;
y = 600;
x=(x > screen.width * 0.7 ? screen.width * 0.7 : x);
y=(y > screen.height * 0.7 ? screen.height * 0.7 : y);
cx=screen.width/2 - (x/2);
cy=screen.height/2-(y/2);
window.open(url,"popup","toolbar=no,status=no,directories=no,menubar=no,resizable=yes,width="+x+",height="+y+",scrollbars=yes,top="+cy+",left="+cx);
}

function ShowWinS(url, x, y, scrl) {
var cx, cy, name;
x=(x > screen.width * 0.7 ? screen.width * 0.7 : x);
y=(y > screen.height * 0.7 ? screen.height * 0.7 : y);
cx=screen.width/2 - (x/2);
cy=screen.height/2-(y/2);
name = getNameByDate();
window.open(url,name,"toolbar=no,status=no,directories=no,menubar=no,resizable=yes,width="+x+",height="+y+",scrollbars="+scrl+",top="+cy+",left="+cx);
}

function getNameByDate() {
          var s = "";
          var d = new Date();
	  s += d.getFullYear();
	  s += (d.getMonth() + 1);
	  s += d.getDate();
	  s += "_" + d.getHours();
	  s += "mn" + d.getMinutes();
	  s += "s" + d.getSeconds();
          return s;
}
