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) {
	validateField($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;

function updateTotals() {
	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 $_price = document.getElementById("total" + $id);
		$_price.innerHTML = "$" + formatPrice($price);
		$items += $quantity;
		$subtotal += $price;
	}
	var $shipping = 10;
	var $total = $subtotal + $shipping;
	var $_subtotal = document.getElementById("subtotal");
	var $_shipping = document.getElementById("shipping");
	var $_total = document.getElementById("total");
	$_subtotal.innerHTML = "$" + formatPrice($subtotal);
	$_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;
}
