

function vbDoMask( sMask, oControl ) {
	var iVirtualMaskLength = sMask.length;
	var iMaskOffSet = 0;
	var iCaseFlag = 0;

	try {
		if(setChangeFlag) {
			setChangeFlag();
		}
	} catch(e) { }

	if( sMask.length == 0 ) { 	//if there is no mask then do nothing...
		return;
	}
	for( var x = 0; x < oControl.value.length; x++ ){ 				 // for each character in the oControl value
		switch(String(sMask).substring( x + iMaskOffSet, ( x + 1 ) + iMaskOffSet)) {
			case "#":
					if( isNaN( oControl.value.substring( x, x + 1 ) ) != isNaN(9) ) {
						oControl.value = oControl.value.substring( 0, x );
					}
					break;

			case "\\":
					iMaskOffSet++;
					iVirtualMaskLength--;
					if(oControl.value.substring( x, x + 1 ) != sMask.substring( x + iMaskOffSet, ( x + 1 ) + iMaskOffSet ) ) {
						oControl.value = oControl.value.substring( 0, x ) + sMask.substring(x + iMaskOffSet, (x + 1) + iMaskOffSet) + oControl.value.substring( x, x + 1 );
					}
					break;

			case "&":
					if( ( !testForASCII( oControl.value.substring( x, x + 1 ) ) ) ) {
						oControl.value = oControl.value.substring( 0, x );
					} else {
						oControl.value = oControl.value.substring( 0, x ) +  caseMaster( oControl.value.substring( x, x + 1 ), iCaseFlag ) + oControl.value.substring( x + 1 );
					}
					break;

			case ">":
					iMaskOffSet++;
					iVirtualMaskLength--;
					iCaseFlag = 1;
					oControl.value = oControl.value.substring( 0, x ) +  caseMaster(oControl.value.substring(x, x + 1), iCaseFlag ) + oControl.value.substring(x + 1);
					break;

			case "<":
					iMaskOffSet++;
					iVirtualMaskLength--;
					iCaseFlag = 2;
					oControl.value = oControl.value.substring( 0, x ) +  caseMaster(oControl.value.substring(x, x + 1), iCaseFlag ) + oControl.value.substring(x + 1);
					break;

			case "A":
					if(!testForAlphaNum(oControl.value.substring( x, x + 1))) {
						oControl.value = oControl.value.substring( 0, x );
					} else {
						oControl.value = oControl.value.substring( 0, x ) +  caseMaster(oControl.value.substring(x, x + 1), iCaseFlag ) + oControl.value.substring(x + 1);
					}
					break;

			case "a":
					if( ( !testForAlphaNum( oControl.value.substring( x, x + 1 ) ) ) || ( String( oControl.value.substring( x, x + 1 ) ) == String(" ") ) ) {
						oControl.value = oControl.value.substring( 0, x );
					} else {
						oControl.value = oControl.value.substring( 0, x ) +  caseMaster( oControl.value.substring( x, x + 1 ), iCaseFlag ) + oControl.value.substring( x + 1 );
					}
					break;

			case "9":
					if( ( isNaN(oControl.value.substring( x, x + 1 ) ) != isNaN(9) ) || ( String( oControl.value.substring( x, x + 1 ) ) == String(" ") ) ) {
						oControl.value = oControl.value.substring( 0, x );
					}
					break;

			case "C":
					oControl.value = oControl.value.substring( 0, x ) +  caseMaster( oControl.value.substring( x, x + 1 ), iCaseFlag ) + oControl.value.substring( x + 1 );
					break;

			case "?":
					if( !testForAlpha( oControl.value.substring( x, x + 1 ) ) ) {
						oControl.value = oControl.value.substring( 0, x );
					} else {
						oControl.value = oControl.value.substring( 0, x ) +  caseMaster( oControl.value.substring( x, x + 1 ), iCaseFlag ) + oControl.value.substring( x + 1 );
					}
					break;

			default:
					var sTestChar		= String(sMask).substring( x + iMaskOffSet, (x + 1) + iMaskOffSet);
					if( sTestChar != oControl.value.substring( x, x + 1 ) ) {
						var sTemp		= oControl.value.substring( x, x + 1 );
						oControl.value	= oControl.value.substring( 0, x ) + String(sMask).substring( x + iMaskOffSet, ( x + 1 ) + iMaskOffSet ) + sTemp;
						return vbDoMask( sMask, oControl );
					}
					break;
		}
	}
	if( iVirtualMaskLength < oControl.value.length ) { 				 // test if too long cut off end...
		oControl.value					= oControl.value.substring(0, iVirtualMaskLength );
	}

	checkNextCharVBS( sMask, oControl, sMask.length, iMaskOffSet, iVirtualMaskLength );
}

