function ForceNumber(evt) {
  evt = (evt) ? evt : window.event;
  var charCode = (evt.which) ? evt.which : evt.keyCode;
  if (charCode >= 46 && charCode <= 57 && (charCode != 47)) {
    status="";
	return true;
  }
  if ((charCode == 8) || (charCode == 9)) {
    status=""
	return true;
  }
  status = "This field only accepts US currency amounts.";
  return false;
}

function UpdTotalAmount() {
  var newTotalAmount = 0;
  var amtTithe = Number(document.frmDonation.amtTithe.value);
  var amtOffering = Number(document.frmDonation.amtOffering.value); 
  var amtSpecialOffering = Number(document.frmDonation.amtSpecialOffering.value);
  if (isNaN(amtTithe)) { amtTithe = 0; }
  if (isNaN(amtOffering)) { amtOffering = 0; }
  if (isNaN(amtSpecialOffering)) { amtSpecialOffering = 0; }
  newTotalAmount = amtTithe + amtOffering + amtSpecialOffering;
  document.frmDonation.amtTithe.value = perRound(amtTithe.toString(),2,'N');
  document.frmDonation.amtOffering.value = perRound(amtOffering.toString(),2,'N');
  document.frmDonation.amtSpecialOffering.value = perRound(amtSpecialOffering.toString(),2,'N');
  document.frmDonation.amtTotalAmount.value = perRound(newTotalAmount.toString(),2,'N');
}

function CheckAmount(fld) {
  var newVal = fld.value;
  newVal = perRound(newVal,2,'N');
  fld.value = newVal;
  if (newVal > 1000) {
    alert("You've entered a dollar amount greater than $1000.00.\nPlease verify this is the correct amount.");
    return false;
  }
  return true;
}

function validate_form(thisForm) {
  if (!myIsNumber(thisForm.amtTotalAmount.value)) {
    alert("You didn't enter a valid donation amount.\nPlease verify your donation amount.");
    return false;
  }
  if (thisForm.amtTotalAmount.value == 0) {
    alert("You didn't enter a donation amount.\nPlease verify your donation amount.");
    return false;
  }
  var updateDescription = thisForm.description.value;
  var updateNote = '';
  if ((myIsNumeric(thisForm.amtTithe.value)) && (thisForm.amtTithe.value > 0.00)) {
  	updateDescription = updateDescription + 'Tithe, ';
  	updateNote = updateNote + 'Tithe: ' + perRound(thisForm.amtTithe.value.toString(),2,'N') + '\n';
  	
  }
  if ((myIsNumeric(thisForm.amtOffering.value)) && (thisForm.amtOffering.value > 0.00)) {
  	updateDescription = updateDescription + 'Offering, ';
  	updateNote = updateNote + 'Offering: ' + perRound(thisForm.amtOffering.value.toString(),2,'N') + '\n';
  }
  if ((myIsNumeric(thisForm.amtSpecialOffering.value)) && (thisForm.amtSpecialOffering.value > 0.00)) {
  	updateDescription = updateDescription + 'Special Offering, ';
  	updateNote = updateNote + 'Special Offering';
  	var specialText = thisForm.txtSpecialOffering.value;
  	if (specialText.length > 0) {
  		updateNote = updateNote + ' ( ' + specialText + ' )';
  	}
  	 updateNote = updateNote + ': ' + perRound(thisForm.amtSpecialOffering.value.toString(),2,'N') + '\n';
  }
  updateDescription = updateDescription.substring(0, updateDescription.length - 2);
  if(thisForm.contributiontype[1].checked) {
		if(thisForm.recurringschedule.value == "none")
		{
			alert("Please select how ofted you would like to donate");
			return false;
		}
		if(isNaN(thisForm.numberoftimes.value))
		{
			alert("Please select how many times you would like to donate");
			return false;
		}					
		var newNote = "Contribution Type: " + thisForm.contributiontype[1].value + "\n";
		newNote = newNote + "Frequency of Donation: " + thisForm.recurringschedule.value + "\n";
		newNote = newNote + "Number of Times: " + thisForm.numberoftimes.value + "\n";
		updateNote = newNote + updateNote;
	}
	else {
		updateNote = "Contribution Type: " + thisForm.contributiontype[0].value + "\n" + updateNote;
	}
  thisForm.description.value = updateDescription;
  thisForm.notes.value = updateNote;
  thisForm.price.value = thisForm.amtTotalAmount.value;
  return true;
}

function perRound(nval,dval,leadingmoney) {
    if (leadingmoney == 'Y' )
    {
      dval = 2  // 2 decimal places
    }
    var xx = nval.indexOf('.')
    var l = nval.length
    var zeroPad = '0000000000000000000000'
    var numLeft = ''
    var numRight = ''
    var numAll = ''
    rfac = ''
    rfacx = 0
    nx = 0
    var xt = parseInt(dval) + 1
    var rstr = '' + zeroPad.substring(1,xt)
    var rfac = '.' + rstr + '5'
    var rfacx = parseFloat(rfac)
    if (xx == -1 )  // No fraction
    {
      numRight = zeroPad
      numLeft = "" + nval
    }
    else if (xx == 0)
    {
      numLeft = '0'
      nx = 0 + parseFloat(nval) + parseFloat(rfacx)
      n = nx + zeroPad
      numRight = '' + nval.substring(1, nval.length)
    }
    else
    {
      numLeft = nval.substring(0,xx)
      nx = parseFloat(nval) + rfacx
      nval = '' + nx + zeroPad
      numRight = '' + nval.substring(xx+1,xx + 1 + parseInt(dval))
      var astr = 'dval = ' + dval
    }
    numRight = numRight.substring(0,parseInt(dval))
    var ii = 0
    numAll = numLeft + '.' + numRight
    if (dval == 0 ) // No decimal places
    {
      numAll = numLeft
    }
    if (leadingmoney == 'Y' ) // leading $
    {
      numAll = '$' + numAll
    }
    return numAll
}

function myIsNumeric(myVal) {
	if (isNaN(parseFloat(myVal))) {
		return false;
	}
	return true;
}

function myIsNumber(str) { 
  isPrice = /^\d+\.\d{2}$/; 
  return isPrice.test( str ); 
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
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));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}