
window.onload=function(){if($('cart'))
{var draggables=$$("div.productImg");draggables.each(function(currentDraggable){new Draggable(currentDraggable,{revert:true,ghosting:true});});Droppables.add("cart",{hoverclass:"cartOnHover",onDrop:function(element){var itemId=element.id.split("_");addToCart(itemId[1],"msg_"+itemId[1]);}});startAjax();}
if($('cos'))
{Event.observe('cos','click',function(event){var element=$(Event.element(event));if(element.name=="tip_factura"||element.name=="metoda_plata"){refreshTransport(element)}
if(element.name=="transport"){recalculeazaCos(element.value);}});}}
function startAjax()
{Ajax.Responders.register({onCreate:function(){if(Ajax.activeRequestCount>0)
Element.show($('indicator'));},onComplete:function(){if(Ajax.activeRequestCount==0)
Element.hide($('indicator'));}});}
function addToCart(id,msg_id)
{new Ajax.Request(url_base+'server.php?action=addToCart&'+Math.random(9999),{parameters:'id='+id,onSuccess:function(resp){var cartUpdate=eval('('+resp.responseText+')');var isNew=cartUpdate['cartItemDetails'][0].isNew;Element.update($('nrItems'),cartUpdate['cartItemDetails'][0].nrItems);if(cartUpdate['cartItemDetails'][0].msg!="")
{Element.update($('cartMsg'),cartUpdate['cartItemDetails'][0].msg);new Effect.Highlight('cartMsg',{startcolor:'#CCCCCC',endcolor:'#F7F7F7',restorecolor:'#F7F7F7'});}
if(cartUpdate['cartItemDetails'][0].discount!="")
{Element.update($('cartTotalDiscount'),'Reducere: -'+cartUpdate['cartItemDetails'][0].discount+' RON');new Effect.Highlight('cartTotalDiscount',{startcolor:'#CCCCCC',endcolor:'#F7F7F7',restorecolor:'#F7F7F7'});}
if(isNew==1)
{$(msg_id).show();Effect.Fade(msg_id);var newItem='<div id="cartItem_'+id+'" style="display:none" class="row">';newItem+='<div class="cell1" id="cartItemQty_'+id+'" valign="top">1</div>';newItem+='<div class="cell2">'+cartUpdate['cartItemDetails'][0].title+'</div>';newItem+='<div class="cell3" id="cartItemPrice_'+id+'">'+cartUpdate['cartItemDetails'][0].newPrice+' RON</div>';newItem+='<div class="cell4"><a href="'+url_base+'server.php?action=removeFromCart&id='+id+'" onclick="return removeFromCart('+id+')">';newItem+='X</a></div><div class="clear"></div></div>';if($('cartIsEmpty'))
Element.hide($('cartIsEmpty'));new Insertion.Bottom('cartItems',newItem);Effect.Appear('cartItem_'+id,{duration:0.5});Element.update($('cartTotalAmount'),'<b>Total: '+cartUpdate['cartItemDetails'][0].total+' RON</b>');new Effect.Highlight('cartTotalAmount',{startcolor:'#CCCCCC',endcolor:'#F7F7F7',restorecolor:'#F7F7F7'});}
else
{$(msg_id).show();Effect.Fade(msg_id);Element.update($('cartItemQty_'+id),cartUpdate['cartItemDetails'][0].newQty);Element.update($('cartItemPrice_'+id),cartUpdate['cartItemDetails'][0].newPrice+' RON');Element.update($('cartTotalAmount'),'<b>Total: '+cartUpdate['cartItemDetails'][0].total+' RON</b>');new Effect.Highlight('cartTotalAmount',{startcolor:'#CCCCCC',endcolor:'#F7F7F7',restorecolor:'#F7F7F7'});new Effect.Highlight('cartItemPrice_'+id,{startcolor:'#CCCCCC',endcolor:'#F7F7F7',restorecolor:'#F7F7F7'});new Effect.Highlight('cartItemQty_'+id,{startcolor:'#CCCCCC',endcolor:'#F7F7F7',restorecolor:'#F7F7F7'});}}});return false;}
function removeFromCart(id)
{Effect.Fade("cartItem_"+id);new Ajax.Request(url_base+"server.php?action=removeFromCart&"+Math.random(9999),{parameters:"id="+id,onSuccess:function(resp){var cartRemove=eval('('+resp.responseText+')');var total=cartRemove['cartDetails'][0].total;var discount=cartRemove['cartDetails'][0].discount;Element.update($('nrItems'),cartRemove['cartDetails'][0].nrItems);if(cartRemove['cartDetails'][0].msg=="")
{Element.update($('cartMsg'),'');}
if(total==0)
{Element.update($('cartItems'),'<div id="cartIsEmpty">Cosul este gol.</div>');Element.update($('cartTotalAmount'),'<b>Total: 0.00 RON</b>');new Effect.Highlight('cartIsEmpty',{startcolor:'#CCCCCC',endcolor:'#F7F7F7',restorecolor:'#F7F7F7'});}
else
{if(discount=="")
{Element.update($('cartTotalDiscount'),'');}
else
{Element.update($('cartTotalDiscount'),'Reducere: -'+discount+' RON');new Effect.Highlight('cartTotalDiscount',{startcolor:'#CCCCCC',endcolor:'#F7F7F7',restorecolor:'#F7F7F7'});}
Element.update($('cartTotalAmount'),'<b>Total: '+total+' RON</b>');}
new Effect.Highlight('cartTotalAmount',{startcolor:'#CCCCCC',endcolor:'#F7F7F7',restorecolor:'#F7F7F7'});}});return false;}
function emptyCart()
{new Ajax.Request(url_base+'server.php?action=emptyCart&'+Math.random(9999),{onSuccess:function(resp){if(resp.responseText==1)
{Element.update($('nrItems'),'0');Element.update($('cartItems'),'<div id="cartIsEmpty">Cosul este gol.</div>');Element.update($('cartTotalDiscount'),'');Element.update($('cartMsg'),'');Element.update($('cartTotalAmount'),'<b>Total: 0.00 RON</b>');new Effect.Highlight('cartIsEmpty',{startcolor:'#CCCCCC',endcolor:'#F7F7F7',restorecolor:'#F7F7F7'});new Effect.Highlight('cartTotalAmount',{startcolor:'#CCCCCC',endcolor:'#F7F7F7',restorecolor:'#F7F7F7'});}}});return false;}