var cartData;
function fnUpdateCart() {
new Ajax.Request('/viewCart.php', {
   onSuccess : function(transport) {
     cartData = eval('('+transport.responseText+')')
     if (cartData.cart.length>0) {
        $('cartCount').update(cartData.cart.length)
        $('cart').show();
      }
     else
    {
       cartData = null;
       $('cart').hide();
    }
   }
});
}

function addit(refr) {
  strPr = "product_id=";
  if (st = refr.indexOf(strPr)) {
    prID = refr.substr(st+strPr.length)
    new Ajax.Request("add2Cart.php?product_id="+prID, {
      method: 'get',
      onSuccess: function(transport) {
         if (transport.responseText!="success") {alert(transport.responseText)}
         else
        {
           if ($('showCart').readAttribute('sts')=="shown") {fnShowCart()}
          fnUpdateCart();
          setTimeout('fnShowCart()', 1250);
        }
      }
    });
  }

}

function fnDel(prID) {
  new Ajax.Request("delFromCart.php?product_id="+prID, {
    method: 'get',
    onSuccess: function(transport) {
      if (transport.responseText!="success") {alert(transport.responseText)}
      else
      {
        fnUpdateCart();
        fnShowCart();
         if (cartData.cart.length>0) {
           setTimeout('fnShowCart()', 1250);
        }
      }
    }
  })
}
function fnShowCart() {
  fnUpdateCart();
  if (($('showCart').readAttribute('sts') == null) || ($('showCart').readAttribute('sts')=='hidden')) {
    prodList = ""
    strTemp = "<table width='324' id='cartTable' cellpadding='0' cellspacing='0' style='padding: 0; margin: 0;'>"
    strTemp +="<tr><td class='ul'></td><td class='u'></td><td class='ur'></td></tr>";
    strTemp += "<tr><td class='l'><td style='background-color: #FFFFFF'>";
    strTemp += "<table width='300'><tr><td>Product</td><td>Price</td><td>remove</td></tr>"
    for (i=0;i<cartData.cart.length;i++) {
      strTemp += "<td>"+decodeURI(cartData.cart[i].product.title)+"</td><td>"+cartData.cart[i].product.price+"</td><td align='center'>"
      strTemp +="<img src='/images/cartDel.gif' class='cartimg' onclick='fnDel(\""+cartData.cart[i].product.product_id+"\")'></td></tr>"
      prodList += "&product_id[]="+cartData.cart[i].product.product_id;
     }
    strTemp += "<tr><td colspan='3' align='right'><a href='/members/signup.php?price_group=1"+prodList+"' onclick='agreement(this.href); return false'><img src='/images/checkOut.gif' class='cartimg'></a></td></tr>"
    strTemp += "</table>"
    strTemp +="</td><td class='r'></tr>"
    strTemp +="<tr><td class='bl'></td><td class='b'></td><td class='br'></td></tr></table>"
    ot = 52;
if (section =="Facilitators") {ot=56;}
    $('showCart').update(strTemp).clonePosition('cartImage', {'setWidth': false, 'setHeight': false, 'offsetLeft': -149, 'offsetTop': ot})
    Effect.BlindDown('showCart', {duration: .5});
    $('showCart').writeAttribute('sts', 'shown');
  }
  else
  {
    $('showCart').writeAttribute('sts', 'hidden');
    Effect.BlindUp('showCart', {duration: .5})
  }
}