
function initialFormat() {
    var form = document.forms.cost_form;

    form.labor_rate.value = formatDecimal(form.labor_rate.value, 2);
    form.before_ttl_inst_tm.value = formatDecimal(form.before_ttl_inst_tm.value, 2);
    form.before_ttl_fast_cost.value = formatDecimal(form.before_ttl_fast_cost.value, 3);
    form.after_ttl_inst_tm.value = formatDecimal(form.after_ttl_inst_tm.value, 2);
    form.after_ttl_fast_cost.value = formatDecimal(form.after_ttl_fast_cost.value, 3);
    form.fast_caused_scrap_rate.value = formatDecimal(form.fast_caused_scrap_rate.value, 2);
    form.fast_scrap_cost.value = formatDecimal(form.fast_scrap_cost.value, 2);
}

function calcForm() {
    var form = document.forms.cost_form;
    var id1;
    var id2;
    var id3;
    var id4;

    // Before - Fastener Install Cost
    id1 = document.getElementById('before_fast_inst_cost');
    id2 = form.before_fast_inst_cost_ff;
    if (id1 && id2) {
        id1.innerHTML = formatDecimal(fldValue(form.before_ttl_inst_tm.value)/3600*fldValue(form.labor_rate.value), 3);
        id2.value = id1.innerHTML;
    }

    // Before - Total Installed Cost
    id1 = document.getElementById('before_fast_inst_cost');
    id2 = document.getElementById('before_ttl_inst_cost');
    id3 = form.before_ttl_inst_cost_ff;
    if (id1 && id2 && id3) {
        id2.innerHTML = formatDecimal(fldValue(id1.innerHTML)+fldValue(form.before_ttl_fast_cost.value), 3);
        id3.value = id2.innerHTML;
    }

    // After - Fastener Install Cost
    id1 = document.getElementById('after_fast_inst_cost');
    id2 = form.after_fast_inst_cost_ff;
    if (id1 && id2) {
        id1.innerHTML = formatDecimal(fldValue(form.after_ttl_inst_tm.value)/3600*fldValue(form.labor_rate.value), 3);
        id2.value = id1.innerHTML;
    }

    // After - Total Installed Cost
    id1 = document.getElementById('after_fast_inst_cost');
    id2 = document.getElementById('after_ttl_inst_cost');
    id3 = form.after_ttl_inst_cost_ff;
    if (id1 && id2 && id3) {
        id2.innerHTML = formatDecimal(fldValue(id1.innerHTML)+fldValue(form.after_ttl_fast_cost.value), 3);
        id3.value = id2.innerHTML;
    }

    // Total Labor Savings per Board
    id1 = document.getElementById('before_ttl_inst_cost');
    id2 = document.getElementById('after_ttl_inst_cost');
    id3 = document.getElementById('ttl_labor_savings_per_board');
    id4 = form.ttl_labor_savings_per_board_ff;
    if (id1 && id2 && id3 && id4) {
        id3.innerHTML = formatDecimal(fldValue(id1.innerHTML)-fldValue(id2.innerHTML), 2);
        id4.value = id3.innerHTML;
    }

    // Scap Cost per Board
    id1 = document.getElementById('fast_scrap_cost_per_board');
    id2 = form.fast_scrap_cost_per_board_ff;
    if (id1 && id2) {
        id1.innerHTML = formatDecimal(fldValue(form.fast_caused_scrap_rate.value)*fldValue(form.fast_scrap_cost.value)*0.01, 2);
        id2.value = id1.innerHTML;
    }

    // Total Savings Scrap and Labor per Board
    id1 = document.getElementById('fast_scrap_cost_per_board');
    id2 = document.getElementById('ttl_labor_savings_per_board');
    id3 = document.getElementById('ttl_savings');
    id4 = form.ttl_savings_ff;
    if (id1 && id2 && id3 && id4) {
        id3.innerHTML = formatDecimal(fldValue(id1.innerHTML)+fldValue(id2.innerHTML), 2);
        id4.value = id3.innerHTML;
    }
}

function fldValue(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if (isNaN(num)) {
        num = "0";
    }
    return num * 1;
}

function formatCurrency(num, decpl) {
    var mod_val = Math.pow(10, decpl);
    num = num.toString().replace(/\$|\,/g,'');
    if (isNaN(num)) {
        num = "0";
    }
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*mod_val+0.50000000001);
    cents = num%mod_val;
    num = Math.floor(num/mod_val).toString();
    cents = zeroPad(cents, decpl);
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
        num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
    }
    return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function zeroPad(num, places) {
    var padded = "";
    num = num.toString().replace(/\$|\,/g,'');
    if (isNaN(num)) {
	num = "0";
    }
    for (i=0; i<places-num.length; i++) {
	padded += "0";
    }
    padded += num;
    return padded;
}


function formatDecimal(num, places) {
    num = num.toString().replace(/\,/g,'');
    if (isNaN(num)) {
        num = "0";
    }
    sign = (num == (num = Math.abs(num)));
    num = num.toFixed(places);
    return (((sign)?'':'-') + num.toString());
}

function handleEnter(field, event) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {
        var i;
        for (i = 0; i < field.form.elements.length; i++) {
            if (field == field.form.elements[i]) {
                break;
            }
        }
        i = (i + 1) % field.form.elements.length;
        field.form.elements[i].focus();
        return false;
    } else {
        return true;
    }
}


