// **************************************************************************************************
// Vibracart Paypal V1.5
// Copyright (c) 2010-2011 Vibralogix
// www.vibralogix.com
// sales@vibralogix.com
// You are licensed to use this product on one domain and with one Paypal account only.
// Please contact us for extra licenses if required.
// **************************************************************************************************
var IE6 = (navigator.appVersion.indexOf("MSIE 6.")==-1) ? false : true;
var IE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
var IE8 = (navigator.appVersion.indexOf("MSIE 8.")==-1) ? false : true;
if (cart_url.charAt(cart_url.length-1)!="/")
  cart_url=cart_url+"/"
if ((IE6) && (IE6usegif))
{
  emptyCartImage=swapTogGif(emptyCartImage)
  deleteButton=swapTogGif(deleteButton)
  closeButton=swapTogGif(closeButton)
  updateButton=swapTogGif(updateButton)
  checkoutButton=swapTogGif(checkoutButton)
  busyImage=swapTogGif(busyImage)
  widgetbackground=swapTogGif(widgetbackground)
  widgetimage=swapTogGif(widgetimage)
}
// Set defaults for new variables that may not be set
if(typeof(usesandbox)=='undefined')
  var usesandbox=''
if(typeof(showTerms)=='undefined')
  var showTerms=false
if(typeof(cart_height_terms)=='undefined')
  var cart_height_terms=20
if(typeof(showItemImage)=='undefined')
  var showItemImage=false
if(typeof(usejavascriptalert)=='undefined')
  var usejavascriptalert=false
if(typeof(msg_checkout)=='undefined')
  var msg_checkout=''
if(typeof(messagedelay)=='undefined')
  var messagedelay=2000
if(typeof(errordelay)=='undefined')
  var errordelay=2000
if(typeof(itemquantitylimit)=='undefined')
  var itemquantitylimit=0
if(typeof(msg_itemquantity)=='undefined')
  var msg_itemquantity=''
if(typeof(cartquantitylimit)=='undefined')
  var cartquantitylimit=0
if(typeof(msg_cartquantity)=='undefined')
  var msg_cartquantity=''


cart_height_content=cart_height-cart_height_header-cart_height_footer
if (showTerms)
  cart_height_content=cart_height_content-cart_height_terms
optionValueSeparator=optionValueSeparator.replace(/&/g, '%26')
optionSeparator=optionSeparator.replace(/&/g, '%26')
idPrefix=idPrefix.replace(/&/g, '%26')
idSuffix=idSuffix.replace(/&/g, '%26')
discountPriceSeparator=discountPriceSeparator.replace(/&/g, '%26')
discountQuantityOperator=discountQuantityOperator.replace(/&/g, '%26')
var cart_item_quantity  = new Array()
var cart_item_image  = new Array()
var cart_item_description  = new Array()
var cart_item_total  = new Array()
var cart_numentries
var cart_total
var cart_currency_symbol
var cart_busy=false
var cart_animating=false
var cart_needs_refresh=true
var cart_fulltextdata=""
var cartinline=false
if (cartposition=="static")
  cartinline=true  
