var sMaskSet = 'aAlLn?'
var sUAscii = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
var bStarting = true;

function doKeyDown(e, textbox, sMask) {
	// 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, sMask) {
	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, sMask) {
	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);
	}
	
	var sNext;
	if (textbox.value.length == sMask.length){
		sNext = 'Complete';
	}else{
		switch (sMask.charAt(textbox.value.length)) {
			case 'a':
				sNext = 'Expecting any alphanumeric character (0-9, A-Z, a-z)';
				break;
			case 'A':
				sNext = 'Expecting an uppercase alphanumeric character (0-9, A-Z)';
				break;
			case 'l':
				sNext = 'Expecting any letter (A-Z, a-z)';
				break;
			case 'L':
				sNext = 'Expecting an uppercase letter (A-Z)';
				break;
			case 'n':
				sNext = 'Expecting any numeric character (0-9)';
				break;
			case '?':
				sNext = 'Expecting any character';
				break;
			default: 
				sNext = '';
		}
		window.status = sNext;
	}
}

function doFocus(e, textbox, sMask) {
	bStarting = true;
	doKeyUp(e, textbox, sMask);
	bStarting = false;
}