// Replace the control names/ids with the proper names of the desired fields
var m_fld1=document.getElementById("cur_884_cur_884_SubTotal");
var m_fld2=document.getElementById("cur_885_cur_885_Taxes");
var m_fld3=document.getElementById("cur_886_cur_886_Total_with_Tax");
var m_fld3b=document.getElementById("cur_886_cur_886_Total_with_Tax_readonly")

var strDelimiter = ",";
var strCurrencyType = "$";

SetOnChange(m_fld1);
SetOnChange(m_fld2);
SetOnChange(m_fld3);

function CalcFlds()
{
	if (m_fld3){
		m_fld3.value= Number(m_fld1.value) + Number(m_fld2.value);
		
		//Format the Total with Tax currency value
		m_fld3.value = parseFloat(m_fld3.value.replace(/,/g, "")).toFixed(2);
		m_fld3b.innerHTML = strCurrencyType + " " + parseFloat(m_fld3.value.replace(/,/g, ""))
												.toFixed(2)
												.toString()
												.replace(/\B(?=(\d{3})+(?!\d))/g, strDelimiter);
	}
}

/********************************************************************/
function SetOnChange(fld)
{
	var strPrevFunc="";
	if(fld){
		if (fld.onchange){
			strPrevFunc=fld.onchange.valueOf();
		}
		fld.onchange=BuildFunction("CalcFlds();",strPrevFunc);
	}
}

function BuildFunction(strNewContent,strContentFunction){
	var strBody;
	if (strContentFunction){		
		strBody="var objFunction=" +
					strContentFunction + ";" + 
					strNewContent + "objFunction();";
	}
	else{
		strBody=strNewContent;
	}
	
	var objFunc=new Function ( strBody );
	return objFunc;
}

