/*********************************************************
' Copyright (C) 2002.  Miroslav Hibler, mhibler?hotmail.com (replace ? with @)
'
' This library is free software; you can redistribute it and/or
' modify it under the terms of the GNU Lesser General Public
' License as published by the Free Software Foundation; either
' version 2.1 of the License, or (at your option) any later version.
'
' This library is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
' Lesser General Public License for more details.
'
' You should have received a copy of the GNU Lesser General Public
' License along with this library; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'********************************************************/

function maskInput(strInput, strMask) {
// Syntax Example:	maskInput( '1203', 'Feb 99 2099' )
//         Output:	'Feb 12 2003'

	var strMasked, LenStr, LenMsk, blnReverse, i;

	strMasked = '';
	LenStr = strInput.length;
	LenMsk = strMask.length;

	if (LenStr===0) {return('');}
	if (LenMsk===0) {return(strInput);}

	if (LenMsk>1) {
		if ((strMask.charAt(LenMsk)=='\\') && (strMask.charAt(LenMsk-1)!='\\')) {strMask = strMask.substring(0, LenMsk-1);}
	}

	blnReverse = false;
	if (strMask.charAt(0) == '~') {							// Reverse masking from right to left instead of from left to right
		if (LenMsk==1) {return(strInput);}
		strMask = strMask.substring(1, LenMsk);
		LenMsk = strMask.length;
		if (LenStr > LenMsk) {return(strInput.substring(0, LenMsk));}
		strMask = reverseString(strMask);
		strInput = reverseString(strInput);
		blnReverse = true;
	} else if (strMask.charAt(0) == '^') {					// Replace all characters in input string with
		if (LenMsk==1) {return(strInput);}					// the second character (real mask)
		strMasked = '';
		for (i=0; i<LenStr; i=i+1) {
			strMasked = strMasked + strMask.charAt(1);
		}
		return(strMasked);
	} else if (strMask.charAt(0) == '*') {					// Treat all characters in input string by the rule
		if (LenMsk==1) {return(strInput);}					// of the second character (mask)
		var multiChar;
		multiChar = strMask.charAt(1);
		if (!isMaskChar(multiChar)) {return(strInput);}
		strMask = '';
		for (i=0; i<=LenStr; i=i+1) {
			strMask = strMask + multiChar;
		}
		LenMsk = strMask.length;
	} else if (strMask.charAt(0) == '@') {					// Fill all empty place holders to the left with zeroes
		if (LenMsk==1) {return(strInput);}					// ('leading zeroes'); Second character is number of place holders
		var zeroNum;
		zeroNum = strMask.substring(1, LenMsk);
		if (isNaN(zeroNum)) {return(strInput);}
		strMask = '';
		for (i=0; i<zeroNum; i=i+1) {
			strMask = strMask + '9';
		}
		for (i=0; i<LenStr; i=i+1) {
			if (isNumberChar(strInput.charAt(i))) {strMasked = strMasked + strInput.charAt(i);}
		}
		i=0;
		while (strMasked.charAt(i)=='0') {i=i+1;}
		if (i>0) {strMasked = strMasked.substring(i, LenStr);}
		strMasked = strMasked.substring(0, zeroNum);
		LenStr = strMasked.length;
		if (LenStr<zeroNum) {
			for (i=LenStr; i<zeroNum; i=i+1) {strMasked = '0' + strMasked;}
		}
		return(strMasked);
	}
	var MskOffset, Count, SubStr, Mask, newChar, newCharLen;
	MskOffset = 0;
	for (Count=0; Count<LenStr; Count=Count+1) {
		SubStr = strInput.charAt(Count);
		if (MskOffset<LenMsk) {
			newChar = '';
			Mask = strMask.substring(MskOffset, LenMsk);
			newChar = checkMask(Mask, SubStr);
			newCharLen = newChar.length;
			if (newCharLen!==0) {strMasked = strMasked + newChar;}
			if (Mask.charAt(0)=='\\') {MskOffset=MskOffset+1;}
			MskOffset = MskOffset + newCharLen;
		}
	}
	if (blnReverse) {return(reverseString(strMasked));}
	return(strMasked);
}

