// JavaScript Document
function popup(doc){
	window.open(doc, 'newWin', 'status=0, toolbar=0, menubar=0, width=500, height=400, scrollbars=1');
}

function replaceImage(elementID, imageName){
	document.getElementById(elementID).src = imageName;
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)){
		num = "0";
	}
	
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10){
		cents = "0" + cents;
	}
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++){
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
	}
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function recalculateOrderTotal(target, price, quantity){
	var subtotal = formatCurrency(quantity * price);
	document.getElementById(target).innerHTML = subtotal;
	var allSubtotals = document.getElementsByClassName('subtotal');
	var total = 0;
	for (i=0; i<allSubtotals.length; i++){
		total += parseFloat(allSubtotals[i].innerHTML.toString().replace(/\$|\,/g,''));
	}
	document.getElementById('subtotal').innerHTML = formatCurrency(total);
}

function billToShip(checkbox){
	document.oeform.billingFirstName.disabled = checkbox.checked;
	document.oeform.billingLastName.disabled = checkbox.checked;
	document.oeform.billingLastName.disabled = checkbox.checked;
	document.oeform.billingAddress1.disabled = checkbox.checked;
	document.oeform.billingAddress2.disabled = checkbox.checked;
	document.oeform.billingCity.disabled = checkbox.checked;
	document.oeform.billingState.disabled = checkbox.checked;
	document.oeform.billingPostal.disabled = checkbox.checked;
	document.oeform.billingPhone.disabled = checkbox.checked;
	document.oeform.billingEmail.disabled = checkbox.checked;
}