//if (!cartinline)
//  startcart()  
function startcart()
{
  if (!cartinline)
    insertcart()
  addButtonListener("")
  var vibracartdiv=document.getElementById("vibracart")   
  if (cartposition=="fixed")
  {
    if (IE6)
    {
      vibracartdiv.style.position="absolute"
      document.getElementById("vibracartwaiting").style.position="absolute"
      document.getElementById("vibracartalert").style.position="absolute"
    }
    else
    {
      vibracartdiv.style.position="fixed"
      document.getElementById("vibracartwaiting").style.position="fixed"
      document.getElementById("vibracartalert").style.position="fixed"
    }
  }
  else
  {
    vibracartdiv.style.position=cartposition
    document.getElementById("vibracartwaiting").style.position="absolute"
    document.getElementById("vibracartalert").style.position="absolute"
  }
  vibracartdiv.style.height="0px"
  vibracartdiv.style.width="0px"
  document.getElementById("vibracart_header").style.height=cart_height_header+"px"
  document.getElementById("vibracart_footer").style.height=cart_height_footer+"px"
  document.getElementById("vibracart_content").style.height=cart_height_content+"px"
  
  if ((IE7) || (IE6))
    document.getElementById("cartTable").style.width=(cart_width-20)+"px"
  
  if (use_widget)
  {
    document.write("<div class='vibracart_widget' id=\"vibracart_widget\">\n")
    document.write("<p class=\"vibracart_widget_image\"><img src=\""+cart_url+widgetimage+"\" onClick=\"showCart();\"></p>\n")
    document.write("<p class=\"vibracart_widget_items\" id=\"vibracart_widget_items\">Updating</p>\n")
    document.write("<p class=\"vibracart_widget_total\" id=\"vibracart_widget_total\">&nbsp;</p>\n")
    document.write("<p class=\"vibracart_widget_view\"><a href=\"\" class=\"vibracart_widget_view\" onclick=\"showCart(); return(false)\">View</a></p>\n")
    document.write("<p class=\"vibracart_widget_check\"><a href=\"\" class=\"vibracart_widget_check\" onClick='checkOut(); return(false)'>Checkout</a></p>\n")
    document.write("</div>\n")
    var widgetdiv=document.getElementById("vibracart_widget")
    widgetdiv.style.backgroundImage="url("+cart_url+widgetbackground+")"
    if (IE6)
      widgetdiv.style.position="absolute"    
    widgetdiv.style.height=widget_height+"px"
    widgetdiv.style.width=widget_width+"px"
    var lefttouse=widget_left
    var toptouse=widget_top 
    var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body
    var dsocleft=document.all? iebody.scrollLeft : pageXOffset
    var dsoctop=document.all? iebody.scrollTop : pageYOffset
    var dsocheight = window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight;
    var dsocwidth = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth; 
  // for CSS absolute
  //  if (widget_left==-1)
  //    lefttouse=Math.floor((dsocwidth-widget_width)/2)+dsocleft
  //  if (widget_top==-1)  
  //    toptouse=Math.floor((dsocheight-widget_height)/2)+dsoctop
  // for CSS fixed
    if (widget_left==-1)
      lefttouse=Math.floor((dsocwidth-widget_width)/2)
    if (widget_left==-2)
      lefttouse=Math.floor(dsocwidth-widget_width)
    if (widget_left<-2)
      lefttouse=Math.floor(dsocwidth-widget_width+widget_left)
    if (widget_top==-1)  
      toptouse=Math.floor((dsocheight-widget_height)/2)
    if (widget_top==-2)  
      toptouse=Math.floor(dsocheight-widget_height)
    if (widget_top<-2)  
      toptouse=Math.floor(dsocheight-widget_height+widget_top)
    widgetdiv.style.left=lefttouse+"px"
    widgetdiv.style.top=toptouse+"px"
  }
//  if ((use_widget) || (document.getElementById("showitemcount"))  || (document.getElementById("showcarttotal")))
//  {
    // Refresh data from server so that we can display items qty and total in widget
    postdata="cart_todo=getcart&ovspt="+optionValueSeparator+"&ospt="+optionSeparator+"&idpref="+idPrefix+"&idsuff="+idSuffix+"&dprspt="+discountPriceSeparator+"&dqtopt="+discountQuantityOperator+"&sandbox="+usesandbox+"&itemlmt="+itemquantitylimit+"&cartlmt="+cartquantitylimit
    // Update cart contents from server
    var xmlhttp = new XMLHttpRequest()
    xmlhttp.open('POST',cart_url+'cart.php',true)
    xmlhttp.onreadystatechange = function()
    {
      if (xmlhttp.readyState==4)
      {
        var cart_errormessage=""
        if (xmlhttp.status == 200)
        {
          cart_errormessage=updateFromXML(xmlhttp)
          updateCartTable()
          if ((initiallyshowemptywidget) && (use_widget))
            widgetdiv.style.visibility = 'visible'
        }
        showWaiting(false)
        cart_busy=false
        if (cart_errormessage!="")
          displayMessage(cart_errormessage)
  if ((showcartatstart) && (!hidecartifempty))
    showCart()
  if ((showcartatstart) && (hidecartifempty) && (cart_numentries>0))
    showCart()
          
      }   
    }
    cart_busy=true
    cart_needs_refresh=false;    
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    xmlhttp.send(postdata)  
//  }
//  if ((showcartatstart) && (!hidecartifempty))
//    showCart()
//  if ((showcartatstart) && (hidecartifempty) && (cart_numentries>0))
//    showCart()
}
//cart_addEvent(window, 'load', addListeners, false);

function insertcart()
{
  document.write("<form name=\"vibracartform\" action=\"\" >\n")
  document.write("<div class='vibracart' id='vibracart'>\n")
  document.write("<div class='vibracart_header' id='vibracart_header'>\n")
  if (closeButton!="")
    document.write("<div class='vibracart_closebutton'><img src='"+cart_url+closeButton+"' alt='Close' class='vibracart_closebutton' onClick='hideCart();'></div>\n")
  document.write("</div>\n")
  document.write("<div class='vibracart_content' id='vibracart_content'>\n")
  document.write("<table class='vibracart_table' id='cartTable'>\n")
  document.write("</table>\n")
  document.write("</div>\n")
  if (showTerms)
    document.write("<div class='vibracart_terms' id='vibracart_terms'><div class='vibracart_termscheckbox'><input class='vibracart_termscheckbox' type='checkbox' name='terms' value='termsagreed'></div><div class='vibracart_termslink'><a class='vibracart_termslink' href='"+termspage+"' target='"+termstarget+"'>"+termstext+"</a></div></div>\n")
  document.write("<div class='vibracart_footer' id='vibracart_footer'>")
  if (updateButton!='')
    document.write("<div class='vibracart_updatebutton'><img src='"+cart_url+updateButton+"' alt='Update' class='vibracart_updatebutton' onClick='updateQuantity();'></div>")
  document.write("<div class='vibracart_checkoutbutton'><img src='"+cart_url+checkoutButton+"' alt='Checkout' class='vibracart_checkoutbutton' onClick='checkOut();'></div><div class='vibracart_total'><span id='vibracarttotal'>0.00</span></div></div>\n")
  document.write("</div>\n")
  document.write("<div class='vibracart_waiting' id='vibracartwaiting'></div>\n")
  document.write("<div class='vibracart_alert' id='vibracartalert'></div>\n")
  document.write("</form>\n")
  if (usesandbox!="")
    document.write("<form action='https://www.sandbox.paypal.com/cgi-bin/webscr' method='post' name='paypalform' id='paypalform'>\n")
  else
    document.write("<form action='https://www.paypal.com/cgi-bin/webscr' method='post' name='paypalform' id='paypalform'>\n")  
  document.write("</form>\n")
}

