function Postage() {
	this.set = function(amount) {
		document.getElementById("postage").innerHTML = amount;
		document.getElementById("total").innerHTML = (parseFloat(document.getElementById("subtotal").value) + parseFloat(document.getElementById("giftvalue").value) + parseFloat(amount)).toFixed(2);
	}
}

function Delivery() {
	this.type = Delivery.STANDARD;
	
	this.standard = document.getElementById("delivery_standard");
	this.special = document.getElementById("delivery_special");
	
	if ( this.standard )
	{
		if ( this.standard.checked )
			this.type = Delivery.STANDARD;
		else if ( this.special.checked )
			this.type = Delivery.SPECIAL;
		
		this.standard.onclick = function() {
			if ( destination.zone == Destination.UK )
				postage.set(document.getElementById("standard_amt").value);
		//	this.type = Delivery.STANDARD;
		};
		this.special.onclick = function() {
	//		this.type = Delivery.SPECIAL;
			if ( destination.zone == Destination.UK )
				postage.set(document.getElementById("special_amt").value);
			else
				this.standard.click();
		};
		this.setType = function(type) {
			this.type = type;
			if ( this.type == Delivery.STANDARD )
				this.standard.click();
			else if ( this.type == Delivery.SPECIAL )
				this.special.click();
		}
		this.disableSpecial = function(yn) {
			if ( yn )
				this.special.disabled = true;
			else
				this.special.disabled = false;
		}
	}
}
Delivery.STANDARD = 1;
Delivery.SPECIAL = 2;

function Destination() {
	
	this.zone = Destination.UK;
	
	this.uk = document.getElementById("destination_uk");
	this.europe = document.getElementById("destination_europe");
	this.world = document.getElementById("destination_world");
	
	if ( this.uk )
	{
		this.uk.owner = this;
		this.europe.owner = this;
		this.world.owner = this;

		this.uk.onclick = function() {
			if ( this.owner.zone != Destination.UK ) {
				delivery.disableSpecial(false);
				postage.set(document.getElementById("standard_amt").value);
			}
			this.owner.zone = Destination.UK;
		};
		this.europe.onclick = function() {
			delivery.setType(Delivery.STANDARD);
			delivery.disableSpecial(true);
			postage.set(9.99);
			this.owner.zone = Destination.EU;
		};
		this.world.onclick = function() {
			delivery.setType(Delivery.STANDARD);
			delivery.disableSpecial(true);
			postage.set(19.99);
			this.owner.zone = Destination.ROW;
		};
		
		if ( this.uk.checked ) {
			this.zone = Destination.UK;
			if ( delivery.type == Delivery.STANDARD )
				postage.set(document.getElementById("standard_amt").value);
			else if ( delivery.type == Delivery.SPECIAL )
				postage.set(document.getElementById("special_amt").value);
		} else if ( this.europe.checked ) {
			postage.set(9.99);
			this.zone = Destination.EU;
		} else if ( this.world.checked ) {
			postage.set(19.99);
			this.zone = Destination.ROW;
		}
	}
}
Destination.UK = 1;
Destination.EU = 2;
Destination.ROW = 3;

function GiftMessage() {
	
	this.yes = document.getElementById("giftmessage_yes");
	this.no = document.getElementById("giftmessage_no");
	this.message = document.getElementById("giftmessage");
	
	if ( this.yes )
	{
		this.yes.owner = this;
		this.no.owner = this;

		this.yes.onclick = function() {
			this.owner.message.disabled = false;
			document.getElementById("giftvalue").value = 0.99;
			document.getElementById("total").innerHTML = (0.99 + parseFloat(document.getElementById("subtotal").value) + parseFloat(document.getElementById("postage").innerHTML)).toFixed(2);
		}
		
		this.no.onclick = function() {
			this.owner.message.disabled = true;
			document.getElementById("giftvalue").value = 0;
			document.getElementById("total").innerHTML = (parseFloat(document.getElementById("subtotal").value) + parseFloat(document.getElementById("postage").innerHTML)).toFixed(2);
		}
		
		if ( this.yes.checked ) {
			this.yes.click();
		} else if ( this.no.checked ) {
			this.no.click();
		}
	}
}

var postage = null;
var delivery = null;
var destination = null;
var gift = null;
window.onload = function() {
	postage = new Postage();
	delivery = new Delivery();
	destination = new Destination();
	gift = new GiftMessage();
}
