﻿function recarga(combo,valor){
	pais=document.getElementById('pais').value;
	denominacion=document.getElementById('zona').value;
	bodega=document.getElementById('bodega').value;
	
	$.ajax({
			type: "POST",
			url: "ajax/recarga.php",
			data: "combo="+combo+"&valor="+valor+"&pais="+pais+"&denominacion="+denominacion+"&bodega="+bodega,
			complete: function(objeto, exito) {
				if(exito=="success") {
					if(objeto.responseText!="")
						switch(combo){
							case "pais":
								document.getElementById('tres').innerHTML=objeto.responseText;
								break;
							case "zona":
								document.getElementById('dos').innerHTML=objeto.responseText;
								break;
							case "bodega":
								document.getElementById('comboTipo').innerHTML=objeto.responseText;
								break;
							case "tipo":
								if(document.getElementById('comboVinos'))
									document.getElementById('comboVinos').innerHTML=objeto.responseText;
								break;
						}
						//document.getElementById(combo).disabled=true;
				} else {

					alert("Se produjo un error al procesar la petición"+combo+valor);
				}
			}
		});
}

function mas_vino(vino,tipo){
	if (tipo==1)
		ira="ajax/mas_vino.php";
	else
		ira="ajax/mas_articulo.php";
	$.ajax({
			type: "POST",
			url: ira,
			data: "id="+vino+"&tipo="+tipo,
			complete: function(objeto, exito) {
				if(exito=="success") {
					if(objeto.responseText!=""){
						if (objeto.responseText==1){
							Shadowbox.open({
								content:    '/catalogo/grabado.html',
								title:      "Vino Seleccionado",
								player:     "iframe",
								height:     200,
								width:      305
							});
						}
						else
						{
							if (objeto.responseText==2){
							Shadowbox.open({
								content:    '/catalogo/producto_grabado.html',
								title:      "Producto Seleccionado",
								player:     "iframe",
								height:     200,
								width:      305
							});
						}
						else
						{
							alert(objeto.responseText);
						}
						}

					}
				} else {

					alert("Se produjo un error al seleccionar el Vino (mas vino)");
				}
			}
		});
}

function mas_comentario(vino,referencia,mibodega){
	$.ajax({
			type: "POST",
			url: "ajax/mas_comentario.php",
			data: "id="+vino+"&linea="+mibodega+"&referencia="+referencia,
			complete: function(objeto, exito) {
				if(exito=="success") {
					if(objeto.responseText=="01")
						window.location=document.getElementById('avanzar').href;
				}
				else
				{
					return false;
					alert("Se produjo un error al seleccionar el Vino (comentarios)");
				}
			}
		});
}

function totalizar(){
	$.ajax({
			type: "POST",
			url: "ajax/totaliza.php",
			data: "id=1",
			complete: function(objeto, exito) {
				if(exito=="success") {
					if(objeto.responseText!=""){
						$("#totaltotal").fadeOut("slow");
						document.getElementById("totaltotal").innerHTML=objeto.responseText;
						$("#totaltotal").fadeIn("slow");
					}
				} else {

					alert("Se produjo un error al seleccionar el Vino (totalizar)");
				}
			}
		});
}

function menos_vino(bodega,tipo){
	if (tipo==1)
		ira="ajax/menos_vino.php";
	else
		ira="ajax/menos_articulo.php";
	$.ajax({
			type: "POST",
			url: ira,
			data: "id="+bodega,
			complete: function(objeto, exito) {
				if(exito=="success") {
					if(objeto.responseText=="borrado"){
						Shadowbox.open({
							content:    '/catalogo/borrado.html',
							title:      "Vino Borrado",
							player:     "iframe",
							height:     200,
							width:      305
						});

					}
					if(objeto.responseText=="borrado2"){
						Shadowbox.open({
							content:    '/catalogo/producto_borrado.html',
							title:      "Producto Borrado",
							player:     "iframe",
							height:     200,
							width:      305
						});

					}
				} else {

					alert("Se produjo un error al seleccionar el Vino (Menos vino)");
				}
			}
		});
}

