/*
 * order.js
 *
 * Subtotal calculation and population for the byot order form.
 * Requires jQuery
 *
 */

$(document).ready(
    function (){

        // bind the subtotal function to the 'qty' fields
        $(".qty").bind("keyup", subtotal);
        // run subtotal on page load to establish subtotal  
        subtotal();
    }
);

function subtotal(){
    var total = 0;
    $(".qty").each( function () { 
        if ($(this).val() != '' && IsNumeric($(this).val())) {
            // Numeric entry - add to total
            total = total + parseInt($(this).val());
        }else{
            // Non-Numeric entry
            // Correct number followed by non-number
            if (isNaN(parseInt($(this).val()))) {
                // This case for non-number from left
                $(this).val('');
            } else {
                // This case for number followed by non-number
                // i.e. - '5.' would be set back to '5'
                $(this).val(parseInt($(this).val()));
            	total = total + parseInt($(this).val());
            }
        }
    });
    //
    // Price per DVD = $7.50
    //
    $("#subtotal-qty").val(total);
    $("#subtotal-qty-hidden").val(total);
    $("#subtotal-cost").val((total * 7.50).toFixed(2));
    $("#subtotal-cost-hidden").val((total * 7.50).toFixed(2));
}

function IsNumeric(sText){
    var ValidChars = "0123456789";
    var IsNumber=true;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++){ 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1){
            IsNumber = false;
        }
    }
    return IsNumber;
}


