// ------------------------------------------------------
// Checks the keyCode - returns true if key code should be
// processed
// ------------------------------------------------------
function CheckKey(e)
{
  if (e.type == "keyup")
  {
    return 	((e.keyCode != 8) && (e.keyCode != 9) && (e.keyCode != 46) && (e.keyCode != 37) && (e.keyCode != 39));  
  }
  else 
  {
    return true;  
  }  
}

// ------------------------------------------------------
// Mask MyCalendar Subject
//-------------------------------------------------------
function MaskMyCalendarSubject(code) { 
    return mask2(code, '!!!!!!!!!!!!!!!!!!!!!!!!');
}

// ------------------------------------------------------
// Mask MyCalendar Detail
//-------------------------------------------------------
function MaskMyCalendarDetail(code) {
    return mask2(code, '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
}

// ------------------------------------------------------
// Mask Security Code
//-------------------------------------------------------
function MaskSecurityCode(code, name)
{
		return mask(code, '####');  
}


// ------------------------------------------------------
// Mask Credit Card #
//-------------------------------------------------------
function MaskCreditCard(credit, name)
{
		return mask(credit, '################');  
}


// ------------------------------------------------------
// Mask SSN
//-------------------------------------------------------
function MaskSSN(snnNum, name)
{
		// first remove any hyphens
		return mask(snnNum, '###-##-####');  
}

// --------------------------------------------------
// Mask phone number (123)123-4567
// --------------------------------------------------
function MaskPhone(phoneNum)
{
  return mask(phoneNum, '###-###-####');
}

//---------------------------------------------------
// Mask Short Zip Code
//---------------------------------------------------
function MaskShortZip(zip)
{
	return mask(zip, '#####');
}

//---------------------------------------------------
// Mask Zip Code
//---------------------------------------------------
function MaskZip(zip)
{
	return mask(zip, '#####-####');
}

//---------------------------------------------------
// Mask DOB
//---------------------------------------------------
function MaskDate(Date)
{
	return mask(Date, '##/##/####');
}

//---------------------------------------------------
// Mask DOB
//---------------------------------------------------
function MaskShortDate(Date)
{
	return mask(Date, '##/####');
}

//----------------------------------------------------
// Mask Routing Number
//------------------------------------------------------
function MaskRoutingNumber(number) {
    return mask(number, '#########');
}


//------------------------------------------------------------------
// Mask Characters:
//		#  = character at this position must be a number
//    ?  = character at this position must be an alphabetic character
//    !  = character at this position can be number OR alphabetic character
//    *  = character at this position can be anything
//
// Literal Seperators allowed "-", "/"
//    
//------------------------------------------------------------------
function mask(InString, Mask)
{
    CleanStr = removeLiterals(InString);
		LenStr = trim(CleanStr).length + 1;
		LenMsk = trim(Mask).length;
		if ((LenStr <= 1) || (LenMsk <= 1)) return InString;

		TempString="";
		StrCount = 0;
		
		for (Count=0; Count <= LenStr; Count++)
		{
			StrChar = CleanStr.substring(StrCount, StrCount+1);
			MskChar = Mask.substring(Count, Count+1);
			
			if (StrChar == '-')
			{
			  TempString += '-';
			  StrCount += 1;
			}
			else if (StrChar == '/')
			{
			  TempString += '/';
			  StrCount += 1;
			}
			else if (MskChar == '#')
			{
			  if(!isNumberChar(StrChar)) return TempString;
				TempString += StrChar;
				StrCount += 1;
			}
			else if (MskChar == '?')
			{
			  if(!isAlphaChar(StrChar)) return TempString;
				TempString += StrChar;
				StrCount += 1;		 			
			}
			else if (MskChar == "!") 
			{
			    if (Count < LenMsk) {
			        if (!isNumOrChar(StrChar)) return TempString;
			        TempString += StrChar;
			        StrCount += 1;	
			    }
			}
			else
			{
				TempString += MskChar;
			}
		}
		
		return TempString;
}

function mask2(InString, Mask) {
    CleanStr = InString;
    LenStr = InString.length;
    LenMsk = trim(Mask).length;
    if ((LenStr <= 1) || (LenMsk <= 1)) return InString;

    TempString = "";
    StrCount = 0;

    for (Count = 0; Count < LenStr; Count++) {
        StrChar = CleanStr.substring(Count, Count + 1);
        MskChar = Mask.substring(Count, Count + 1);

        if (StrChar == '-') {
            TempString += '-';
            StrCount += 1;
        }
        else if (StrChar == '/') {
            TempString += '/';
            StrCount += 1;
        }
        else if (MskChar == '#') {
            if (!isNumberChar(StrChar)) return TempString;
            TempString += StrChar;
            StrCount += 1;
        }
        else if (MskChar == '?') {
            if (!isAlphaChar(StrChar)) return TempString;
            TempString += StrChar;
            StrCount += 1;
        }
        else if (MskChar == "!") {
            if (Count < LenMsk) {
                if (StrChar == ' ') {
                    TempString += ' ';
                }
                else {
                    if (!isNumOrChar(StrChar)) return TempString;
                    TempString += StrChar;
                    StrCount += 1;
                }
            }
        }
        else {
            TempString += MskChar;
        }
    }

    return TempString;
}

function removeLiterals(inString)
{
  numLen = trim(inString).length;
  tmpStr = "";

	for (Count=0; Count <= numLen; Count++)
	{  
	  StrChar = inString.substring(Count, Count+1);
	
	  if (StrChar != "-" && StrChar != "/")
	  {
			tmpStr += StrChar;
	  }	
	}	
	return tmpStr;
}

function trim(unTrimmed)
{
	return( unTrimmed.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') ); 
}

function isAlphaChar (InString)  {
	if(InString.length!=1) return (false);
	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);
	RefString="1234567890";
	if (RefString.indexOf (InString, 0)==-1) 
		return (false);
	return (true);
}

function isNumOrChar(InString) {
    if (InString.length != 1) return (false);
    InString = InString.toLowerCase();
    RefString = "1234567890abcdefghijklmnopqrstuvwxyz><";
    if (RefString.indexOf(InString, 0) == -1) return (false);
    return (true);
}

/**************************************************************
AllowOnly: This function allow entering just the specified
Expression to a textbox or textarea control.

Parameters:
Expression = Allowed characters.
a..z => ONLY LETTERS
0..9 => ONLY NUMBERS
other symbols...

Example: use the onKeyPress event to make this function work:
//Allows only from A to Z
onKeyPress="AllowOnly(event,'a..z');"

//Allows only from 0 to 9
onKeyPress="AllowOnly(event,'0..9');"

//Allows only A,B,C,1,2 and 3
onKeyPress="AllowOnly(event,'abc123');"

//Allows only A TO Z,@,#,$ and %
onKeyPress="AllowOnly(event,'a..z||@#$%');"

//Allows only A,B,C,0 TO 9,.,,,+ and -
onKeyPress="AllowOnly(event,'ABC||0..9||.,+-');"
		  
//Allows a..z 0..9 and most specials ~`!@#$%^*()_-++{}[]\:;<>,.?/
onKeyPress="AllowOnly(event,'a..z||0..9||~`!@#$%^*()_-++{}[]|\\:;'<>,.?/');"

Remarks: 	Use the pipe "||" symbol to separate a..z from 0..9 and symbols
the \ needs to be \\ to use

Returns: None
***************************************************************/
function AllowOnly(e, Expression) {
    //Determine our browser
    var key_code, strKey;
    var strUserAgent = navigator.userAgent.toLowerCase();
    var isIE = strUserAgent.indexOf("msie") > -1;
	
    var key_code = "";

    if (isIE)
    {key_code = e.keyCode;}
    else
    { key_code = e.charCode; }


    if (key_code != 13 && key_code != 8 && key_code != 46 && key_code != 37 && key_code != 39 && key_code != 0) {
        Expression = Expression.toLowerCase();
        Expression = Replace(Expression, 'a..z', 'abcdefghijklmnopqrstuvwxyz');
        Expression = Replace(Expression, '0..9', '0123456789');
        Expression = Replace(Expression, '||', '');

        var num = Expression.indexOf("0123456789");
        var pipe = Expression.indexOf("||");
        var letters = Expression.indexOf("abcdefghijklmnopqrstuvwxyz");

        //-- allow spaces if there is letters or letters and numbers
        if (num != -1 && pipe != -1 && key_code == 32) {
            if (isIE)
            { e.keyCode = ""; } //- ie stopped working for some reason clear the keycode
            return false; 
        }
        else if (letters != -1 && key_code == 32) {
            return true;
        }
        else {
            var ch = String.fromCharCode(key_code);
            ch = ch.toLowerCase();
            Expression = Expression.toLowerCase();
            var a = Expression.indexOf(ch);
            if (a == -1) {
                if (isIE)
                { e.keyCode = ""; } //- ie stopped working for some reason clear the keycode
            return false; }
        }


    }

}

/**************************************************************
Replace: Returns a string in which a specified substring has 
been replaced with another substring a specified 
number of times.

Parameters:
Expression = String expression containing substring to replace
Find       = Substring being searched for.
Replace    = Replacement substring.

Returns: String
***************************************************************/
function Replace(Expression, Find, Replace) {
    var temp = Expression;
    var a = 0;

    for (var i = 0; i < Expression.length; i++) {
        a = temp.indexOf(Find);
        if (a == -1)
        { break; }
        else
        { temp = temp.substring(0, a) + Replace + temp.substring((a + Find.length)); }
    }

    return temp;
}

