//  -----   feel free to use this stuff   ---   play with it   ---   copy as You  like   -----
//  -----   but leave this authors 2 lines untouched:   pseliger@gmx.net [august 2004]   -----
//
//  extended javascript-api-methods   :
//  * jsApi-extension-name / file-name: "jsApi.NumberString.flexibleConverting.js"
//  * original download-location      : "http://www.pseliger.de/jsExtendedApi/jsApi.NumberString.flexibleConverting.js"
//
//  last revision       :  august 2004  - introduction of a string type argument that is recognized
//                                        as comma separator that will overrule the methods automatic
//                                        format detection;
//  fourth revision     :   march 2004  - full code review and partly code rebuild;
//                                      - CAUTION, renaming: - "flexibleRound" to "round";
//                                                           - "forceToNumber" to "forceNumber";
//                                      - the pure js-code comes with 3.532 bytes;
//  third revision      :    june 2003  - there was still some tuning to do according to size and
//                                        performance of the code  -  no further revision planned;
//                                      - the pure js-code comes with 3.356 bytes;
//  second revision     :   march 2003  - code of all methods, I belief, is on its edge now;
//                                      - no further revision planned (5.435 / 11.338 bytes);
//  first revision      : january 2003  - the pure js-code has now 3.167 bytes only;
//  first public release:  august 2002  - the pure js-code comes with 3.265 bytes;
//

  Number.prototype.round = function() {var numberValue=this;if(!isNaN(numberValue)){if((arguments.length>=1)&&(!isNaN(parseFloat(arguments[0])))){var decimalPlaces=Math.round(parseFloat(arguments[0]));numberValue=((decimalPlaces>=1)?(Math.round(numberValue*Math.pow(10,decimalPlaces))/Math.pow(10,decimalPlaces)):(Math.round(numberValue)));}else{numberValue=Math.round(numberValue);}}return numberValue;};
  String.prototype.round = function() {var numberValue=parseFloat(this).round((arguments.length>=1)?(arguments[0]):(""));return((isNaN(numberValue))?(numberValue):(numberValue.toString()));};
//String.prototype.round = function() {var numberValue=Number.prototype.round.apply(parseFloat(this),arguments);return((isNaN(numberValue))?(numberValue):(numberValue.toString()));};
//"Function.apply": implemented in nnav 4.06++, msie 5.5++;  also proofed for opera 7.22/windows;

  Number.prototype.fixedDigits = function() {var content=this;if(!isNaN(content)){var separatorFlag="comma",decimalPlaces=-1,commaSymbol="",blockSymbol="",i;for(i=0;i<arguments.length;i++){if(arguments[i].constructor==Number){decimalPlaces=Math.round(arguments[i]);}else if(arguments[i].constructor==String){if(separatorFlag=="comma"){commaSymbol=arguments[i];}else if(separatorFlag=="block"){blockSymbol=arguments[i];}separatorFlag=((separatorFlag=="block")?("comma"):((separatorFlag=="comma")?("block"):("comma")));}}if(decimalPlaces<0){decimalPlaces=((content.toString().indexOf(".")>=0)?((content.toString().split("."))[1].length):(0));}if(commaSymbol===""){commaSymbol=".";}content=((decimalPlaces>=1)?(Math.round(content*Math.pow(10,decimalPlaces))/Math.pow(10,decimalPlaces)):(Math.round(content))).toString();if(decimalPlaces>=1){content=content.split(".");}else{content=new Array(content);commaSymbol="";}content[content.length]="";var blockCounter=0;for(i=content[0].length-1;i>=0;i--){content[content.length-1]=content[0].charAt(i)+content[content.length-1];blockCounter++;if(blockCounter==3){blockCounter=0;content[content.length-1]=blockSymbol+content[content.length-1];}}content[0]=content[content.length-1];content.length=content.length-1;if(content.length==1){content[1]="";}for(i=content[1].length;i<decimalPlaces;i++){content[1]+="0";}content=new String(content[0]+commaSymbol+content[1]);if(content.charAt(0)==blockSymbol){content=content.substring(1);}}return content;};
  String.prototype.fixedDigits = function() {var content=parseFloat(this);if(!isNaN(content)){var separatorFlag="comma",decimalPlaces=-1,commaSymbol="",blockSymbol="",i;for(i=0;i<arguments.length;i++){if(arguments[i].constructor==Number){decimalPlaces=Math.round(arguments[i]);}else if(arguments[i].constructor==String){if(separatorFlag=="comma"){commaSymbol=arguments[i];}else if(separatorFlag=="block"){blockSymbol=arguments[i];}separatorFlag=((separatorFlag=="block")?("comma"):((separatorFlag=="comma")?("block"):("comma")));}}content=content.fixedDigits(decimalPlaces,commaSymbol,blockSymbol);}return content;};