function borra_bodega(bodega,tipo){
	if (tipo=="1"){
		var lalinea="lineav";
	}
	if (tipo=="2"){
		var lalinea="lineap";
	}
	$.ajax({
			type: "POST",
			url: "ajax/borra_mi_bodega.php",
			data: "id="+bodega+"&tipo="+tipo,
			complete: function(objeto, exito) {
				if(exito=="success") {
					if(objeto.responseText!=""){
						var tabla = document.getElementById(lalinea+bodega).parentNode;
						tabla.removeChild(document.getElementById(lalinea+bodega));
						totalizar();
						if (objeto.responseText==0)
							document.getElementById('total').innerHTML='<p style="padding:30px 0 400px 0;">No ha añadido ningún vino en su Bodega.<br /><a href="./" class="guardar">VOLVER AL CATÁLOGO</a></p>';
						else{
						document.getElementById('numero_el2').innerHTML=objeto.responseText;
						document.getElementById('numero_el').innerHTML=objeto.responseText;
						}
					}
				} else {
					alert("Se produjo un error al seleccionar el Producto");
				}
			}
		});
}

function actualiza_bodega(bodega,estilo,tipo){
	if (tipo=="1"){
		var lacantidad="cantidadv";
		var lalinea="lineav";
	}
	if (tipo=="2"){
		var lacantidad="cantidadp";
		var lalinea="lineap";
	}
	var cantidad=document.getElementById(lacantidad+bodega).value;
	var idtab=document.getElementById(lacantidad+bodega).tabIndex;
	$.ajax({
			type: "POST",
			url: "ajax/actualiza_mi_bodega.php",
			data: "id="+bodega+"&estilo="+estilo+"&cantidad="+cantidad+"&tipo="+tipo+"&idtab="+idtab,
			complete: function(objeto, exito) {
				if(exito=="success") {
					if(objeto.responseText!=""){
						$("#"+lalinea+bodega).fadeOut("slow");
						document.getElementById(lalinea+bodega).innerHTML=objeto.responseText;
						$("#"+lalinea+bodega).fadeIn("slow");
						totalizar();
					}
				} else {

					alert("Se produjo un error al seleccionar el Vino (actualiza Bodega)");
				}
			}
		});
}

function actualiza_precio(que,cuanto,linea){
	
	var req = $.ajax({
		type: "POST",
		url: "ajax/actualiza_precio.php",
		data: "linea=" + linea + "&cuanto=" + cuanto + "&que=" + que,
		complete: function(objeto, exito) {
			if (exito == "success") {
				if (objeto.responseText != ""){
					alert(objeto.responseText);
				}
			} else {
				alert("Se produjo un error al actualizar el Vino (No posicione el cursor en el campo a modificar)");
			}
		}
	});

	$(window).bind("beforeunload", function() { 
		while (req.readyState != 4) {
			alert('Su última modificación aún se está guardando.');
		}
    });	
		
}

function destaca(linea){
	if (document.getElementById('destacado'+linea).checked==true)
		sino=1;
	else
		sino=0;
	$.ajax({
			type: "POST",
			url: "ajax/destaca.php",
			data: "linea="+linea+"&sino="+sino,
			complete: function(objeto, exito) {
				if(exito=="success") {
					if(objeto.responseText!=""){
						alert(objeto.responseText);
					}
				} else {

					alert("Se produjo un error al seleccionar el Vino (destacado)");
				}
			}
		});
}

function resetea(){
	var combos=new Array("pais","zona","bodega","tipo","vino","categoria");
	
	for (unidad in combos){
		if (document.getElementById(combos[unidad])){
			document.getElementById(combos[unidad]).disabled=false;		
		}
	}
	
}
