function CalendarTest(name, format) {   
    this.firstControl = document.getElementById(name + '_TextBox0')
    if(this.firstControl)
    {
       this.format = format
       this.keyPressed = CT_keyPressed
       
       this.checkMonth = CT_checkMonth
       this.checkDay = CT_checkDay
       this.checkYear = CT_checkYear
       
       this.moveNext = CT_moveNext
       this.quickMoveNext = CT_quickMoveNext
        
       this.txtMonth = document.getElementById(name + '_TextBox' + (format.indexOf('m')/2) )
       this.txtDay = document.getElementById(name + '_TextBox' + (format.indexOf('d')/2) )
       this.txtYear = document.getElementById(name + '_TextBox'+ (format.indexOf('y')/2) )    
       
       this.txtMonth.onkeypress = new Function('return ' + name + '.keyPressed(event, \'m\')')    
       this.txtDay.onkeypress = new Function('return ' + name + '.keyPressed(event, \'d\')')
       this.txtYear.onkeypress = new Function('return ' + name + '.keyPressed(event, \'y\')')    
          
       this.txtMonth.onfocus = null;
       this.txtDay.onfocus = null;
       this.txtYear.onfocus = null;

       this.disabled = false;    
    }
}

//handles the keyPressed event
function CT_keyPressed(e, id) {    
    t1 = new Date();
    if (this.disabled) { return false; }
    var keyChar = String.fromCharCode(e.keyCode)
    var keyC = e.keyCode
    //alert('keyCode : ' + e.keyCode + ' keyChar : ' + keyChar)
    if (keyChar == '/' || keyChar == '-' || keyChar == '.' || e.keyCode == 13) {
        this.quickMoveNext(id)
        e.keyCode = 0;
        return false;
    }
    var numCheck = /\d/
    e.keyCode = 0;
    if (!numCheck.test(keyChar)) {
        return false
    }
    var txtVal;
    var intVal;

    switch (id) {
        case 'm':            
            txtVal = DCResultValue(this.txtMonth, keyChar)                        
            intVal = parseInt(txtVal, 10)            
            if (!this.checkMonth(txtVal, intVal)) {            
                return false
            }
            break
        case 'd':
            txtVal = DCResultValue(this.txtDay, keyChar)            
            intVal = parseInt(txtVal, 10)         
            if (!this.checkDay(txtVal, intVal)) {
                return false
            }
            break
        case 'y':
            txtVal = DCResultValue(this.txtYear, keyChar)
            intVal = parseInt(txtVal, 10)
            if (!this.checkYear(txtVal, intVal)) {
                return false
            }
            break
        default:
            return false;           
    }
    e.keyCode = keyC;
}

//move next function
function CT_quickMoveNext(id) {
    var txt;
    switch (id) {
        case 'm':
            txt = this.txtMonth
            break
        case 'd':
            txt = this.txtDay
            break
        case 'y':
            txt = this.txtYear
            break
        default :
            return
    }
    var txtLength = txt.value.length;
    if (txtLength != 0) {
        if (document.selection.createRange().text.length != txtLength) {
            this.moveNext(txt)
        }
    }
}

// check month
function CT_checkMonth(txtVal, intVal) {    
    if (intVal > 12 || intVal < 1 && txtVal.length == 2) {
        return false
    }
    
    if (txtVal.length == 2) {
        this.txtMonth.value = txtVal
        this.moveNext(this.txtMonth)
        return false
    }
    return true
}

// check day
function CT_checkDay(txtVal, intVal) {
    if (intVal > 31 || intVal < 1 && txtVal.length == 2) {
        return false
    }        
    
    if (txtVal.length == 2) {
        this.txtDay.value = txtVal        
        this.moveNext(this.txtDay)
        return false
    }
    return true
}

// check year
function CT_checkYear(txtVal, intVal) {

    if (txtVal.length == 2) {
        this.txtYear.value = txtVal
        this.moveNext(this.txtYear)
        return false
    }
    return true
}

//move next
function CT_moveNext(txt) {
        DCNextText(txt)
}

//move focus to the next text in control
function DCNextText(txt) {
    var txtid = txt.id;
    var id = (parseInt(txtid.charAt(txtid.length - 1), 10) + 1) % 3
    var nextText = document.getElementById(txtid.substr(0, txtid.length - 1) + id)
    nextText.focus()
    var range = nextText.createTextRange()
    range.moveEnd('character', nextText.value.length)
    range.select()
}

//gets the result text value of the textbox after a keypressed
function DCResultValue(txt, keyChar) {
    var selectedLength = document.selection.createRange().text.length
    var txtValue = txt.value;
    
    if (selectedLength > 0 && txtValue.length - selectedLength == 2) {
        return null
    }
    
    // calculate caret position
    var insertPos = txtValue.length
    var range = document.selection.createRange().duplicate()
    while (range.parentElement() == txt && range.move('character', 1) == 1) {
        --insertPos
    }

    if (selectedLength == 0)
    {
        var range = txt.createTextRange();
        range.moveEnd('character', txtValue.length);
        range.moveStart('character', insertPos);
        range.select();
        return txtValue.slice(0, insertPos) + keyChar;
    }
    
    return txtValue.slice(0, insertPos) + keyChar+ txtValue.substr(insertPos + selectedLength)
}