// JavaScript Document

// calculate total

function calcValues(form,price) {
	var quantity = form.qty.value;
	var orderTotal = 0;
	
	//check for empty/non-numeric qty fields
	if (isNaN(quantity)) {
		quantity = 0;
	}
			
	//get values for order subtotal, total quantity, and item subtotal
	if (quantity >= 0) {
		orderTotal += quantity * price;
	}
	
	//update item total
	document.orderForm.total.value = roundValue(orderTotal,2);
}

// get rounded values
function roundValue(inputNumber, decimals) {
    var result1 = inputNumber * Math.pow(10,decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10,decimals)
    return addZeroes(result3,decimals)
}

// add zeroes to values
function addZeroes(roundedValue, decimalPlaces) {

    // convert number to string
    var valueString = roundedValue.toString()
    
    // find decimal position
    var decimalLocation = valueString.indexOf(".")

    // if there is a decimal
    if (decimalLocation == -1) {
        // if no, pad all
        decimalPartLength = 0
        
        // add decimal point
        valueString += decimalPlaces > 0 ? "." : ""
    }
	// if no decimal
    else {
        // add zeroes to decimal places
        decimalPartLength = valueString.length - decimalLocation - 1
    }
    
    // number of decimal points to be padded
    var totalPad = decimalPlaces - decimalPartLength
    
    if (totalPad > 0) {
        
        // add 0s
        for (var counter = 1; counter <= totalPad; counter++) 
            valueString += "0"
        }
    return valueString
}
