// JavaScript Document
var producto;

function agregarCarrito(productoId, cantidad, talla){
	producto = productoId;
	var add = 1;
	//var cantidad = parseInt($('#cantidad').val());
	$.ajax({
		type: "POST",
		url: "http://www.mall506.com/tienda-nueva/add-cart.php",
		data: "productoId="+productoId+"&cantidad="+cantidad+"&talla="+talla+"&accion=1",
		success: function(datos){
			switch(datos){
				case '1':
				
					break;
				case '2':
				
					//$('#dialog-cart-mod').dialog('open');
					add=0;
					break;
				case '3':
					$('#dialog-preorder-list').dialog('open');
					add=0;
					break;
				default:
					alert("Ha ocurrido un error, intentelo nuevamente" );
					break;
			}
			var cant = parseInt($('#cant-cart').html());
			cant = cant + add;
			$('#cant-cart').html(cant.toString());

			if(datos!='3'){
				alert("Su producto ha sido agregado al carrito de compras.");
				window.location = 'carrito-compras.php';
			}
			if(datos=='3'){
				alert("El producto se encuentra sin stock en este momento.");
			}
				
		},
		error: function(objeto, quepaso, otroobj){
			alert("Ha ocurrido un error intentelo nuevamente");
		}
	});
}

function eliminarCarrito(productoId){
	$.ajax({
		type: "POST",
		url: "add-cart.php",
		data: "productoId="+productoId+"&accion=2",
		success: function(datos){
			if(datos==1){
				jqRow = $("#producto-"+productoId);	
				jqRow.fadeOut("slow", function()
				{
					actualizaPrecio();
					jqRow.remove();
					var cant = parseInt($('#cant-cart').html());
					cant = cant - 1;
					$('#cant-cart').html(cant.toString());
					alert("Producto eliminado del carrito de compras.");
				});
			}
		},
		error: function(objeto, quepaso, otroobj){
			alert("Ha ocurrido un error intentelo nuevamente");
		}
	});
}

function actualizaPrecio(){
	$.ajax({
		type: "POST",
		url: "add-cart.php",
		data: "accion=3",
		success: function(datos){
			$("#subtotal").html("&cent;"+datos);
		},
		error: function(objeto, quepaso, otroobj){
			alert("Ha ocurrido un error intentelo nuevamente");
		}
	});
	
	$.ajax({
		type: "POST",
		url: "add-cart.php",
		data: "accion=4",
		success: function(datos){
			$("#descuento").html("&cent;"+datos);
		},
		error: function(objeto, quepaso, otroobj){
			alert("Ha ocurrido un error intentelo nuevamente");
		}
	});
	
	$.ajax({
		type: "POST",
		url: "add-cart.php",
		data: "accion=5",
		success: function(datos){
			$("#total_label").html("&cent;"+datos);
			$("#total").val(datos);
		},
		error: function(objeto, quepaso, otroobj){
			alert("Ha ocurrido un error intentelo nuevamente");
		}
	});
	
}

function comentar(id, cual){
	if($("#CONSUMIDOR_ID").val()!='0'){
		$("#id").val(id);
		$("#id_cual").val(cual);
		$("#dialog-comment").dialog('open');
	}else{
		alert("Debe ser un usuario registrado para poder dejar su comentario");
	}
}

$(document).ready( function(){
	var titulo = $( "#titulo-comentario" ),
		comentario = $( "#nuevo-comentario" ),
		id_cual = $( "#id_cual" ),
		id = $( "#id" ),
		allFields = $( [] ).add( titulo ).add( comentario ).add( id_cual ).add( id );
	
	$("#dialog-comment-product").dialog({
		autoOpen: false,
		resizable: false,
		bgiframe: true,
		modal: true,
		width:480,
		buttons: {
			Guardar: function() {
				//alert(producto);
				$.ajax({
					type: "POST",
					url: "procesa-comentario.php",
					data: "id="+$("#id").val()+"&id_cual=" + $("#id_cual").val() + "&titulo=" + $("#titulo-comentario").val() + "&comentario=" + $("#nuevo-comentario").val(),
					success: function(datos){
						alert(datos);
					},
					error: function(objeto, quepaso, otroobj){
						alert("Ha ocurrido un error intentelo nuevamente");
					}
				});
				$(this).dialog('close');
			},
			Cancelar: function(){
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val( "" );
		}
	});
	
	$("#dialog-comment").dialog({
		autoOpen: false,
		resizable: false,
		bgiframe: true,
		modal: true,
		width:480,
		buttons: {
			Guardar: function() {
				//alert(producto);
				$.ajax({
					type: "POST",
					url: "procesa-comentario.php",
					data: "id="+$("#id").val()+"&id_cual=" + $("#id_cual").val() + "&titulo=" + $("#titulo-comentario").val() + "&comentario=" + $("#nuevo-comentario").val(),
					success: function(datos){
						alert(datos);
					},
					error: function(objeto, quepaso, otroobj){
						alert("Ha ocurrido un error intentelo nuevamente");
					}
				});
				$(this).dialog('close');
			},
			Cancelar: function(){
				$(this).dialog('close');
			}
		},close: function() {
			allFields.val( "" );
		}
	});
	
	$("#dialog-cotizar").dialog({
		autoOpen: false,
		resizable: false,
		bgiframe: true,
		modal: true,
		width:480,
		buttons: {
			Enviar: function() {
				//alert(producto);
				$.ajax({
					type: "POST",
					url: "procesa-cotizacion.php",
					data: "productoId=" + $("#productoId_cot").val() + "&nombre="+$("#nombre_cot").val()+"&email=" + $("#email_cot").val() + "&telefono=" + $("#telefono_cot").val() + "&celular=" + $("#celular_cot").val() + "&comentario=" + $("#comentario_cot").val(),
					success: function(datos){
						alert(datos);
					},
					error: function(objeto, quepaso, otroobj){
						alert("Ha ocurrido un error intentelo nuevamente");
					}
				});
				$(this).dialog('close');
			},
			Cancelar: function(){
				$(this).dialog('close');
			}
		},close: function() {
			allFields.val( "" );
		}
	});
});
