﻿// Checks if included BaseEdit.js
if( typeof( Syncfusion_BaseEdit_version ) == "undefined" || Syncfusion_BaseEdit_version != "1.0" )
  alert( "\"BaseEdit.js (1.0 version)\" not included!" ); 

// Version info
Syncfusion_NumericEdit_version = "1.0";

var Syncfusion_TempThis = null;

//////////////////////////////////////////////////////////////////
// NumericEditKeyHndl class
/////////////////////////////////////////////////////////////////
function SF_NumericTextBox( sRootID )
{ 
  SF_BaseEditTextBox.call( this, sRootID );
  
  /////////////////////////////////////////////////////////////
  // Constants
  /////////////////////////////////////////////////////////////
  
  var c_nDefTimeIncrement		= 200;
  var c_nDefPushTimeIncrement = 50;
  this.DEF_INCREMENT_WHEEL_DELTA = 100;
  
  /////////////////////////////////////////////////////////////
  // Fields
  /////////////////////////////////////////////////////////////
  
  var m_clrNegativeColor	= null;
  var m_clrPositiveColor	= null;
  var m_clrForeColor		= null;
  var m_sGrpSep	            = null;
  
  var m_bUseNegativeColor	= false;
  var m_bUsePositiveColor	= false;
  var m_bUsePreparing		= true;
  var m_bIsNullPushTimeInterval = true;
  
  this.posCaret = 0;
  this.negValue = false;
  
  /////////////////////////////////////////////////////////////
  // Properties
  /////////////////////////////////////////////////////////////
  
  this.CanSelectSectionAfterStopIncrement = true;
  
  this.GetText = function()
  {
	this.EnsureTextBox();
	var sRes = this.textbox.value;
	return sRes;
  }
  ///////////////////////////////////////////////////////////// 
  this.SetText = function( sValue )
  {
	if( !IsEmpty( sValue ) )
	{
		this.EnsureTextBox();
		this.SetValue( this.RemoveMask( sValue ) );
	}
  }
  /////////////////////////////////////////////////////////////
	this.GetValue = function()
	{
		this.EnsureTextBox();
		var sRes = this.GetTextBox().value;
		sRes = this.RemoveMask( sRes );
		var nVal = this.GetTypedValue( sRes );
		return nVal;
	}
  ///////////////////////////////////////////////////////////// 
	this.SetValue = function( val )
	{
		if( !IsEmpty( val ) )
		{
			this.EnsureTextBox();
			var sValue = this.ConvertTypedValueToStr( val );
			sValue = this.ValidateRangeValue( sValue );
			this.ProcessSetValue( sValue, false );
		}
	}
  
  /////////////////////////////////////////////////////////////
  // Overrides methods
  /////////////////////////////////////////////////////////////
  this.Preparing = function()
  {	
  	this.posCaret = this.hlp.getCaretPos( this.textbox );
	if( m_bUsePreparing )
	{
	  this.InernalPreparing();
	  m_bUsePreparing = false;
	}
  }
  
  this.InernalPreparing = function ()
  {
    if ( this.textbox.attributes [ "isNegative" ].nodeValue )
    {
      this.negValue = 
        ( this.textbox.attributes[ "isNegative" ].nodeValue.toLowerCase() == "true" );
    }

	m_clrForeColor		= this.textbox.getAttribute( "ForeColor");
	m_sGrpSep			= this.textbox.getAttribute( "grpSep" );

	if(  "" != this.textbox.getAttribute( "NegativeColor" ) )
	{
		m_clrNegativeColor	= this.textbox.getAttribute( "NegativeColor" );
		m_bUseNegativeColor = true;
	}
	if( "" != this.textbox.getAttribute( "PositiveColor" ) )
	{
		m_clrPositiveColor	= this.textbox.getAttribute( "PositiveColor" );
		m_bUsePositiveColor = true;
	}
  }
  /////////////////////////////////////////////////////////////
  this.ProcessKey = function()
  {	
	var sDecSep = this.textbox.getAttribute( "decSep" );
	var keyChar = this.keys.keyCharCode();
	  
    if( !this.IsOpera() )
    {
		if( !this.keys.isNumericKey() &&
		!this.keys.isSignKey() &&
		!this.keys.isSignKey( this.lastSpecChar ) &&
		!this.keys.isDelKey() &&
		!this.keys.isBackSpaceKey() && 
		!this.keys.isPasteKey() &&
		!this.keys.isCopyKey() &&
		!this.keys.isDecimalSeparatorKey() &&
		keyChar != sDecSep )
		{
			return ( this.IsKeyDownEvent() );
		}
    }
    
    if( this.IsOpera() && this.keys.isMoveCaretKey() )
	{
		return true;
	}
      
    var newText	 = this.textbox.value;
    var signChar = this.textbox.attributes [ "negSign" ].nodeValue;
    this.Preparing();
      
    // Pressed Numeric key 
    if( this.keys.isNumericKey() )
    {
      newText = newText.insert( keyChar, this.posCaret );
      this.posCaret++;
    }
    // Pressed Shift+Ins or Ctrl+V
    else if( this.keys.isPasteKey() )
    {
		var clipboardText = this.keys.bc.getClipboardData();
			
		newText = this.ProcessPasteValue( clipboardText );
    }
    // Pressed Ctrl+Ins or Ctrl+C
    else if( this.keys.isCopyKey() )
    {
      return true;
    }
    // Pressed Sign key 
    else if( this.keys.isSignKey() || this.keys.isSignKey( this.lastSpecChar ) )
    {
      newText = this.RemoveMask( newText );
                 
      this.negValue = !this.negValue;
 
      newText = this.AddMask( newText );
    }
    
	if( ( !this.IsOpera() && this.IsKeyDownEvent() &&  this.keys.isDelKey() ) ||
   		( this.IsOpera() && 0 == this.keys.lastEvent.keyCode) ) 
	{
		var lenSelect = this.hlp.getSelectedLength( this.textbox );
		if( this.posCaret <= newText.length ) 
		{
			if( lenSelect > 0 )
			{
				// Remove selected block 
				var prefix, suffix;
				var decSep = this.textbox.attributes[ "decSep" ].nodeValue;
				var fromSelect = this.posCaret - lenSelect;

				if( this.negValue )
				{
					prefix = this.textbox.getAttribute( "negPrefix" );
					suffix = this.textbox.getAttribute( "negSuffix" );
				}
				else
				{
					prefix = this.textbox.getAttribute( "posPrefix" );
					suffix = this.textbox.getAttribute( "posSuffix" );
				}

				if( fromSelect < prefix.length )
					fromSelect = prefix.length;
		        
				if( fromSelect + lenSelect > newText.length - suffix.length )
					lenSelect = newText.length - suffix.length - fromSelect;
		        
				var remSection = newText.substr( fromSelect, lenSelect );
				var decSepPos = remSection.indexOf( decSep );
				
		        
				// Delete "-"
				var sSelectedText = this.hlp.getSelectedText( this.textbox );
				if( -1 != sSelectedText.indexOf("-") )
				{
					lenSelect = ( 0 < lenSelect)? (lenSelect-1) : lenSelect;
					newText = newText.remove( fromSelect, lenSelect );
					newText = newText.replace( "-" , "" );
			        
					this.negValue = false;
				}
				else
				{
					//Remove selected text
					if( this.CanUpdateSelectedText( sSelectedText ) )
					{	
						var sValue = this.RemoveMask( newText ); 
						if( sValue.Equals( sSelectedText ) )
						{
							var sDefText = this.textbox.getAttribute( "defText" );
							newText = sDefText;
						}
						else
						{
							newText = newText.remove( fromSelect, lenSelect );
						}
					}
				} 
				
				this.posCaret -= lenSelect;
				if( 0 == this.posCaret )
				{
					this.posCaret = -1;
				}
			}      
			else
			{
				// Remove one symbol
				var decSep = this.textbox.attributes [ "decSep" ].nodeValue;
		        
				if( newText.substr( this.posCaret, m_sGrpSep.length ) == m_sGrpSep )
					this.posCaret += m_sGrpSep.length; // Skip number group separator 

				if( newText.substr( this.posCaret, decSep.length ) == decSep )
					this.posCaret += decSep.length; // Skip decimal separator 
		        
				if( 0 == this.posCaret && this.negValue )
				{  
					newText = newText.replace( "-", "" );
					this.negValue = false;
				}
				else
				{
					var chSymbol = newText.charAt( this.posCaret );
					if( IsDigit( chSymbol ) )
					{
						newText = newText.remove( this.posCaret, 1 );
					}
				}
			}
		}
	}
    // Pressed "BackSpace" key 
    else if( this.keys.isBackSpaceKey() )
    {
	  // If text is selected
	  var sSelectedText = this.hlp.getSelectedText( this.textbox );
      var lenSelect = this.hlp.getSelectedLength( this.textbox );
	  if( 0 < lenSelect  )
	  {
			if( this.CanUpdateSelectedText( sSelectedText ) )
			{
				var fromSelect = this.posCaret - lenSelect;
				
				var sValue = this.RemoveMask( newText ); 
				if( sValue.Equals( sSelectedText ) )
				{
					var sDefText = this.textbox.getAttribute( "defText" );
					newText = sDefText;
				}
				else
				{
					newText = newText.remove( fromSelect, lenSelect );
				}
				
				this.posCaret = fromSelect;
				this.negValue = ( -1 != newText.indexOf("-") ) ? true : false;
				if( 0 == this.posCaret )
				{
					this.posCaret = -1;
				}
			}
			else
			{
				this.posCaret -= fromSelect;
			}
	  }  
      else if( this.posCaret > 0 )
      { 
        if( newText.substr( this.posCaret - m_sGrpSep.length, m_sGrpSep.length ) == m_sGrpSep )
          this.posCaret -= m_sGrpSep.length; // Skip number group separator 

        if( newText.substr( this.posCaret - sDecSep.length, sDecSep.length ) == sDecSep )
          this.posCaret -= sDecSep.length; // Skip decimal separator 

		  if( 1 == this.posCaret && this.negValue )
          {  
              newText = newText.replace( "-", "" );
              this.negValue = false;
			  this.posCaret --;
          }
          else
          {
            var chSymbol = newText.charAt( this.posCaret-1 );
			if( IsDigit( chSymbol ) )
			{
				newText = newText.remove( this.posCaret-1, 1 );
				this.posCaret --;
			}
          }
      }
    }
    else
    {
		var lenSelect = this.hlp.getSelectedLength( this.textbox );
		if( 0 < lenSelect && !this.keys.isPasteKey() )
		{
			var sValue = this.RemoveMask( this.textbox.value );
			var sSelectedText = this.hlp.getSelectedText( this.textbox );
			
			if( this.CanUpdateSelectedText( sSelectedText ) )
			{
				if( sValue.Equals( sSelectedText ) )
				{
					var sDefText = this.RemoveMask( this.textbox.getAttribute( "defText" ) );
					newText = sDefText.insert( keyChar, 1 );
					newText = this.AddMask( newText );
				
					var nCursorPos = newText.indexOf( keyChar );
					this.posCaret = nCursorPos + 1;
				}
				else
				{
					var fromSelect = this.posCaret - lenSelect;
					newText = newText.remove( fromSelect-1, lenSelect );
					this.posCaret -= lenSelect;
				}
			}
			else 
			{
				this.posCaret = -1;
				newText = this.textbox.value;
			}
			this.negValue = ( -1 != newText.indexOf("-") ) ? true : false;
		} 
		else
		{
			var sValue = this.textbox.value;
			
			var nPosCaret = this.hlp.getCaretPos( this.textbox );
			var nIndex = sValue.indexOf( sDecSep );
			if( IsDigit( keyChar ) && ( nIndex < nPosCaret ))
			{
				sValue = sValue.update( keyChar, nPosCaret );
				newText = sValue;
			}
		}
	}
	
    var decDigit = this.textbox.getAttribute( "decDigits" );
    if( this.IsIE() || this.IsNetscape() )
    {
		if( ( this.IsKeyDownEvent() && this.keys.isDecimalSeparatorKey() ) ||  
			( this.IsKeyPressEvent() && ((keyChar == sDecSep) || this.keys.isPointKey() )) )
		{
			var chSymbol = newText.charAt( this.posCaret );
			if( null != decDigit && 0 != decDigit && !IsEmpty( sDecSep ) )
			{
				var nIndex = newText.indexOf( sDecSep );
				this.posCaret = nIndex+1;
			}
		}
    }
    else if( this.IsOpera() )
    {
		var nKeyCode =  this.keys.bc.getKeyCode( this.keys.lastEvent );
		if( (78 == nKeyCode) || (keyChar == sDecSep) || this.keys.isPointKey() )
		{
			var chSymbol = newText.charAt( this.posCaret );
			if( null != decDigit && 0 != decDigit && !IsEmpty( sDecSep ) )
			{
				var nIndex = newText.indexOf( sDecSep );
				this.posCaret = nIndex+1;
			}
		}
    }
    
    newText = this.RemoveMask( newText )
    if( 0 == decDigit && "" == newText )
	{
		newText = "0";
	}
      
    this.ValidateAndSet( newText );
    
    return false;    
  }
  ////////////////////////////////////////////////////////////////
  this.ProcessPasteValue = function( clipboardText )
  {
	var newText = this.GetText();
	var lenSelect = this.hlp.getSelectedLength( this.GetTextBox() );
	
	this.posCaret = this.hlp.getCaretPos( this.textbox );
	if( lenSelect > 0 )
	{
		var sSelectedText =  this.hlp.getSelectedText( this.GetTextBox() );
		if( this.CanUpdateSelectedText( sSelectedText ) )
		{
			var fromSelect = this.posCaret - lenSelect;
			newText = newText.remove( fromSelect, lenSelect );
			newText = newText.insert(  clipboardText, fromSelect );
			this.posCaret -= lenSelect;
		}
		else 
		{
			this.posCaret = -1;
			newText = this.textbox.value;
		}
	}
	else
	{
		newText = newText.insert( clipboardText, this.posCaret );
	}
	
	return newText;
  }
  ////////////////////////////////////////////////////////////////
  this.CanUpdateSelectedText = function( sText )
  {
	var bRes = false;

	sText = sText.replaceStr(  " ", "" );
	if( "" != m_sGrpSep )
	{
		sText = sText.replaceStr(  m_sGrpSep, "" );
	}
	sText = sText.replaceStr( this.textbox.getAttribute( "negSign" ), "" );
	bRes = IsNumber( sText, this.textbox.getAttribute( "decSep" ) );

	return bRes;
  }
  ////////////////////////////////////////////////////////////////
  this.ValidateAndSet = function( value )
  {
    value = this.ValidateRangeValue( value );
    this.ProcessSetValue( value, true );
  }
  ////////////////////////////////////////////////////////////////
  this.ValidateRangeValue = function( value )
  {
	var decSep = this.textbox.attributes [ "decSep" ].nodeValue;
    
    value = this.CorrectLeftZero( value ); 
    value = this.CorrectRightSection( value );

    // Check if value = -0.00
    var testVal = value;
    if( decSep!="." ) testVal = testVal.replaceStr( decSep, "." );
    if( parseFloat( testVal ) == 0 && this.negValue ) this.negValue = false;
    
    if( this.negValue )
    { 
	  this.textbox.style.color = m_bUseNegativeColor ? 
			m_clrNegativeColor : m_clrForeColor;
    }
    else
    {
      this.textbox.style.color = m_bUsePositiveColor ? 
			m_clrPositiveColor : m_clrForeColor;
    }
    
    return value;
  }
  ////////////////////////////////////////////////////////////////
  this.ProcessSetValue = function( value, bCanAttachEvent )
  {
	// Validate result value
    if( this.Validate( this.AddMask( value ) ))
    {
	  var sOldValue = this.textbox.value;
	  
      value = this.CorrectGroupSeparators( value ); 
      this.hlp.setText( this.textbox,  this.AddMask( value ), this.posCaret );
      
      if( IsTrue( bCanAttachEvent ) )
      {
  		if( IsTrue( this.ProcessOnValueChange( this.GetRoot() , this.keys.lastEvent )) )
  		{
  			this.textbox.attributes[ "isNegative" ].nodeValue = this.negValue.toString();
  		}
  		else
  		{
  	  		this.hlp.setText( this.textbox, sOldValue, this.posCaret );
  		}
  	  }
  	  else
  	  {
  		this.textbox.attributes[ "isNegative" ].nodeValue = this.negValue.toString();
  	  }
    }
    
    this.SetHiddenData( this.GetValue() );
  }
  //////////////////////////////////////////////////////////////// 
  this.ResetValueToDefault = function()
  {
	this.negValue = ( -1 != this.m_sSavingValue.indexOf("-") ) ? true : false;
	this.ValidateAndSet( this.RemoveMask( this.m_sSavingValue ) );
  }
  ////////////////////////////////////////////////////////////////
  this.Validate = function( value )
  {
    var res;
    if( !this.negValue )
    {
      res = this.base_Validate( value );
    }
    else
    {
      var strRegExp = this.textbox.attributes [ "negRegEx" ].nodeValue;
           
      var regExp = new RegExp( strRegExp );
      res = regExp.test( value );    
      //alert(res);
      //if( res && parseFloat( value ) == 0 ) this.negValue = false; // if value == "-0.00"
      //alert( "res:" + res );
    }
    // Validate range
    if( res ) res = this.ValidateRange( value );
    return res;
  }
  ////////////////////////////////////////////////////////////////
  this.ValidateRange = this.base_ValidateRange = function( value )
  {
    var res = true;
   
    value = this.RemoveMask( value );
    
    if( this.textbox.attributes [ "minVal" ].nodeValue != null && 
      this.textbox.attributes [ "maxVal" ].nodeValue != null )
    {
      minVal = parseFloat( this.textbox.attributes [ "minVal" ].nodeValue );
      maxVal = parseFloat( this.textbox.attributes [ "maxVal" ].nodeValue );
      
      var val = value;
      if( m_sGrpSep )
      {
		val = parseFloat( value.replaceStr( m_sGrpSep, "" ) );
	  }
	  else
	  {
		val = parseFloat( val );
	  }
                
      if ( this.negValue ) val = -val;
      
      if( (val > maxVal) || (val < minVal) ) res = false;
      //alert( minVal + " > " + val + " > " + maxVal + " [ " + res  + " ]");
    }
    return res;  
  }
  ////////////////////////////////////////////////////////////////
	this.GetTypedValue = function( sVal )
	{
		var tb = this.GetTextBox();
		var decSep = tb.getAttribute( "decSep" );
		var signChar = tb.getAttribute( "negSign" );

		var sValTmp = sVal;
		// Remove numeric group separator and change decimal separator to standart
		if( "" != m_sGrpSep )
		{
			sValTmp = sValTmp.replaceStr( m_sGrpSep, "" );
		}
   
		if( "." != decSep )
		{
			sValTmp = sValTmp.replaceStr( decSep, ".");
		}
        
		var prefix = tb.getAttribute( this.negValue ? "negPrefix" : "posPrefix" );

		var numVal = 0;
		//when trying parse negative value parseFloat returns NaN
		if ( sValTmp.indexOf( prefix ) == -1 )
		{
			numVal = parseFloat( sValTmp );
		}
		else
		{
			numVal = null;
			//omit first character
			if( "" == prefix )
			{
				numVal = parseFloat( sValTmp );
			}
			else
			{
				numVal = parseFloat( sValTmp.substr( prefix.lenght ) );
			}
		}

		if( this.negValue && (numVal > 0) )
		{
			numVal = -numVal;
		}
		return numVal;
	}

	this.ConvertTypedValueToStr = function( val )
	{
		var tb = this.GetTextBox();
		var decSep = tb.getAttribute( "decSep" );
		var signChar = tb.getAttribute( "negSign" );
		var decDigit = tb.getAttribute( "decDigits" ); 
		
		if( val < 0 )
		{ 
			val = -val;
			this.negValue = true;
		}
		else
		{
			this.negValue = false;
		}

		var valText = val.toString();
		var posDecSep = valText.indexOf( "." );

		if( decSep!="." )
			valText = valText.replaceStr( ".", decSep );
   
	    if( posDecSep < 0 && 0 != decDigit )
			valText += decSep + '0';
		return valText;
	}
  
	////////////////////////////////////////////////////////////////
	this.IncrementTextValue = this.base_IncrementTextValue = function( sign, value )
	{
	    if( sign == null ) sign = 1;
	    var incrStep = this.textbox.attributes [ "incrStep" ].nodeValue;
	    var step = parseFloat( incrStep ) * sign;
	
		var val = this.GetTypedValue( value );
		
	    if( this.negValue && (val > 0) )
			val = -val;
	    
	    // Increment/Decrement value  
	    val = ( val + step );

		var valText = this.ConvertTypedValueToStr( val );
		return valText;
	}
  ////////////////////////////////////////////////////////////////
  this.StopAutoIncrement = function()
  {
    if( this.oInterval != "" )
    {
      window.clearInterval( this.oInterval ); 
      this.oInterval = "";
      m_bIsNullPushTimeInterval = true;
   
      this.textbox.focus();
      if( IsTrue( this.CanSelectSectionAfterStopIncrement ) )
      {
		this.CorrectStartSelection();
      }
    }
  }
  ////////////////////////////////////////////////////////////////
  this._IncrementValue = function()
  {
    Syncfusion_TempThis.IncrementValue()
  }
  ////////////////////////////////////////////////////////////////
  this._DecrementValue = function()
  {
    Syncfusion_TempThis.DecrementValue()
  }
   
  ////////////////////////////////////////////////////////////////
  this.SetTimerInterval = function( value )
  {
    if( this.oInterval == "" )
    {
      this.oInterval = window.setInterval( value, c_nDefTimeIncrement );    
      Syncfusion_TempThis = this;
    }
    else if( m_bIsNullPushTimeInterval )
    {		
		this.StopAutoIncrement();
		this.oInterval = window.setInterval( value, c_nDefPushTimeIncrement );    
		Syncfusion_TempThis = this;
		
		m_bIsNullPushTimeInterval = false;
    }
  }
  //Increment/////////////////////////////////////////////////////
  this.IncrementValue = function( sign )
  {
    this.SetTimerInterval( this._IncrementValue );
    this.InternalIncrementValue( sign );
  }
  ////////////////////////////////////////////////////////////////
  this.InternalIncrementValue = function( sign )
  {
    //alert('inside InternalIncrementValue');
    
    this.Preparing();
    var iOldPos = this.posCaret;
    var iPosFromEnd = this.textbox.value.length - iOldPos;
    
    var value = this.RemoveMask( this.textbox.value );
       
    var valText = this.IncrementTextValue( sign, value );
    
    this.ValidateAndSet( valText );
    
    this.posCaret = this.hlp.getCaretPos( this.textbox );
    
    var iNewPos = this.textbox.value.length - iPosFromEnd;
    
    if( iNewPos < 0 ) iNewPos = 0;
    
    this.hlp.moveCaret( this.textbox, iNewPos );//iOldPos );

    this.posCaret = this.hlp.getCaretPos( this.textbox );
       
  }
  //Decrement/////////////////////////////////////////////////////
  this.DecrementValue = function()
  {
    this.SetTimerInterval( this._DecrementValue );

    this.InternalIncrementValue( -1 );
  }
  ////////////////////////////////////////////////////////////////
  this.CorrectLeftZero = this.base_CorrectLeftZero = function( value )
  {
	if( !value.Equals( "0" ) )
	{
		var decSep = this.textbox.attributes[ "decSep" ].nodeValue;
		var decDigit = this.textbox.getAttribute( "decDigits" );

		//value = this.RemoveMask( value );
		if( 0 < value.length )
		{
			var chSymbol = value.substr( 0, 1 );
			if( ('0' == chSymbol || m_sGrpSep == chSymbol ) && value.substr( 1, decSep.length ) != decSep )
			{
				value = value.substr( 1 ); // Remove left zero
				this.posCaret--;
				value = this.CorrectLeftZero( value );
			}
			else if( value.substr( 0, decSep.length ) == decSep )
			{
				value = '0' + value; // Add left zero if whole section empty
				//this.posCaret--;
			}
		}
    }
    return value; //this.AddMask( value );
  }
  ////////////////////////////////////////////////////////////////
  this.CorrectRightSection = this.base_CorrectRightSection = function( value )
  {
    var zeroStr = '00000000000000000000'; // 20 positions
    var decDigits = parseInt( this.textbox.attributes[ "decDigits" ].nodeValue );
    var decSep = this.textbox.getAttribute( "decSep" );
    
    var decSepLength = ( 0 == decDigits )? 0 : decSep.length
    
    //value = this.RemoveMask( value );
    if( value.length > 0 )
    {
      posDecSep = value.indexOf( decSep );
      if( posDecSep > -1 )
      { 
        if( value.length - posDecSep - decSepLength > decDigits )
        {
          // Trunc right section to "decDigits" number symbols
          value = value.substr( 0, posDecSep + decDigits + decSepLength ); 
        }
        else if( value.length - posDecSep - decSepLength < decDigits )
        {
          // Fill '0' to end of right section
          value = value + zeroStr.substr( 0, decDigits - value.length + posDecSep + decSepLength );
        }
      }
    } 
    
    return value; //this.AddMask( value );
  }
  ////////////////////////////////////////////////////////////////
  this.CorrectGroupSeparators = this.base_CorrectGroupSeparators = function( value )
  {
    var decSep = this.textbox.attributes[ "decSep" ].nodeValue;

    //var grpSizes = this.textbox.grpSize.toIntArray();
    var grpSizes = this.textbox.attributes[ "grpSize" ].nodeValue.toIntArray();
    var grpSizeLast = grpSizes.length - 1;
    var grpSizeCurr = 0;
    var grpPos = grpSizes[ grpSizeCurr ];
    var pos = 0;
    
    //value = this.RemoveMask( value );
    if( "" != m_sGrpSep )
    {
		while( ( pos = value.indexOf( m_sGrpSep, pos+1 )) > 0 )
		{
			this.posCaret -= m_sGrpSep.length;
		}
		value = value.replaceStr( m_sGrpSep, "" );
		var posDecSep = value.indexOf( decSep );
    }
    if( posDecSep < 0 ) posDecSep = value.length;
    
    for( var i=posDecSep-1, j=1; i > 0; i-- , j++ )
    {
      // Add group separator
      if( j == grpPos )
      {
        value = value.insert( m_sGrpSep, i ) ;

        if( ++grpSizeCurr > grpSizeLast ) grpSizeCurr = 0;

        grpPos += grpSizes[ grpSizeCurr ];
        this.posCaret += m_sGrpSep.length;
      }
    }
    return value;//this.AddMask( value );
  }
  ////////////////////////////////////////////////////////////////
  this.RemoveMask = function( str )
  {
	var sRes = str;
	
    var prefix, suffix;
    if( this.negValue )
    {
      prefix = this.textbox.getAttribute( "negPrefix" );
      suffix = this.textbox.getAttribute( "negSuffix" );
    }
    else
    {
      prefix = this.textbox.getAttribute( "posPrefix" );
      suffix = this.textbox.getAttribute( "posSuffix" );
    }
    if( null != prefix && null != suffix && null != str )
    {
		sRes = str.substring( prefix.length, str.length - suffix.length );
    }
    
    return sRes;
  }
  ////////////////////////////////////////////////////////////////
  this.AddMask = function( str )    
  {
	var sRes = str;
	
    var prefix, suffix;
    if( this.negValue )
    {
      prefix = this.textbox.getAttribute( "negPrefix" );
      suffix = this.textbox.getAttribute( "negSuffix" );
    }
    else
    {
      prefix = this.textbox.getAttribute( "posPrefix" );
      suffix = this.textbox.getAttribute( "posSuffix" );
    }
    if( null != prefix && null != suffix )
    {
		sRes = prefix + str + suffix;
    }
    
    return sRes;
  }
  ////////////////////////////////////////////////////////////////
  this.OnMouseWheel = function( event, element )
  {
    //alert( 'Catched' );
    this.textbox = element;
    this.Preparing();

    if( event.wheelDelta > this.DEF_INCREMENT_WHEEL_DELTA )
    {
      this.InternalIncrementValue();
    }
    else if( event.wheelDelta < -this.DEF_INCREMENT_WHEEL_DELTA )
    {
      this.InternalIncrementValue( -1 );
    }
    
    return false;
  }
  
  this.FixInputWidth = function( sID )
  {
    function frResize()
    {
        var el=document.getElementById(sID);
        if (el)
        {
            var oInp = el.getElementsByTagName('INPUT')[0], oPar;
            if (oInp && (oPar = oInp.offsetParent))
            {
                oInp.style.width = oPar.clientWidth;
            }
        }
    }
    PostExec( frResize );
  }
}


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();