home *** CD-ROM | disk | FTP | other *** search
- function com_netobjects_currencyComponent( params ) {
-
- this.centsToDollars = centsToDollars;
- this.formatMoneyStr = formatMoneyStr;
- this.setCents = setCents;
- this.setMoneyToFormat = setMoneyToFormat;
-
- this.centValue = (params.centValue+"" != "undefined" ? params.centValue : "");
- this.moneyValue = (params.moneyValue+"" != "undefined" ? params.moneyValue : "");
- this.name = (params.name+"" != "undefined" ? params.name : "currencyComponent1");
- this.decimalSeparator = (params.decimalSeparator+"" != "undefined" ? params.decimalSeparator : ".");
- this.thousandSeparator = (params.thousandSeparator+"" != "undefined" ? params.thousandSeparator : ",");
-
- /* ======================================================================
- FUNCTION: setCemts
-
- INPUT: cents (int/string) - an integer or string representing a value in cents
- with no decimal point in the string
-
- RETURNS: None
-
- DESC: Sets the cents within the component that is used to convert into a string
- representing dollars and cents. For example, converts 125 cents to
- 1.25 dollars.
- ====================================================================== */
-
- function setCents(value) {
- if(value+"" != "null" && value+"" != "undefined")
- this.centsValue = value;
- }
-
- /* ======================================================================
- FUNCTION: setMoneyToFormat
-
- INPUT: str (string) - a string representing a money value in dollars
- or dollars and cents
-
- RETURNS: None
-
- DESC: Sets the moneyValue within the component that is used to convert into a string
- representing dollars and cents. e.g. $12.00.
- This prevents oddly formatted values such as $12.5 by replacing it with
- $12.50. Note that this function does not add the '$' character.
- ====================================================================== */
-
- function setMoneyToFormat(value) {
- if(value+"" != null && value+"" != "undefined")
- this.moneyValue = value;
- }
-
- /* ======================================================================
- FUNCTION: centsToDollars
-
- INPUT: cents (int/string) - an integer or string representing a value in cents
- with no decimal point in the string
-
- RETURNS: a string representing the input value in dollars;
- if 'cents' is null or undefined, returns -1.
-
- DESC: Converts an integer cents value into a string representing
- dollars and cents. For example, converts 125 cents to
- 1.25 dollars.
- ====================================================================== */
- function centsToDollars() {
- var resultStr = "0" + this.decimalSeparator + "00";
-
- // Return immediately if an invalid value was passed in
- if (this.centValue+"" == "undefined" || this.centValue+"" == "null")
- return null;
-
- // Convert to string in case we were passed a numeric value
- this.centValue = this.centValue + "";
-
- if (this.centValue.length == 1) // handle case where value is 1 to 9 cents
- resultStr = "0" + this.decimalSeparator + "0" + this.centValue;
- else {
- if (this.centValue.length == 2) // handle case where value is 10 to 99 cents
- resultStr = "0" + this.decimalSeparator + this.centValue;
- else {
- if ( this.centValue.length > 5) {
- resultStr = this.centValue.substring(0, this.centValue.length - 5) + this.thousandSeparator +
- this.centValue.substring(this.centValue.length - 5, this.centValue.length - 2) + this.decimalSeparator +
- this.centValue.substring(this.centValue.length - 2, this.centValue.length);
- }
- else {
- resultStr = this.centValue.substring(0, this.centValue.length - 2) + this.decimalSeparator +
- this.centValue.substring(this.centValue.length - 2, this.centValue.length);
- }
- }
- }
-
- return resultStr;
- } // end centsToDollars
-
- /* ======================================================================
- FUNCTION: formatMoneyStr
-
- INPUT: str (string) - a string representing a money value in dollars
- or dollars and cents
-
- RETURNS: a string representing the input value in dollars
- with two decimal places for cents;
- returns null if invalid arguments were passed
-
- DESC: This function ensures all values of dollars and cents appear with two
- decimal places representing cents after the dollar amount, e.g. $12.00.
- This prevents oddly formatted values such as $12.5 by replacing it with
- $12.50. Note that this function does not add the '$' character.
- ====================================================================== */
- function formatMoneyStr() {
- resultStr = "";
- resultStr2 = "";
-
- // Return immediately if an invalid value was passed in
- if (this.moneyValue+"" == "undefined" || this.moneyValue+"" == "null" || this.moneyValue+"" == "")
- return null;
-
- // Make sure argument is a string
- this.moneyValue += "";
-
- // Get the index of the decimal point.
- idx = this.moneyValue.indexOf(this.decimalSeparator);
- idx2 = this.moneyValue.indexOf(this.thousandSeparator);
-
- // If there is no decimal point, add ".00"
- if (idx < 0)
- resultStr = this.moneyValue + this.decimalSeparator + "00";
- else
- {
- resultStr = this.moneyValue.substring(0, idx + 3);
- if (resultStr.length < (idx + 3))
- resultStr += "0";
- }
-
- if( idx2 < 0 && resultStr.length > 6)
- resultStr2 = resultStr.substring(0, resultStr.length - 6) + this.thousandSeparator +
- resultStr.substring(resultStr.length - 6, resultStr.length)
- else
- resultStr2 = resultStr;
- return resultStr2;
- } // end formatMoneyStr
- }