var quantities = new Array();

//integers_only, update_total, addCommas, and deleteItem added 8-20-07/JMB
function integers_only(value, inputId)
{
	//This function makes sure there are only integers in 'value' and if not, strips out un-integers and puts it back into 'inputId' which is the quantity field. It will also round any floating double
	var newvalue = '';
	for (i = 0; i < value.length; i++)
	{
		var char = value.charAt(i);
		var reg = new RegExp("[0-9\.\-]");
		if (reg.test(char))
			newvalue = newvalue + char;
	}
	newvalue = Math.round(newvalue);
	if (inputId != null)
		document.getElementById(inputId).value = newvalue;
	else
		return newvalue;
}

function update_total(count)
{
	var quantity = document.getElementById('quantity' + count).value; //0
	var price = document.getElementById('money' + count).innerHTML;
	price = integers_only(price, null);
	var update_price = quantity * price;
	update_price =addCommas(update_price);
	document.getElementById('subtotal' + count).innerHTML = '$' + update_price;
	
	//var grand_total = document.getElementById('grandtotal').innerHTML;
	//grand_total = integers_only(grand_total);
	var i = 0;
	var grand_total = 0;
	do {
		var cart_item = document.getElementById('subtotal' + i).innerHTML;
		cart_item = integers_only(cart_item);
		grand_total += cart_item;
		i++;
	}
	while (isNaN(document.getElementById('subtotal' + i)));
	
	grand_total = addCommas(grand_total);
	document.getElementById('grandtotal').innerHTML = '$' + grand_total;
	
	return update_price;
}

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 previousQuantities()
{
	var i = 0;
	do { //this adds the global variable quantities
		var quantity = document.getElementById('quantity' + i).innerHTML;
		quantity = integers_only(quantity);
		quantities['quantity'+i] = quantity;
		i++;
	}
	while (isNaN(document.getElementById('subtotal' + i)));
	
	return quantities;
}

function deleteItem(count)
{
	//when someone selects 'delete' on an item it'll put the quantity to zero. Maybe should remember the old quantity in case they want to resore it (unclick)? yes let's do that.
	if (document.getElementById('delete' + count).checked)
	{
		var quantity = document.getElementById('quantity' + count).value;
		quantities['quantity'+count] = quantity;
		
		var curquant = document.getElementById('quantity'+count);
		curquant.value = 0;
		curquant.disabled = "disabled";
		//document.getElementById('row'+count).style.display = 'none';
		
	}
	else
	{
		document.getElementById('quantity'+count).value = quantities['quantity'+count];
		document.getElementById('quantity'+count).disabled = false;
	}
	update_total(count);
}

function enable()
{
	var inputs = document.getElementsByTagName('input');
	for (i = 0; i < inputs.length; i++)
	{
		inputs[i].disabled = false;
	}
}
