/*  Author          : Object Structure CEM. Functions CMP with a little advice from MFC/SC!
	Date            : 19/06/1998 & 19/07/2002
	Updates
	=======
	17/07/2002 (RHH) Converted functions from Visual Basic
	19/06/2002 (CEM) Put all the functions into a JavaScript Object
	-------------------------------------------------------------------------------
*/

	//Define functions that will be used by the Mask Object
	function Mask_money(keyCode,sValue)
	{							
		if (sValue.indexOf(".") == -1)
		{	
			if(sValue == "")
				keyCode = Mask_NumbersOnly(keyCode, "");
			else
				keyCode = Mask_NumbersOnly(keyCode, ".");
		}
		else
		{
			//	We need to ensure that only two numerics can be placed after the decimal point.
			if(sValue.length == sValue.indexOf(".") + 3)
				keyCode = false;
			else
				keyCode = Mask_NumbersOnly(keyCode, "");
		}
		return keyCode;
	}
    function Mask_AlphasOnly(keyCode, sValids, bUpper)
    {
		/*  Author          : CMP with a little advice from MFC/SC!
			Date            : 18/06/1998
			Updates
			=======
			17/07/2002 (RHH)
			Converted from Visual Basic
			-------------------------------------------------------------------------------
			This procedure will only allow characters from A-Z in a control.
			Will also only allow characters specified in the string sValids.
		*/
		if (bUpper == undefined)
			bUpper = true;
		
		upperKeyCode = (String.fromCharCode(keyCode).toUpperCase()).charCodeAt(0);
		
        if (((upperKeyCode < 65 || upperKeyCode > 90) && upperKeyCode != 13) && 
			(sValids.indexOf(String.fromCharCode(upperKeyCode))) == -1)
            return false;
        else
		{
			if (bUpper == "true")
				return upperKeyCode;
            else
				return keyCode;
		}
    }

    function Mask_NumbersOnly(keyCode, sValids)
    {
		/*  Author          : CMP with a little advice from MFC/SC!
			Date            : 18/06/1998
			Updates
			=======
			17/07/2002 (RHH)
			Converted from Visual Basic
			-------------------------------------------------------------------------------
			This procedure will only allow characters from 0-9 in a control.
			Will also only allow characters specified in the string sValids.
		*/
        if (((keyCode < 48 || keyCode > 57) && keyCode != 13) && 
			(sValids.indexOf(String.fromCharCode(keyCode))) == -1)
            return false;
        else
            return keyCode;
    }


    function Mask_AlphaNumerics(keyCode, sValids, bUpper)
    {
		/*  Author          : CMP with a little advice from MFC/SC!
			Date            : 18/06/98
			Updates
			=======
			17/07/2002 (RHH)
			Converted from Visual Basic
			-------------------------------------------------------------------------------
			This procedure will only allow characters from A-Z and 0-9 in a control.
			Will also only allow characters specified in the string sValids.
		*/
		
		bNumberCode = Mask_NumbersOnly(keyCode,sValids);
        bAlphaCode = Mask_AlphasOnly(keyCode,sValids, bUpper);

		if( bNumberCode==0 && bAlphaCode==0)
			keyCode = false;

		else if( bAlphaCode != 0 )
			keyCode = bAlphaCode;

        return keyCode;
    }
	
	function Mask_TelephoneNumber(keyCode, pControl)
    {
		/*  Author          : CMP with a little advice from MFC/SC!
			Date            : 18/06/98
			Updates
			=======
			17/07/2002 (RHH)
			Converted from Visual Basic
			-------------------------------------------------------------------------------
			This procedure will only allow characters from A-Z and 0-9 in a control.
			Will also only allow characters specified in the string sValids.
		*/

		var objMask = new Mask();
					
		var sOnceOnlyCharacter;
		var curContent;
		var range;
		var sSelectedText;
		var iSelStart;
		var iSelEnd;
		var iSelLength;
			
		curContent = pControl.value;
		/*
		range = document.selection.createRange();
			
		sSelectedText = range.text;
		
		if(sSelectedText.length > 0)
		{

			iSelStart = curContent.indexOf(sSelectedText);
			iSelEnd = iSelStart + sSelectedText.length;
			iSelLength = sSelectedText.length;
		
			frmUserAdmin.txtCountry.value = 'Sel Start ' + iSelStart + ' Sel End ' + iSelEnd + ' SelLength ' + iSelLength;
		}
		*/
		
		if (keyCode == 40)  // Open brace character
			iOnceOnlyCharacter = 40;
		else if (keyCode == 41) // Close brace character
			iOnceOnlyCharacter = 41;
		else if (keyCode == 43) // Plus character
			iOnceOnlyCharacter = 43;
		else
			iOnceOnlyCharacter = -1;

// Character that only allowed once detected?			
		if (iOnceOnlyCharacter != -1)
		{			
		
//  Does the character already exist? if so suppress the new one
			if(curContent.indexOf(String.fromCharCode(iOnceOnlyCharacter)) > -1)
				keyCode = false;

		}
		else
			keyCode = Mask_NumbersOnly(keyCode," ");

        return keyCode;

    }
	function Mask_email(keyCode,pControl)
	{
		var sValue = String(pControl.value);

		if (sValue == '')
			keyCode = Mask_AlphaNumerics(keyCode,"",false);
		else if (sValue.indexOf("@") == -1)
		{
			if (sValue.lastIndexOf(".") + 1 == (sValue.length))
				keyCode = Mask_AlphaNumerics(keyCode,"-_@",false);
			else
				keyCode = Mask_AlphaNumerics(keyCode,"-_@.",false);
		}
		else if (sValue.lastIndexOf("@") > 0)
		{
			iValue = sValue.lastIndexOf("@");

			if (sValue.length == iValue + 1)
					keyCode = Mask_AlphaNumerics(keyCode,"",false);
			else
			{
				if (sValue.lastIndexOf(".") + 1 == (sValue.length))
					keyCode = Mask_AlphaNumerics(keyCode,"-_",false);
				else
					keyCode = Mask_AlphaNumerics(keyCode,"-_.",false);
			}							
				//e.keyCode = objMask.alphaNumerics(e.keyCode,".-_",false);
			
			//iValueDot = sValue.lastIndexOf(".");
			
			//if (sValue.length == iValueDot + 4 && iValueDot > iValue)
			///	e.keyCode = "";
		}
		return keyCode;
	}
	function Mask_date(keyCode,pControl)
	{
		/*  
		Author          : SS
		Date            : 17/06/2005
			
		-------------------------------------------------------------------------------
			Allows Masking to be entered for a date in the format of DDMMYY. To lasy to do the rest
			but might get round to doing it.
		*/
		switch(pControl.value.length)
		{
			case 0:
				if(keyCode < 48 || keyCode > 51)
          			keyCode = false;
			break;
			case 1:
				if(pControl.value.substring(0,1) == "3")
				{
					if(keyCode < 48 || keyCode > 49)
	          			keyCode = false;
				}
				else
				{
					if(keyCode < 48 || keyCode > 57)
	          			keyCode = false;
				}
			break;
			case 2:
				if(keyCode < 48 || keyCode > 49)
	          			keyCode = false;
			break;
			case 3:
				if(pControl.value.substring(3,2) == "1")
				{
					if(keyCode < 48 || keyCode > 50)
	          			keyCode = false;
				}
				else
				{
					if(keyCode < 49 || keyCode > 57)
	          			keyCode = false;
				}
			break;
		}
		return keyCode;
	}
	function Mask_hoursOnly(keyCode, pControl)
	{
		/*  
		Author          : KMF
		Date            : 24/05/2004
			
		-------------------------------------------------------------------------------
			This process allows only an hour string to be inserted into a HTML text box
			this allows between 0 and 23 to be inserted
		*/
		var curContent = "";
		var range;
		var sSelectedText;
		var iSelStart = 0;
		var iSelEnd;
		var iSelLength;
		
		if (pControl != "")
			curContent = pControl.value;
			
		range = document.selection.createRange();
			
		sSelectedText = range.text;
		
		if(sSelectedText.length > 0)
		{
			iSelStart = curContent.indexOf(sSelectedText);
			iSelEnd = iSelStart + sSelectedText.length;
			iSelLength = sSelectedText.length;
		}
		else
		{
			if (pControl != "")
				iSelStart = curContent.length;
		}
		if(iSelStart == 0)
		{			
			if ((keyCode < 48 || keyCode > 50) && (keyCode != 13))
          			keyCode = 0;
		}
		else if(iSelStart == 1)
		{
			if(curContent.substring(0,1) <= "2")
			{			
				if(curContent <= "1")
				{					
					if ((keyCode < 48 || keyCode > 57) && (keyCode != 13))
           				keyCode = 0;
				}
				else
				{							
					if ((keyCode < 48 || keyCode > 51) && (keyCode != 13))
	           			keyCode = 0;
				}
			}
			else
				keyCode = 0;
		}
		else if(iSelStart == 2)
		{
			if ((keyCode < 48 || keyCode > 53) && (keyCode != 13))
				keyCode = 0;
		}
		return keyCode;
	}
		
	function Mask()
	{
		/*  Author          : CEM
			Date            : 19/07/2002
			Version			: 1.0
			Updates
			=======
			-------------------------------------------------------------------------------
			This object is to be used when masking keyboard input.
			13/05/04	RHH		Add telephone number mask
		*/

		this.sVersion = "1.1";
	
		this.alphaNumerics = Mask_AlphaNumerics;
		this.numbersOnly = Mask_NumbersOnly;
		this.alphasOnly = Mask_AlphasOnly;
		this.telephoneOnly = Mask_TelephoneNumber;
		this.hoursOnly = Mask_hoursOnly;
		this.dateOnly = Mask_date;
		this.money = Mask_money;
	}