function showCart()
{
  var lefttouse=cart_left
  var toptouse=cart_top 
  var dsocheight = window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight;
  var dsocwidth = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth; 
/*
  if (window.innerWidth)
  {
    var dsocwidth=window.innerWidth
    var dsocheight=window.innerHeight
  }
  else if (document.all)
  {
    var dsocwidth=document.body.clientWidth
    var dsocheight=document.body.clientHeight
  }
*/
  // Get top left of visible screen
  var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body
  var dsocleft=document.all? iebody.scrollLeft : pageXOffset
  var dsoctop=document.all? iebody.scrollTop : pageYOffset
  var vibracartdiv=document.getElementById("vibracart")
  if (!vibracartdiv)
    return
  var vibracart_waitingdiv = document.getElementById("vibracartwaiting")    
  var vibracart_alertdiv = document.getElementById("vibracartalert")    
  var vibracart_contentdiv=document.getElementById("vibracart_content")
  if (cartposition=="absolute")
  {
    if (cart_left==-1)
      lefttouse=Math.floor((dsocwidth-cart_width)/2)+dsocleft
    if (cart_left==-2)
      lefttouse=Math.floor(dsocwidth-cart_width)+dsocleft-20
    if (cart_top==-1)  
      toptouse=Math.floor((dsocheight-cart_height)/2)+dsoctop
    if (cart_top==-2)  
      toptouse=Math.floor(dsocheight-cart_height)+dsoctop-20
  }
  if (cartposition=="fixed")
  {
    if (cart_left==-1)
      lefttouse=Math.floor((dsocwidth-cart_width)/2)
    if (cart_left==-2)
      lefttouse=Math.floor(dsocwidth-cart_width)
    if (cart_left<-2)
      lefttouse=Math.floor(dsocwidth-cart_width+cart_left)    
    if (cart_top==-1)  
      toptouse=Math.floor((dsocheight-cart_height)/2)
    if (cart_top==-2)  
      toptouse=Math.floor(dsocheight-cart_height)
    if (cart_top<-2)  
      toptouse=Math.floor(dsocheight-cart_height+cart_top)
  }
  if (!cartinline) 
  {
    vibracartdiv.style.left=lefttouse+"px"
    vibracartdiv.style.top=toptouse+"px"
    vibracart_waitingdiv.style.left=vibracartdiv.style.left
    vibracart_waitingdiv.style.top=vibracartdiv.style.top  
    vibracart_alertdiv.style.left=vibracartdiv.style.left
    vibracart_alertdiv.style.top=vibracartdiv.style.top  
  }
  else
  {
    if ((IE7) || (IE6))
    {
      vibracart_waitingdiv.style.left=vibracartdiv.offsetParent.offsetLeft+vibracartdiv.offsetLeft+"px"
      vibracart_alertdiv.style.left=vibracartdiv.offsetParent.offsetLeft+vibracartdiv.offsetLeft+"px"
    }  
    else 
    { 
      vibracart_waitingdiv.style.left=vibracartdiv.offsetLeft+"px"
      vibracart_alertdiv.style.left=vibracartdiv.offsetLeft+"px"
    }  
    vibracart_waitingdiv.style.top=vibracartdiv.offsetTop+"px"    
    vibracart_alertdiv.style.top=vibracartdiv.offsetTop+"px"    
  }
  vibracartdiv.style.visibility = 'visible'
  if (animate_style=="none")
  {
    vibracartdiv.style.width=cart_width+"px"
    vibracartdiv.style.height=cart_height+"px"
    vibracart_waitingdiv.style.width=vibracartdiv.style.width
    vibracart_waitingdiv.style.height=vibracartdiv.style.height  
    vibracart_alertdiv.style.width=vibracartdiv.style.width
    vibracart_alertdiv.style.height=vibracartdiv.style.height  
  }   
  if (animate_style=="down")
  {
    cart_animating=true
    var curheight=parseInt(vibracartdiv.style.height)
    if (curheight!=cart_height)
    {
      vibracart_contentdiv.style.overflowY="hidden"
      vibracartdiv.style.width=cart_width+"px"
      var newheight=curheight+animate_pixels
      if (newheight>=cart_height)
        newheight=cart_height
      vibracartdiv.style.height=newheight+"px"
      vibracart_waitingdiv.style.width=vibracartdiv.style.width
      vibracart_waitingdiv.style.height=vibracartdiv.style.height  
      vibracart_alertdiv.style.width=vibracartdiv.style.width
      vibracart_alertdiv.style.height=vibracartdiv.style.height  
      if (newheight==cart_height)
      {
        vibracart_contentdiv.style.overflowY="auto"
        if ((IE7) || (IE6))
        {
          document.getElementById('cartTable').style.width="100px"
          document.getElementById('cartTable').style.width=(cart_width-20)+"px"
        }        
        cart_animating=false
      }
      if (newheight<cart_height)
        setTimeout("showCart()",animate_speed);
    }
  }
  if (animate_style=="right")
  {
    cart_animating=true
    var curwidth=parseInt(vibracartdiv.style.width)
    if (curwidth!=cart_width)
    {
      vibracart_contentdiv.style.overflowY="hidden"
      vibracartdiv.style.height=cart_height+"px"
      var newwidth=curwidth+animate_pixels
      if (newwidth>=cart_width)
        newwidth=cart_width
      vibracartdiv.style.width=newwidth+"px"
      vibracart_waitingdiv.style.width=vibracartdiv.style.width
      vibracart_waitingdiv.style.height=vibracartdiv.style.height  
      vibracart_alertdiv.style.width=vibracartdiv.style.width
      vibracart_alertdiv.style.height=vibracartdiv.style.height  
      if (newwidth==cart_width)
      {
        vibracart_contentdiv.style.overflowY="auto"
        if ((IE7) || (IE6))
        {
          document.getElementById('cartTable').style.width="100px"
          document.getElementById('cartTable').style.width=(cart_width-20)+"px"
        }
        cart_animating=false
      }
      if(newwidth<cart_width)
        setTimeout("showCart()",animate_speed);
    }
  }
  // If necessary refresh cart from server
  // Bug ridden IE always needs refresh to update scroll bars
//  cart_needs_refresh=true;
  if (cart_needs_refresh)
  {  
    postdata="cart_todo=getcart&ovspt="+optionValueSeparator+"&ospt="+optionSeparator+"&idpref="+idPrefix+"&idsuff="+idSuffix+"&dprspt="+discountPriceSeparator+"&dqtopt="+discountQuantityOperator+"&sandbox="+usesandbox+"&itemlmt="+itemquantitylimit+"&cartlmt="+cartquantitylimit
    // Update cart contents from server
    var xmlhttp = new XMLHttpRequest()
    xmlhttp.open('POST',cart_url+'cart.php',true)
    xmlhttp.onreadystatechange = function()
    {
      if (xmlhttp.readyState==4)
      {
        var cart_errormessage=""
        if (xmlhttp.status == 200)
        {
          cart_errormessage=updateFromXML(xmlhttp)
          updateCartTable()
        }
        showWaiting(false)
        cart_busy=false
        if (cart_errormessage!="")
          displayMessage(cart_errormessage)
      }   
    }
    cart_busy=true
    showWaiting(true)
    cart_needs_refresh=false;    
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    xmlhttp.send(postdata)
  }
}
function showWaiting(wait)
{
  var box= document.getElementById("vibracart")
  var boxcontent = document.getElementById("vibracart_content")
  var boxwait = document.getElementById("vibracartwaiting")
  if (!wait)
  {
    boxwait.style.visibility = 'hidden'
    boxcontent.style.opacity = '1.0'
    if (IE8)
      boxcontent.style.filter="none"
  }
  else	  
  {
    if (!cartinline) 
    {
      boxwait.style.left=box.style.left
      boxwait.style.top=box.style.top
    }
    else
    {
      if ((IE7) || (IE6))  
          boxwait.style.left=box.offsetParent.offsetLeft+box.offsetLeft+"px"
      else
        boxwait.style.left=box.offsetLeft+"px"
      boxwait.style.top=box.offsetTop+"px"
    }
    boxwait.style.width=box.style.width
    boxwait.style.height=box.style.height
    boxwait.style.backgroundImage="url("+cart_url+busyImage+")"
    boxwait.style.visibility = 'visible'
    boxcontent.style.opacity = '0.3'
    if (IE8)
      boxcontent.style.filter="alpha(opacity=30)"
    if (use_widget)
    {
      // Update widget fields
      document.getElementById("vibracart_widget_items").innerHTML="U"
      document.getElementById("vibracart_widget_total").innerHTML="&nbsp;"
    }
    if (document.getElementById("showitemcount"))
      document.getElementById("showitemcount").innerHTML="U"
    if (document.getElementById("showcarttotal"))
      document.getElementById("showcarttotal").innerHTML="&nbsp;"      
  }  
}
function showAlert(show,msg,type,delay)
{
  var box= document.getElementById("vibracart")
  var boxcontent = document.getElementById("vibracart_content")
  var boxalert = document.getElementById("vibracartalert")
  if (!show)
  {
    boxalert.style.visibility = 'hidden'
    boxcontent.style.opacity = '1.0'
    if (IE8)
      boxcontent.style.filter="none"
  }
  else	  
  {
    showCart()
    if (!cartinline) 
    {
      boxalert.style.left=box.style.left
      boxalert.style.top=box.style.top
    }
    else
    {
      if ((IE7) || (IE6))  
          boxalert.style.left=box.offsetParent.offsetLeft+box.offsetLeft+"px"
      else
        boxalert.style.left=box.offsetLeft+"px"
      boxalert.style.top=box.offsetTop+"px"
    }
    boxalert.style.width=box.style.width
    boxalert.style.height=box.style.height
    if (type==0)
      document.getElementById("vibracartalert").innerHTML="<p class='vibracart_alertmessage'>"+msg+"</p>"
    if (type==1)
      document.getElementById("vibracartalert").innerHTML="<p class='vibracart_alerterror'>"+msg+"</p>"
    boxalert.style.visibility = 'visible'
    boxcontent.style.opacity = '0.2'
    if (IE8)
      boxcontent.style.filter="alpha(opacity=20)"
    if (delay>0)  
      setTimeout('showAlert(false,"")',delay);      
  }  
}
function hideCart()
{
  var vibracartdiv=document.getElementById("vibracart")
  if (!vibracartdiv)
    return
  var vibracart_contentdiv=document.getElementById("vibracart_content")
  var vibracart_waitingdiv = document.getElementById("vibracartwaiting")         
  var vibracart_alertdiv = document.getElementById("vibracartalert")         
  if (animate_style=="none")
  {
/*
      vibracartdiv.style.visibility = 'hidden'
      vibracart_waitingdiv.style.visibility = 'hidden'
      vibracart_waitingdiv.style.width=vibracartdiv.style.width
      vibracart_waitingdiv.style.height=vibracartdiv.style.height      
      vibracart_alertdiv.style.visibility = 'hidden'
      vibracart_alertdiv.style.width=vibracartdiv.style.width
      vibracart_alertdiv.style.height=vibracartdiv.style.height      
*/
      vibracartdiv.style.visibility = 'hidden'
      vibracartdiv.style.width=0
      vibracartdiv.style.height=0
      vibracart_waitingdiv.style.visibility = 'hidden'
      vibracart_waitingdiv.style.width=0
      vibracart_waitingdiv.style.height=0      
      vibracart_alertdiv.style.visibility = 'hidden'
      vibracart_alertdiv.style.width=0
      vibracart_alertdiv.style.height=0      
  }
  if (animate_style=="down")
  {
    cart_animating=true
    var curheight=parseInt(vibracartdiv.style.height)
    vibracart_contentdiv.style.overflowY="hidden"   
    var newheight=curheight-animate_pixels
    if (newheight<0)
      newheight=0
    vibracartdiv.style.height=newheight+"px";
    vibracart_waitingdiv.style.width=vibracartdiv.style.width
    vibracart_waitingdiv.style.height=vibracartdiv.style.height   
    vibracart_alertdiv.style.width=vibracartdiv.style.width
    vibracart_alertdiv.style.height=vibracartdiv.style.height   
    if(newheight>0)
      setTimeout("hideCart()",animate_speed);
    if (newheight==0)
    {
      vibracartdiv.style.visibility = 'hidden'
      vibracart_contentdiv.style.overflowY="auto"        
      cart_animating=false
    }
  }
  if (animate_style=="right")
  {
    cart_animating=true
    var curwidth=parseInt(vibracartdiv.style.width)
    vibracart_contentdiv.style.overflowY="hidden"   
    var newwidth=curwidth-animate_pixels
    if (newwidth<0)
      newwidth=0
    vibracartdiv.style.width=newwidth+"px";
    vibracart_waitingdiv.style.width=vibracartdiv.style.width
    vibracart_waitingdiv.style.height=vibracartdiv.style.height      
    vibracart_alertdiv.style.width=vibracartdiv.style.width
    vibracart_alertdiv.style.height=vibracartdiv.style.height      
    if(newwidth>0)
      setTimeout("hideCart()",animate_speed);
    if (newwidth==0)
    {
      vibracartdiv.style.visibility = 'hidden'
      vibracart_contentdiv.style.overflowY="auto"            
      cart_animating=false
    }  
  }
}