function caseMaster( cMyChar, iTheFlag ) {
	if(testForAlpha(cMyChar)){
		switch(iTheFlag){
			case 1: 	return cMyChar.toUpperCase();
			case 2: 	return cMyChar.toLowerCase();
			default: 	return cMyChar;
		}
	}
	return cMyChar;
}

function doMask( sMask, oControl ) { // field mask 
	var iVirtualMaskLength = sMask.length;
	var iMaskOffSet = 0;

	try {
		if(setChangeFlag) {
			setChangeFlag();
		}
	} catch(e) { }

	if( sMask.length == 0 ) { 	//if there is no mask then do nothing...
		return;
	}

	for( var x = 0; x < oControl.value.length; x++ ) { 				//for each character in the oControl value
		switch( String(sMask).substring( x + iMaskOffSet, ( x + 1 ) + iMaskOffSet) ) {
			case "9":
				if( isNaN(oControl.value.substring( x, x + 1 ) ) != isNaN(9) && oControl.value.substring( x, x + 1 ) != "." ) {
					oControl.value = oControl.value.substring( 0, x );
				}
				break;

			case "d":
			case "D":
				if( isNaN(oControl.value.substring( x, x + 1 ) ) != isNaN(9) && oControl.value.substring( x, x + 1 ) != " " ) {
					oControl.value = oControl.value.substring( 0, x );
				}
				break;

			case "/":
				iMaskOffSet++;
				iVirtualMaskLength--;
				if( oControl.value.substring( x, x + 1 ) != sMask.substring(x + iMaskOffSet, ( x + 1 ) + iMaskOffSet ) ) {
					oControl.value = oControl.value.substring( 0, x - 1 );
				}
				break;

			case "N":
			case "n":
				if( !testForAlphaNum( oControl.value.substring( x, x + 1 ) ) ) {
					oControl.value = oControl.value.substring(0, x);
				}
				break;

			case "A":
			case "a":
				if( !testForAlpha( oControl.value.substring( x, x + 1 ) ) ) {
					oControl.value = oControl.value.substring( 0, x );
				}
				break;

			case "U":
			case "u":
				if( !testForAlpha(oControl.value.substring( x, x + 1 ) ) ) {
					oControl.value = oControl.value.substring(0, x);
				} else {
					oControl.value = oControl.value.substring(0, x) + oControl.value.substring( x, x + 1 ).toUpperCase() + oControl.value.substring(x + 1);
				}
				break;

			case "L":
			case "l":
				if( !testForAlpha(oControl.value.substring( x, x + 1 ) ) ) {
					oControl.value = oControl.value.substring( 0, x );
				} else {
					oControl.value = oControl.value.substring( 0, x ) + oControl.value.substring( x, x + 1 ).toLowerCase() + oControl.value.substring(x + 1);
				}
				break;

			case "h":
			case "H":
				if( !testForHex( oControl.value.substring( x, x + 1 ) ) ) {
					oControl.value = oControl.value.substring( 0, x );
				}
				break;

			case ".":
				if( oControl.value.substring( x, x + 1 ) != "." ) {
					oControl.value = oControl.value.substring( 0, x );
				}
				break;

			case ",":
				if( oControl.value.substring( x, x + 1 ) != "," ) {
					oControl.value = oControl.value.substring(0, x);
				}
				break;

			case ":":
				if( oControl.value.substring( x, x + 1 ) != ":" ) {
					oControl.value = oControl.value.substring( 0, x );
				}
				break;

			case ";":
				if( oControl.value.substring( x, x + 1 ) != ";" ) {
					oControl.value = oControl.value.substring( 0, x );
				}
				break;

			case "-":
				if( oControl.value.substring(x, x + 1) != "-" ) {
					oControl.value = oControl.value.substring( 0, x );
				}
				break;

			case "":
			case "x":
			case "X": 	break;

			default:
					var sTestChar = String(sMask).substring( x + iMaskOffSet, (x + 1) + iMaskOffSet );
					if( sTestChar != oControl.value.substring( x, x + 1 ) ) {
						var sTemp = oControl.value.substring( x, x + 1 );
						oControl.value = oControl.value.substring( 0, x ) + String(sMask).substring( x + iMaskOffSet, ( x + 1 ) + iMaskOffSet ) + sTemp;
						return doMask( sMask, oControl );
					}
					break;
		}
	}
	if( iVirtualMaskLength < oControl.value.length ) { 				// test if too long cut off end...
		oControl.value = oControl.value.substring( 0, iVirtualMaskLength );
	}

	checkNextChar( sMask, oControl, sMask.length, iMaskOffSet, iVirtualMaskLength  );

}

