/** * Financial Functions**  &copy;Copyright Mike Brockington 2007 - 2009 All rights reserved.*  Build Number:10:* This version first released: 22/03/2007*/
/** Construct an object to handle calculations using discrete objects, rather than continuous numbers* @class* Concrete class - based on simpleCalculator(), with details of required fields and calculations added* @constructor* @return Nothing*/
function financeCalc(){/*  Integer */
this.numberOfItems=0;/** ID of the result that the context menu is 'attached' to.*  Integer*/
this.currentMenuRef="";/** Currency value;may be float or integer*  Number*/
this.totalAmount=0;/*  defaultDataObject */
this.arrItems=[new defaultDataObject()];/** Usually a single character*  String*/
this.charCurrencySymbol="\xA4";this.creditAmount=creditThisAmount;this.debitAmount=debitThisAmount;this.clearCurrent=clearCurrentCalc;this.removeItem=removeCalcItem;this.editItem=editCalcItem;this.removeTotal=removeThisTotal;this.showTotal=showThisTotal;}/** Adds the latest value to the list. Alternative No.1* s None.*  Function*  Boolean indicating success(false) or failure(true).* @see #calcSum2*/
function calcSum1(){checkResultsVisible();thisCalculator.charCurrencySymbol=getSelectValue("Currency");var creditField=document.getElementById("txtCredit");var debitField=document.getElementById("txtDebit");if(creditField.value){thisCalculator.creditAmount(creditField.value);creditField.value="";}else{thisCalculator.debitAmount(debitField.value);debitField.value="";}var strFieldName="txtCredit";if(document.frmCalc && document.frmCalc.elements && document.frmCalc.elements[strFieldName]){document.frmCalc.elements[strFieldName].focus();}else{logMsg("[200104] Unable to find form element:"+strFieldName, params={level:"error"});}return false;}/** Adds the given value to the list.* s debitValue unformatted number/currency*  Function*  Nothing*/
function debitThisAmount(debitValue){this.numberOfItems++;this.removeTotal();this.arrItems.push(new financeCalcData("Debit", debitValue) );this.totalAmount -=(1 * debitValue);var output="("+debitValue.toCurrency(this.charCurrencySymbol)+")";recordResultText(output, params={CSSclassName:"debit", elementID:"debit"+this.numberOfItems, showActions:true});this.showTotal();}/** Adds the given value to the list.* s creditValue unformatted number/currency*  Function*  Nothing*/
function creditThisAmount(creditValue){this.numberOfItems++;this.removeTotal();this.arrItems.push(new financeCalcData("Credit", creditValue) );this.totalAmount+=(1 * creditValue);recordResultText(creditValue.toCurrency(this.charCurrencySymbol), params={CSSclassName:"credit", elementID:"credit"+this.numberOfItems, showActions:true});this.showTotal();}/** Adds the latest value to the list. Alternative No.2* s None.*  Function*  Boolean indicating success(false) or failure(true).*/
function calcSum2(buttonUsed){if((buttonUsed=="Credit") || (buttonUsed=="Debit")){checkResultsVisible();thisCalculator.charCurrencySymbol=getSelectValue("Currency2");var valueField=document.getElementById("txtAmount");if(buttonUsed=="Credit"){thisCalculator.creditAmount(valueField.value);}else{thisCalculator.debitAmount(valueField.value);}valueField.value="";var strFieldName="txtAmount";if(document.frmCalc2 && document.frmCalc2.elements && document.frmCalc2.elements[strFieldName]){document.frmCalc2.elements[strFieldName].focus();}else{logMsg("[200106] Unable to find form element:"+strFieldName, params={level:"error"});}return false;}else{logMsg("[200105] Unforeseen error;no Submit details.", params={level:"error"});}return false;}/** Construct an object to store calculations using discrete objects, rather than continuous numbers* @class* Concrete class - subclass of simpleDataObject(), with details of required fields and calculations added* @constructor* @return Nothing*/
function financeCalcData(type, amount){/*  String Array */
this.arrErrorMsg=new Array();/**  Number*/
this.numAmount=amount;/**  Boolean*/
this.isCredit=(type=="Credit");}/** Carry out the base calculation.*  Function*  Nothing*/
function showThisTotal(){recordResultText("Total: "+this.totalAmount.toCurrency(this.charCurrencySymbol), params={CSSclassName:"total", showActions:false});}/** This constructor defines a default object to fill up the Zero index in a calculator's data store* Concrete class - may be subclassed if required* Copied from simpleObject.js for convenience* @constructor*/
function defaultDataObject(){}/** Remove an existing entry.*  Function*  Nothing*/
function removeCalcItem(calcNumber){var oldData=this.arrItems[calcNumber];if(oldData.isCredit ){this.totalAmount -=(1 * oldData.numAmount);}else{this.totalAmount+=(1 * oldData.numAmount);}clearLastResult();this.showTotal();}/** Change an existing entry.*  Function*  Nothing*/
function editCalcItem(calcNumber){var elementID="";var oldData=this.arrItems[calcNumber];var newData=prompt("Please enter the new value, \nUse a minus sign if the amount is a debit. \n\nLeave blank to cancel this change.");if(newData.length > 0){if(isFinite(newData) ){if(oldData.isCredit ){this.totalAmount -=(1 * oldData.numAmount);elementID="credit"+calcNumber;}else{this.totalAmount+=(1 * oldData.numAmount);elementID="debit"+calcNumber;}this.totalAmount+=(1 * newData);oldData.numAmount=(1 * newData);oldData.isCredit=true;var newElement=document.createTextNode( newData.toCurrency(this.charCurrencySymbol) );modifyResult(elementID, newElement, params={oldClassName:"debit", CSSclassName:"credit"});clearLastResult();this.showTotal();}else{alert("Please only enter numeric values, without extra symbols.");}}else{}}/** Carry out the base calculation.*  Function*  Nothing*/
function removeThisTotal(){clearLastResult();appSettings.oddResultsLine=!(appSettings.oddResultsLine);}/** Carry out the base calculation.*  Function*  Nothing*/
function clearCurrentCalc(){this.numberOfItems=0;this.totalAmount=0;this.arrItems=[new defaultDataObject()];this.charCurrencySymbol="\xA4";}