function addButtonListener(e)
{
  if (!supportsAjax())
  {
    // Browser does not support AJAX
    return;
  }
  for( i=0; i < document.forms.length; i++)
  {
    // See if form is Paypal add to cart button
    if (document.forms[i].action)
    {
      formaction=document.forms[i].action.toLowerCase()
      if ((formaction.indexOf("https://www.paypal.",0)>-1) || (formaction.indexOf("https://paypal.",0)>-1) || (formaction.indexOf("https://www.sandbox.paypal.",0)>-1) || (formaction.indexOf("https://sandbox.paypal.",0)>-1))
      {
        if (document.forms[i].cmd)
        {
          if (document.forms[i].cmd.value=="_cart")
          {
            if (document.forms[i].display)
            {
              if (document.forms[i].display.value=="1")
              {
                // View cart button          
                cart_addEvent(document.forms[i], 'submit', viewCart, false)
                document.forms[i].target=""
                document.forms[i].action=""
              }  
            }
            else
            {
              // Add to cart button
              cart_addEvent(document.forms[i], 'submit', addToCart, false)
              document.forms[i].target=""
              document.forms[i].action=""          
            }  
          }
        }
      } 
    } 
  }
}

function addToCart(e)
{
  if (showcartonadd)
    showCart()
  // Get form
  var el;
  if (window.event && window.event.srcElement)
    el = window.event.srcElement;
  if (e && e.target)
    el = e.target;
  // Get item details
  var postdata=""
  for (k=0;k<el.elements.length;k++)
  {
    if (postdata!="")
      postdata=postdata+"&"
    postdata=postdata+el.elements[k].name+"="+el.elements[k].value
  }
  postdata=postdata+"&cart_todo=additem&ovspt="+optionValueSeparator+"&ospt="+optionSeparator+"&idpref="+idPrefix+"&idsuff="+idSuffix+"&dprspt="+discountPriceSeparator+"&dqtopt="+discountQuantityOperator+"&sandbox="+usesandbox+"&itemlmt="+itemquantitylimit+"&cartlmt="+cartquantitylimit
  //Send item details to server
  var xmlhttp = new XMLHttpRequest()
  xmlhttp.open('POST',cart_url+'cart.php',true)
  xmlhttp.onreadystatechange = function()
  {
    if (xmlhttp.readyState==4)
    {
      var cart_errormessage=""
      if (xmlhttp.status == 200)
      {
        cart_errormessage=updateFromXML(xmlhttp)
        updateCartTable()
      }
      showWaiting(false)
      cart_busy=false
      if (cart_errormessage!="")
        displayMessage(cart_errormessage)
    }   
  }
  cart_busy=true
  showWaiting(true)
  cart_needs_refresh=false;
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
  xmlhttp.send(postdata)
//  el.action=""
//  el.target=""
  cart_StopEvent(e)
}

