function nuevoAjax()
{ 
	var xmlhttp=false; 
	try 
	{ 
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{ 
		try
		{ 
			// Creacion del objet AJAX para IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); } 

	return xmlhttp; 
}

function DisponibleInventario(IdProducto,UltimaCantidad, elemento, extra, auxiliar)
{
	if (elemento=="pp_cantidad") 
		{ 
			IdProducto = document.getElementById("pp_id").value; 
		}
	if (elemento.slice(0,6)=="txtQty") 
		{
			//alert(elemento);
			IdProducto = document.getElementById("hidProductId"+elemento.slice(6,8)).value; 
			//alert(IdProducto);
		}
	if (elemento.slice(0,11)=="aa_cantidad") 
		{
			//alert(elemento);
			IdProducto = document.getElementById("a_id"+elemento.slice(11,13)).value; 
			//alert(IdProducto);
		}	
	var ajax = nuevoAjax();
	ajax.open("post", "ajax/inventario/inventario.php", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	var URLparaEnviar = ("id="+IdProducto+"&cantidad="+UltimaCantidad);
	ajax.send(URLparaEnviar);
	ajax.onreadystatechange = function()
	{
		if (ajax.readyState==4)
		{
			if (ajax.responseText=="sin")
				{
					if (UltimaCantidad>=2) 
						{
							UltimaCantidad = UltimaCantidad -1;
						} else {
							UltimaCantidad = 0;
						}
					document.getElementById(elemento).value=UltimaCantidad;
					document.getElementById(auxiliar).value=UltimaCantidad;
					
					if (extra=="") 
						{
							nombreP='El producto seleccionado';
							alert("La cantidad indicada en el producto excede las cantidades disponibles!");
							
						} else { 
							nombreP = document.getElementById("titulo_prod").innerHTML;
							alert("La cantidad indicada en el producto < "+ nombreP+" > excede las cantidades disponibles!");
						}
				}			
		}
	}
	
}

function sumar(elemento, auxiliar, producto)
	{
		
	v = document.getElementById(elemento);
	tot  = document.getElementById("totalpro");
	
	aux = document.getElementById(auxiliar);
	if (v.value>=0)
		{
			v.value = parseInt(v.value) + 1;
			aux.value = v.value;
			tot.value = parseInt(tot.value) + 1;
		}
			
		var e3 = document.getElementById("botonImagen");
			if (tot.value>0)
		{
			
			e3.style.display = 'block';
		}else{
			e3.style.display = 'none';
			}
		DisponibleInventario(producto,v.value, elemento, "", auxiliar);
		return true
	}
	
function restar(elemento, auxiliar, producto)
	{
		tot  = document.getElementById("totalpro");
		
		v = document.getElementById(elemento);
	aux = document.getElementById(auxiliar);
	if (v.value>0)
		{
			v.value = parseInt(v.value) - 1;
			aux.value = v.value;
			
			tot.value = parseInt(tot.value) - 1;

		}

		var e3 = document.getElementById("botonImagen");
			if (tot.value>0)
		{
			
			e3.style.display = 'block';
		}else{
			e3.style.display = 'none';
			}
		//DisponibleInventario();	
		return true
	}