var MathLib = function(){
	
	this.delimiter = " ";
	this.delimiter2 = ",";
	
	this.countValuePercentage = function(total, value, result) {
		var total_value = jQuery(total).val();
		var total_percent = 100;
		var find_value = jQuery(value).val();
		var find_percent = total_percent - ( (find_value * total_percent) / total_value );
		jQuery(result).val( this.roundNumber(find_percent, 2) );
	}
	
	this.countPercentageValue = function(total, percent, result) {
		var find_value = 0;
		var total_value = jQuery(total).val();
		var total_percent = 100;
		var find_percent = jQuery(percent).val();
		if(total_value > 0 && find_percent > 0 && total_percent > 0 ) find_value = total_value - ( (total_value * find_percent) / total_percent );
		if(find_percent == 0) find_value = total_value;
		jQuery(result).val( this.roundNumber(find_value, 2) );
	}
	
	this.roundNumber = function(num, dec) {
		if(typeof(dec) == 'undefined' || dec < 0) dec = 2;
		var result = Math.round( Math.round( num * Math.pow( 10, dec + 1 ) ) / Math.pow( 10, 1 ) ) / Math.pow(10,dec);
		if(!isFinite(result)) result = 0;
		return result;
	}
	
	this.formatAmount = function(s) {
		// convert to string
		if(typeof(s) != 'string') s = new String(s).valueOf();
		// add .00 if needed
		if (String(s).indexOf(".") < 0) {
			  return this.commaFormatted(s + ".00");
		}
		
		
		return this.commaFormatted(s);
	}
	
	this.parseCommaFormatted = function(amount) {
		if(amount == null) return 0;
		var a = amount.split(this.delimiter);
		a = a.join('');
		a = a.split(this.delimiter2);
		return new Number( a.join('') ).valueOf();
	}
	
	this.intval = function(mixed_var, base) {
	    var tmp;
	    var type = typeof( mixed_var );

	    if (type === 'boolean') {
	        return (mixed_var) ? 1 : 0;
	    } else if (type === 'string') {
	        tmp = parseInt(mixed_var, base || 10);
	        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
	    } else if (type === 'number' && isFinite(mixed_var) ) {
	        return Math.floor(mixed_var);
	    } else {
	        return 0;
	    }
	}
	
	this.isNumeric = function(num_var){
		return (typeof(num_var) === 'number' || typeof(num_var) === 'string') && num_var !== '' && !isNaN(num_var);
	}
	
	this.commaFormatted = function(amount)
	{
		var delimiter = this.delimiter; // replace comma if desired
		var a = amount.split('.',2);
		var d = a[1];
		var i = parseInt(a[0]);
		
		if(isNaN(i)) { 
			return ''; 
		}
		
		var minus = '';
		if(i < 0) { 
			minus = '-'; 
		}
		i = Math.abs(i);
		var n = new String(i);
		var a = [];
		
		while(n.length > 3)
		{
			var nn = n.substr(n.length-3);
			a.unshift(nn);
			n = n.substr(0,n.length-3);
		}
		
		if(n.length > 0) { 
			a.unshift(n); 
		}
		
		n = a.join(delimiter);
		
		if(d.length < 1) { 
			amount = n; 
		} else { 
			amount = n + '.' + d; 
		}
		amount = minus + amount;
		return amount;
	}
	
	this.sum = function(a, b) {
		return new Number( a ).valueOf() + new Number( b ).valueOf();
	}
	
	this.minus = function(a, b) {
		return new Number( a ).valueOf() - new Number( b ).valueOf();
	}
	
	this.number_format = function(number, decimals, dec_point, thousands_sep) {
	    // Formats a number with grouped thousands  
	    // 
	    // version: 1004.2314
	    // discuss at: http://phpjs.org/functions/number_format
	    // *     example 1: number_format(1234.56);
	    // *     returns 1: '1,235'
	    // *     example 2: number_format(1234.56, 2, ',', ' ');
	    // *     returns 2: '1 234,56'
	    // *     example 3: number_format(1234.5678, 2, '.', '');
	    // *     returns 3: '1234.57'
	    // *     example 4: number_format(67, 2, ',', '.');
	    // *     returns 4: '67,00'
	    // *     example 5: number_format(1000);
	    // *     returns 5: '1,000'
	    // *     example 6: number_format(67.311, 2);
	    // *     returns 6: '67.31'
	    // *     example 7: number_format(1000.55, 1);
	    // *     returns 7: '1,000.6'
	    // *     example 8: number_format(67000, 5, ',', '.');
	    // *     returns 8: '67.000,00000'
	    // *     example 9: number_format(0.9, 0);
	    // *     returns 9: '1'
	    // *    example 10: number_format('1.20', 2);
	    // *    returns 10: '1.20'
	    // *    example 11: number_format('1.20', 4);
	    // *    returns 11: '1.2000'
	    // *    example 12: number_format('1.2000', 3);
	    // *    returns 12: '1.200'
	    var n = !isFinite(+number) ? 0 : +number, 
	        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
	        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
	        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
	        s = '',
	        toFixedFix = function (n, prec) {
	            var k = Math.pow(10, prec);
	            return '' + Math.round(n * k) / k;
	        };
	    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
	    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
	    if (s[0].length > 3) {
	        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
	    }
	    if ((s[1] || '').length < prec) {
	        s[1] = s[1] || '';
	        s[1] += new Array(prec - s[1].length + 1).join('0');
	    }
	    return s.join(dec);
	}
	
	this.formatNumber = function(theNumber,theThousands,theDecimal) {
		
		if(typeof(theThousands) == 'undefined' ) theThousands = ' ';
		if(typeof(theDecimal) == 'undefined' ) theDecimal = '.';
		
		var theOutput = '';
		theNumber = theNumber.valueOf();
		var theDecimalDigits = Math.round((theNumber*100)-(Math.floor(theNumber)*100));
		
		if (theDecimalDigits < 10) {theDecimalDigits = "0"+theDecimalDigits;}
		
		theDecimalDigits= "" + (theDecimalDigits + "0").substring(0,2);
		theNumber = "" + Math.floor(theNumber);
		var count = 0;
		
		for (x = theNumber.length-1; x > -1 ; x--) {
			if(count > 2) {theOutput = theThousands + theOutput; count = 0;}
			
			theOutput = theNumber.substring(x,x+1) + theOutput;
			count++;
		};
		
		theOutput += theDecimal + theDecimalDigits;
		return theOutput;
	}

}

var cmsMath = new MathLib();