function viewCart(e)
{
  showCart()
  cart_StopEvent(e)
}

function removeFromCart(num)
{
  postdata="cart_todo=removeitem&num="+num+"&ovspt="+optionValueSeparator+"&ospt="+optionSeparator+"&idpref="+idPrefix+"&idsuff="+idSuffix+"&dprspt="+discountPriceSeparator+"&dqtopt="+discountQuantityOperator+"&sandbox="+usesandbox+"&itemlmt="+itemquantitylimit+"&cartlmt="+cartquantitylimit
  //Send item details to server
  var xmlhttp = new XMLHttpRequest()
  xmlhttp.open('POST',cart_url+'cart.php',true)
  xmlhttp.onreadystatechange = function()
  {
    if (xmlhttp.readyState==4)
    {
      var cart_errormessage=""
      if (xmlhttp.status == 200)
      {
        cart_errormessage=updateFromXML(xmlhttp)
        updateCartTable()
      }
      showWaiting(false)
      cart_busy=false
      if (cart_errormessage!="")
        displayMessage(cart_errormessage)
    }   
  }
  cart_busy=true  
  showWaiting(true)
  cart_needs_refresh=false;
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
  xmlhttp.send(postdata)
}

function updateQuantity()
{
  if (cart_busy)
    return
  var el=document.vibracartform
  // Get item details
  var postdata=""
  for (k=0;k<el.elements.length;k++)
  {
    if (postdata!="")
      postdata=postdata+"&"
    postdata=postdata+el.elements[k].name+"="+el.elements[k].value
  }
  postdata=postdata+"&cart_todo=updateqty&ovspt="+optionValueSeparator+"&ospt="+optionSeparator+"&idpref="+idPrefix+"&idsuff="+idSuffix+"&dprspt="+discountPriceSeparator+"&dqtopt="+discountQuantityOperator+"&sandbox="+usesandbox+"&itemlmt="+itemquantitylimit+"&cartlmt="+cartquantitylimit
  //Send item details to server
  var xmlhttp = new XMLHttpRequest()
  xmlhttp.open('POST',cart_url+'cart.php',true)
  xmlhttp.onreadystatechange = function()
  {
    if (xmlhttp.readyState==4)
    {
      var cart_errormessage=""
      if (xmlhttp.status == 200)
      {
        cart_errormessage=updateFromXML(xmlhttp)
        updateCartTable()
      }
      showWaiting(false)
      cart_busy=false
      if (cart_errormessage!="")
        displayMessage(cart_errormessage)
    }   
  }
  cart_busy=true
  showWaiting(true)
  cart_needs_refresh=false;
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
  xmlhttp.send(postdata)
}

