//<![CDATA[
var http;
var totalItems = 0;
var iconPlus = new Image(10, 10);
var iconMinus = new Image(10, 10);
iconMinus.src = RELATIVE_PATH + 'images/icons/minus.gif';
iconPlus.src = RELATIVE_PATH + 'images/icons/plus.gif';

var listDisplayId = 0;
var listDisplay = null;
var listDisplayRow = null;
var listProducts = true;

function hidePreviousList(id, obj, reset) {
	if (listProducts) {
		if (!reset) {
			if (listDisplayId != id && listDisplay != null && listDisplayId > 0) {
				showHide(listDisplayId, false, listDisplay);
			}
			
			listDisplayId = id;
			listDisplay = obj;
		} else {
			listDisplayId = 0;
			listDisplay = null;
			listDisplayRow = null;
		}	
	}
}

function hidePreviousRow(id, row) {
	if (listProducts) {
		if (listDisplayId != id && listDisplayRow != null && listDisplayId > 0) {
			showHideRow(listDisplayId, false, listDisplayRow);
		}
		
		listDisplayRow = row;
	}
}

function showHide(id, show, obj) {
	if (show) {
		obj.src = iconMinus.src;
		obj.alt = 'Ocultar';
		obj.title = 'Ocultar';
		obj.onclick = function() {
			showHide(id, false, this);
		}
		
		document.getElementById('list_' + id).style.display = 'inline';
		
		hidePreviousList(id, obj, false);		
	} else {
		obj.src = iconPlus.src;
		obj.alt = 'Ampliar';
		obj.title = 'Ampliar';
		obj.onclick = function() {
			showHide(id, true, this);
		}
		document.getElementById('list_' + id).style.display = 'none';

		hidePreviousList(id, obj, true);
	}
}

function showHideRow(id, show, row) {
	if (show) {
		row.onclick = function() {
			showHideRow(id, false, this);
		}

		hidePreviousRow(id, row);
	} else {
		row.onclick = function() {
			showHideRow(id, true, this);
		}
	}

	var obj = row.parentNode.cells[0].childNodes[0];
	showHide(id, show, obj);
}

function addProductToBasket(id, catId, sbcatId, quantity, price, ratio) {
	http = new HttpRequest(RELATIVE_PATH + 'actions/basket.inc.php', [ 'op',
			'id', 'catId', 'sbcatId', 'quantity', 'price', 'ratio' ], [ 'add',
			id, catId, sbcatId, quantity, price, ratio ], function() {
		if (http.getReadyState() == 4 && http.getStatus() == 200) {
			if (http.getResponseText() != '') {
				var arr = http.getResponseText().split(':');
				var total = parseFloat(arr[0]);
				var items = arr[1];				
				var currentCarry = 0.00;
				if(total < maxCarry){
					currentCarry = carry;
					total += currentCarry;	
				}
				currentCarry = currentCarry.toFixed(2).replace('.', ',');
				
				loadTotalProducts(total.toFixed(2).replace('.', ','), items, currentCarry);
				
				addProductToMiniBasket(id, catId, sbcatId, quantity, price, ratio);
			}
		}
	});
	http.makeRequest();
}

function addProductToMiniBasket(id, catId, sbcatId, quantity, price, ratio) {
	var mainDiv = document.getElementById('minibasketview');

	if (!mainDiv) {
		return;
	}

	var table = document.getElementById('mini_products');
	var info = document.getElementById('noproducts');
	
	if (!table && info) {
		table = document.createElement('table');
		table.id = 'mini_products';
		mainDiv.appendChild(table);
	}
	
	var rows = table.rows;

	if (rows.length == 0 && info) {
		table.parentNode.removeChild(info);
	}

	var row = document.getElementById('mini_' + id);
	if (row) {
		if (quantity == 0) {
			table.deleteRow(row.rowIndex + 1);
			table.deleteRow(row.rowIndex);

			var n = 1;
			for ( var i = 0; i < rows.length; i = i + 2) {
				var className = (n % 2 == 0) ? 'bg_grey' : 'bg_white';

				rows[i].cells[0].className = className;
				for ( var j = 0; j < rows[i + 1].cells.length; j++) {
					rows[i + 1].cells[j].className = 'line ' + className;
				}
				n++;
			}

		} else {
			document.getElementById('minibasket_quantity' + id).innerHTML = quantity;
			document.getElementById('minibasket_import' + id).innerHTML = document
					.getElementById('imp_' + id).innerHTML;
		}
	} else if (quantity > 0) {
		var className = (rows.length > 0 && rows[rows.length - 1].cells[0].className == 'line bg_white') ? 'bg_grey'
				: 'bg_white';
		var divideFlag = document.getElementById('divide_' + id).value;

		row = table.insertRow(rows.length);
		row.id = 'mini_' + id;

		var cell = row.insertCell(row.cells.length);
		cell.className = className;
		cell.colSpan = '3';
		cell.style.width = '80px';
		cell.innerHTML = '<strong><em>'
				+ document.getElementById('name_' + id).innerHTML
				+ '</em></strong>';

		row = table.insertRow(rows.length);
		cell = row.insertCell(row.cells.length);
		cell.className = 'line ' + className;
		cell.style.textAlign = 'left';
		cell.innerHTML = document.getElementById('price_' + id).innerHTML;

		cell = row.insertCell(row.cells.length);
		cell.className = 'line ' + className;
		cell.id = 'minibasket_quantity' + id;
		cell.style.textAlign = 'right';
		cell.style.width = '50px';
		cell.innerHTML = quantity;

		cell = row.insertCell(row.cells.length);
		cell.className = 'line ' + className;
		cell.id = 'minibasket_import' + id;
		cell.style.textAlign = 'right';
		cell.style.width = '50px';
		cell.innerHTML = document.getElementById('imp_' + id).innerHTML;
	}

	if (rows.length == 0) {
		var div = document.createElement('div');
		div.id = 'noproducts';
		div.className = 'div_content';
		div.innerHTML = 'Aún no has añadido productos a tu cesta.<br/><br/>' +
						'Te recordamos que también puede cargar en tu cesta el ultimo pedido que realizaste.<br/>' +
						'<div style="text-align: center; padding-top: 10px;"><a href="' + RELATIVE_PATH + 'miultimopedido.php?add=1">Comenzar con mi último pedido</a></div>';
		
		table.parentNode.appendChild(div);
		div.parentNode.removeChild(table);
	}

}

