// JavaScript Document
function getCartItems() {
	var cartCount = document.getElementById('cartitems').innerHTML;
	return cartCount;
}

function AddProducts(id, pavad) {
	/*
	<div id="product_<?php print $id; ?>" class="w162 fl" align="left">
		<span class="fl">&nbsp;&nbsp;&nbsp;</span>
		<span class="fl bef_cifr"><?php print $id; ?></span>
		<span class="fr cifr"><a href="javascript:RemoveFromCart('<?php print $id; ?>');" style="position:relative;"><?php print $amount; ?></a></span>
	</div>
	*/
	
	var cartObj = document.getElementById('productscart');
	var parentElement = document.createElement("div");
	parentElement.setAttribute("id", "product_"+id);
	parentElement.className = "fl bdborder";
	
	if (document.getElementById("cart["+id+"]")) {
		var amount = document.getElementById("cart["+id+"]").value;
		amount = amount*1;
	}

	var span = document.createElement("div");
	span.className = "fl";
	span.innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;";
	parentElement.appendChild(span);
	
	var span = document.createElement("div");
	span.className = "fl bef_cifr w77";
	var textNode = document.createTextNode(pavad);
 	span.appendChild(textNode);
	parentElement.appendChild(span);
	
	var span = document.createElement("div");

	span.className = "fr cifr";
   	span.innerHTML = amount + "&nbsp;<a href=javascript:RemoveFromCart('"+id+"'); style=\"position:relative; top:2px;\"><img src='images/delete.gif' alt='Panaikinti'></a>";
	parentElement.appendChild(span);
	
	cartObj.appendChild(parentElement);
}

function RemoveProduct(id) {
	var parentNode = document.getElementById('productscart');
	var childNode = document.getElementById("product_"+id);
	if (id>0) parentNode.removeChild(childNode);
}

function ChangeRowsCellBackground(row, color) {
	var cell = null;
	row.cells[0].innerHTML = row.rowIndex;
	for (var c=0; c<row.cells.length; c++) {
		cell = row.cells[c];
		cell.style.backgroundColor = color;
	}
}

function DeleteProduct(id) {
	var tableObj = document.getElementById('carttable');
	var row = document.getElementById("cartrow["+id+"]");
	
	try {
		tableObj.deleteRow(row.rowIndex);
		RemoveFromCart(id);
	} catch(ex) {
		alert(ex);
	}
	
	var rowIndex = 0;
	var rows = tableObj.rows.length;
	rows = rows-2;
	//alert(rows);
	//if (rows == 1)
		//alert('paskutinis...');
	
	for (var r=1; r<rows; r++) {
		if (rowIndex%2 == 0) {
			ChangeRowsCellBackground(tableObj.rows[r], '#ecedec');
		} else {
			ChangeRowsCellBackground(tableObj.rows[r], '#f4f4f3');
		}
		rowIndex++;
	}
}

function EditCartItem(id, name) {
	var amount = document.getElementById("cart["+id+"]").value;	
	amount = amount*1;
	
	if (document.getElementById("product_"+id)) {
		//alert("removing...");
		RemoveFromCart(id);
	}
	
	if (amount > 0) {
		var ajax = new AJAX(true);
		ajax.OnStateChange(CartResponse);
		ajax.Open("GET", "shoplibs/ajax/cart.php?"+id+"="+amount+"&pavad="+name+"&"+Math.floor(Math.random()*11), true);
		ajax.SetContentType("text/xml");
		ajax.Send(null);
	}
}

function CartResponse(request) {
	var cartItemsObject = document.getElementById('cartitems');
	var cartItems = cartItemsObject.innerHTML;
	
	var xml = request.responseXML;
	
	var items = xml.getElementsByTagName("items")[0].firstChild.nodeValue;
	var added = xml.getElementsByTagName("added")[0].firstChild.nodeValue;
	var removed = xml.getElementsByTagName("removed")[0].firstChild.nodeValue;
	var name = xml.getElementsByTagName("pavad")[0].firstChild.nodeValue;
	
	//alert(cartItems+" < "+items);
	
	if (cartItems < items) {
		//alert("adding");
		AddProducts(added, name);
		cartItemsObject.innerHTML = parseInt(cartItemsObject.innerHTML)+1;
	} else {
		//alert("removing");
		RemoveProduct(removed);
		cartItemsObject.innerHTML = parseInt(cartItemsObject.innerHTML)-1;
	}
	cartItems.innerHTML = items;
}

function AddToCart(id, pavad) {
	var amount = document.getElementById("cart["+id+"]").value;
	amount = amount*1;
	
	if (amount > 0) {
		var ajax = new AJAX(true);
		ajax.OnStateChange(CartResponse);
		ajax.Open("GET", "shoplibs/ajax/cart.php?"+id+"="+amount+"&pavad="+pavad+"&"+Math.floor(Math.random()*11), true);
		ajax.SetContentType("text/xml");
		ajax.Send(null);
	}

}

function RemoveFromCart(id) {
	var ajax = new AJAX(true);
	ajax.OnStateChange(CartResponse);
	ajax.Open("GET", "shoplibs/ajax/cart.php?remove="+id+"&"+Math.floor(Math.random()*11), true);
	ajax.SetContentType("text/xml");
	ajax.Send(null);
}

var t1 = null;
var t2 = null;

function Increase(id) {
	var obj = document.getElementById(id);
	var value = obj.value;
	
	if (value == "" || value == null)
		obj.value = 0;
	obj.value++;
	
	t1 = setTimeout("Increase('"+id+"')", 250);
}

function Decrease(id) {
	var obj = document.getElementById(id);
	var value = obj.value;
	
	if (value == "" || value == null)
		obj.value = 0;
		
	if (obj.value > 0)
		obj.value--;
	t2 = setTimeout("Decrease('"+id+"')", 250);
}

function stopFunction() {
	clearTimeout(t1);
	t1 = null;
	
	clearTimeout(t2);
	t2 = null;
}

function showDescription(id, display) {
	var obj = document.getElementById("descriptions["+id+"]");
	//var parent = obj.parentNode;

	obj.style.fontSize = "10px";
	//obj.style.padding = "2px";
	obj.style.color = "#747474";

	if ( obj.style.display == 'block' ) {  obj.style.display = 'none'; }  else { obj.style.display = 'block';}
}