function checkOut()
{
  if (cart_busy)
    return
  if (cart_numentries==0)
    return
  var el=document.vibracartform
  if (showTerms)
  {
    if(!el.terms.checked)
    {
      el.terms.focus()
      displayMessage("3")
      return
    }  
  }    
  // Get item details
  var postdata=""
  for (k=0;k<el.elements.length;k++)
  {
    if (postdata!="")
      postdata=postdata+"&"
    postdata=postdata+el.elements[k].name+"="+el.elements[k].value
  }
  postdata=postdata+"&cart_todo=updateqty&ovspt="+optionValueSeparator+"&ospt="+optionSeparator+"&idpref="+idPrefix+"&idsuff="+idSuffix+"&dprspt="+discountPriceSeparator+"&dqtopt="+discountQuantityOperator+"&sandbox="+usesandbox+"&itemlmt="+itemquantitylimit+"&cartlmt="+cartquantitylimit
  //Send item details to server
  var xmlhttp = new XMLHttpRequest()
  xmlhttp.open('POST',cart_url+'cart.php',true)
  xmlhttp.onreadystatechange = function()
  {
    if (xmlhttp.readyState==4)
    {
      var cart_errormessage=""
      if (xmlhttp.status == 200)
      {
        cart_errormessage=updateFromXML(xmlhttp)
        updateCartTable()
        var pos1=cart_fulltextdata.indexOf("<checkoutform>",0)
        var pos2=cart_fulltextdata.indexOf("</checkoutform>",pos2)
        pos1=cart_fulltextdata.indexOf("<![CDATA[",pos1)
        pos2=cart_fulltextdata.indexOf("]]>",pos1)
        var formhtml=cart_fulltextdata.substring(pos1+10,pos2)
        checkoutform=document.getElementById('paypalform')
        checkoutform.innerHTML=formhtml
        document.paypalform.submit()
      }
//      showWaiting(false)
//      cart_busy=false      
      if (cart_errormessage!="")
        displayMessage(cart_errormessage)
    }   
  }
//  cart_busy=true
//  showWaiting(true)
  if (msg_checkout!="")
    displayMessage("4")  
  cart_needs_refresh=false;
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
  xmlhttp.send(postdata)
}

