<!-- 

//------------------------------------------------------------------------------
// (C) 2002-2004 ООО "Стандартные технологии"
// название файла: /admin/sys/jslib/cart.js
// назначение файла: работа с корзиной заказа
// разработчик: 
// версия: 1.0
//------------------------------------------------------------------------------

	/**
	 * Ф-я кладет товар node в корину
	 */
	function ShoppingCart( node ) {

		// node - текстовое поле ввода
		// node.parentNode.id - ID товара

		var id = node.parentNode.id;

		// в куке 'shoppingcart' лежит список пар ID товара и
		// заказанного количества, разделенных запятой:
		// ID1:NUM1,ID2:NUM2,ID3,NUM3

		var shoppingcart = 'shoppingcart';
		var data = get_cookie(shoppingcart);
		if( !data ) {
			data = ',';
		} else {
			data = ','+data+',';
		}

		var val = 0;

		switch(node.type.toUpperCase()) {
			case 'CHECKBOX' :
				val = node.checked ? 1 : 0;
				break;

			case 'TEXT' :
				val = node.value > 0 ? node.value : 0;
				break;

			default:
		}

		var template = new RegExp(","+id+":[0-9]*,") 

		data = data.replace(template, ',');
		if (val > 0) {
			node.style.color = "red";
			data = data + id + ':' + val + ',';
		}

		data = data.substring(1,data.length-1);

		set_cookie(shoppingcart, data, '/');
	}

	
	if (get_cookie('shoppingcart')) {

		var shoppingcart = get_cookie('shoppingcart').split(',');
		var total = 0;
		var price;
		var productlist=new Array();
		
		//Создаем список товаров
		inputs=document.getElementsByTagName('INPUT');
		for(var i=0; i<inputs.length; i++)
			if(inputs[i].id=='ShoppingCart')
				productlist.push(inputs[i]);
		//Считаем стоимость
		price = 1;
		var defined_products = new Array();
		for( i = 0; i < productlist.length; i++ ) {
			for ( j = 0; j < shoppingcart.length; j++ ) {
				node = productlist[i];
				if( shoppingcart[j].indexOf(node.parentNode.id+":") == 0 ) {
					ind = shoppingcart[j].indexOf(":");
					node.value = shoppingcart[j].substring(ind+1);
					if (node.type.toUpperCase() == 'CHECKBOX' ) {
						node.checked = true;
					}
					var product_id=node.parentNode.id;
					var found=false;
					for(var k=0; k<defined_products.length; k++)
						if(product_id==defined_products[k])
						{
							found=true;
							break;
						}
					if( node.parentNode.getAttribute('price') && !found ) {
						price = node.parentNode.getAttribute('price');
						price = price.replace(/ /g,'').replace(new RegExp('^[^0-9]*([0-9]*\.?[0-9]+).*$'),"$1");
						if (price.match(new RegExp('^[0-9]*\.?[0-9]+$')) ) {
							price = price.replace(new RegExp(','),".");
							total += node.value * price;
							defined_products.push(product_id);
						}
					}
				}
			}
		}
		//Создаем список итоговых стоимостей
		var totals=new Array();
		var td=document.getElementsByTagName('TD');
		for(var i=0; i<td.length; i++)
			if(td[i].id=='totalPrice')
				totals.push(td[i]);

			var str = total.toString();
			if (str.match(/\./i)) { 
				total = parseFloat(str.substr(0,str.indexOf('.')+3));
			}
	
			for (i=0; i<totals.length; i++) {
				totals[i].innerHTML = total;
			}
	}

/*
	onclick для кнопки "добавить в корзину"
*/
function addToCart() {
	//alert(dump_properties(top,true));
	/*sc = document.all['ShoppingCart'];
	if(!sc.length) sc=new Array(sc);
	for(i=0; i < sc.length; i++)
		if(sc[i].tagName=='INPUT') {
			if(sc[i].type=='text' && sc[i].value <= 0)
				sc[i].value = 1;
			if(sc[i].type=='checkbox' && !sc[i].checked)
				sc[i].checked = true;
			ShoppingCart(sc[i]);
		}*/
	reg=/popup=true/;
	if(reg.test(location))
	{
		if(typeof window.opener != 'undefined')
		{
			window.opener.location.reload();
			this.window.close();
		}
		else if(typeof top.siteframe != 'undefined')
			top.siteframe.location.reload();
		else
			top.location.reload();
	}
	else
		location.href = '/shoppingcart';
}
// -->


