﻿//Valid characters for the mask property
//======================================
//a = any alphanumeric character (0-9, A-Z, a-z)
//A = an uppercase alphanumeric character (0-9, A-Z)
//l = any letter character (A-Z, a-z)
//L = an uppercase letter character (A-Z)
//n = any numeric character (0-9)
//? = any character

var sMaskSet = 'aAlLn?'
var sUAscii = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
var bStarting = true;

    function doKeyDown(e, textbox) {
        var sMask = window.event.srcElement.mask

        // trap and cancel keys that are not appropriate
        var iKeyCode = 0;    // collect key code

        if (window.event) 
            iKeyCode = window.event.keyCode;
        else if (e) 
            iKeyCode = e.which;

        if (iKeyCode == 32 || iKeyCode == 39 || iKeyCode == 35 || iKeyCode == 8 || iKeyCode == 9)
            return true;       // space left end backspace tab
        
        if (iKeyCode < 47)   // non-printable character
            return false;
    }

    function doKeyPress(e, textbox) {
        var sMask = window.event.srcElement.mask
        window.status = '';
        var iKeyCode = 0;    // collect key code
    
        if (window.event) 
            iKeyCode = window.event.keyCode;
        else if (e) 
            iKeyCode = e.which;
    
        // check if mask already filled, and not backspace
        var iLength = textbox.value.length;
        if ((iLength == sMask.length) && (iKeyCode != 8))
            return false;
            
        // get mask character for this position in textbox
        var sMaskChar = sMask.charAt(iLength);
        
        // see if it's a special character
        if (sMaskSet.indexOf(sMaskChar) > -1) {
            // masked character required
            switch (sMaskChar) {
                case 'a':   // any alphanumeric character
                    if ((iKeyCode > 47 && iKeyCode < 58) || (iKeyCode > 64 && iKeyCode < 91) || (iKeyCode > 96 && iKeyCode < 123))
                        return true
                    else 
                        return false;

                case 'A':   // uppercase alphanumeric character
                    if ((iKeyCode > 47 && iKeyCode < 58) || (iKeyCode > 64 && iKeyCode < 91))
                        return true
                    else if (iKeyCode > 96 && iKeyCode < 123) {
                        textbox.value += sUAscii.charAt(iKeyCode - 97);
                        return false;
                    }
                    else 
                        return false;

                case 'l':   // any letter
                    if ((iKeyCode > 64 && iKeyCode < 91) || (iKeyCode > 96 && iKeyCode < 123))
                        return true
                    else
                        return false;

                case 'L':   // uppercase letter
                    if (iKeyCode > 64 && iKeyCode < 91)
                        return true
                    else if (iKeyCode > 96 && iKeyCode < 123) {
                        textbox.value += sUAscii.charAt(iKeyCode - 97);
                        return false;
                    }
                    else 
                        return false;

                case 'n':   // any numeric character
                    if (iKeyCode > 47 && iKeyCode < 58)
                        return true
                    else 
                        return false;

                case '?':   // any character
                    return true;

                default: 
                    return false;
            }
        }
        else
            return true;
    }

    function doKeyUp(e, textbox) {
        var sMask = window.event.srcElement.mask
    
        if (bStarting != true) {
            var iKeyCode = 0;    // collect key code
            if (window.event) 
                iKeyCode = window.event.keyCode;
            else if (e) 
                iKeyCode = e.which;

            if (iKeyCode < 47 && iKeyCode != 32) 
                return;
        }

        // check if next mask characters are literals
        // and add to text box if they are
        while ((textbox.value.length < sMask.length) && (sMaskSet.indexOf(sMask.charAt(textbox.value.length)) == -1)) {
            textbox.value += sMask.charAt(textbox.value.length);
        }
    
    }

    function doFocus(e, textbox) {
        var sMask = window.event.srcElement.mask
        bStarting = true;
        doKeyUp(e, textbox, sMask);
        bStarting = false;
    }