function updateFromXML(xmlhttp)
{
  // Get cart contents back from server for display
  var cart_errormessage=""
  if (null!=xmlhttp.responseXML.getElementsByTagName('errormessage')[0].firstChild)
    cart_errormessage=xmlhttp.responseXML.getElementsByTagName('errormessage')[0].firstChild.data
  cart_numentries=xmlhttp.responseXML.getElementsByTagName('numentries')[0].firstChild.data
  cart_numitems=xmlhttp.responseXML.getElementsByTagName('numitems')[0].firstChild.data
  cart_total=xmlhttp.responseXML.getElementsByTagName('carttotal')[0].firstChild.data
  cart_itemtoshow=xmlhttp.responseXML.getElementsByTagName('itemtoshow')[0].firstChild.data
  for (k=0;k<cart_numentries;k++)
  {
    cart_item_quantity[k]=xmlhttp.responseXML.getElementsByTagName('quantity')[k].firstChild.data
    cart_item_image[k]=xmlhttp.responseXML.getElementsByTagName('image')[k].firstChild.data
    cart_item_description[k]=xmlhttp.responseXML.getElementsByTagName('description')[k].firstChild.data
    cart_item_total[k]=xmlhttp.responseXML.getElementsByTagName('total')[k].firstChild.data  
  }
  cart_fulltextdata=xmlhttp.responseText
//  alert(cart_fulltextdata)
  return(cart_errormessage)     
}

function updateCartTable()
{
  objectContent=document.getElementById("vibracart_content")
  objectTable=document.getElementById('cartTable')
  // Get number of rows in table currently
  var oRows = objectTable.getElementsByTagName('tr');
  var existingrowcount = oRows.length;
  // for each item update or create row
  var col0, col1, col2, col3, col4, row, colcount
  if (cart_numentries>0)
    objectContent.style.backgroundImage="none"
  for (var k=0;k<cart_numentries;k++)
  {
    // If row already exists in table then update cell contents
    if (k<existingrowcount)
    {
      row=objectTable.rows[k].cells
      row.id="item"+k
      colcount=0
      if (showItemRemove)
      {
        row[colcount].innerHTML='<p class="vibracart_itemdelete"><img src="'+cart_url+deleteButton+'" alt="Remove Item" class="vibracart_itemdelete" onClick="removeFromCart('+k+');"></p>'
        colcount++
      }
      if (showItemQuantity)
      {
        row[colcount].innerHTML='<p class="vibracart_itemquantity"><input type="text" name="itemqty[]" value="'+cart_item_quantity[k]+'" size="2" class="vibracart_itemquantity"></p>'
        colcount++
      }
      if (showItemImage)
      {
        if (cart_item_image[k]!="null")
          row[colcount].innerHTML='<p class="vibracart_itemimage"><img src="'+cart_item_image[k]+'" class="vibracart_itemimage"></p>'
        else  
          row[colcount].innerHTML='<p class="vibracart_itemimage">&nbsp;</p>'
        colcount++
      }      
      if (showItemDescription)
      {
        row[colcount].innerHTML='<p class="vibracart_itemdescription">'+cart_item_description[k]+'</p>'
        colcount++
      }
      if (showItemTotal)
      {              
        row[colcount].innerHTML='<p class="vibracart_itemtotal">'+cart_item_total[k]+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>'          
      }
    }
    else
    {
      // If not then add row
      row=objectTable.insertRow(-1)
      row.id="item"+k
      if (k%2 == 0)
        row.className='vibracart_item_even'
      else
        row.className='vibracart_item_odd'
      colcount=0  
      if (showItemRemove)
      {            
        col0=row.insertCell(colcount)
        col0.className='vibracart_itemdelete'
        colcount++     
      }  
      if (showItemQuantity)
      {
        col1=row.insertCell(colcount)
        col1.className='vibracart_itemquantity'
        colcount++
      } 
      if (showItemImage)
      {
        col2=row.insertCell(colcount)
        col2.className='vibracart_itemimage'
        colcount++
      }       
      if (showItemDescription)
      {
        col3=row.insertCell(colcount)
        col3.className='vibracart_itemdescription'
        colcount++
      }  
      if (showItemTotal)
      {
        col4=row.insertCell(colcount)
        col4.className='vibracart_itemtotal'        
      }  
      if (showItemRemove)        
        col0.innerHTML='<p class="vibracart_itemdelete"><img src="'+cart_url+deleteButton+'" alt="Remove Item" class="vibracart_itemdelete" onClick="removeFromCart('+k+');"></p>'
      if (showItemQuantity)
        col1.innerHTML='<p class="vibracart_itemquantity"><input type="text" name="itemqty[]" value="'+cart_item_quantity[k]+'" size="2" class="vibracart_itemquantity"></p>'
      if (showItemImage)
      {
        if (cart_item_image[k]!="null")
          col2.innerHTML='<p class="vibracart_itemimage"><img src="'+cart_item_image[k]+'" class="vibracart_itemimage"></p>'
        else
          col2.innerHTML='<p class="vibracart_itemimage">&nbsp;</p>'          
      }  
      if (showItemDescription)
        col3.innerHTML='<p class="vibracart_itemdescription">'+cart_item_description[k]+'</p>'
      if (showItemTotal)
        col4.innerHTML='<p class="vibracart_itemtotal">'+cart_item_total[k]+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>'          
    }
  }
  // If there are rows not used anymore then remove them
  if (existingrowcount>cart_numentries)
  {
    for (k=cart_numentries;k<existingrowcount;k++)
    {
      objectTable.deleteRow(k)
    }
  }
  if (cart_numentries==0)
  {
    // Cart is empty
    objectContent.style.backgroundImage="url("+cart_url+emptyCartImage+")"
    if (hidecartifempty)
    {
      hideCart()
    }  
  }
  // Update total on cart
  var carttotaltext = document.getElementById("vibracarttotal")
  carttotaltext.innerHTML=cart_total
  if (cart_itemtoshow>-1)
  {
    obj=document.getElementById("item"+cart_itemtoshow)
    objectContent.scrollTop = obj.offsetTop
  }
  if (use_widget)
  {
    // Update widget fields
    document.getElementById("vibracart_widget_items").innerHTML=cart_numitems+" "+msg_items
    document.getElementById("vibracart_widget_total").innerHTML=cart_total
    // If cart empty then hide widget if required
    if ((hidewidgetifempty) && (cart_numentries==0) && (use_widget))
      document.getElementById("vibracart_widget").style.visibility = 'hidden'
    else
      document.getElementById("vibracart_widget").style.visibility = 'visible'  
  }
  if (document.getElementById("showitemcount"))
    document.getElementById("showitemcount").innerHTML=cart_numitems
  if (document.getElementById("showcarttotal"))
    document.getElementById("showcarttotal").innerHTML=cart_total       
}

