function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

MM_preloadImages('../images/menu_1_f2.jpg','../images/menu_2_f2.jpg','../images/menu_3_f2.jpg','../images/menu_4_f2.jpg','../images/menu_5_f2.jpg','../images/menu_6_f2.jpg','../images/menu_7_f2.jpg','../images/menu_8_f2.jpg','../images/menu_9_f2.jpg','../images/menu_10_f2.jpg','../images/menu_11_f2.jpg','../images/menu_12_f2.jpg');
MM_preloadImages('../images/menu_1_h_f2.jpg','../images/menu_2_h_f2.jpg','../images/menu_3_h_f2.jpg','../images/menu_4_h_f2.jpg','../images/menu_5_h_f2.jpg','../images/menu_6_h_f2.jpg','../images/menu_7_h_f2.jpg','../images/menu_8_h_f2.jpg','../images/menu_9_h_f2.jpg','../images/menu_10_h_f2.jpg','../images/menu_11_h_f2.jpg','../images/menu_12_h_f2.jpg');

function changeURL() {
	window.location.href = document.getElementById('properties_operations').value;
}

function changeType() {
	window.location.href = document.getElementById('properties_types').value;
}

function goBack() {
	history.back();
}

// **************
// CART FUNCTIONS
// **************

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';
		}
	}
}
