function calcPercentage() {

  var txtFatTotal = document.getElementById( 'txtFatTotal' );
  var txtFatTrans = document.getElementById( 'txtFatTrans' );

  txtFatTotal.value = (txtFatTotal.value.match( /^\.$/ ) ? '0' : '') + txtFatTotal.value;
  txtFatTrans.value = (txtFatTrans.value.match( /^\.$/ ) ? '0' : '') + txtFatTrans.value;

  var oFat = {};
  oFat.total = parseFloat( txtFatTotal.value );
  oFat.trans = parseFloat( txtFatTrans.value );

  // if valid values...
  if (!isNaN( oFat.total ) && !isNaN( oFat.trans )) {

    // if trans is greater than total...
    if (oFat.trans > oFat.total) {

      alert( 'The amount of trans fat is higher than the amount of total fat.' );
      txtFatTrans.focus();

    } else {

      var nPercentage = Math.round( oFat.trans / oFat.total * 100 * 10 ) / 10;
      if (!isNaN( nPercentage ) && nPercentage != 0) { document.getElementById( 'txtPercentage' ).value = nPercentage.toString() + '%'; }
    }

  } else {

    if (isNaN( oFat.trans ) && !isNaN( oFat.total ) && txtFatTrans.value != '') { alert( 'Please enter a valid trans fat amount.' ); }
    if (isNaN( oFat.total ) && !isNaN( oFat.trans ) && txtFatTotal.value != '') { alert( 'Please enter a valid total fat amount.' ); }
  }
}

window.onload = function () { document.getElementById( 'txtFatTotal' ).focus(); }
