var num = 9;

function removeTr(id){
//	alert(i);
	table = document.getElementById('tablet');
	trcount = table.rows.length;

	idd = id.substr(2, id.length);
	document.getElementById('summa').innerHTML = '$' + parseFloat(parseFloat(document.getElementById('summa').innerHTML.replace('$','')) - parseFloat(document.getElementById('price'+idd).innerHTML.replace('$','')));
	table.deleteRow(document.getElementById('tr'+idd).rowIndex);	

}

function addToBasket(id){
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4){
			var cart = document.getElementById('cartdiv');
			if(cart)
				cart.innerHTML = req.responseJS.cart_html;
			openCartWindow(req.responseJS.window);	
		}
	}	
	req.open('GET', 'http://uca.com.ua/buy', true);
	req.send({id: id});
}

function openCartWindow(html){
	window.cartwnd = document.createElement('div');
	cartwnd.innerHTML = html;
	cartwnd.style.position = 'absolute';
	cartwnd.style.left = '50%';
	cartwnd.style.marginLeft = '-290px';
	document.body.appendChild(cartwnd);
	cartwnd.style.top = (document.documentElement.scrollTop || window.scrollY) + parseInt((window.innerHeight || document.documentElement.clientHeight) / 2 - cartwnd.clientHeight / 2) + 'px';
}                                                                                                                    

function windowClose(){
	cartwnd.parentNode.removeChild(cartwnd);
}

function recalcCart(){
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4){
			var cart = document.getElementById('cartdiv');
			if(cart)
				cart.innerHTML = req.responseJS.cart_html;
			windowClose();	
			openCartWindow(req.responseJS.window);	
		}
	}	
	var els = document.cart.elements;
	var count = [];
	for(var i = 0; i < els.length; i++){
		if(els[i].name.match(/count/))
			count[els[i].name.replace(/[^\d]*/ig, '')] = els[i].value;
	}
	req.open('GET', 'http://uca.com.ua/recalccart', true);
	req.send({count: count});
}

function clearCart(){
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4){
			var cart = document.getElementById('cartdiv');
			if(cart)
				cart.innerHTML = req.responseJS.cart_html;
			windowClose();	
		}
	}	
	req.open('GET', 'http://uca.com.ua/clearcart', true);
	req.send({});
}

function addMore(cat){
	var req = new JsHttpRequest	();

	req.onreadystatechange = function() {
		if (req.readyState == 4){
			table = document.getElementById('tablet');
			trcount = table.rows.length;
			tr = table.insertRow(trcount-2);
			td1 = tr.insertCell(0);
			td2 = tr.insertCell(1);
			td3 = tr.insertCell(2);
			td4 = tr.insertCell(3);
			td5 = tr.insertCell(4);

			td1.className = 'td';
			td2.className = 'td';
			td3.className = 'td';
			td4.className = 'td';
			td5.className = 'td';
			td5.style.border = '0';

			//nnm = num;
			tr.id = 'tr'+num;//req.responseJS.ids[0];
			td1.innerHTML = '<img src=\'/images/remove.jpg\' border=\'0\' style=\'float:right;cursor:pointer;width:16pxheight:16px;\' title=\'Удалить\' onClick=\"removeTr(\''+tr.id+'\');\">'+req.responseJS.category;

			list = document.createElement('select');
			list.name='select['+num+']';
			nnm = num;
			list.onchange = function(){
			        	setItem(this.value, nnm);
				}

			for(var i=0;i<req.responseJS.count;i++){
				list.options[i] = new Option();
				list.options[i].innerHTML = '$'+req.responseJS.prices[i]+'&nbsp;&nbsp;&nbsp;'+req.responseJS.titles[i];
				list.options[i].value = req.responseJS.ids[i];
				list.options[i].id = 'val_'+req.responseJS.ids[i];
			}	

			td2.appendChild(list);
			td3.innerHTML = '<b id=\'cnt'+num+'\' style=\'float:left;font-weight:normal;\'>1</b><img src=\'/images/mm.png\' class=\'mmimg\' onClick=\'setMm('+num+');\'><img src=\'/images/pp.png\' class=\'ppimg\' onClick=\'setPp('+num+');\'><input type=\'hidden\' id=\'count'+num+'\' name=\'count['+num+']\' value=\'1\'></td>';
			td4.id = 'price'+num;
			td4.innerHTML = '$'+req.responseJS.prices[0];

			td5.id = 'summ'+num;
			td5.innerHTML = '$'+req.responseJS.prices[0];
       			document.getElementById('summa').innerHTML = '$' + Math.round(parseFloat(parseFloat(document.getElementById('summa').innerHTML.replace('$','')) + parseFloat(document.getElementById('price'+num).innerHTML.replace('$','')))*100)/100;
			
			cat.options[0].selected = true;
			num++;
		}
	}
	req.open(null, '/getproducts', true);
	req.send({cat: cat.value});
}
function openPopup(url,w,h)
{
	height = h + 1;
	width = w + 1;
	newWin = open(url, '_blank', 'resizable=yes,left=50,top=50,width='+width+',height='+height+',scrollbars=1,status=no,toolbar=no,menubar=no');
}


