
function OSTMaskedTextBox(id)
{
    this._elementId = id;
    this._tempText = '';
}

OSTMaskedTextBox.prototype.TextChange = function(mode, maxLen)
{
    var obj = document.getElementById(this._elementId);   
    var inputStr = obj.value;
    var text = this._tempText;
    var input = text.substring(-1, 1);
	
    if(typeof(maxLen) == 'undefined')
    {
        maxLen = -1;
    }

    if(maxLen > -1 && text.length > maxLen)
    {
        //Trim end of text.
        obj.value = text.substring(0, maxLen);
    }
    	
    var newStr = '';
    var taken = 0; 
    	
    if(mode == 'Numeric')
    {
        text = inputStr.replace(/[^\d]/g, '');
        
        newStr += text; 
    } 
    else if(mode == 'Phone')
    {
        text = inputStr.replace(/[^\d]/g, '');
	   
        if(text.length > 10)
            text = text.substring(0, 10);  
          
        if(text.length > 3)
        {
            taken = 3;
        
            newStr = text.substring(0, 3) + '-';
            
            if(text.length > 6)
            {
               newStr = '(' + text.substring(0, 3) + ') ' + text.substring(3, 6) + '-';
               taken += 3;
            } 
            
            var rest = text.substring(taken);
            
            newStr += rest;
        }
       else
       {
            newStr = text;
       } 
    }
    else if(mode == 'SSN')
    {
        text = inputStr.replace(/[^\d]/g, '');
        
        if(text.length > 9)
            text = text.substring(0, 9);
            
        if(text.length > 3)
        {
            taken = 3;
            
            newStr = text.substr(0, 3) + '-';
            
            if(text.length > 5)
            {
                newStr += text.substr(3, 2) + '-';
                taken += 2; 
            }
            
            var rest = text.substring(taken);
            
            newStr += rest;
        }
        else
        {
            newStr = text;
        }
    } 
    else if(mode == 'Date')
    {
        var part = 0;
        
        text = inputStr.replace(/[^\d]/g, '');
       
        if(text.length > 8)
            text = text.substring(0, 8);  
          
        if(text.length > 2)
        {
            var month = text.substr(0, 2);
            
            if(parseInt(month) > 12)
            {
                newStr = text.substr(0, 1) + '/';
                taken += 1;
                ++part;
               
                if(text.length > 4)
                {
                    taken += 2;

                    if(parseInt(text.substr(1, 2)) > 31)
                    {
                        newStr += text.substr(1, 1) + '/' + text.substr(2, 1);
                        ++part;
                    }
                    else
                    {
                        newStr += text.substr(1, 2) + '/';
                        ++part; 
                    }
                } 
            }
            else
            {
                newStr = text.substr(0, 2) + '/';
                taken += 2;
                ++part; 
               
                if(text.length > 4)
                {
                    if(parseInt(text.substr(2, 2)) > 31)
                    {
                        newStr += text.substr(2, 1) + '/' + text.substr(3, 1);
                        taken += 2; 
                        ++part; 
                    }
                    else
                    {
                        newStr += text.substr(2, 2) + '/';
                        taken += 2; 
                        ++part; 
                    }
                } 
            }

            var rest = text.substr(taken);
            
            if(part == 2)
            {
                if(rest.length >= 4)
                    newStr += rest.substr(0, 4);
                else
                    newStr += rest;
            }
            else
                newStr += rest;  
        }
       else
            newStr = text;
    }
   
    obj.value = newStr;   
}