function unMaskInput(strMaskedInput, strUnMask) {
// Syntax Example:	unMaskInput( "Feb 12 2003", "Feb 99 2099" )
//         Output:	"1203"

	var strUnMasked, LenStr, LenMsk, blnReverse;

	strUnMasked = '';
	LenStr = strMaskedInput.length;
	LenMsk = strUnMask.length;

	if (LenStr===0) {return('');}
	if (LenMsk===0) {return(strMaskedInput);}

	if (LenMsk>1) {
		if ((strUnMask.charAt(LenMsk)=='\\') && (strUnMask.charAt(LenMsk-1)!='\\')) {strUnMask = strUnMask.substring(0, LenMsk-1);}
	}
	if (strUnMask.charAt(0) == '~') {						// Revert masking from right to left instead of left from right
		if (LenMsk==1) {return(strMaskedInput);}
		strUnMask = strUnMask.substring(1, LenMsk);
		LenMsk = strUnMask.length;
		if (LenStr > LenMsk) {return(strMaskedInput.substring(0, LenMsk));}
		strUnMask = reverseString(strUnMask);
		strMaskedInput = reverseString(strMaskedInput);
		blnReverse = true;
	} else if (strUnMask.charAt(0) == '^') {				// Replace all characters in input string with
		return(strMaskedInput);								// the second character (real mask) IGNORED FOR UnMask
	} else if (strUnMask.charAt(0) == '*') {				// Treat all characters in input string by the rule
		if (LenMsk==1) {return(strMaskedInput);}				// of the second character (mask)
		var multiChar;
		multiChar = strUnMask.charAt(1);
		if (!isMaskChar(multiChar)) {return(strMaskedInput);}
		strUnMask = '';
		var i;
		for (i=0; i<LenStr; i=i+1) {
			strUnMask = strUnMask + multiChar;
		}
		LenMsk = strUnMask.length;
	} else if (strUnMask.charAt(0) == '@') {				// Fill all empty place holders to the left with zeroes
		i=0;
		while (strMaskedInput.charAt(i)=='0') {i=i+1;}
		strUnMasked = strMaskedInput.substring(i, LenStr);
		return(strUnMasked);
	}
	var MskOffset, Count, SubStr;
	MskOffset = 0;
	for (Count=0; Count<LenStr; Count=Count+1) {
		SubStr = strMaskedInput.charAt(Count);
		if (MskOffset<LenMsk) {
			if (isMaskChar(strUnMask.charAt(MskOffset))) {
				strUnMasked = strUnMasked + SubStr;
				MskOffset=MskOffset+1;
			} else if (strUnMask.charAt(MskOffset=MskOffset+1)=='\\') {MskOffset=MskOffset+1;}
		}
	}
	if (blnReverse) {return(reverseString(strUnMasked));}
	return(strUnMasked);
}

function checkMask(strCheckMask, strInpChar) {
	var strOut;

	strOut = strCheckMask.charAt(0);
	switch (strOut) {
		case '\\':
			strOut = strCheckMask.charAt(1);
			if (strOut!='\\') {
				if ((strCheckMask.length>2) && (strOut!=strInpChar)) {
					strOut = strOut + checkMask(strCheckMask.substring(2, strCheckMask.length), strInpChar);
				}
			} else {
				if ((strCheckMask.length>3) && (strOut!=strInpChar)) {
					strOut = '\\' + checkMask(strCheckMask.substring(3, strCheckMask.length), strInpChar);
				}
			}
			return(strOut);
			//brake;
		case '#':		// Numeric: ".,-1234567890"
			if (isNumericChar(strInpChar)) {return(strInpChar);}
			return('');
			//brake;
		case '9':		// Number: "1234567890"
			if (isNumberChar(strInpChar)) {return(strInpChar);}
			return('');
			//brake;
		case '>':		// ToLoverCase: A -> a
			if (isAlphabeticChar(strInpChar)) {return(strInpChar.toLowerCase());}
			return(strInpChar);
			//brake;
		case '<':		// ToUpperCase: a -> A
			if (isAlphabeticChar(strInpChar)) {return(strInpChar.toUpperCase());}
			return(strInpChar);
			//brake;
		case 'A':		// Alphabetic: "abcdefghijklmnopqrstuvwxyz"
			if (isAlphabeticChar(strInpChar)) {return(strInpChar);}
			return('');
			//brake;
		case '?':		// AlphaNumeric: "1234567890abcdefghijklmnopqrstuvwxyz"
			if (isNumOrChar(strInpChar)) {return(strInpChar);}
			return('');
			//brake;
		default :		// Enything else
			if ((strCheckMask.length>1) && (strOut!=strInpChar)) {
				strOut = strOut + checkMask(strCheckMask.substring(1, strCheckMask.length), strInpChar);
			} 
			return(strOut);
			//brake;
	}
}

function isMaskChar(InString) {
	if (InString.length!=1) {return(false);}
	var RefString;
	RefString="#9><A?";
	if (RefString.indexOf(InString, 0)==-1) {return(false);}
	return(true);
}

function isAlphabeticChar(InString) {
	if (InString.length!=1) {return(false);}
	var RefString;
	InString=InString.toLowerCase();
	RefString="abcdefghijklmnopqrstuvwxyz ";
	if (RefString.indexOf(InString.toLowerCase(), 0)==-1) {return(false);}
	return(true);
}

function isNumberChar(InString) {
	if (InString.length!=1) {return(false);}
	var RefString;
	RefString="1234567890";
	if (RefString.indexOf(InString, 0)==-1) {return(false);}
	return(true);
}

function isNumericChar(InString) {
	if (InString.length!=1) {return(false);}
	var RefString;
	RefString=".,-1234567890";
	if (RefString.indexOf(InString, 0)==-1) {return(false);}
	return(true);
}

function isNumOrChar(InString) {
	if (InString.length!=1) {return(false);}
	var RefString;
	InString=InString.toLowerCase();
	RefString="1234567890abcdefghijklmnopqrstuvwxyz ";
	if (RefString.indexOf(InString, 0)==-1) {return(false);}
	return(true);
}

function reverseString(string) {
	if (string === '') {return(string);}
	var strTemp, i;
	strTemp='';
	for (i=string.length; i>=0; i=i-1) {
		strTemp = strTemp + string.charAt(i-1);
	}
	return(strTemp);
}