function checkNextCharVBS( sMask, oControl, iCount, iMaskOffSet, iVirtualMaskLength  ) {
	for( var z = 0; z < iCount; z++ ) {
		var sNext = sMask.substring(oControl.value.length + iMaskOffSet, oControl.value.length + 1 + iMaskOffSet );	// if next mask char is a not a mask type...

		if( sNext != "#" && sNext != "\\" && sNext != "&" && sNext != ">" && sNext != "<" && sNext != "A" && sNext != "a" && sNext != "9" && sNext != "C" && sNext != "?" ) {
			if( sNext == "\\" ) {
				oControl.value += sMask.substring(oControl.value.length + iMaskOffSet + 1, ( oControl.value.length + 1 ) + iMaskOffSet + 1);
				if( sMask.substring(oControl.value.length + iMaskOffSet + 2, ( oControl.value.length + 1 ) + iMaskOffSet + 2 ) == "\\" ) {
					iMaskOffSet++;
					iVirtualMaskLength--;
					oControl.value += sMask.substring(oControl.value.length + iMaskOffSet + 3, ( oControl.value.length + 1 ) + iMaskOffSet + 3 );
				}
			} else {
				oControl.value += sMask.substring(oControl.value.length + iMaskOffSet, ( oControl.value.length + 1 ) + iMaskOffSet );
			}
		} else return;
	}
}

function checkNextChar( sMask, oControl, iCount, iMaskOffSet, iVirtualMaskLength  ) {
	for( var z = 0; z < iCount; z++ ) {
		var sNext = String(sMask.substring(oControl.value.length + iMaskOffSet, oControl.value.length + 1 + iMaskOffSet));	// if next mask char is a not a mask type...
		if( sNext != "d" && sNext != "D" && sNext != "9" && sNext != "N" && sNext != "n" && sNext != "A" && sNext != "a" && sNext != "L" && sNext != "l" && sNext != "H" && sNext != "h" && sNext != "U" && sNext != "u" && sNext != "X" && sNext != "x") {
			if( sNext == "/" ) {
				oControl.value += sMask.substring( oControl.value.length + iMaskOffSet + 1, (oControl.value.length + 1) + iMaskOffSet + 1 );
				if( sMask.substring( oControl.value.length + iMaskOffSet + 2, ( oControl.value.length + 1 ) + iMaskOffSet + 2 ) == "/") {
					iMaskOffSet++;
					iVirtualMaskLength--;
					oControl.value += sMask.substring( oControl.value.length + iMaskOffSet + 3, ( oControl.value.length + 1 ) + iMaskOffSet + 3 );
				} else {
					iMaskOffSet++;
					iVirtualMaskLength--;
					continue;
				}
			} else {
				oControl.value += sMask.substring( oControl.value.length + iMaskOffSet, ( oControl.value.length + 1 ) + iMaskOffSet );
			}
		} else continue;
	}
}

function testForAlphaNum(sTestValue) {
	var reObj = /[A-Za-z0-9]{1}/gi;
	if(sTestValue.match(reObj)) {
		return true;
	}
	return false;
}