function removeProduct(id, catId, sbcatId, obj) {
	if (obj == null
			|| confirm('¿Realmente deseas eliminar este producto de tu cesta?')) {
		http = new HttpRequest(
				RELATIVE_PATH + 'actions/basket.inc.php',
				[ 'op', 'id', 'catId', 'sbcatId' ],
				[ 'remove', id, catId, sbcatId ],
				function() {
					if (http.getReadyState() == 4 && http.getStatus() == 200) {
						// location.reload();

						if (http.getResponseText() != '') {
							if (obj != null) {
								var table = obj.parentNode.parentNode.parentNode;
								table
										.deleteRow(obj.parentNode.parentNode.rowIndex);

								var rows = table.rows;

								if (rows.length > 0) {
									var arrClassNames = [ 'name ',
											'description ', 'price ',
											'quantity ', 'cost ', '' ];
									for ( var i = 0; i < rows.length; i++) {
										var className = (i % 2 == 0) ? 'bg_white'
												: 'bg_grey';

										for ( var j = 0; j < rows[i].cells.length; j++) {
											rows[i].cells[j].className = arrClassNames[j]
													+ className;
										}
									}
								}

								if (rows.length == 0) {
									location.href = script;
								}
							}

							var arr = http.getResponseText().split(':');
							var total = parseFloat(arr[0]);
							var items = arr[1];				
							var currentCarry = 0.00;
							if(total < maxCarry){
								currentCarry = carry;
								total += currentCarry;	
							}
							currentCarry = currentCarry.toFixed(2).replace('.', ',');
							
							loadTotalProducts(total.toFixed(2).replace('.', ','), items, currentCarry);
							addProductToMiniBasket(id, catId, sbcatId, 0, 0, 0);
							arrTotalLocal[id] = 0.00;
						}
					}
				});
		http.makeRequest();
	}
}

function loadTotalProducts(total, totalItems, currentCarry) {
	document.getElementById('total').innerHTML = total + ' €';
	document.getElementById('items').innerHTML = totalItems;
	document.getElementById('carry').innerHTML = currentCarry + ' €';
		
	
	if(document.getElementById('minibasket_data')) {
		document.getElementById('minibasket_total').innerHTML = total + ' €';
		document.getElementById('minibasket_items').innerHTML = totalItems;
	}

	if(document.getElementById('basket_data')) {
		document.getElementById('basket_carry').innerHTML = currentCarry + ' €';
		document.getElementById('basket_total').innerHTML = total + ' €';
		document.getElementById('basket_items').innerHTML = totalItems;
	}
}

function addsubtract(isAdd, id, catId, sbcatId, divide) {
	var input = document.getElementById('qntty_' + id);
	var value = 0;
	if (input.value != '') {
		value = parseFloat(input.value);
	}
	
	if (isAdd) {
		value += (divide == 1) ? 0.5 : 1;		
	}else{
		value -= (divide == 1) ? 0.5 : 1;
		if (value < 0) {
			value = 0;
		}
	}

	timer = setTimeout('calculateProductPrice(' + id + ',' + catId + ','
			+ sbcatId + ')', 1);
	
	input.value = value;
}
//]]>

