﻿function worksheet_process() {

/*-- run the calculation for each of the totals --*/
/*-- if one fails don't bother with the rest --*/

    var processed = true;
    if (processed) processed = worksheet_calculate("a_", 18, "subtotala");
    if (processed) processed = worksheet_calculate("b_", 18, "subtotalb");
    if (processed) processed = worksheet_calculate("b_", 20, "totalcurrentb");
}




function worksheet_calculate(idName, loops, totalField) {
   var runTotal=0;
   var nonErrorColor = "#ffffff";
   var errorColor = "#ffff00";
   var homeProceedsLine = 17; //actual line - 1


/*-- resets field colors back to original --*/
for (a=0; a<loops; a++) {
    d = document.getElementById(idName+(a+1));
    if (d) {
    	    d.style.backgroundColor=nonErrorColor;
    }
} //for


    for (a=0; a<loops; a++) {
        d = document.getElementById(idName+(a+1));
        if (d) {
            h = d.value;


            /*- check that entry is only digits, maybe with decimal --*/
            if (h!="") {
                h = h.replace(/,/g, "");
                regex = new RegExp(/^\d+$|^\d+\.\d{2}$/); 
                x = h.match(regex);
                if (!x) 
	                {alert('please recheck your entries: they must be numeric, be greater than one, and if cents are entered, must be to two decimal places.');
	                d.style.backgroundColor=errorColor;
	                return false;
	                } //if

                /*-- turn values into addable numbers then add to running total--*/
	            var f =  parseFloat(h);
    	        
	            /*-- calcs from home sale --*/
                if (a==homeProceedsLine) {
                    var g = (f*.06).toFixed(2); 
                    var j = (parseFloat(g)/12).toFixed(2);
	                document.getElementById("a_"+(a+2)).value= g;
	                document.getElementById("a_"+(a+3)).value= j;
	                document.getElementById("totalcurrenta").value=(runTotal+parseFloat(j)).toFixed(2);

                } else {
                
                runTotal += f;
                
                }
                    
                } //if
         } //if
    }//for

document.getElementById(totalField).value=runTotal.toFixed(2);
return true;
}


function printPage(){
	window.print();
	return true;
}