// This javascript file gets included on every page that has a form on it [jj 07Feb14]

// for the "income" field type
function UpdateIncome (fieldId) {
	var valueField = document.getElementById("value_"+ fieldId);
	var periodField = document.getElementById("period_"+ fieldId);
	var hiddenField = document.getElementById(fieldId);
	
	// strip commas and such from value field
	var v = valueField.value;
	v = v.toString().replace(/\$|\,/g,'');
	if (isNaN(v)) v = "0";
	valueField.value = v;
	
	// store data as "income/period" (e.g., "70000/year") in hidden field with proper name
	hiddenField.value = valueField.value +"/"+ periodField.value;
}

// for the "money" field type
function convertNumericToMoney (num, includeCents) {
	num = num.toString().replace(/\$|\,/g,'');
	if (isNaN(num)) num = "0";
	var sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	var cents = num%100;
	num = Math.floor(num/100);
	if (cents < 10) cents = "0" + cents;
	if (includeCents == 'False' && cents >= 50) num++;
	num = num.toString();	
	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));
	var money = ((sign) ? '' : '-') + '$' + num;
	if (includeCents == 'True') money += '.' + cents;
	return money;
}
function convertMoneyToNumeric (money) {
	var num = "";
	for (var i=0; i < money.length; i++) {
		var c = money.substring(i, i+1);
		if ((c >= '0' && c <= '9') || c == '.') num += c;
	}
	return num;
}