//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
// File History                                                                  
//-------------------------------------------------------------------------------
// Date:        21/02/2008                                                       
// Author:      Andrew Lucas-Dean                                                
// Description: Modified the function ValidateNumberInput so that it also handles
//              the situation when the caret is in the middle of the string or a 
//              number of the characters in the string are selected.             
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------

                           
//-------------------------------------------------------------------------------
// Returns the Caret Position in a text control.                                 
//-------------------------------------------------------------------------------
function GetCaretPosition(control) 
{
  //Set the default return values.
  var CaretSPos = 0;
  var CaretEPos = 0;

  // IE Support
  if (document.selection) 
  {
    control.focus();       
      
    var Sel = document.selection.createRange ();	    
    var len =  Sel.text.length;
    	
    Sel.moveStart ('character', -control.value.length);		
    
    CaretEPos = Sel.text.length;	    
    CaretSPos = CaretEPos-len;	 
  }
  else // Firefox, Opera support
  {
    CaretSPos = control.selectionStart;
    CaretEPos = control.selectionEnd;
    control.focus ();
  }                  
  
  return [CaretSPos, CaretEPos];    
}  
                                                                             
//-------------------------------------------------------------------------------
// Validates the characters typed into an edit box.                              
//-------------------------------------------------------------------------------
function ValidateNumberInput( inputControl, e, allowedChars, min, max )
{
  var keyValue;    
  var key = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
      
  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==27))
  {
    return true;
  }
  else
  {   
    //test for numbers
    keyValue = String.fromCharCode(key);
    if( allowedChars.indexOf(keyValue) < 0 )
    {        
      return false;
    }
  }   
  
  var cpos   = GetCaretPosition(inputControl);  
  var fstBit = inputControl.value.substring(0,cpos[0]);    
  var lstBit = inputControl.value.substring(cpos[1], inputControl.value.length); 
     
  //parse the current contents of the inputcontrol + added character
  var newstr = fstBit + String.fromCharCode(key) + lstBit;
  var input = parseFloat(newstr);  

  if( newstr != input || ( input < min && min != null ) || ( input > max && max != null ) )  
  {
    return false;
  }
  
  //Check number of decimal places
  if ( allowedChars.indexOf('.') >= 0 )
  {         
    var inputLen = newstr.length;  
        
    if (str.indexOf('.') != -1 && inputLen-str.indexOf('.') > 3 )
    {
      return false;
    }                           
  }
   
  else
  {
    return true;
  } 
}