function testForHex(sTestValue) {      //test one char at a time...
	var reObj = /[\x20|0-9|a-f|A-F]{1}/gi;
	if(sTestValue.match(reObj)) {
		return true;
	}
	return false;
}

function testForAlpha(sTestValue) {
	var reObj = /[a-z|A-Z]{1}/gi;
	if(sTestValue.match(reObj)) {
		return true;
	}
	return false;
}

function testForASCIIsp(sTestValue) {
	var reObj = /./gi;
	if(sTestValue.match(reObj)) {
		return true;
	}
	return false;
}

function extractVBMaskData( sMask, sToBeUnMasked ) {
	var sOutput				= "";
	var sToBeUnMasked		= String(sToBeUnMasked);
	var iUnMaskIndex		= 0;

	for( var x = 0; x < sMask.length; x++ ) {
		switch( sMask.substring( x, x + 1 ) ) {
			case "A":
			case "a":
			case "#":
			case "&":
			case "?":
			case "C":
			case "9":
				if( sToBeUnMasked.substring( iUnMaskIndex, iUnMaskIndex + 1 ) == "" ) {
					return sOutput;
				}
				sOutput		+= sToBeUnMasked.substring( iUnMaskIndex, iUnMaskIndex + 1 );
				iUnMaskIndex++;
				break;
			case "\\":
				x++;
				iUnMaskIndex++;
				break;
			default:
				iUnMaskIndex++;
				break;
		}
	}
	return sOutput;
}

function extractFieldMaskData( sMask, sToBeUnMasked ) {
	var sOutput				= "";
	var sToBeUnMasked		= String(sToBeUnMasked);
	var iUnMaskIndex		= 0;

	for( var x = 0; x < sMask.length; x++ ) {
		switch( sMask.substring( x, x + 1 ) ) {
			case "A":
			case "a":
			case "L":
			case "l":
			case "U":
			case "u":
			case "N":
			case "n":
			case "X":
			case "x":
			case "H":
			case "h":
			case "9":
			case "d":
			case "D":
				if( sToBeUnMasked.substring( iUnMaskIndex, iUnMaskIndex + 1 ) == "" ) {
					return sOutput;
				}
				sOutput		+= sToBeUnMasked.substring( iUnMaskIndex, iUnMaskIndex + 1 );
				iUnMaskIndex++;
				break;
			case "/":
				x++;
				iUnMaskIndex++;
				break;
			default:
				iUnMaskIndex++;
				break;
		}
	}
	return sOutput;
}

function applyFieldMask( sMask, sToBeMasked ) {
	var sOutput = "";
	var sToBeIndex = 0;
	for( var x = 0; x < sMask.length; x++ ) {
		switch( sMask.substring( x, x + 1 ) ) {
			case "/":
				x++;
				sOutput += sMask.substring( x, x + 1 );
				break;
			case "A":
			case "a":
			case "L":
			case "l":
			case "U":
			case "u":
			case "N":
			case "n":
			case "X":
			case "x":
			case "H":
			case "h":
			case "9":
			case "d":
			case "D":
				var temp = sToBeMasked.substring( sToBeIndex, sToBeIndex + 1 );
				if( temp.length != 1 ) {
					return sOutput;
				}
				sOutput += temp;
				sToBeIndex++;
				break;
			default:
				sOutput += sMask.substring( x, x + 1 );
				break;
		}
	}
	return sOutput;
}

function applyVBMask( sMask, sToBeMasked ) {
	var sOutput			= "";
	var sToBeIndex		= 0;
	for( var x = 0; x < sMask.length; x++ ) {
		switch( sMask.substring( x, x + 1 ) ) {
			case "\\":
				x++;
				sOutput += sMask.substring( x, x + 1 );
				break;
			case ">":
			case "<":
				break;
			case "A":
			case "a":
			case "#":
			case "&":
			case "?":
			case "C":
			case "9":
				var temp = sToBeMasked.substring( sToBeIndex, sToBeIndex + 1 );
				if( temp.length != 1 ) {
					return sOutput;
				}
				sOutput += temp;
				sToBeIndex++;
				break;
			default:
				sOutput += sMask.substring( x, x + 1 );
				break;
		}
	}
	return sOutput;
}
		