function SCGetVal(id)
{
	return document.getElementById(id).firstChild.nodeValue;
}

function SCSetVal(id, val, m_format)
{
	if(m_format == true)
		val = SCMoneyFormat(val);
		
	document.getElementById(id).firstChild.nodeValue = val;
}

function SCMoneyFormat(value)
{
	if (isNaN(value)) {
		return 0;
	}

	var n = new Number(value);
	if (n < 0) {
		var fv = new String('($' + SCCommaFormat(Math.abs(n).toFixed(2)) + ')');
	} else {
		fv = new String('$' + SCCommaFormat(n.toFixed(2)));
	}
	return fv;
}

function SCCommaFormat(value)
{
	var workval = new String(value);

	// Nothing necessary?
	if (workval.length < 4 ||
		(workval.substr(workval.length - 3, 1) == '.' && workval.length < 6)) {
		return workval;
	}

	// Strip decimals
	if (workval.substr(workval.length - 3, 1) == '.') {
		var decim = workval.substr(workval.length - 3);
		workval = workval.substr(0, workval.length - 3);
	}

	var ncommas = Math.floor(workval.length / 3);
	var pt = Math.floor(workval.length % 3);

	var finalval = workval.substring(0, pt);
	for (var i = 0; i < ncommas; i++) {
		finalval += ',' + workval.substr(pt + (i * 3), 3);
	}

	// Normalize
	if (finalval.charAt(0) == ',') {
		finalval = finalval.substr(1);
	}

	if (decim) {
		finalval += decim;
	}
	return finalval;
}

function SCMoneyUnformat(value)
{
	var uf = Number(value.replace(/[%\,\$\(\)]/g, ''));
	if (value.charAt(0) == '(') {
		uf -= (uf * 2);
	}
	return uf;
}

function recalculate()
{
	f = document.sc_form;
	
	purchase_from_cj_val = f.purchase_from_cj.value;			   // Row 10
	max_credit_allowed_val = 500000
	ytd_purchases_before_cj_val = f.ytd_purchases_before_cj.value; // Row 14
	
	// Top Section --------------------------------------------------
	// Calc row 16
	if(ytd_purchases_before_cj_val > max_credit_allowed_val)
		eligible_for_sec179_val = 0;
	else
		eligible_for_sec179_val = max_credit_allowed_val * 1 - ytd_purchases_before_cj_val * 1;
		
	SCSetVal('eligible_for_sec179', eligible_for_sec179_val, true);
	
	// Calc row 18
	if(purchase_from_cj_val < eligible_for_sec179_val)
		avail_for_sec179_val = purchase_from_cj_val * 1;
	else
		avail_for_sec179_val = eligible_for_sec179_val * 1;
		
	SCSetVal('avail_for_sec179', avail_for_sec179_val, true);

	// Middle Section ---------------------------------------------
	// Calc row 24
	remaining_after_179_val = purchase_from_cj_val * 1 - avail_for_sec179_val * 1;
	SCSetVal('remaining_after_179', remaining_after_179_val, true);
	
	// Calc row 28
	bonus_dep_val = remaining_after_179_val * .5;
	SCSetVal('bonus_dep', bonus_dep_val, true);
	
	// Calc row 30
	remain_dep_basis_val = purchase_from_cj_val * 1 - avail_for_sec179_val * 1 - bonus_dep_val * 1;
	SCSetVal('remain_dep_basis', remain_dep_basis_val, true);
	
	// Calc row 34
	add_08_dep_val = remain_dep_basis_val * (1/7);
	add_08_dep_val = Math.round(add_08_dep_val);
	SCSetVal('add_08_dep', add_08_dep_val, true);
	
	// Calc row 36
	total_allow_08_dep_val = avail_for_sec179_val * 1 + bonus_dep_val * 1 + add_08_dep_val * 1;
	SCSetVal('total_allow_08_dep', total_allow_08_dep_val, true);

	// Bottom Section
	// Update row 42
	SCSetVal('pur_from_cj_08', purchase_from_cj_val, true);
	
	// Calc row 46
	tot_norm_allow_dep_val = purchase_from_cj_val * (1/7);
	tot_norm_allow_dep_val = Math.round(tot_norm_allow_dep_val);
	SCSetVal('tot_norm_allow_dep', tot_norm_allow_dep_val, true);
	
	// Calc row 48
	add_depr_08_val = total_allow_08_dep_val * 1 - tot_norm_allow_dep_val * 1;
	SCSetVal('add_depr_08', add_depr_08_val, true);
	
	// Calc row 53
	tax_rate =  f.corp_tax_rate.value;
	tax_savings_08_val = add_depr_08_val * tax_rate;
	tax_savings_08_val = Math.round(tax_savings_08_val);
	SCSetVal('tax_savings_08', tax_savings_08_val, true);
	
	// Calc row 55
	if(purchase_from_cj_val != 0)
	{
		reduction_val = tax_savings_08_val / purchase_from_cj_val;
		reduction_val = reduction_val * 100;
		reduction_val = Math.round(reduction_val * 10) / 10;
		SCSetVal('reduction', reduction_val + '%', false);
	}
	else
		SCSetVal('reduction', '0%', false);

}

