function callOrPrice(chk){
	
	if($(chk).attr("checked") == true){
		$("#price").attr("disabled","disabled");
		$("#price").val('0');
	}else{
		$("#price").attr("disabled","");
	}
}

function IsNumeric(strString, decimals) {
	var error = 0;
	
	if (!decimals) {
		var strValidChars = "0123456789";
	}else{
		var strValidChars = ".0123456789";
	}
	
	var strChar;
	var blnResult = true;

	//if (strString.length == 0 || strString == 0) error = 2;
	if (strString.length == 0) error = 2;
	
	if (!error) {
		for (i = 0; i < strString.length && error == 0; i++){
			strChar = strString.charAt(i);
			if (strValidChars.indexOf(strChar) == -1){
				error = 1;
			}
		}
	}

	if (error) {
		return false;
	}else{
		return true;
	}
}

function addProduct(product_id) {
	var quantity = $("#quantity_"+product_id).val();
	
	if (!IsNumeric(quantity, false)) {
		alert("La cantidad debe ser un número");
	}else{
		document.getElementById("products_marquee").stop();
		
		$("#notification").fadeIn("slow", function() {
			$("#products_scroller").fadeOut("fast", function() {
				$("#cart_loading").fadeIn("fast", function() {
					$.ajax({
						url: 'add-product.php', 
						type: 'POST', 
						data: 'product_id='+product_id+'&quantity='+quantity, 
						dataType: 'html', 
						
						success: function(html) {
							setTimeout(function() {
								$("#notification").fadeOut("slow", function() {
									htmlArray = html.split('{-SEP-}');
									
									if (htmlArray[0] == "1") {
										$("#cart_products").html(htmlArray[0] + ' producto en su carrito');
									}else{
										$("#cart_products").html(htmlArray[0] + ' productos en su carrito');
									}
									
									$("#products_scroller_table").html(htmlArray[1]);
									
									$("#quantity_"+product_id).val('');
									
									$("#cart_loading").fadeOut("fast", function () {
										$("#products_scroller").fadeIn("slow", function() {
											document.getElementById("products_marquee").start();
										});
									});
								});
							}, 1000);
						}
					});
				});
			});
		});
	}
}

function deleteProduct(id) {
	$.ajax({
		url: 'delete-product.php', 
		type: 'POST', 
		data: 'id='+id, 
		dataType: 'html', 
		
		success: function(html) {
			if (html == "empty") {
				$("#tr_product_"+id).slideUp("slow", function () {
					window.location.reload();
				});
			}else if (html){
				$("#tr_product_"+id).slideUp();
				
				$("#products_scroller").fadeOut("fast", function() {
					$("#cart_loading").fadeIn("fast", function() {
						setTimeout(function() {
							htmlArray = html.split('{-SEP-}');
							
							var cart_products = htmlArray[0];
							var total = htmlArray[1];
							
							if (htmlArray[0] == "1") {
								$("#cart_products").html(htmlArray[0] + ' producto en su carrito');
							}else{
								$("#cart_products").html(htmlArray[0] + ' productos en su carrito');
							}
							
							$("#products_scroller_table").html(htmlArray[2]);
							
							$("#total").html(total);
							
							$("#cart_loading").fadeOut("fast", function () {
								$("#products_scroller").fadeIn("slow", function() {
									document.getElementById("products_marquee").start();
								});
							});
						}, 1000);
					});
				});
			}
		}
	});
}

function refreshProductQuantity(id, actual) {
	var quantity = $("#quantity_product_"+id).val();
	
	if (!IsNumeric(quantity, false)) {
		alert("Por favor, ingrese un número");
	}else{
		if (quantity == 0) {
			// JUST DELETE
			deleteProduct(id);
		}else{
			$("#sub_product_"+id).fadeOut("slow", function() {
				$("#sub_loading_product_"+id).fadeIn("slow", function() {
					$("#products_scroller").fadeOut("fast", function() {
						$("#cart_loading").fadeIn("fast", function() {
							$.ajax({
								url: 'update-product.php', 
								type: 'POST', 
								data: 'id='+id+'&quantity='+quantity+'&actual='+actual, 
								dataType: 'html', 
								
								success: function(html) {
									htmlArray = html.split('{-SEP-}');
									
									var subtotal = htmlArray[0];
									var total = htmlArray[1];
									
									$("#sub_loading_product_"+id).fadeOut("slow", function() {
										$("#sub_product_"+id).html(subtotal);
										
										$("#sub_product_"+id).fadeIn("slow");
										
										$("#products_scroller_table").html(htmlArray[2]);
										
										$("#total").html(total);
										
										$("#cart_loading").fadeOut("fast", function () {
											$("#products_scroller").fadeIn("slow", function() {
												document.getElementById("products_marquee").start();
											});
										});
									});
								}
							});
						});
					});
				});
			});
		}
	}
}

function showHideTR(what, act) {
	if (act == "hide") {
		obj = document.getElementsByTagName('TR');
		for (i=0; i<obj.length; i++) {
			if (obj[i].id == what)
			obj[i].style.display = 'none';
		}
	}else{
		obj = document.getElementsByTagName('TR');
		for (i=0; i<obj.length; i++) {
			if (obj[i].id == what)
			obj[i].style.display = 'block';
		}
	}
}