/*
Utilities.js v. 0.1: A JavaScript library.
Copyright (c) 2005 Solidium


Changes: 25-09-2008 MJB geen euro teken meer doorgeven maar 'EUR'.
	     06-10-2008 MJB 'EUR' uit output van functies gehaald

*/

/***********************************************************
*               Section: Formatting Functions              *
*                                                          *
* These functions format field values. Each field is       *
* assigned the formatting function appropriate to its data *
* type as a method. Each method reformats the contents of  *
* the field value in a way appropriate to the data type.   *
* Each method returns the formatted value or an error      *
* string. The error string is always in the form           *
* <code>"ERROR:<i>message</i>"</code>.                     *
***********************************************************/

/**
 * Formats a value to a positive currency string
 * (<code>$##,###</code>). It is assigned to fields with
 * data type "Currency".
 *
 * @param value The value to be formatted. When assigned
 *              as a validation method, the field's value
 *              is used as the parameter for this method,
 *              and that value is reset to the result of
 *              this method.
 * @return The currency string or an error string if 
 *         the formatting failed.
 */
function formatCurrency(value) {
	var decimalvalue;

    value = '' + value; // Force conversion to string
    value = trim( value );
 
    if (value == '') {
		return '0,00';
    }
  
    // delete decimals
	if (value.indexOf(',') > -1) {
		value = value.substring(0, value.indexOf(','));
	}
	  	  
    if( isNaN( value ) ) {
        return value;
    }
	
    value = parseDecimal(value);
    // Add scaling value for rounding:
    value += 0,005;

    value += ''; // Force conversion to string
    // In Netscape, decimals under 1.0 have no leading zero:
    if (value.charAt(0) == '.') {
        value += '0';
    }

    var newValue = '';
    for (var i=value.length; i>3; i-=3) {
        newValue = '.' + value.substring(i-3, i) + newValue;
    }

 	if( isNaN( value ) ) {
        return value;
    }

	newValue = value.substring(0, i) + newValue + ",-"; 
		
    return newValue;
}

/**
* Formats a value to a signed currency string. It is
* assigned to fields with data type "SignedCurrency".
*
* @param value The value to be formatted. When assigned
*              as a validation method, the field's value
*              is used as the parameter for this method,
*              and that value is reset to the result of
*              this method.
* @return The currency string or an error string if 
*         the formatting failed.
*/
function formatSignedCurrency(value) {  
    value = '' + value; // Force conversion to string
    value += ''; // Force conversion to string
    var sign = '';

    // Strip leading spaces:
    while (value.charAt(0) == ' ') value = value.substring(1);
    if (value.charAt(0) == '-') sign = '-';

        var result = formatCurrency(value);
    if (result.indexOf('ERROR') == 0) {
        return 'ERROR';
    } else {
        return sign + result;
    }
}


/***********************************************************
*                Section: Parsing Functions                *
*                                                          *
* These methods parse string values into various data      *
* types. They are used primarily to parse the values of    *
* form fields. Each field is assigned the parsing function *
* appropriate to its data type. The parsing methods return *
* a value of <code>NaN</code> if the value passed to the   *
* method does not parse to the desired value.              *
***********************************************************/

/**
 * This method parses a string into a decimal value.
 * It is more forgiving than Javascript's standard
 * <code>parseFloat()</code> method because it ignores
 * <i>all</i> non-decimal characters in the string.
 * This method returns a positive number. To allow
 * negative numbers, use <code>parseSignedDecimal()</code>
 * instead.
 *
 * @param value The value to be parsed.
 * @return The positive decimal value of the string, or
 *         <code>NaN</code> if the string contains no
 *         numbers.
 */
function parseDecimal(value) {  
    return parseFloat(stripNonDecimal(value));
}

/**
 * As <code>parseDecimal()</code>, but allows negative
 * values as well. This is not the default because for
 * most applications, allowing negative values can lead
 * to unexpected results and possibly security violations.
 *
 * @param value The value to be parsed.
 * @return The signed decimal value of the string, or
 *         <code>NaN</code> if the string contains no
 *         numbers.
 */
function parseSignedDecimal(value) {
    value += ''; // Force conversion to string
    while (value.charAt(0) == ' ') {
        value = value.substring(1);
    }
    var sign = 1;
    if (value.charAt(0) == '-') {
        sign = -1;
    }
    return sign * parseDecimal(value);
}

/**
 * This method parses currency into a decimal value.
 *
 * @param value The value to be parsed.
 * @return The positive decimal value of the string, or
 *         <code>NaN</code> if the string contains no
 *         numbers.
 */
function parseCurrency(value) {
    return parseDecimal( value );
}

/**
 * As <code>parseCurrency()</code>, but allows negative
 * values as well. This is not the default because for
 * most applications, allowing negative values can lead
 * to unexpected results and possibly security violations.
 *
 * @param value The value to be parsed.
 * @return The signed decimal value of the string, or
 *         <code>NaN</code> if the string contains no
 *         numbers.
 */
function parseSignedCurrency(value) {
    return parseSignedDecimal( value );
}

/**
 * This method strips the value of any non-numeric
 * characters. It is useful for the preliminary
 * formatting of such fields as Social Security
 * Numbers, Postal Codes and Phone Numbers.
 *
 * @param value The value to be stripped.
 * @return The string with all non-numeric characters
 *         stripped.
 */
function stripNonNumeric(value) {
  var result = '';
  var c;              // The current character.

  value += ''; // Force conversion to string

  for (var i=0; i<value.length; i++) {
    c = value.charAt(i);
    if ((c >= '0') && (c <= '9')) {
      result += c;
    }
  }
  return result;
}

/**
 * This method strips the value of any non-decimal
 * characters. In particular, it retains only "," and
 * the values "0" to "9". It is a useful preliminary
 * for parsing of integer and decimal values.
 *
 * @param value The value to be stripped.
 * @return The string with all non-decimal characters
 *         stripped.
 */
function stripNonDecimal(value) {
  var result = '';
  var c;              // The current character.

  value += ''; // Force conversion to string

  for (var i=0; i<value.length; i++) {
    c = value.charAt(i);
    if (((c >= '0') && (c <= '9'))){ // || (c == '.')) {
      result += c;
    }
  }
  return result;
}


function trim(str) {
    return str.replace(/^\s*|\s*$/g, '');
}
