function CheckZipCode() {
	if ($('zipcode').value) {
		// get new shipping rates
		GetShippingRates('form-continue');
	} else {
		ClearShippingRates();
	}
	
	// reset current zipcode value
	$('zipcode_current').value = $('zipcode').value;
}
function ClearShippingRates() {
	$('shipping_method').update('<option value="">please choose</option>');
	
	// reset totals
	ClearTotals();
}
function ClearTotals() {
	/*
	// update discount
	if ($('discount-cell')) {
		$('discount').value = 0;
		$('discount-cell').update(formatNumber(parseFloat($('discount').value), 2, ',', '.', '$', '', ''));
	}
	*/

	// update shipping
	$('shipping-description').update('Shipping:');
	$('shipping').value = 0;
	$('shipping-cell').update(formatNumber(parseFloat($('shipping').value), 2, ',', '.', '$', '', ''));
	
	// update tax
	$('tax').value = 0;
	$('tax-cell').update(formatNumber(parseFloat($('tax').value), 2, ',', '.', '$', '', ''));
	
	// update total
	$('total').value = parseFloat($('subtotal').value) - parseFloat($('discount').value) + parseFloat($('shipping').value) + parseFloat($('tax').value);
	$('total-cell').update(formatNumber(parseFloat($('total').value), 2, ',', '.', '$', '', ''));
}
function GetShippingRates(id) {
	if ($(id)) {
		if ($('zipcode').value) {
			// clear previous errors
			ClearErrors(id);
			ClearNotice();
			
			// validate input
			var valid_zipcode = new RegExp(/^[0-9]{5}(-[0-9]{4})?$/);
			if (!valid_zipcode.test($('zipcode').value)) {
				// invalid zip code, display notice
				var notice = '<strong>Please note the following issues:</strong><ul><li>Invalid format: <a href="#zipcode" class="required" onclick="$(\'zipcode\').focus();return false;">Zip Code</a></li></ul>';
				SetNotice(escape(notice), 'error');
				
				ClearShippingRates();
			} else {
				// disable button
				$('submit-button').innerHTML = 'Wait&hellip;';
			  $('submit-button').disabled  = true;
				
				// get rates via AJAX
	      var url    = '/ajax/get_shipping_rates/' + $('zipcode').value;
	      var myAjax = new Ajax.Request(
	        url,
	        {
	          method:'get',
	          onComplete:function(transport) {
							// get response (expecting JSON)
							var response = transport.responseJSON;
							
							// show form field and update button
							$('submit-button').innerHTML = 'Get Rates';
							$('submit-button').disabled  = false;
							
							// errors?
							if (response.errors) {
								// clear previous errors
								ClearErrors(id);
								
								// build error message and highlight fields
								var msg    = '';
								var errors = $H(response.errors);
								errors.each(function(pair) {
									// KLUDGE: need to assign errors differently for form errors and UPS errors
									if (pair.key == 'zipcode') {
										$('f-' + pair.key).addClassName('error');
										msg = msg + '<li>' + pair.value + '</li>';
									} else {
										msg = msg + '<li>Error Code ' + pair.key + ': ' + pair.value + '</li>';
									}
								});
								
								// display notice
								var notice = '<strong>Please note the following issues:</strong><ul>' + msg + '</ul>';
								SetNotice(escape(notice), 'error');
								
								// apply focus
						  	$('zipcode').focus();
							} else if (response.rates) {
								// clear errors
								ClearErrors(id);
								
								// build options for shipping methods
								var options = '<option value="">please choose</option>';
								var rates = $H(response.rates);
								rates.each(function(pair) {
									options = options + '<option value="' + pair.key + '"' + ((response.selected_method && (response.selected_method == pair.key)) ? ' selected="selected"' : '') + '>' + pair.value + '</option>';
								});
								$('shipping_method').update(options);
				
								// apply focus
						  	$('shipping_method').focus();

								// apply changes to shipping cost (will also update tax)
								UpdateShipping();
							}
	          }
	        }
	      );
			}
		} else {
			// missing zip code, display notice
			var notice = '<strong>Please note the following issues:</strong><ul><li>Missing: <a href="#zipcode" class="required" onclick="$(\'zipcode\').focus();return false;">Delivery Zip Code</a></li></ul>';
			SetNotice(escape(notice), 'error');
			
			ClearShippingRates();
		}
		
		// reset current zipcode value
		$('zipcode_current').value = $('zipcode').value;
	}
}
function RemoveProduct(id) {
  if ($('product' + id)) {
    // highlight selected row
    Element.addClassName($('product' + id), 'highlight');
    
    // ask for confirmation
    response = confirm("Please confirm you wish to remove the highlighted product.");
    if (response === true) {
      // remove item from session
      var url    = '/ajax/remove_product/' + id;
      var myAjax = new Ajax.Request(
        url,
        {
          method:'get',
          onComplete:function(transport) {
            // remove element
            Element.remove('product' + id);
						
						// get response (expecting JSON)
						var response = transport.responseJSON;
						
						if (response.empty_cart) {
							$('cart-content').update(response.empty_cart);
						} else {
							// update totals
							$('subtotal').value = response.subtotal;
							$('subtotal-cell').update(formatNumber(parseFloat($('subtotal').value), 2, ',', '.', '$', '', ''));
							
							if ($('discount-cell')) {
								$('discount').value = response.discount;
								$('discount-cell').update('- ' + formatNumber(parseFloat($('discount').value), 2, ',', '.', '$', '', ''));
							}

							// get shipping rates (discount, shipping and tax will be updated)
							GetShippingRates('form-continue');
							
							// update total
							$('total').value = parseFloat($('subtotal').value) - parseFloat($('discount').value) + parseFloat($('shipping').value) + parseFloat($('tax').value);
							$('total-cell').update(formatNumber(parseFloat($('total').value), 2, ',', '.', '$', '', ''));
						}
          }
        }
      );
    } else {
      // remove highlight
      Element.removeClassName($('product' + id), 'highlight');
    }
  }
}
function UpdateSalesTax() {
	var url    = '/ajax/get_tax_from_zipcode/' + $('zipcode').value + '/' + $('subtotal').value + '/' + $('discount').value + '/' + $('shipping').value;
	var myAjax = new Ajax.Request(
		url,
		{
			method:'get',
			onComplete:function(transport) {
				// get response (expecting JSON)
				var response = transport.responseJSON;
				
				// update tax
				$('tax').value = response.tax;
				$('tax-cell').update(formatNumber(parseFloat($('tax').value), 2, ',', '.', '$', '', ''));
				
				// update total
				$('total').value = parseFloat($('subtotal').value) - parseFloat($('discount').value) + parseFloat($('shipping').value) + parseFloat($('tax').value);
				$('total-cell').update(formatNumber(parseFloat($('total').value), 2, ',', '.', '$', '', ''));
			}
		}
	);
}
function UpdateShipping(promotion) {
	if ($('shipping_method').value) {
		// prep shipping amount
		var shipping_method_info = $('shipping_method').value.split('-');
		
		// update shipping amount
		if (shipping_method_info[0]) {
			var description = 'Shipping:<br /><span class="shipping-method">(' + shipping_method_info[0] + ')</span>';
			if (shipping_method_info[2] && shipping_method_info[3]) {
				description = description + '<br /><span class="shipping-method">(' + shipping_method_info[2] + ' - $<del>' + shipping_method_info[3] + '</del>)</span>';
			}
			$('shipping-description').update(description);
		}
		$('shipping').value = (shipping_method_info[1]) ? shipping_method_info[1] : 0;
		var shipping_amount = formatNumber(parseFloat($('shipping').value), 2, ',', '.', '$', '', '');
		
		// show highlight?
		if (shipping_method_info[2] && shipping_method_info[3]) {
			$('shipping-cell').update('<span class="discount">' + shipping_amount + '</span>');
		} else {
			$('shipping-cell').update(shipping_amount);
		}
	
		// update sales tax for TX zip codes
		UpdateSalesTax();
	} else {
		// reset totals
		ClearTotals();
	}
	
	if ($('zipcode').value && $('shipping_method').value) {
		// show checkout buttons
		$('navigation-proceed-top').show()
		$('navigation-proceed-bottom').show();
	} else {
		// hide checkout buttons
		$('navigation-proceed-top').hide()
		$('navigation-proceed-bottom').hide();
	}
}
