﻿/*
* Calculates the item total for the Order Item and updates the Total Price on the table.
* Then the sub total, GST and total will be calculated.
*/

function CalculateTotals(quantityTextBoxID, itemPriceLabelID, itemTotalLabelID) {
    var quantityTextBox = document.getElementById(quantityTextBoxID);
    var itemPriceLabel = document.getElementById(itemPriceLabelID);
    var itemTotalLabel = document.getElementById(itemTotalLabelID);

    if (isNaN(quantityTextBox.value)) {
        alert('Please enter a numerical quantity. Non-numeric characters are not allowed.');
        quantityTextBox.value = 1;
        quantityTextBox.focus();
    }

    if (quantityTextBox.value != ''){
        var regexp = /^[1-9][0-9]{0,9}$/;
        if (!regexp.test(quantityTextBox.value)) {
            alert('Please enter quantity to the nearest whole number greater than 0.');
            quantityTextBox.value = 1;
            quantityTextBox.focus();
        }
    }

    itemTotalLabel.innerHTML = (quantityTextBox.value * itemPriceLabel.innerHTML).toFixed(2);
    CalculateOrderTotals();
}