function Comma(num) {
  num = '' + num;
  var output = num;
  if (num.length > 3) {
    var mod = num.length % 3;
    output = (mod > 0 ? (num.substring(0,mod)) : '');
    for (i=0 ; i < Math.floor(num.length / 3); i++) {
      if ((mod == 0) && (i == 0)) { 
	    output += num.substring(mod+ 3 * i, mod + 3 * i + 3); 
      }
      else { 
	    output+= ',' + num.substring(mod + 3 * i, mod + 3 * i + 3); 
	  }
    }
  }
  return (output);
}

function removeComma(num) {
  var s = num;
  s = s.replace(/\,/g, "");
  return s;
}

function removeDollar(num) {
  var s = num;
  s = s.replace(/\$/g, "");
  return s;
}

function trim(st) {
  while(''+st.charAt(st.length-1)==' ') st=st.substring(0,st.length-1);
  while(''+st.charAt(0)==' ') st=st.substring(1,st.length);
  return st;
}

function CheckRangeAndFormat(field, category) {
  var returnValue = 0;
  field.value=removeDollar(field.value);
  if (trim(field.value)==""){
    field.value="0";
    return 0;
  }

  if (field.value > 9999999){
    alert(category + " cannot be more than $9,999,999.");
    field.value="0";
	field.focus();
	return -1;
  }
  
  if (field.value < 0 || field.value == NaN){
  	alert(category + " must be greater than or equal to 0.");
	field.value="0";
	field.focus();
	return -1;
  }
  
  returnValue = removeComma(field.value);
  if (isNaN(returnValue)){
	alert("Invalid number.\n Please re-enter.");
	field.value="0";
	field.focus();
	return -1;
  }
  
  return returnValue;
}

function doCalc() {
  var temp = 0;
  var salary_amt = 0;
  var monthly = 0;
  var yearly = 0;
  
  temp = CheckRangeAndFormat(document.tithe.salary, "Annual Salary Amount");
  if (temp < 0) return; else salary_amt = temp;

  document.tithe.tithe_m2.value = '$' + Comma(parseInt(salary_amt/12 * (2/100)));
  document.tithe.tithe_y2.value = '$' + Comma(parseInt(salary_amt * (2/100)));
  document.tithe.tithe_m3.value = '$' + Comma(parseInt(salary_amt/12 * (3/100)));
  document.tithe.tithe_y3.value = '$' + Comma(parseInt(salary_amt * (3/100)));
  document.tithe.tithe_m4.value = '$' + Comma(parseInt(salary_amt/12 * (4/100)));
  document.tithe.tithe_y4.value = '$' + Comma(parseInt(salary_amt * (4/100)));
  document.tithe.tithe_m5.value = '$' + Comma(parseInt(salary_amt/12 * (5/100)));
  document.tithe.tithe_y5.value = '$' + Comma(parseInt(salary_amt * (5/100)));
  document.tithe.tithe_m6.value = '$' + Comma(parseInt(salary_amt/12 * (6/100)));
  document.tithe.tithe_y6.value = '$' + Comma(parseInt(salary_amt * (6/100)));
  document.tithe.tithe_m7.value = '$' + Comma(parseInt(salary_amt/12 * (7/100)));
  document.tithe.tithe_y7.value = '$' + Comma(parseInt(salary_amt * (7/100)));
  document.tithe.tithe_m8.value = '$' + Comma(parseInt(salary_amt/12 * (8/100)));
  document.tithe.tithe_y8.value = '$' + Comma(parseInt(salary_amt * (8/100)));
  document.tithe.tithe_m9.value = '$' + Comma(parseInt(salary_amt/12 * (9/100)));
  document.tithe.tithe_y9.value = '$' + Comma(parseInt(salary_amt * (9/100)));
  document.tithe.tithe_m10.value = '$' + Comma(parseInt(salary_amt/12 * (10/100)));
  document.tithe.tithe_y10.value = '$' + Comma(parseInt(salary_amt * (10/100)));
	   
  document.tithe.salary.value = '$' + Comma(salary_amt);
  document.tithe.salary.focus;
}
