var DecimalSeparator = ".";
var ThousandSeparator = ",";
var CurrencyDecimalDigits = 2;


function QxInt(Value) {
	return Math.floor(Value); // -2.9 = -2 2.9 = 2
}

function QxFrac(Value) {
	return Value - Math.floor(Value); // -2.9 = -0.9 2.9 = 0.9
}

function QxFloatRoundTo(Value, NumberOfDigits) {
	var FracPart = QxFrac(Value);
	var FracMultiplier = Math.pow(10, NumberOfDigits);
	var MultipliedFrac = FracPart * FracMultiplier;
	var RoundedMultipliedFrac = Math.round(MultipliedFrac);
	var RoundedFracPart = RoundedMultipliedFrac / FracMultiplier;
	return QxInt(Value) + RoundedFracPart;
}

function QxNumberStringToFloat(Value, ThousandSeparator, DecimalSeparator) {
	var A = new RegExp("["+ThousandSeparator+"]", "g");
	var ValueCleanIntegerPart = Value.replace(A, "");
	var B = new RegExp("["+DecimalSeparator+"]", "g");
	var Result = ValueCleanIntegerPart.replace(B, ".");
	return parseFloat(Result);
}

function QxElementValueNumberStringToFloat(Element, ThousandSeparator, DecimalSeparator) {
	var Value = Element.value;
	return QxNumberStringToFloat(Value, ThousandSeparator, DecimalSeparator);
}


function strstr (haystack, needle, bool) {
    // Finds first occurrence of a string within another
    //
    // version: 909.322    // discuss at: http://phpjs.org/functions/strstr
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strstr('Kevin van Zonneveld', 'van');
    // *     returns 1: 'van Zonneveld'
    // *     example 2: strstr('Kevin van Zonneveld', 'van', true);
    // *     returns 2: 'Kevin '
    // *     example 3: strstr('name@example.com', '@');
    // *     returns 3: '@example.com'
    // *     example 4: strstr('name@example.com', '@', true);
    // *     returns 4: 'name'
    var pos = 0;

    haystack += '';
    pos = haystack.indexOf( needle );
    if (pos == -1) {
        return false;
    } else{        if (bool){
           return haystack.substr( 0, pos );
       } else{
            return haystack.slice( pos );
        }    }
}

function number_format (number, decimals, dec_point, thousands_sep) {

    var n = number, prec = decimals;

    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };

    n = !isFinite(+n) ? 0 : +n;
   prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
   }

    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}


function QxFloatToNumberString(Value, ThousandSeparator, DecimalSeparator, DecimalDigits) {
     return number_format(Value,DecimalDigits,'.',',');

     
     var m=0;
   if(Value*1<0 ) {
       var m=1;
       Value=Value*-1;
   }
	if(isNaN(Value)){Value=0;}
	var RoundedValue = QxFloatRoundTo(Value, DecimalDigits);
	var AbsValue = Math.abs(RoundedValue); // -1234567890.12345 Decimaldigits=2 // [1234567890.12345]  
	var ValueSign = (RoundedValue>=0?true:false); // if True then Positif Sign, False is Negatif Sign  // [-]
	var AbsValueAsString = AbsValue.toString();
	var p = AbsValueAsString.indexOf(".");
	
	if (p==-1) {
		var AbsValueFractionPartAsString = "";
		var AbsValueIntegerPartAsString = AbsValueAsString;
	}
	else {
		var AbsValueFractionPartAsString = AbsValueAsString.substr(p+1);
		var AbsValueIntegerPartAsString = AbsValueAsString.substr(0, p);
	}
    
	if (DecimalDigits==-1) {
		ResultFractionalPart = AbsValueFractionPartAsString;
	}
	else {
		while (AbsValueFractionPartAsString.length < DecimalDigits) {
			AbsValueFractionPartAsString = AbsValueFractionPartAsString + "0";
		}
		ResultFractionalPart = AbsValueFractionPartAsString.substr(0, DecimalDigits);
	}
	
	var l = AbsValueIntegerPartAsString.length;
	var ThousandSeparatorStartIndex = l%3;
	
	var j = ((ThousandSeparatorStartIndex==0)?3:ThousandSeparatorStartIndex);
	
	var ResultIntegerPart = "";
	for (var i=0; i<l; i++) {
		if (i==j) { 
			ResultIntegerPart = ResultIntegerPart + ThousandSeparator;
			j = j + 3;
		}
		ResultIntegerPart = ResultIntegerPart + AbsValueIntegerPartAsString.charAt(i);
	}
	var Result = ((ValueSign)?"":"-") + ResultIntegerPart + ((DecimalDigits==0)?"":(DecimalSeparator + ResultFractionalPart));
    if(m==1) {
        return "-"+ Result;
    }
    else {
        return Result;
    }
}

function QxElementValueFloatToNumberString(Element, ThousandSeparator, DecimalSeparator, DecimalDigits) {
	var Value = Element.value;
	var NewValue = QxFloatToNumberString(Value, ThousandSeparator, DecimalSeparator, DecimalDigits);
	Element.value = NewValue;
}

function QxSelectElementDeleteValue(ElementID)
{
	var Element = document.getElementById(ElementID);
   while (Element.options.length>0)
    {deleteIndex =Element.options.length-1;Element.options[deleteIndex]=null;}
}

function QxSelectElementAddValue(ElementID, ElementValue, ElementCaption){
	var Element = document.getElementById(ElementID);
	var NewOption = document.createElement("OPTION");
	NewOption.text=ElementCaption;
	NewOption.value=ElementValue;
	Element.add(NewOption);
}