// express order number fields

function sum(i)
{
var totalitems = 7;
sum1= eval('document.coffeeorder.grind'+i+'.value')
sum2= eval('document.coffeeorder.weight'+i+'.value')
cost= eval('document.coffeeorder.cost'+i+'.value')
sum1 = parseInt(sum1)
sum2 = parseFloat(sum2)
cost = parseInt(cost)
if(sum1 > 1){
grindcost = 1
}
else
{
grindcost = 0
}
if(sum2 == 0.5){
	total=((sum2*cost)+grindcost)+1
	}
	else
	{
	total=sum2*(cost+grindcost)
}

var subtotal = eval('document.coffeeorder.subtotal' + i);
subtotal.value = "$"+roundNumber(total);
var total = 0;
for(var i=1; i<=totalitems; i++){
	var subadd = eval('document.coffeeorder.subtotal' + i);
	subadd = subadd.value.replace("$", '') ;
	totaladd = parseFloat(subadd)
	total += totaladd;
}

document.coffeeorder.total.value = "$"+roundNumber(total)+"*";
}
function roundNumber(number) {
	var rlength = 2; // The number of decimal places to round to
	var newnumber = Math.round(number*Math.pow(10,rlength))/Math.pow(10,rlength);
	return pad_with_zeros(newnumber, 2)

}
function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()

    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {

        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0

        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }

    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length

    if (pad_total > 0) {

        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++)
            value_string += "0"
        }
    return value_string
}

// end express order number fields

function submit_hitlist()
{
   var d = document.hitlistform;
   var inputEmail = d.email.value;
   /* newInputEmail = stripHTML(inputEmail); */
   if (!test(inputEmail)) {
   	alert('Please enter a correct email address');
    } else {

        d.submit();
    }
}

function test(src) {
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
  }