document.write('<link rel="stylesheet" type="text/css" href="/css/inputs.css" media="screen" />');

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function addUpTotal(){
	varTotalPrice = 0
	var divs = document.getElementsByTagName("div");
		for(var i = 0; i < divs.length; i++) {
			if(divs[i].className.match("checkbox selected")){
				varThisPrice = parseInt(divs[i].getAttribute("rel"));
				varTotalPrice = varTotalPrice + varThisPrice;
				//alert(varTotalPrice + " " + varThisPrice);
				
				//alert(varTotalPrice)
			}
		}
	var obj = document.getElementById("totalprice")
	obj.innerHTML = ("$" + addCommas(varTotalPrice));	
}

var Input = {
	initialize: function() {
		
		image0 = new Image();
		image0.src = "/graphics/purchase-button-backgrounds.gif";
		image1 = new Image();
		image1.src = "/graphics/purchase-button-checkbox.gif";
		image2 = new Image();
		image2.src = "/graphics/purchase-button-radio.gif";
		
		addUpTotal();
		if(document.getElementsByTagName("form")) {
			var divs = document.getElementsByTagName("div");
			for(var i = 0; i < divs.length; i++) {
				if(divs[i].className.match("checkbox") || divs[i].className.match("radio")) {
					divs[i].onmousedown = Input.effect;
					divs[i].onmouseup = Input.handle;
					window.onmouseup = Input.clear;
				}
			}
		}
	},

	effect: function() {
		if(this.className == "checkbox" || this.className == "radio") {
			this.style.backgroundPosition = "0 1px";
			
		} else {
			this.style.backgroundPosition = "0 -79px";
			
		}
	},

	handle: function() {
		
		selector = this.getElementsByTagName("input")[0];
		if(this.className == "checkbox") {
			selector.checked = true;
			this.className = "checkbox selected";
			
			this.style.backgroundPosition = "0 -11px";//IE
		} else if(this.className == "checkbox selected") {
			selector.checked = false;
			this.className = "checkbox";
			this.style.backgroundPosition = "0 1px";
			
		} else {
			selector.checked = true;
			this.className = "radio selected";
			this.style.backgroundPosition = "0 -11px";
			
			inputs = document.getElementsByTagName("input");
			for(i = 0; i < inputs.length; i++) {
				if(inputs[i].getAttribute("name") == selector.getAttribute("name")) {
					if(inputs[i] != selector) {
						inputs[i].parentNode.className = "radio";
						inputs[i].parentNode.style.backgroundPosition = "0 1px";
						//alert("!")
					}
				}
			}
		
		}
		addUpTotal();
	},

	clear: function() {
		divs = document.getElementsByTagName("div");
		for(var i = 0; i < divs.length; i++) {
			if(divs[i].className == "checkbox" || divs[i].className == "radio") {
				divs[i].style.backgroundPosition = "0 1px";
			} else if(divs[i].className == "checkbox selected" || divs[i].className == "radio selected") {
				divs[i].style.backgroundPosition = "0 -11px";
			}
		}
	}
}
window.onload = Input.initialize;
