var req;

function makeRequest(url, backFunction) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = backFunction;
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = backFunction;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function addToCart(prod_id) {
	if(prod_id > 0) {
		document.getElementById('prodAddToCart_' + prod_id).style.display = "none";
		document.getElementById('prodAddToCart_' + prod_id + '_Complite').style.display = "block";
//		document.getElementById('prodAddToCart_' + prod_id + '_Complite').innerHTML = "<img src=\"/images/ajax-loader.gif\" style=\"width:16px; height:16px;\" align=\"left\">";
		var ajax_loader_image = document.createElement("img"); ajax_loader_image.src = '/images/ajax-loader.gif'; ajax_loader_image.style.width = '16px'; ajax_loader_image.style.height = '16px';
		document.getElementById('prodAddToCart_' + prod_id + '_Complite').innerHTML = '';
		document.getElementById('prodAddToCart_' + prod_id + '_Complite').appendChild(ajax_loader_image);
		makeRequest('/inc/ajax_functions.php?functionName=addToCart&prod_id=' + prod_id, ajax_addToCart);
	}
}

function ajax_addToCart() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var the_object;
			the_object = eval("(" + req.responseText + ")");
			
			updateTopCartCounter(the_object.TotalCartProducts);
			
			document.getElementById('prodAddToCart_' + the_object.prod_id + '_Complite').innerHTML = "המוצר נוסף ל<a href=\"/cart.php\">סל קניות</a>";
			
		} else {
//          alert('There was a problem with the request.');
		}
	}
}

function cartChangeProductQantity(prod_id, action) {
	if(action == 'plus') {
		document.getElementById('cartPricesAjaxLoader').style.display = '';
		document.getElementById('cartPrices').style.display = 'none';
		document.getElementById('cartOrderButton').style.display = 'none';
		
		document.getElementById('cartProductQuantity_' + prod_id).innerHTML = parseInt(document.getElementById('cartProductQuantity_' + prod_id).innerHTML) + 1;
		makeRequest('/inc/ajax_functions.php?functionName=changeProductQantity&prod_id=' + prod_id + "&action=plus", ajax_cartChangeProductQantity);
	}
	if(action == 'minus') {
		if(parseInt(document.getElementById('cartProductQuantity_' + prod_id).innerHTML) > 1) {
			document.getElementById('cartPricesAjaxLoader').style.display = '';
			document.getElementById('cartPrices').style.display = 'none';
			document.getElementById('cartOrderButton').style.display = 'none';

			document.getElementById('cartProductQuantity_' + prod_id).innerHTML = parseInt(document.getElementById('cartProductQuantity_' + prod_id).innerHTML) - 1;
			makeRequest('/inc/ajax_functions.php?functionName=changeProductQantity&prod_id=' + prod_id + "&action=minus", ajax_cartChangeProductQantity);
		} else {
			cartProductRemove(prod_id);
		}
	}

	cartListShow();
}

function ajax_cartChangeProductQantity() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var the_object;
			the_object = eval("(" + req.responseText + ")");
			
			updateTopCartCounter(the_object.total_prods);
			
			document.getElementById('cartProductPrice_' + the_object.prod_id).innerHTML = the_object.prod_price_quantity;

			document.getElementById('span_total_prods').innerHTML = the_object.total_prods;
			document.getElementById('span_total_price_novat').innerHTML = the_object.total_price_novat;
			document.getElementById('span_total_price_incvat').innerHTML = the_object.total_price_incvat;
			document.getElementById('span_send').innerHTML = the_object.send + ' ש&quot;ח';
			document.getElementById('span_total_price_inc_send').innerHTML = the_object.total_price_inc_send;

			document.getElementById('cartPricesAjaxLoader').style.display = 'none';
			document.getElementById('cartPrices').style.display = '';
			document.getElementById('cartOrderButton').style.display = '';
		} else {
//          alert('There was a problem with the request.');
		}
	}
}

function cartProductRemove(prod_id) {
	document.getElementById('cartPricesAjaxLoader').style.display = '';
	document.getElementById('cartPrices').style.display = 'none';
	document.getElementById('cartOrderButton').style.display = 'none';

	document.getElementById('cartProductRow_' + prod_id).style.display = "none";
	makeRequest('/inc/ajax_functions.php?functionName=removeFromCart&prod_id=' + prod_id, ajax_cartProductRemove);
}

function ajax_cartProductRemove() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var the_object;
			the_object = eval("(" + req.responseText + ")");
			
			updateTopCartCounter(the_object.total_prods);
			document.getElementById('span_total_prods').innerHTML = the_object.total_prods;
			document.getElementById('span_total_price_novat').innerHTML = the_object.total_price_novat;
			document.getElementById('span_total_price_incvat').innerHTML = the_object.total_price_incvat;
			document.getElementById('span_send').innerHTML = the_object.send + ' ש&quot;ח';
			document.getElementById('span_total_price_inc_send').innerHTML = the_object.total_price_inc_send;
			
			document.getElementById('cartPricesAjaxLoader').style.display = 'none';
			document.getElementById('cartPrices').style.display = '';
			document.getElementById('cartOrderButton').style.display = '';
		} else {
//          alert('There was a problem with the request.');
		}
	}
	
	cartListShow();
}

function updateTopCartCounter(TotalCartProducts) {
	if(TotalCartProducts == 1) {
		document.getElementById('cart_quantity').innerHTML = ' פריט אחד';
	} else {
		document.getElementById('cart_quantity').innerHTML = TotalCartProducts + ' פריטים';
	}
}

function cartListShow() {
	var totalHiddenRows = 0;
	for(i=0; i<document.getElementById('cartProductList').rows.length; i++) {
		if(document.getElementById('cartProductList').rows[i].style.display == 'none') {
			totalHiddenRows++;
		}
	}
	if((document.getElementById('cartProductList').rows.length - totalHiddenRows) < 2) {
		document.getElementById('cartProductList').style.display = "none";
		document.getElementById('cartClearAll').style.display = "none";
		document.getElementById('cartPricesAjaxLoader').style.display = 'none';
		document.getElementById('cartPrices').style.display = "none";
		document.getElementById('cartOrderButton').style.display = "none";
		
		document.getElementById('cartEmpty_1').style.display = "";
		document.getElementById('cartEmpty_2').style.display = "";
	}
}

function cartProductRemoveAll() {
	document.getElementById('cartPricesAjaxLoader').style.display = '';
	document.getElementById('cartPrices').style.display = 'none';

	makeRequest('/inc/ajax_functions.php?functionName=removeAllFromCart', ajax_cartProductRemoveAll);
}

function ajax_cartProductRemoveAll() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			for(i=1; i<document.getElementById('cartProductList').rows.length; i++) {
				document.getElementById('cartProductList').rows[i].style.display = 'none';
			}
			
			updateTopCartCounter(0);
			cartListShow();
			window.location.reload();
		}
	}
}
