/*document.observe("dom:loaded", function() {
	// Show the Checkout area when they request it
	if($('checkout_info')) $('checkout_info').previous('.checkout').observe('click', function(event) {
		$('checkout_info').show();
		this.hide();
	});
	
	if($('get_shipping_estimate')) $('get_shipping_estimate').observe('dom:click', function(event) {
		var shippingEstimateButton = this;
		
		var postalCode = shippingEstimateButton.previous('input.postal_code_shipping_estimator').value.gsub(/\s/, '');
		var country = (postalCode.length == 6)? 'CA':'US';
		cart.country = country;
		var dimentions = cart.getTotalDimentions();
		
		shippingEstimateButton.addClassName('working');
*/		
		/* // This is the old JSONP method that IE was complainging about.
		var rateURL = 'http://www.pointscape.org/yp/ups/get_rates.php?postalCode='+escape(postalCode)+'&country='+country+'&length='+dimentions.length+'&width='+dimentions.width+'&height='+dimentions.height+'&weight='+dimentions.weight+'&cachekill=' + new Date().getTime();
		
		window['get_shipping_estimate_result'] = function(data) {
			var shippingEstimateButton = $('get_shipping_estimate');
			
			shippingEstimateButton.previous('input.postal_code_shipping_estimator').removeClassName('invalid');
			
			if(!data || data.rate == '') {
				shippingEstimateButton.next('span').update('--.--');
				shippingEstimateButton.previous('input.postal_code_shipping_estimator').addClassName('invalid');
				shippingEstimateButton.previous('input.postal_code_shipping_estimator').focus();
			}
			else {
				shippingEstimateButton.next('span').update(parseFloat(data.rate).toFixed(2));
				shippingEstimateButton.next('span.currency').update(data.currency);
			}
			
			shippingEstimateButton.removeClassName('working');
			$$('.observesCart').invoke('fire', 'cart:refresh');
		};
		
		var tag = new Element('script', { src: rateURL, type: "text/javascript" });
		$(document.body).insert(tag);
		*/
/*		
		new Ajax.Request('/ups/loopydo.php?cachekill=' + new Date().getTime(), {
			method: 'get',
			parameters: {
				postalCode: escape(postalCode),
				country: country,
				length: dimentions.length,
				width: dimentions.width,
				height: dimentions.height,
				weight: dimentions.weight
			},
			onComplete: function(transport) {
				var data = transport.responseText.evalJSON();
				
				shippingEstimateButton.previous('input.postal_code_shipping_estimator').removeClassName('invalid');
				
				if(!data || data.rate == '') {
					shippingEstimateButton.next('span').update('--.--');
					shippingEstimateButton.previous('input.postal_code_shipping_estimator').addClassName('invalid');
					shippingEstimateButton.previous('input.postal_code_shipping_estimator').focus();
				}
				else {
					shippingEstimateButton.next('span').update(parseFloat(data.rate).toFixed(2));
					shippingEstimateButton.next('span.currency').update(data.currency);
				}
				
				shippingEstimateButton.removeClassName('working');
				$$('.observesCart').invoke('fire', 'cart:refresh');
			}
		});
	});
	if($('get_shipping_estimate')) $('get_shipping_estimate').previous('input.postal_code_shipping_estimator').observe('keypress', function(event) {
		if(event.keyCode == Event.KEY_RETURN)
			$('get_shipping_estimate').fire('dom:click');
	});
	
	$$('#separate_shipping_address').invoke('observe', 'click', function(event) {
		if(this.checked)
			$('shipping_address').show();
		else
			$('shipping_address').hide();
	});
	
	// Make the Country Chooser update the provinces/states listed
	$$('#checkout_info .country_chooser').invoke('observe', 'change', function(event) {
		if(this.value == 'US') {
			this.up('table').down('.province_us').show();
			this.up('table').down('.province_ca').hide();
		}
		else {
			this.up('table').down('.province_ca').show();
			this.up('table').down('.province_us').hide();
		}
	});
	
	if($('checkout_info')) $('checkout_info').down('.payByPaypal').observe('click', function(event) {
		if(cart.getShippingTotal() <= 0) {
			alert('Please calculate your shipping cost before continuing.');
			event.stop();
			return;
		}
		
		var dataArea = this.previous('.dataArea');
		
		var data = {};
		
		data.cartItems = cart.getItems();
		data.item_amount = cart.getItemTotal().toFixed(2);
		data.tax_amount = cart.getTaxTotal().toFixed(2);
		data.shipping_amount = cart.getShippingTotal().toFixed(2);
		data.total_amount = cart.getTotal().toFixed(2);
		
		dataArea.value = $H(data).toJSON();
	});
	
	if($('checkout_info')) $('checkout_info').down('.submitOrder').observe('click', function(event) {
		if(this.hasClassName('disabled')) {
			event.stop();
			return;
		}
		
		if(cart.getShippingTotal() <= 0) {
			alert('Please calculate your shipping cost before continuing.');
			event.stop();
			return;
		}
		
		this.addClassName('disabled');
		this.setOpacity(0.5);
		this.next('.submitOrder_spinner').show();
		
		var data = {};
		
		$('checkout_info').select('[name]').each(function(dataItem) {
			data[dataItem.readAttribute('name')] = (dataItem.value == dataItem.readAttribute('placeholderText'))? '':dataItem.value;
		});
		
		if(data.country == 'CA')
			data.province = data.province_ca;
		else
			data.province = data.province_us;
		
		if(data.shipping_country == 'CA')
			data.shipping_province = data.shipping_province_ca;
		else
			data.shipping_province = data.shipping_province_us;
		
		data.card_expiry = data.card_expiry_month + data.card_expiry_year;
		
		data.cartItems = cart.getItems();
		data.item_amount = cart.getItemTotal().toFixed(2);
		data.tax_amount = cart.getTaxTotal().toFixed(2);
		data.shipping_amount = cart.getShippingTotal().toFixed(2);
		data.total_amount = cart.getTotal().toFixed(2);
		
		//alert($H(data).toJSON());
		
		new Ajax.Request('/paypal/doCheckout.php', {
			method: 'post',
			parameters: {
				data: $H(data).toJSON()
			},
			onComplete: function(transport) {
				//alert(transport.responseText);
				var result = transport.responseText.evalJSON();
				
				if(result.status == 'success') {
					cart.clearCart();
					window.location = '/thankyou?confirm='+result.transaction_id;
					// alert(result.message);
				}
				else{
					$('checkout_info').down('.checkout_error').show().update(result.message+'&nbsp;&nbsp;&nbsp;&nbsp;<span>Error Code: '+result.code+'</span><p>If this error continues please email us at: info@orangedog.ca</p>');
					$('checkout_info').down('.checkout_error span').setOpacity(0.5);
					
					$('checkout_info').down('.submitOrder').removeClassName('disabled');
					$('checkout_info').down('.submitOrder').setOpacity(1);
					$('checkout_info').down('.submitOrder_spinner').hide();
				}
			}
		});
		
		event.stop();
	});
});*/
