function validateNumber(event) {
    var key = window.event ? event.keyCode : event.which;

    if (event.keyCode == 8 || event.keyCode == 46
     || event.keyCode == 37 || event.keyCode == 39) {
        return true;
    }
    else if ( key < 48 || key > 57 ) {
        return false;
    }
    else return true;
};

$(document).ready(function() {
	 $('.quantity').keypress(validateNumber);

	/* when an item is added, display the cart */

  $('.continue').click(function(){
      window.location = "index.html";
  });	

  $('.addToCart').bind('click', function () {
  	window.location = 'cart.html';
  }); 
     
	$(document).plum('shop', {
		cartitem: '<span class="title">{title}<br/><em>{description}</em></span>'
		+ '<span class="quantity">{quantity}</span>'
		+ '<span class="price">{pricesingle}</span>'
		+ '<span class="price">{pricetotal}</span>'
		+ '<button class="remove checkout btn">Remove</button>',
		
		headerurl: 'http://www.prontolandmeasure.com/images/pronto-land-measure-header.jpg',
	  returnurl: 'http://www.prontolandmeasure.com/thanks-you.html',				

	// Callback function when the cart is emptied
		emptycart: function () {
		  		return confirm('Push "OK" to empty your shopping cart.');
		},
		
	shippingtype: 'function',
	
	/* NOTE: the first item in the cart will carry the shippingCost of the cart in it's 
	* shippingCost class (see updateTotals function).  s
	*/
	
	shipping: function() {
		if (this.shippingCost) {
			return this.shippingCost;
		}
		else {
			return 0;
		}
	},
	
	/* NOTE: we're only using this to update the cart when the 'remove' button is pushed.  Had to find a way to get
	 * the shipping totals updated and displayed again, and this was the function that seemed to be called LAST from
	 * plum.shop to make it happen. (there's no remove callback) The "checkout" class is added to the remove button…
	 */
	 
	checkout: function() {
	  window.location = 'cart.html';
	},
	
	/* Pronto has table pricing based on quantities of items. So, we have to loop through
		all items in the cart, count how many in each category, then loop again to reset
		their prices based on how many are in the cart. */
				
		updatetotals: function() {
			var qtotal = ctotal = ltotal = ktotal = 0;
			var qPrice = 7;
			var cPrice = 8;
			var lPrice = 5;
			var count = 0;
			var shipCalc = 0;
			
		/* count how many of each type of product */
			this.cart.each(function () {
        if (this.group) {
          switch (this.group) {
            case "scale": qtotal +=  this.quantity;
            break;
            case "compass": ctotal += this.quantity;
            break;
            case "triangle": case "map" :  ltotal += this.quantity;
            break;
            case "kit": ktotal += this.quantity;
            break;
            default: 
            break;
          }
        }
      });
 
 	/* figure out the discount pricing, if any */     
		  qPrice = qtotal > 5 ? (qtotal > 11 ? 6.0: 6.5) : 7;
		  cPrice = ctotal > 5 ? (ctotal > 11 ? 7.0: 7.5) : 8;
		  lPrice = ltotal > 5 ? (ltotal > 11 ? 4.0: 4.5) : 5;
		  count = qtotal + ctotal + ltotal;
		  if (count) {
  		  shipCalc = count >= 20 ? 9.95 : (count >= 13 ? 9.00 : (count >= 7 ? 7.0 : (count >= 3 ? 4.0 : 2.50)));
		  }
		  if (ktotal) {
		  	moreCost = ktotal < 5 ? 12.0 : 20.0;
		  	shipCalc += moreCost;
		  }
		  count = 0;
		  
	/* reset the pricing -
	* NOTE: We're sneaking in the price of the shipping for the cart in the first item's shippingCost class.
	* It's determined by the number of items in the cart: 1-2 = $2.50, 3-6 = $4, 7-12 = $7. Since this is
	* the place where we have those numbers, this is where we're storing it.  Then, in the shipping function,
	* it will return this cost from this first item.  All other items in the cart will have the ShippingCost
	* class be 0;
	*/
			this.cart.each(function () {
        if (this.group) {
        	if (!count) {
        	  this.shippingCost = shipCalc;
        	  count = 1;
        	}
          switch (this.group) {
            case "scale": 
            this.price = qPrice;
            break;   
            case "compass": this.price = cPrice;
            break;
            case "triangle": case "map": this.price = lPrice;
            break;
            default: 
            break;
          }
        }
      });
      return true;
		},
		cookie: 'plum_shop_pronto',
		paypaluser: 'prontolandmeasure',
		paypaldomain: 'comcast.net',
		returnurl: 'http://www.prontolandmeasure.com/thank-you.html'
	});
	
});

