// Il codice sorgente originale si trova in NumericalTextBox.cs
function ntxt_GetCaretPosition(oControl) 
{
    var iCaretPos = 0;
    if (document.selection) 
    { 
        oControl.focus ();
        var oSel = document.selection.createRange ();
        oSel.moveStart ('character', -oControl.value.length);
        iCaretPos = oSel.text.length;
    }
    else if (oControl.selectionStart || oControl.selectionStart == '0')
        iCaretPos = oControl.selectionStart;
    return (iCaretPos);
}
function ntxt_SetCaretPosition (oControl, iCaretPos) 
{
    if (document.selection) 
    { 
        oControl.focus ();
        var oSel = document.selection.createRange ();
        oSel.moveStart ('character', -oControl.value.length);
        oSel.moveEnd ('character', -oControl.value.length);
        oSel.moveEnd ('character', iCaretPos);
        oSel.moveStart ('character', iCaretPos);
        oSel.select ();
    }
    else if (oControl.selectionStart || oControl.selectionStart == '0') 
    {
        oControl.selectionStart = iCaretPos;
        oControl.selectionEnd = iCaretPos;
        oControl.focus ();
    }
}    
function ntxt_extractNumber(obj, decimalPlaces, allowNegative, decimalSign)
{
    var temp = obj.value;
    var reg0Str = '[0-9]*';
    if (decimalPlaces > 0) 
    {
        reg0Str += '\\' + decimalSign + '?[0-9]{0,' + decimalPlaces + '}';
    } 
    else if (decimalPlaces < 0) 
    {
        reg0Str += '\\' + decimalSign + '?[0-9]*';
    }
    reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
    reg0Str = reg0Str + '$';
    var reg0 = new RegExp(reg0Str);
    if (reg0.test(temp)) return true;
    if (decimalSign == '.')
        temp = temp.replace(',', decimalSign);
    else
        temp = temp.replace('.', decimalSign);
    var reg1Str = '[^0-9' + (decimalPlaces != 0 ? decimalSign : '') + (allowNegative ? '-' : '') + ']';
    var reg1 = new RegExp(reg1Str, 'g');
    temp = temp.replace(reg1, '');
    if (allowNegative) 
    {
        var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
        var reg2 = /-/g;
        temp = temp.replace(reg2, '');
        if (hasNegative) temp = '-' + temp;
    }
    if (decimalPlaces != 0) 
    {
        var reg3str = '\\' + decimalSign;
        var reg3 = new RegExp(reg3str, 'g')
        var reg3Array = reg3.exec(temp);
        if (reg3Array != null) 
        {
            var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
            reg3Right = reg3Right.replace(reg3, '');
            reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
            temp = temp.substring(0,reg3Array.index) + decimalSign + reg3Right;
        }
    }
    if (obj.value != temp)
    {
        var caretIndex = ntxt_GetCaretPosition(obj);
        obj.value = temp;
        ntxt_SetCaretPosition(obj, caretIndex);
    }
}
function ntxt_blockNonNumbers(obj, e, allowDecimal, allowNegative, decimalSign)
{
    var key;
    var isCtrl = false;
    var keychar;
    var reg;
    if(window.event) 
    {
        key = e.keyCode;
        isCtrl = window.event.ctrlKey
    }
    else if(e.which) 
    {
        key = e.which;
        isCtrl = e.ctrlKey;
    }
    if (isNaN(key)) return true;
    keychar = String.fromCharCode(key);
    if (key == 8 || isCtrl)
    {
        return true;
    }
    reg = /\d/;
    var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
    var isFirstD = allowDecimal ? ((keychar == '.') || (keychar == ',')) && (obj.value.indexOf('.') == -1) && (obj.value.indexOf(',') == -1) : false;
    return isFirstN || isFirstD || reg.test(keychar);
}
function ntxt_autoFormat(obj, prefix, groupingSign, decimalSign, decimalPlaces, postfix, useLimit, min, max)
{
    var temp = obj.value;
    if (temp == '') return true;
    
    if (useLimit==1)
    {
		var value = parseFloat(temp.replace(decimalSign, '.'));
		if (value<min || value>max)
		{
			if (value<min) value = min;
			else if (value>max) value = max;
			temp = '' + value;
			temp = temp.replace('.', decimalSign);
		}
    }
    
    var tempRight = '';
    var tempLeft = '';
    var tempSign = '';
    var reg0Str = '[-]';
    var reg0 = new RegExp(reg0Str, 'g');
    if (reg0.test(temp))
    {
        temp = temp.replace(reg0, '');
        tempSign = '-';
    }
    var reg1str = '\\' + decimalSign;
    var reg1 = new RegExp(reg1str, 'g')
    var reg1Array = reg1.exec(temp);
    if (reg1Array != null) 
    {
        tempRight = temp.substring(reg1Array.index + reg1Array[0].length);
        tempRight = tempRight.replace(reg1, '');
        tempRight = decimalPlaces > 0 ? tempRight.substring(0, decimalPlaces) : tempRight;
        tempLeft = temp.substring(0,reg1Array.index);
    }
    else
    {
        tempRight = '';
        tempLeft = temp;
    }
    if (decimalPlaces>0)
    {
        while (decimalPlaces>tempRight.length)
        {  
            tempRight += '0';
        }            
    }
    if (tempLeft == '')
        tempLeft = '0';
    if (groupingSign != '')
    {
        var tempLeft2 = '';
        while (tempLeft.length>3)
        {
            tempLeft2 = groupingSign + tempLeft.substring(tempLeft.length-3) + tempLeft2;
            tempLeft = tempLeft.substring(0, tempLeft.length-3);
        }
        tempLeft = tempLeft + tempLeft2;
    }    
    if ((decimalPlaces != 0) && (tempRight!=''))
        temp = tempSign + tempLeft + decimalSign + tempRight;
    else 
        temp = tempSign + tempLeft;
    temp = prefix + temp + postfix;            
    if (obj.value != temp) 
        obj.value = temp;
}
function ntxt_clearGrouping(obj, prefix, groupingSign, postfix)
{
    var temp = obj.value;
    var reg0Str = '';
    if (groupingSign != '') reg0Str = '[' + groupingSign + ']';
    if (prefix != '') 
    {
		if (reg0Str!='') reg0Str = '|' + reg0Str;
		reg0Str = '^(' + prefix + ')' + reg0Str;
    }
    if (postfix != '') 
    {
		if (reg0Str!='') reg0Str = reg0Str + '|';
		reg0Str = reg0Str + '(' + postfix + ')$';
    }
    var reg0 = new RegExp(reg0Str, 'g');
    if (reg0.test(temp))
    {
        temp = temp.replace(reg0, '');
        obj.value = temp;
    }
    if (document.selection) 
    { 
        obj.focus ();
        var oSel = document.selection.createRange();
        oSel.moveStart ('character', -obj.value.length);
        oSel.moveEnd ('character', obj.value.length);
        oSel.select ();
    }
}
function ntxta(obj, decimalPlaces, allowNegative, decimalSign)
{
    return ntxt_extractNumber(obj, decimalPlaces, allowNegative, decimalSign);
}
function ntxtb(obj, e, allowDecimal, allowNegative, decimalSign)
{
    return ntxt_blockNonNumbers(obj, e, allowDecimal, allowNegative, decimalSign);
}
function ntxtc(obj, prefix, groupingSign, decimalSign, decimalPlaces, postfix, useLimit, min, max)
{
    return ntxt_autoFormat(obj, prefix, groupingSign, decimalSign, decimalPlaces, postfix, useLimit, min, max);
}
function ntxtd(obj, prefix, groupingSign, postfix)
{
	var newPrefix = prefix.replace(/\$/,"\\$");
	var newPostfix = postfix.replace(/\$/,"\\$");
    ntxt_clearGrouping(obj, newPrefix, groupingSign, newPostfix);
}