//-----------------------------------------Begin------------------------------------------//
			
	/*		
	===================================================================
	NAME: numberFormat
	TYPE: javascript (User Defined Functions)
	VERSION: 2.05.10.04
	CREATED ON: 1/17/2005
	AUTHOR: Dean Dal Bozzo, dean@pagefx.com
	===================================================================	

	DESIGNED FOR: Use within javascript coding

	DESCRIPTION: Rounds and formats numbers.

	NOTES: Highest number this fuction can format is under 1 quadrillion (15 zeros)

	USAGE: with a form, form events or in a function
		
	ATTRIBUTES: 
		numN			OPTIONAL	integer or float, default is 0
		fmt				OPTIONAL	initial of format type, default is g for General
		presn			OPTIONAL	integer, default is 0 (d for Dollar, default is 2)
		cmas			OPTIONAL	0 or 1, defualt is 0
		negDis			OPTIONAL	-,(),<>,CR, defualt is none
		
	OUTPUT: Formated number.
	
	EXAMPLES:
			numberFormat(numN,fmt);					
			
			numberFormat(1200.569,'g');				1200.569		// General
			numberFormat(1200.569,'f');				1201			// Fixed
			numberFormat(1200.569,'d',2,1);			$1,200.57		// Dollar
			numberFormat(.569,'p',3);				56.900%			// Percentage
			numberFormat(12.569,'s',2);				1.26e+1			// Scientific
	
	UPDATES: 
		10/4/05 New version. Allows for format types, commas and or how to display negative numbers.
	===================================================================
	*/

		//--Number format--------------------------------------------------//			
		function numberFormat(numN,fmt,presn,cmas,negDis){

			// Set format pre-defaults (General & Dollar)
			presnD	= presn					|| null; 
		
			// Set defaults
			numN	= numN 					|| 0;
			fmt		= fmt				 	|| 'g';	
			presn	= presn 				|| 0;
			cmas	= cmas 					|| 0;
			negDis	= negDis 				|| '-';
			decNum 	= 0;
			prefix 	= '';
			suffix 	= '';
			negPrx 	= '';
			negSux 	= '';
	
			// Check if we have a number
			re		= /^(\-?\d*)(\.?)(\d*)$/
    		OK		= re.exec(numN);
    	
    		if(!OK){
    			// Not a valid number
    			return 'That is not a valid number';    		
    		}else{
    			// Passed    	
				numN	= new Number(numN);		
				numStr	= numN.toString();
				numN	= Math.abs(numN);		
		
				// Check for decimal point and extract fractional part (hundredths)
				if(numStr.indexOf('.') != -1){
					decIDX 	= numStr.indexOf('.');				
					decNum	= numStr.substr((decIDX+1),numStr.length);
					numStr 	= numStr.substr(0,decIDX);
				}
		 
				numLen = numStr.length;
		
				// Check if number is to large to format
				if(numLen > 15){
					return 'Sorry cannot format a number this large.';
				}else{		
					//--[Check if we need to display a negative character(s)]---------------------------/
					if(numStr.indexOf('-') == 0){
						switch(negDis){
							case '()':
								negPrx = '(';
								negSux = ')';
							break;
		
							case '<>':
								negPrx = '<';
								negSux = '>';
							break;
		
							case '-':
								negPrx = '-';
								negSux = '';
							break;
		
							case 'CR':
								negPrx = '';
								negSux = ' CR';
							break;
		
							// Invalid input
							// Set error
							default:
								return 'Error in function argument negDis.\nInvalid negative display characters.\nAccectable values: -,(),<>,CR';
							break;
						}
					}

					//--[Set format]------------------------------------------/
					switch(fmt.toLowerCase()){
						// General
						case 'g':						
							decNumLen =	decNum.length;
							if(decNumLen > 0){
								presn = presnD || decNumLen;	
							}
						break;
		
						// Fixed
						case 'f':							
						break;
			
						// US Dollar
						case 'd':
							prefix 	= '$';
							presn 	= presnD || 2;		
						break;
		
						// Percentage
						case 'p':
							suffix	= '%';			
							numCal	= numN * 100;			
							numN	= numCal;
						break;
						
						// Scientific
						case 's':							
							if(numN == 0){
								ePow 		= 0;
								operator	= '+';				
							}else{		
								if(numN < 1){
									zre 		= /^(0*)[1-9]*\d*$/
									zeros  		= zre.exec(decNum);				
									ePow 		= zeros[1].length+1;				
									numPow		= Math.pow(10, ePow);
									numCal 		= numN * numPow;	
									operator	= '-';			
								}else{					 
									ePow 		= numLen-1;									
									numPow		= Math.pow(10, ePow);
									numCal		= numN / numPow;
									operator	= '+';				
								}				
				
								// Check if we need to increase/decrease the power and move the decimal point
								sCal	= Math.pow(10, presn); 
    							sNum	= Math.round(numCal * sCal);          
   								sNum	= sNum / sCal;
				
								// Postive number, increase power and move the decimal point
								if(sNum == 10){
									ePow++;
									numCal = numCal / 10;
								}
				
								// Negative number, decrease power and move the decimal point
								if(sNum < 1){
									ePow--;
									numCal = numCal * 10;
								}
					
								numN = new Number(numCal);
							}
				
							suffix = 'e' + operator  + ePow;
						break;
		
						// Invalid fmt input
						// Set error				
						default:
							return 'Error in function argument fmt.\nInvalid format charcter.\nAccectable values: g,f,d,p,s';
						break;
					}
	
					//--[Precision]-----------------------------//	
					presnCal	= Math.pow(10, presn); 
    				presnNum	= Math.round(numN * presnCal);
   					presnNum	= presnNum / presnCal;  
   					presnNum	= presnNum.toString();
					numInterger	= presnNum;
					numN		= presnNum;
					decChr		= '';
					numFloat	= '';
		
					// Skip this section if precision is 0	
					if(presn != 0){
						decChr 			= '.';
						numFloat		= '';  		
    					numParsed		= re.exec(presnNum);
    	
    					// Check if a decimal exist	
    					if(numParsed[2] == decChr){
    						numInterger	= numParsed[1];
    						numFloat	= numParsed[3];
    					}
    	
    					// If needed, pad float numbers with 0's
						while(presn != numFloat.length){
  							numFloat+= '0';
  						}   	
    
   						// Build result 
   						numN = numInterger + decChr + numFloat;	
					}

					//--[Check if we need to display commas]-----------------------------//
					if(cmas !=0){
						numStr 		= numInterger;
						numLen		= numStr.length;		
						loopLen		= numLen / 3;
						nextIDX 	= numLen;
						stIDX 		= 0;
						comStr 		= '';
		
						// Check if we need to add commas
						if(loopLen > 1){
							// Loop through number sets to build string
							for(i=0; i < (loopLen-1); i++){
								numSet = nextIDX % 3;
							
								switch(numSet){
									case 1:	
										endIDX 	= 1;
									break;
		
									case 2:	
										endIDX 	= 2;
									break;
		
									case 0:	
										endIDX 	= 3;
									break;
								}
								// Build string
								comStr	+= numStr.substr(stIDX,endIDX) + ',';
				
								// Set variables for next loop
								stIDX  	+= endIDX;
								nextIDX	-= endIDX;			
							}
						}
		
						// Build string with commas
						comStr += numStr.substr(stIDX,(numLen-stIDX));
		
						// Build result 
						numN = comStr + decChr + numFloat;
					}
	
					return negPrx + prefix + numN + suffix + negSux;
				}
			}
		}