function addToCart(idp, qty){   
    /// Aqui podemos enviarle alguna variable a nuestro script PHP
    tip = $('#jcart-tooltip');
    var messageItemAdded = 'Producto agregado al carro';
    /// { item_id: idp, item_qty: qty }
    /// Invocamos a nuestro script PHP 
     $.post("addtocart.php", { item_id: idp, item_qty: qty }, function(data){
       /// Ponemos la respuesta de nuestro script en el DIV recargado       
    //$("#basquet").html(data);   
    
        if(data=='1') //producto se agrego al carro
        {
        //alert("producto agregado");
        var btn_id = 'btn_'+idp;
        showHighlightMessage(messageItemAdded, btn_id);
		recargar();
		}else{
	
		var stock_real = 'stock'+idp;
		var stockValor = document.getElementById(stock_real).value;
		//alert(sr);
		if(data=='0'){
			if(qty > 0){
			confirm("Tenemos <b>"+stockValor+"</b> unidades disponible de este producto<br>&iquest;Deseas agregarlo a tu carro de compras?", function () {
				addToCart(idp, stockValor);
			});
			}
		}else{
			if(qty == 0){
			confirm2("No puedes ingresar cantidad con valor cero", function(){ recargar();});
			}else{
		    confirm2("El producto ya esta agregado en tu carro de compras", function(){ recargar();});
		    }
		}
		}
    });
                   
}

  function showHighlightMessage(msg, idboton){	
	var myDiv = document.getElementById(idboton);
	var xDiv = getDimensions(myDiv).x;	
	var yDiv = getDimensions(myDiv).y;
	var xxDiv = xDiv - 51;
	var yyDiv = yDiv + 24;
	
	tip.css({top: yyDiv + 'px', left: xxDiv + 'px'});

                  tip.html(msg);
          				tip.fadeIn("fast", function() {
          					setTimeout(function() { tip.fadeOut("fast"); }, 2000); 
          				}); 
          		
                }

 function recargar(){             
 /// Aqui podemos enviarle alguna variable a nuestro script PHP          
 var variable_post="Mi texto actualizado";             
 /// Invocamos a nuestro script PHP          
 $.post("cart.php", { variable: variable_post }, function(data){             
 /// Ponemos la respuesta de nuestro script en el p‡rrafo recargado  
 $("#basquet").html(data);      
 });  
 }

getDimensions = function(oElement) {

var x, y, w, h;

x = y = w = h = 0;

if (document.getBoxObjectFor) { // Mozilla

var oBox = document.getBoxObjectFor(oElement);

x = oBox.x-1;

w = oBox.width;

y = oBox.y-1;

h = oBox.height;

}

else if (oElement.getBoundingClientRect) { // IE

var oRect = oElement.getBoundingClientRect();

x = oRect.left-2;

w = oElement.clientWidth;

y = oRect.top-2;

h = oElement.clientHeight;

}

return {x: x, y: y, w: w, h: h};

}
