function UpdateTotals(id)
{

    var qty = document.getElementById('qty_'+id);
    var price = document.getElementById('price_'+id);
    var total = document.getElementById('total_'+id);
    if (qty && total) {
        var subtot = qty.value * price.value;
        total.innerHTML = subtot.numberFormat('$0.00');
    }

    var totals_list = document.getElementById('totals_list');
    var tot_ids = totals_list.value.split(",");

    var grand_total = 0.0;
    for (var i = 0; i < tot_ids.length; i++) {
        var e = document.getElementById(tot_ids[i]);
        if (!e) continue;
        ts = e.innerHTML.replace(/[^\d.]/g,''); 
        if (ts=='') continue;
        grand_total += parseFloat(ts);
    }
    var gt = document.getElementById('grand_total');
    if (gt) gt.innerHTML = grand_total.numberFormat('$0.00');
}

function ValidateNumeric(o)
{
    if (!o) return false;
    if (o.value.match(/^[\d.]*$/)) return true;
    o.value = '';
    alert('Please enter a numeric value.');
    return false;
}


function ToggleShipping(o)
{
    var fields = ['last_name','first_name','address','city','state','zip'];
    if (o.checked) { 
         for (var i = 0; i < fields.length; i++) {
            var src = document.getElementById('x_'+fields[i]);
            var dst = document.getElementById('x_ship_to_'+fields[i]);
            if (!src || !dst) continue;
            dst.value = src.value;
        }
    } else {
        for (var i = 0; i < fields.length; i++) {
            var e = document.getElementById('x_ship_to_'+fields[i]);
            if (!e) continue;
            e.value = '';
        }
    }
}

