window.addEvent('domready', function()
	{
	
	function isIE() {
    	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
    }
    
    if (isIE() == false) {
		var myFormValidation = new Validate('myForm',{
			errorClass: 'red'
		});
	}
    

	
	init();
	updateTotal();
	showBilling("hide");
	
	justOnce();
	
	function justOnce() {
		showRows("ship", $('ship_country').value)
		showRows("tax", $('ship_state').value)

	}
	
	function init() {
		$('ship_country').addEvent('change', function() { if (this.value != "") { showRows("ship", this.value); }	});

		$('ship_state').addEvent('change', function() { if (this.value != "") {	showRows("tax", this.value); }});

		$('coupon_id').addEvent('keyup', function() { if (this.value != "") {	updateCoupon(this.value) }} );

		updateBad = false;
	}
	
	function setOther(this_id, value) {
		if (value == "US") {
			$(this_id).value = "AL";
			$(this_id+"_extra").style.display = "none";
		} else {
			$(this_id).value = "other";
			$(this_id+"_extra").style.display = "inline";
		}
	}
	
	function otherBox(this_id, value) {
		if (value == "other") {
			$(this_id).style.display = "inline";
		} else {
			$(this_id).style.display = "none";
			$(this_id).value = "";
		}
	}
	
	function showRows(update_what, value)
	{	
		var subtotal = $('subtotal_id').value;
		var subvalue = $('subvalue_id').value;
		var tax = $('tax_id').value;
		var shipping_id = $('shipping_id').value;
		
		new Ajax("/update_"+update_what+"/"+value +"?subtotal="+subtotal+"&subvalue="+subvalue+"&tax="+tax+"&shipping_id="+shipping_id, {
			method: 'get',
			update: $(update_what),
			onComplete: function(responseText) { updateTotal(); $$('#ship select').addEvent('change', function() { if (this.value != "") { updateTotal(); }}); },
			evalScripts: true
			}).request();
	}
	
	function updateCoupon(value)
	{
		
		if (value.length == 8) {
			updateBad = true;
			new Ajax("/update_coupon/"+value, {
				method: 'get',
				update: 'couponinfo',
				autoCancel: 'true',
				onSuccess: function(responseText) { updateTotal(); },
				evalScripts: true
				}).request();
		} else {
			if (updateBad == true) {
			new Ajax("/update_coupon/invalid", {
				method: 'get',
				update: $('couponinfo'),
				autoCancel: 'true',
				onSuccess: function(responseText) { updateTotal(); },
				evalScripts: true
				}).request();
			}
			
		}
	}
	
	function updateTotal()
	{			
		var subtotal = $('subtotal_id').value;
		var subvalue = $('subvalue_id').value;
		var tax = $('tax_id').value;
		var shipping_id = $('shipping_id').value;	
		var coupon_value = $('coupon_value').value;
		var coupon_type = $('coupon_type').value;
				
		new Ajax("/update_total/?subtotal="+subtotal+"&subvalue="+subvalue+"&tax="+tax+"&shipping_id="+shipping_id+"&coupon_value="+coupon_value+"&coupon_type="+coupon_type, {
			method: 'get',
			update: $('total'),
			evalScripts: true,
			onComplete: function(responseText) { showhideCC(); }
			}).request();
		}
		
	function showhideCC()
	{
		var total_id = $('total_id').value;
		
		if (total_id > 0) {
			$('creditcard').style.display = "block";
 	  } else {
			$('creditcard').style.display = "none";
		}
	}
	
	function showBilling(value)
	{
		if (value == "show") {
			$('address billing').style.display = "block";
			$('shipname').style.display = "block";
			$('bill_address').className = "required";
			$('bill_city').className = "required";
			$('bill_postcode').className = "required";
			$('bill_country').className = "required";
			$('ship_firstName').className = "required";
			$('ship_lastName').className = "required";

			$('different_addresses_2').addEvent('click', function() { if (this.value != "") {	showBilling("hide") }});
			$('whataddress').innerHTML = "<span>What is your Shipping Address?</span>";
			
		} else {
			$('address billing').style.display = "none";
			$('shipname').style.display = "none";
			$('bill_address').className = "none";
			$('bill_city').className = "none";
			$('bill_postcode').className = "none";
			$('bill_country').className = "none";
			$('ship_firstName').className = "none";
			$('ship_lastName').className = "none";
			
			$('whataddress').innerHTML = "<span>What is your Address? <a href='#' id='different_addresses'>(My Billing &amp; Shipping addresses are different.)</a></span>";
			$('different_addresses').addEvent('click', function() { if (this.value != "") {	showBilling("show") }});
		}
		
		var myFormValidation = new Validate('myForm',{
			errorClass: 'red'
		});
	}

});
 
