function calculate(frm, f) {

    var order_total = 0;

    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {

        // Get the current field
        form_field = frm.elements[i]
        // Get the field's name
        form_name = form_field.name
	
        // Is it a "product" field?
        if (form_name.substring(0,1) == "q") {
			
	        if (form_field.value) {
				var test_num = /^\d+$/.test(form_field.value);
				if (!test_num) {
					alert("Only numbers please!");
					form_field.focus();
					form_field.select();
				}
			}

			spl=form_name.split('_');
            // If so, extract the price&item# from the name
            item_price = parseFloat(spl[2])
			item_item  = parseFloat(spl[1])

            // Get the quantity
            item_quantity = parseInt(form_field.value)
            // Test for deletion
            item_total_form = 't_' + item_item.toString()
            if(isNaN(item_quantity) && !isNaN(parseInt(frm[item_total_form].value))) {
				frm[item_total_form].value = '';
           	}
            // Update the order total
            if (item_quantity >= 0) {
            	item_total = item_quantity * item_price
                order_total += item_total
                //display the total for this item
                item_total_form = 't_' + item_item.toString()
                frm[item_total_form].value = round_decimals(item_total,2)
            }
        }
    }

    // Display the total rounded to two decimal places
    frm.total.value = round_decimals(order_total, 2)
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

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
}

function maskphone(f) {
	tel='(';
	nums=f.value.replace(/[^\d]/g,"");
	val=nums.split('');
	for(var i=0; i<val.length; i++){
		if(i==2){ val[i]=val[i]+') ' }
		if(i==5){ val[i]=val[i]+'-' }
	tel=tel+val[i]
	}
	f.value=tel;
}

function checknum(f) {
	n = f.value.replace(/[^\d]/g,'');
	f.value = n;
}

function validate_form() {
	if (document.form.fname.value == "" || document.form.lname.value == "") {
		alert("Name is required!");
		document.form.fname.focus();
		return false;
	}
	if (document.form.email.value == "") {
		alert("Email is required!");
		document.form.email.focus();
		return false;
	}
	cell=document.form.cell.value;
	if (cell == "") {
		alert("Phone number is required!");
		document.form.cell.focus();
		return false;
	}
	num=cell.replace(/[\s\-\(\)]/g,'');
	var test_num = /^\d+$/.test(num);
	if (num.length != 10 || !test_num ) {
		alert("Phone number is incomplete or invalid...");
		document.form.cell.focus();
		return false;
	}
	if (document.form.total.value == "" || document.form.total.value == "0.00") {
		alert("Don't you want to order something?");
		return false;
	}
	return true;
}

function checkbox(cbox,frm) {
	var conjugate = 0;
	if (cbox.checked) {
		if (cbox.name.substring(0,1) == "c") {
			conjugate = "d_" + cbox.name.substring(2);
		} else if (cbox.name.substring(0,1) == "d") {
			conjugate = "c_" + cbox.name.substring(2);
		}
		console.log(conjugate);
		frm[conjugate].checked = false;
	}
}

function checkAll(frm,chr,chckd) {
	var conjugate=0;
    for (var i=0; i < frm.elements.length; ++i) {
        form_field = frm.elements[i]
        form_name = form_field.name
        if (form_name.substring(0,1) == chr) {
			form_field.checked=chckd;
			if (chr=='c') conjugate = "d_"+form_name.substring(2);
			if (chr=='d') conjugate = "c_"+form_name.substring(2);
			frm[conjugate].checked=!chckd;	
        }
    }
}

function noatsign(f) {
	var test_email = /[\s\-\(\)\@\.]/.test(f.value);
	if (test_email) {
		alert('Only UC Davis emails! (do not include @ucdavis.edu)');
		f.focus();
		f.select();
		}
}
