﻿var KEY_BACKSPACE = 8;
var KEY_SLASH = 191;
var KEY_TAB = 9;

function DecavDateBox(id)
{
    this.id = null;
    this.isNumeric = /\d/;

    this.monthBox = document.getElementById(id + "_Month");
    this.dayBox = document.getElementById(id + "_Day");
    this.yearBox = document.getElementById(id + "_Year");
   
    
    this.GetKeyCode = function GetKeyCode(e)
    {
        return (window.event) ? window.event.keyCode : e.which;
    }
     
    this.Month_KeyDown = function Month_KeyDown(e)
    {
        if(this != arguments.callee._scope)
          return arguments.callee.apply(arguments.callee._scope, arguments);
    
        var key = this.GetKeyCode(e);
        var keyChar = String.fromCharCode(key);

        if (key == KEY_BACKSPACE)
            return true;
        
        if (keyChar == "/" || key == KEY_SLASH || key == KEY_TAB)
        {
            if (this.monthBox.value.length > 0)
                this.dayBox.focus();
            
            return false;
        }
        
        if (!this.isNumeric.test(keyChar))
            return false;
            
        if (this.monthBox.value.length == 1)
        {
            if (keyChar > 2) // Over 12 not allowed
                return false;
        
            this.monthBox.value += keyChar; // Only two digits.
            this.dayBox.focus();
            return false
        }
           
        if (this.monthBox.value.length == 0 && keyChar > 1) // over 12 won't be allowed, so move on
        {
            this.monthBox.value += keyChar;
            this.dayBox.focus();
            return false
        }
           
        return true;
        
    }
    this.Month_KeyDown._scope = this;
    
    this.Day_KeyDown = function Day_KeyDown(e)
    {
        if(this != arguments.callee._scope)
          return arguments.callee.apply(arguments.callee._scope, arguments);
    
        var key = this.GetKeyCode(e);
        var keyChar = String.fromCharCode(key);
        
        if (key == KEY_BACKSPACE)
        {
            if (this.dayBox.value.length == 0)
            {
                this.monthBox.value = "";
                this.monthBox.focus()
            }
            
            return true;
        }
        
        if (keyChar == "/" || key == KEY_SLASH || key == KEY_TAB)
        {
            if (this.dayBox.value.length > 0)
                this.yearBox.focus();
            return false;
        }
        
        if (!this.isNumeric.test(keyChar))
            return false;
            
        if (this.dayBox.value.length == 1)
        {
            if (this.dayBox.value == 3 && keyChar > 1) // Over 31 not allowed
                return false;
        
            this.dayBox.value += keyChar; // Only two digits.
            this.yearBox.focus();
            return false
        }
           
        if (this.dayBox.value.length == 0 && keyChar > 3) // over 31 won't be allowed, so move on
        {
            this.dayBox.value += keyChar;
            this.yearBox.focus();
            return false
        }
           
        return true;
    }
    this.Day_KeyDown._scope = this;
    
    this.Year_KeyDown = function Year_KeyDown(e)
    {
        if(this != arguments.callee._scope)
          return arguments.callee.apply(arguments.callee._scope, arguments);
    
        var key = this.GetKeyCode(e);
        var keyChar = String.fromCharCode(key);
        
        if (key == KEY_BACKSPACE)
        {
            if (this.yearBox.value.length == 0)
            {
                this.dayBox.value = "";
                this.dayBox.focus();
            }
            
            return true;
        }
        
        if (key == KEY_TAB)
            return true;
            
        return this.isNumeric.test(keyChar);
    }
    this.Year_KeyDown._scope = this;
    
    
    this.monthBox.onkeydown = this.Month_KeyDown;
    this.dayBox.onkeydown = this.Day_KeyDown;
    this.yearBox.onkeydown = this.Year_KeyDown;
}