function cart_addEvent(elm, evType, fn, useCapture)
// cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
// By Scott Andrew
{
  if (elm.addEventListener) {
    elm.addEventListener(evType, fn, useCapture); 
    return true; 
  } else if (elm.attachEvent) {
    var r = elm.attachEvent('on' + evType, fn); 
    return r; 
  } else {
    elm['on' + evType] = fn;
  }
}

function cart_StopEvent(pE)
{
   if (!pE)
     if (window.event)
	pE = window.event;
     else
	return;
   if (pE.cancelBubble != null)
      pE.cancelBubble = true;
   if (pE.stopPropagation)
      pE.stopPropagation();
   if (pE.preventDefault)
      pE.preventDefault();
   if (window.event)
      pE.returnValue = false;
   if (pE.cancel != null)
      pE.cancel = true;
}

function supportsAjax()
{
  var xhr = null
  try { xhr = new XMLHttpRequest(); } catch (e) {}
  try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  return (xhr!=null)
}

function displayMessage(msg)
{
  if (msg=="1")
  {
    msgtxt=msg_button
    type=1
    delay=errordelay
  }  
  if (msg=="2")
  {
    msgtxt=msg_currency
    type=1
    delay=errordelay
  }  
  if (msg=="3")
  {
    msgtxt=msg_terms
    type=1
    delay=errordelay
  }    
  if (msg=="4")
  {
    msgtxt=msg_checkout
    type=0
    delay=messagedelay
  }  
  if (msg=="5")
  {
    msgtxt=msg_itemquantity
    type=1
    delay=errordelay
  }  
  if (msg=="6")
  {
    msgtxt=msg_cartquantity
    type=1
    delay=errordelay
  }  
  if (msgtxt=="")
    return
  if (usejavascriptalert)       
    alert (msgtxt)
  else
    showAlert(true,msgtxt,type,delay)
}

function swapTogGif(fn)
{
  return (fn.replace (/\.[^\.]*$/, '.gif'))
}

function cart_addItemLink(lnk)
{
  lnk=lnk.substring(lnk.indexOf('?')+1, lnk.length)
  if (showcartonadd)
    showCart()
  postdata=lnk+"&cart_todo=additem&ovspt="+optionValueSeparator+"&ospt="+optionSeparator+"&idpref="+idPrefix+"&idsuff="+idSuffix+"&dprspt="+discountPriceSeparator+"&dqtopt="+discountQuantityOperator+"&sandbox="+usesandbox+"&itemlmt="+itemquantitylimit+"&cartlmt="+cartquantitylimit
  //Send item details to server
  var xmlhttp = new XMLHttpRequest()
  xmlhttp.open('POST',cart_url+'cart.php',true)
  xmlhttp.onreadystatechange = function()
  {
    if (xmlhttp.readyState==4)
    {
      var cart_errormessage=""
      if (xmlhttp.status == 200)
      {
        cart_errormessage=updateFromXML(xmlhttp)
        updateCartTable()
      }
      showWaiting(false)
      cart_busy=false
      if (cart_errormessage!="")
        displayMessage(cart_errormessage)
    }   
  }
  cart_busy=true
  showWaiting(true)
  cart_needs_refresh=false;
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
  xmlhttp.send(postdata)
}


