function formatPrice($price) {
	$price = Math.round($price * 100) / 100;
	$price = String($price);
	if($price.indexOf(".") < 0) {
		$price += ".";
	}
	while($price.indexOf(".") > $price.length - 3) {
		$price += "0";
	}
	return $price;
}

function validateKeyUp($field, $price) {
	var $value = "";
	var $valid = true;
	var $decimal = false;
	var $decimals = 2;
	for(var $i = 0; $i < $field.value.length; $i ++) {
		var $character = $field.value.charAt($i);
		if(/^\d$/.test($character) && $decimals) {
			$value += $character;
			if($decimal) $decimals --;
		} else {
			if(!$decimal && $character == "." && $price) {
				if(!$i) {
					$value += "0";
					$valid = false;
				}
				$value += $character;
				$decimal = true;
			} else {
				$valid = false;
			}
		}
	}
	if(!$valid) $field.value = $value;
}

function validateFocus($field) {
	if(/^[0.]*$/.test($field.value)) $field.value = "";
}

function validateBlur($field, $price) {
	validateKeyUp($field, $price);
	while($field.value.charAt(0) == "0") $field.value = $field.value.substring(1);
	if(/^[0.]*$/.test($field.value)) $field.value = "0";
	if($price) $field.value = formatPrice($field.value);
}

var items = [];
var quantity;
var total;
var valid;

function updateTotals() {
	valid = true;
	var $items = 0;
	var $subtotal = 0;
	for(var $i = 0; $i < items.length; $i ++) {
		var $id = items[$i];
		var $input = document.form["_" + $id];
		var $quantity = parseInt($input.value);
		if(!$quantity) $quantity = 0;
		var $price = $quantity * parseFloat($input.getAttribute("price"));
		var $stock = parseInt($input.getAttribute("stock"));
		var $_price = document.getElementById("total" + $id);
		$_price.innerHTML = "$" + formatPrice($price);
		var $_stock = document.getElementById("stock" + $id);
		$_stock.style.visibility = $quantity > $stock ? "visible" : "hidden";
		$input.style.color = $quantity > $stock ? "e04848" : "";
		if($quantity > $stock) valid = false;
		$items += $quantity;
		$subtotal += $price;
	}
	if($subtotal >= coupon.minimum) {
		$coupon = coupon.value;
	} else {
		$coupon = 0;	
	}
	var $shipping = 10;
	var $total = $subtotal - $coupon + $shipping;
	var $voucher = Math.min($total, voucher);
	$total -= $voucher;
	var $_subtotal = document.getElementById("subtotal");
	var $_coupon = document.getElementById("coupon");
	var $_shipping = document.getElementById("shipping");
	var $_voucher = document.getElementById("voucher");
	var $_total = document.getElementById("total");
	$_subtotal.innerHTML = "$" + formatPrice($subtotal);
	if($_coupon) $_coupon.innerHTML = "-$" + formatPrice($coupon);
	if($_voucher) $_voucher.innerHTML = "-$" + formatPrice($voucher);
	$_shipping.innerHTML = "$" + formatPrice($shipping);
	$_total.innerHTML = "$" + formatPrice($total);
	if($items == 0) valid = false;
	quantity = $items;
	total = $total;
}

function updateCart() {
	var $vars = [];
	for(var $i = 0; $i < items.length; $i ++) {
		var $id = items[$i];
		$vars.push("_" + $id + "=" + document.form["_" + $id].value);
	}
	$vars = $vars.join("&");
	ajax("/scripts/updatecart.php", true, $vars, null, true);
}

function cleanWhitespace($node) {
	for(var $i = 0; $i < $node.childNodes.length; $i++) {
		var $childNode = $node.childNodes[$i];
		if($childNode.nodeType == 3 && /^[\s]*$/.test($childNode.nodeValue)) {
			$node.removeChild($node.childNodes[$i]);
			$i --;
		}
		if($childNode.nodeType == 1) {
			cleanWhitespace($childNode);
		}
	}
}

function encode($string) {
	var chars = [
		{search: "&",	replace: "&amp;"},
		{search: '"',	replace: "&quot;"},
		{search: "<",	replace: "&lt;"},
		{search: ">",	replace: "&gt;"}
	];
	for(var $i = 0; $i < chars.length; $i ++) {
		var $char = chars[$i];
		$string = $string.replace($char.search, $char.replace);
	}
	return $string;
}

function decode($string) {
	$string = $string.replace(/\+/g, " ");
    $string = unescape($string);
	return $string;
}

var sizes = [];

function setColour($colour) {
	with(document.form) {
		while(size.length > 0) size.remove(0);
		size.disabled = false;
		var $price = 0;
		var $sale = 0;
		var $sizes = sizes["_" + $colour];
		if($sizes) {
			for($i = 0; $i < $sizes.length; $i ++) {
				var $size = $sizes[$i];
				if($size.price > $price) $price = $size.price;
				if($size.sale > $sale) $sale = $size.sale;
				var $option = document.createElement("option");
				$option.value = $size.value;
				$option.code = $size.code;
				$option.text = $size.label;
				try {
					size.add($option, null);
				} catch(e) {
					size.add($option);
				}
			}
			if($sizes.length < 3) {
				size.value = $size.value;
				size.disabled = true;
			}
		} else {
			size.disabled = true;
		}
		setPrice($price, $sale);
		setSize(size.value);
	}
}

function setSize($size) {
	with(document.form) {
		price.value = $size ? size.options[size.selectedIndex].code : "";
		quantity.disabled = $size ? false : true;
	}
}

function setPrice($price, $sale) {
	var price = document.getElementById("price");
	if($sale) {
		price.innerHTML = "$" + formatPrice($sale) + "<span><br />was $" + formatPrice($price) + "</span>";
		price.className = "sale";
	} else {
		price.innerHTML = $price ? "$" + formatPrice($price) : "";
		price.className = "";
	}
}

function addToBag($wishlist) {
	with(document.form) {
		if(size.options.length) {
			if(size.value) {
				if(quantity.value > 0 || $wishlist) {
					if($wishlist) wishlist.value = 1;
					stock.value = size.value;
					return true;
				} else {
					alert("Please enter a valid quantity");
					return false;
				}
			} else {
				alert("Please select a size");
				return false;
			}
		} else {
			alert("Please select a colour");
			return false;
		}
	}
}

function checkout() {
	if(valid) {
		return true;
	} else {
		if(quantity > 0) {
			alert("There is unsufficient stock available to fill your order");
		} else {
			alert("There are no items in your shopping bag");
		}
		return false;
	}
}
