var theApp = null;
var displayedCart = false;
var timeoutID = null;

var ra = new Object();

ra.Cart = Class.create();
ra.Cart.prototype = {
  
  initialize: function() {
    this.subtotal = 0;
    theApp = this;
  },

  update_subtotal: function() {
    check_string = 'price_for_';
    subtotal = 0;
    i = 0;
    form_hash = document.forms[0];
    while (i < form_hash.length)
    {
      field = form_hash[i];
      i++;
      if (field.id.substring(0, check_string.length) == check_string)
      {
        id = field.id.substring(check_string.length, field.id.length);
        price = field.value;
        quantity = $(id).value;
        subtotal += quantity * price;
      }
    }
    $('total').innerHTML = '$' + subtotal.toFixed(2);
  },

  inc_quantity: function() {
    field = $A(arguments)[1];
    new_value = field.value - 0;
    new_value++;
    field.value = new_value;
    theApp.update_subtotal();
  },

  dec_quantity: function() {
    field = $A(arguments)[1];
    new_value = field.value - 0;
    new_value--;
	if (new_value < 0)
    {
       new_value = 0;
    }
    field.value = new_value;
    theApp.update_subtotal();
  }

};


function showCart()
{
  $('cart_overlay').style.display='block';
  $('cart').style.display='block';
  raCart = new ra.Cart();
  time = new Date();
  nocache = time.getTime();
  new Ajax.Updater('cart', 'http://www.rataway.com/cart.php?nocache=' + nocache, 
     { method: 'get', onComplete: wireCart}
  );
  timeoutID = setTimeout(cartFail, 7000);
  return false;
}

function cartSubmit()
{
  /*if ($('ack').checked != true) 
  { 
    alert('You must agree to the terms before continuing'); 
    return false;
  }
  else
  */
  if (($('rataway_regular').value - 0) + ($('rataway_waterproof').value - 0) == 0) 
  {
    alert('you must order something to continue'); 
    return false;
  }
  else 
  {
    $('rataway_regular').enable(); 
    $('rataway_waterproof').enable();
  }
}


function cartFail()
{
  if (displayedCart)
  {
    return;
  }
  cartLink = document.createTextNode('The cart redirect is taking too long, please follow this link...');
  $('cart').appendChild(cartLink);
  $('cart').appendChild(document.createElement('br'));
  cartLink = document.createElement('a');
  nocache = time.getTime();
  url = 'http://www.rataway.com/cart.php?nojs=true&nocache=' + nocache;
  cartLink.setAttribute('href', url);
  cartLink.appendChild(document.createTextNode(url));
  $('cart').appendChild(cartLink);
}

function wireCart()
{
  displayedCart = true;
  clearTimeout(timeoutID);
  Event.observe($('add_rataway_regular'), "click", raCart.inc_quantity.bindAsEventListener(null, $('rataway_regular')));    
  Event.observe($('sub_rataway_regular'), "click", raCart.dec_quantity.bindAsEventListener(null, $('rataway_regular')));  
  Event.observe($('add_rataway_waterproof'), "click", raCart.inc_quantity.bindAsEventListener(null, $('rataway_waterproof')));    
  Event.observe($('sub_rataway_waterproof'), "click", raCart.dec_quantity.bindAsEventListener(null, $('rataway_waterproof')));  
  
  $('spinner_for_rataway_regular').style.display = 'block';
  $('spinner_for_rataway_waterproof').style.display = 'block';

  $('rataway_regular').disable();
  $('rataway_waterproof').disable();

  raCart.update_subtotal();
}
