﻿        function isNumeric(x) { 
            //checks if x is a valid numeric
            //negatives are accepted
            //ONLY decimal points are accepted
            //for other decimal separators use chgToDecPoint first!
            var re = new RegExp(/^(-)?(\d*)(\.?)(\d*)$/);
            var result = x.match(re); 
            return result; 
        }
        
        function chgToDecPoint(x,localDecSep) {
            //changes local decimal separator to decimal point
            //localDecSep is the local character read from the browser culture
            var st = x.toString(); 
            x = st.replace(localDecSep.toString(), ".");
            return x;
        }
        
        function chgToDecLocal(x,localDecSep) {
            //changed decimal point back to local decimal separator
            //localDecSep is the local character read from the browser culture
            var st = x.toString(); 
            x = st.replace(".", localDecSep.toString());
            return x;
        }

        function SDigitRound(x,n) {
            //returns x rounded DOWN with a specific number of signficant digits n
            //this is useful for rounding values which if rounded up at all may cause an issue (e.g. gr/lb)
            var tmpNum = x*1;
	        var iSign = x*1 < 0 ? -1 : 1;		// Get sign of number
	        tmpNum *= Math.pow(10,n);
	        tmpNum = Math.round(Math.abs(tmpNum));
	        tmpNum /= Math.pow(10,n);
	        tmpNum *= iSign;					// Readjust for sign
	        return tmpNum;
        }
        function SDigitRoundDown(x,n) {
            //returns x rounded DOWN with a specific number of signficant digits n
            //this is useful for rounding values which if rounded up at all may cause an issue (e.g. gr/lb)
            var i = -10;
            var y = 0;
            for (i=-10;i<=10;i++) {
                if (x*1 > Math.pow(10,i) && x*1 < Math.pow(10, i+1)) {
                    y = Math.floor(x*1 * Math.pow(10,(-1 * (i - n +1))))* Math.pow(10,(i - n +1));
                    return y;
                }
            }
        }
        
        function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
        /**********************************************************************
	        IN:
		        NUM - the number to format
		        decimalNum - the number of decimal places to format the number to
		        bolLeadingZero - true / false - display a leading zero for
										        numbers between -1 and 1
		        bolParens - true / false - use parenthesis around negative numbers
		        bolCommas - put commas as number separators.
         
	        RETVAL:
		        The formatted number!
         **********************************************************************/
        { 
            if (isNaN(parseInt(num))) return "NaN";
            var tmpInt = 0;
            var tmpDec = 0;
            var tmpZeros = new String("0000000000000");
	        var tmpNum = num;
	        var iSign = num < 0 ? -1 : 1;		// Get sign of number
        	
	        // Adjust number so only the specified number of numbers after
	        // the decimal point are shown.
	        tmpInt = Math.floor(Math.abs(tmpNum));
	        //alert("tmpInt = " + tmpInt); 
	        tmpNum *= Math.pow(10,decimalNum);
	        //alert("tmpNum = " + tmpNum); 
	        tmpNum = Math.round(Math.abs(tmpNum));
	        //alert("tmpNum = " + tmpNum); 
	        tmpNum /= Math.pow(10,decimalNum);
	        //alert("tmpNum = " + tmpNum); 
	        tmpDec = Math.abs(tmpNum) - tmpInt;
	        //alert("tmpDec = " + tmpDec); 
	        tmpNum *= iSign;					// Readjust for sign
	        //alert("tmpNum = " + tmpNum); 
	        
	        //alert (tmpNum.toString() + "\r" + tmpInt.toString() + "\r" + tmpDec.toString() + "\r" + decimalNum);
        	
        	
	        // Create a string object to do our formatting on
	        var tmpNumStr = new String(tmpNum);
	        var tmpIntStr = new String(tmpInt);
	        var tmpDecStr = new String(tmpDec);
	        
	        //alert (tmpNumStr + "\r" + tmpInt.toString() + "\r" + tmpDecStr + "\r" + decimalNum);

	        // See if we need to strip out the leading zero or not.
	        if (!bolLeadingZero && num < 1 && num > -1 && num != 0){
		        if (num > 0) {
			        tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
			    }
		        else {
			        tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
			    }
			}        
			// See if we need to add trailing zeros
			//alert(tmpDecStr.length.toString() + "\r" + tmpDecStr)
			//if (tmpDecStr.length < decimalNum + 1) {
			
			//alert(tmpDecStr + "\r" + tmpDecStr.length.toString() + "\r" + decimalNum.toString() + "\r" + tmpZeros.substring(0,(tmpDecStr.length - decimalNum - 1)))
			//alert(num.toString() + "\r" + tmpDec.toString());
			//if (tmpDec*1 == 0) {
			if (Math.floor(tmpNum)==tmpNum) {
			    //alert(tmpNum.toString() + "\r" + "tmpDec==0");
			    tmpNumStr = tmpNumStr + "." + tmpZeros.substring(0,decimalNum);
		    }
		    else {
		        //alert(tmpNum.toString() + "\r" + "tmpDec!=0");
		        tmpNumStr = tmpNumStr + tmpZeros;
		        tmpNumStr = tmpNumStr.substring(0,tmpIntStr.length + decimalNum + 1);
		        
		    }
        		
	        // See if we need to put in the commas
	        if (bolCommas && (num >= 1000 || num <= -1000)) {
		        var iStart = tmpNumStr.indexOf(".");
		        if (iStart < 0) {
			        iStart = tmpNumStr.length;
                }
		        iStart -= 3;
		        while (iStart >= 1) {
			        tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			        iStart -= 3;
		        }		
	        }

	        // See if we need to use parenthesis
	        if (bolParens && num < 0)
		        tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	        return tmpNumStr;		// Return our formatted string!
        }
        
        function FormatNumberSRoundDown(num,n,bolLeadingZero,bolParens,bolCommas)
        /**********************************************************************
	        IN:
		        NUM - the number to format
		        n - the number of significant digits to format the number to
		        bolLeadingZero - true / false - display a leading zero for
										        numbers between -1 and 1
		        bolParens - true / false - use parenthesis around negative numbers
		        bolCommas - true / false - put in commas as number separators.
         
	        RETVAL:
		        The formatted number!
		        
		    This routine rounds down the value of num using n significant figures
		    to establish the number of places after the decimal point.
         **********************************************************************/
        { 
            if (isNaN(parseInt(num))) return "NaN";
            var decimalNum = 0;
            var tmpInt = 0;
            var tmpDec = 0;
            var tmpZeros = new String("0000000000000");
	        var iSign = num < 0 ? -1 : 1;		// Get sign of number
	        
	        num = num * iSign;
            var i = -10;
            var tmpNum = 0;
            if (iSign==1){
                for (i=-10;i<=10;i++) {
                    if (num*1 >= Math.pow(10,i) && num*1 < Math.pow(10, i+1)) {
                        tmpNum = Math.floor(num*1 * Math.pow(10,(-1 * (i - n +1))))* Math.pow(10,(i - n +1));
                        break;
                    }
                }
	        }
	        else {
                for (i=-10;i<=10;i++) {
                    if (num*1 > Math.pow(10,i) && num*1 <= Math.pow(10, i+1)) {
                        tmpNum = Math.floor(num*1 * Math.pow(10,(-1 * (i - n +1))))* Math.pow(10,(i - n +1));
                        break;
                    }
                }
	        }
	        
	        tmpNum *= iSign;					// Readjust for sign
	        decimalNum = n-i;
	        
	        tmpInt = Math.floor(Math.abs(tmpNum));
	        //alert("tmpInt = " + tmpInt); 
	        tmpNum *= Math.pow(10,decimalNum);
	        //alert("tmpNum = " + tmpNum); 
	        tmpNum = Math.round(Math.abs(tmpNum));
	        //alert("tmpNum = " + tmpNum); 
	        tmpNum /= Math.pow(10,decimalNum);
	        //alert("tmpNum = " + tmpNum); 
	        tmpDec = Math.abs(tmpNum) - tmpInt;
	        //alert("tmpDec = " + tmpDec); 
	        tmpNum *= iSign;					// Readjust for sign
        	
	        
	        //alert (tmpNum.toString() + "\r" + tmpInt.toString() + "\r" + tmpDec.toString() + "\r" + decimalNum);
        	
        	
	        // Create a string object to do our formatting on
	        var tmpNumStr = new String(tmpNum);
	        var tmpIntStr = new String(tmpInt);
	        var tmpDecStr = new String(tmpDec);
	        
	        //alert (tmpNumStr + "\r" + tmpInt.toString() + "\r" + tmpDecStr + "\r" + decimalNum);

	        // See if we need to strip out the leading zero or not.
	        if (!bolLeadingZero && num < 1 && num > -1 && num != 0){
		        if (num > 0) {
			        tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
			    }
		        else {
			        tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
			    }
			}        
			// See if we need to add trailing zeros
			if (Math.floor(tmpNum)==tmpNum) {
			    //alert(tmpNum.toString() + "\r" + "tmpDec==0");
			    tmpNumStr = tmpNumStr + "." + tmpZeros.substring(0,decimalNum-1);
		    }
		    else {
		        //alert(tmpNum.toString() + "\r" + "tmpDec!=0");
		        if (iSign==1) {
		            tmpNumStr = tmpNumStr + tmpZeros;
		            tmpNumStr = tmpNumStr.substring(0,tmpIntStr.length + decimalNum);
		        }
		        else {
		            tmpNumStr = tmpNumStr + tmpZeros;
		            tmpNumStr = tmpNumStr.substring(0,tmpIntStr.length + decimalNum + 1);
		        }
		        
		    }
        		
	        // See if we need to put in the commas
	        if (bolCommas && (num >= 1000 || num <= -1000)) {
		        var iStart = tmpNumStr.indexOf(".");
		        if (iStart < 0) {
			        iStart = tmpNumStr.length;
                }
		        iStart -= 3;
		        while (iStart >= 1) {
			        tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			        iStart -= 3;
		        }		
	        }

	        // See if we need to use parenthesis
	        if (bolParens && num < 0)
		        tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	        return tmpNumStr;		// Return our formatted string!
        }