function qtyOnChange(elt) {
    //alert('order.js qtyOnChange() enter');

    if (isNaN(elt.value)) {
        alert('Please enter a number for the quantity.');
        elt.focus();
        elt.select();
        return;
    }

    if (elt.value.indexOf('.') >= 0) {
        alert('Please enter a whole number (0, 1, 2, etc.) for the quantity.');
        elt.focus();
        elt.select();
        return;
    }

    if (elt.value < 0) {
        alert('Please enter a positive number (1, 2, etc.) for the quantity.');
        elt.focus();
        elt.select();
        return;
    }

    var frm = document.mainFrm;
    //this indicates some sort of odd event; just ignore it
    if (typeof frm == 'undefined') return(false);

    if (!checkTypeInputs(elt.name)) {
        //alert('order.js qtyOnChange() checkTypeInputs() failed');
        return;
    }

    frm.action = 'orderForm.cgi';
    frm.submit();
}

function checkQtyInputs(frm) {
    var i;
    var total = 0;

    for (i = 0; i < frm.elements.length; i++) {
        var elt = frm.elements[i];
        if (elt.name.indexOf('qty_vol_') != 0) continue;

        //check all items in case qty_vol_N is 0
        total += elt.value;
    }

    if (total == 0) {
        alert('Please select an item to buy.');
        return(false);
    }

    return(true);
}

function checkTypeInputs(eltName) {
    if (typeof eltName == 'undefined') eltName = null;

    var frm = document.mainFrm;
    //this indicates some sort of odd event; just ignore it
    if (typeof frm == 'undefined') return(false);

    var i;
    var total = 0;

    for (i = 0; i < frm.elements.length; i++) {
        var elt = frm.elements[i];
        if (elt.name.indexOf('qty_vol_') != 0) continue;
        if (elt.type != 'text') continue; //do not want hidden variables

        var qty = elt.value;
        if (qty.length == 0) {
            //only report a message if the input is the same as the input just changed, when specified
            if (eltName == null || eltName == elt.name) {
                alert('Please enter a quantity for each item or remove it.');
                elt.focus();
            }
            return(false);
        }

        if (isNaN(elt.value)) {
            alert('Please enter a number for the quantity.');
            elt.focus();
            return(false);
        }

        qty = parseInt(elt.value);
        if (qty == 0) continue;

        if (qty < 0) {
            alert('Please enter a positive number (0, 1, 2, etc.) for the quantity.');
            elt.focus();
            return(false);
        }

        total += qty;
        
        var a = elt.name.split('_');
        var id = a[2];
        var j;
        var typeElt;
        for (j = 0; j < frm.elements.length; j++) {
            var t = frm.elements[j];
            if (t.name == 'type_vol_' + id) {
                typeElt = t;
                //there may be duplicates, so get the select list
                if (typeof typeElt.selectedIndex != 'undefined') break; 
            }
        }
        if (typeof typeElt == 'undefined') continue;

        //if (typeElt.selectedIndex <= 0) {
            //s = 'Please select a format type (like Hardcopy Book) for each item ordered.';
            //s += "\n" + 'If you do not want an item, set the quantity to 0.';
            //alert(s);
            //return(false);
        //}
    }

    if (total == 0) {
        s = 'You have ordered no items.' + "\n\nClick 'Continue Shopping' to go back to the book store.";
        alert(s);
        return(false);
    }

    return(true);
}

function itemBuyOnClick(qtyElement) {
    var frm = document.mainFrm;
    if (qtyElement.value <= 0) qtyElement.value = 'FORCE';
}

function removeItemOnClick(frm,volID) {
    var elt = null;
    var eltID = null;
    for (i = 0; i < frm.elements.length; i++) {
        elt = frm.elements[i];
        if (elt.name.indexOf('qty_vol_' + volID) != 0) continue;
        if (elt.type != 'text') continue; //do not want hidden variables

        eltID = i;
        elt.value = 0;
        break;
    }

    if (eltID != null) {
        qtyOnChange(elt);
    }
}