//String.prototype.fixedDigits = function() {return Number.prototype.fixedDigits.apply(parseFloat(this),arguments);};
//"Function.apply": implemented in nnav 4.06++, msie 5.5++;  also proofed for opera 7.22/windows;

  String.prototype.forceNumber = function(commaSeparator) {var content=this;if((typeof commaSeparator=="string")&&((commaSeparator==".")||(commaSeparator==","))){content=content.split(commaSeparator);}else{var posDot=content.lastIndexOf(".");var posComma=content.lastIndexOf(",");if((posDot>=0)||(posComma>=0)){if((content.split(".").length==2)&&(content.split(",").length==2)){content=content.split((posDot>posComma)?("."):(","));}else if((content.split(".").length==2)&&(content.split(",").length!=2)){content=content.split(".");}else if((content.split(".").length!=2)&&(content.split(",").length==2)){content=content.split(",");}else{content=new Array(content);}}else{content=new Array(content);}}var regX=/\d+/g;var regXMatch,i,k;for(i=0;i<content.length;i++){regXMatch=content[i].match(regX);content[i]="";if(regXMatch){for(k=0;k<regXMatch.length;k++){content[i]+=regXMatch[k];}}}return parseFloat((content.length==1)?(content[0]):(content[0]+"."+content[1]));};


  Number.prototype.Tausender = function (tausendTrenner) {
var number=this;
number = '' + number;
if (number.length > 3) {
var mod = number.length % 3;
var output = (mod > 0 ? (number.substring(0,mod)) : '');
for (i=0 ; i < Math.floor(number.length / 3); i++) {
if ((mod == 0) && (i == 0))
output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
else

// hier wird das Trennzeichen festgelegt mit '.'
output+= '.' + number.substring(mod + 3 * i, mod + 3 * i + 3);
}
return (output);
}
else return number;
}


//
//  "round"       - [DE] wird auf Numbers oder Strings angewandt - liefert einen gerundeten zahlenwert im selben typ (number oder string) zurueck oder aber NaN - erwartet einen ganzzahligen parameter, der die anzahl der nachkommastellen enthaelt, auf die es zu runden gilt - bei anderen parametern oder ohne jegliche angabe verhaelt sich die methode wie "Math.round()";
//                - [EN] is to be used with both Numbers/Strings - returns the roundet value of a "number" transforming it into the calling-type or returns NaN - accepts a parameter of type integer that represents the number of decimal places that will be rounded to       - with other parameters or without any specification the method behaves like "Math.round()";
//
//  "fixedDigits" - [DE] wird auf Numbers oder Strings angewandt - liefert immer einen als string formatierten zahlenwert oder aber NaN zurueck - akzeptiert kombinationen aus bis zu maximal 3 parametern: ein parameter vom typ integer wird immer als anzahl der erzwungenen nachkommastellen (z.b.:586.0100) interpretiert / der erste string-parameter (der vorletzte bei mehreren string-parametern) wird bei der formatierung als komma verwendet                 / der zweite string-parameter (der letzte bei mehr als 2 string-parametern) wird als trennzeichen fuer die visuelle teilung der zahl in tausender-bloecke genutzt             - fehlt das argument vom typ number, werden alle nachkommastellen uebernommen;
//                - [EN] is to be used with both Numbers/Strings - returns always a string that represents a formatted number  or  returns NaN  - accepts combinations of 3 or less parameters            : a integer type parameter always gets interpreted as amount of forced decimal places (e.g.: 586.0100)               / the first string type parameter (the next to last if there are more string parameters) gets used as comma within the formatting process / the second string type parameter (the last one if there are more then 2 string parameters) gets used as separator while dividing the number visually in blocks of thousends - if the number type argument is missing all decimal places behind the comma are accepted;
//
//  "forceNumber" - [DE] wird nur auf Strings angewandt - versucht, einen formatierten dezimalzahl-string in einen zahlenwert umzuwandeln    - liefert diesen wert oder NaN zurueck;
//                - [EN] to be used with Strings only   - tries to convert a formatted string that represents a decimal number into a number - returns this value or returns NaN;


//supporting the deprecated naming convention:
//
  Number.prototype.flexibleRound = Number.prototype.round;
  String.prototype.flexibleRound = String.prototype.round;
  String.prototype.forceToNumber = String.prototype.forceNumber;
