function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function calculateCosts() {
	var theForm = document.forms.calculator;
	var sr, pr, cr;
	var sp, hp, cp;
	var ts, th, tc;
	var rec, pb, pc;
	var ST, HT, CT, T;
	var SC, HC, CC, C;
	
	// total number of electronic holdings
	rec = parseInt(theForm.rec.value.replace(',',''));
	
	// percentage of total holdings that are simple, harder, and complex
	sp = parseFloat(theForm.sp.value);
	hp = parseFloat(theForm.hp.value);
	cp = parseFloat(theForm.cp.value);

	// time in minutes spent on simple, harder, and complex records
	ts = parseFloat(theForm.ts.value);
	th = parseFloat(theForm.th.value);
	tc = parseFloat(theForm.tc.value);

	// student, paraprofessional, and professional hourly rates
	sr = parseFloat(theForm.sr.value);
	pr = parseFloat(theForm.pr.value);
	cr = parseFloat(theForm.cr.value);
	
	// percent benefits for paraprofessionals and professionals
	pb = parseFloat(theForm.pb.value);
	pc = parseFloat(theForm.pc.value);

	if (sp + hp + cp != 100) {
		alert("Note: Percentage values for holdings information must add up to 100%");
	} else if (theForm.sp.value == '' || theForm.hp.value == '' || theForm.cp.value == '' || 
	           theForm.ts.value == '' || theForm.th.value == '' || theForm.tc.value == '' || 
	           theForm.sr.value == '' || theForm.pr.value == '' || theForm.cr.value == '' || 
	           theForm.pb.value == '' || theForm.pc.value == '' || theForm.rec.value == '') {
		alert("Note: Required input is missing");
	} else if (isNaN(sp) || isNaN(hp) || isNaN(cp) || 
	           isNaN(ts) || isNaN(th) || isNaN(tc) ||
	           isNaN(sr) || isNaN(pr) || isNaN(cr) || 
	           isNaN(pb) || isNaN(pc) || isNaN(rec)) {
		alert("Note: Invalid number entered");
	} else if (sp < 0 || hp < 0 || cp < 0 || 
	           ts < 0 || th < 0 || tc < 0 || 
	           sr < 0 || pr < 0 || cr < 0 || 
	           pb < 0 || pc < 0 || rec < 0) {
		alert("Note: Negative numbers are not valid");
	} else {
		// cost in dollars for simple, harder, and complex records
		SC = ((rec * (sp / 100)) * ((sr / 60) * ts)).toFixed(2);
		HC = ((rec * (hp / 100)) * ((pr / 60) * (1 + (pb/100)) * th)).toFixed(2);
		CC = ((rec * (cp / 100)) * ((cr / 60) * (1 + (pc/100)) * tc)).toFixed(2);
		C = (parseFloat(SC) + parseFloat(HC) + parseFloat(CC)).toFixed(2);

		// time in hours for simple, harder, and complex records
		ST = (((rec * (sp / 100)) * ts) / 60).toFixed(1);
		HT = (((rec * (hp / 100)) * th) / 60).toFixed(1);
		CT = (((rec * (cp / 100)) * tc) / 60).toFixed(1);
		T = (parseFloat(ST) + parseFloat(HT) + parseFloat(CT)).toFixed(1);

		theForm.SC.value = addCommas(SC);
		theForm.HC.value = addCommas(HC);
		theForm.CC.value = addCommas(CC);
		theForm.C.value = addCommas(C);
		
		theForm.ST.value = ST;
		theForm.HT.value = HT;
		theForm.CT.value = CT;
		theForm.T.value = T;

		theForm.rec_2.value = addCommas(rec);
		
		if (rec <= 7000) {
			theForm.cost.value = "6,750.00";
		} else if (rec > 7000 && rec <= 10000) {
			theForm.cost.value = "8,750.00";
		} else if (rec > 10000 && rec <= 15000) {
			theForm.cost.value = "9,900.00";
		} else if (rec > 15000 && rec <= 20000) {
			theForm.cost.value = "12,500.00";
		} else if (rec > 20000 && rec <= 25000) {
			theForm.cost.value = "15,000.00";
		} else if (rec > 25000 && rec <= 35000) {
			theForm.cost.value = "17,250.00";
		} else {
			theForm.cost.value = "19,500.00";
		}
	}
}
