

var HARMLESS_KEY_CODE = 16;  //The keycode for the shift key to kick off an initial mask application to a textbox. 

var sBrowserName = navigator.appName;

if (sBrowserName.substr(1, 8) != "icrosoft")
{
	document.captureEvents(Event.KEYUP);
	document.onkeyup=onKeyUpHandler;
}

//This list allows Netscape to differentiate which controls to apply the masking
// for and which to ignore.  A control will insert itself into the array in-line.
var aMaskedControlIDs = new Array();
//This is a parallel list of the mask strings that should be applied to the
// respective control.
var aMaskedControlMasks = new Array();

var regExpPhone = new RegExp("((\\(\\d{3}\\) ?)|(\\d{3}-))?\\d{3}-\\d{4}");
var regExpDD = new RegExp("\\d{2}");
var regExpIntlPhone = new RegExp("\\d{15}");
var regExpIntlCC = new RegExp("\\d{20}");
var regExpZip = new RegExp("\\d{5}");
var regExpZipExtended = new RegExp("\\d{5}-\\d{4}");
var regExpZipAlphaNumeric = new RegExp("\\w{5}-\\w{4}");

///PUBLIC
///////////////////////////////////////////////////////////////////////////////////////////
// Call the below functions for validation
///////////////////////////////////////////////////////////////////////////////////////////
function RequiredFieldValidate(ClientID)
{
    var oTextBox = document.getElementById(ClientID);
    if(oTextBox != null && oTextBox.value != '') 
        return true;
    else
        return false;
}

function ValidatePhone(ClientID)
{
    var regExp=regExpPhone;
    var oTextBox = document.getElementById(ClientID);
     if(oTextBox != null && oTextBox.value.search(regExp) != -1)
        return true;
    else
        return false;
}

function ValidateDoubleDigit(ClientID)
{
    var oTextBox = document.getElementById(ClientID);
    var regExp=regExpDD;
    var oTextBox = document.getElementById(ClientID);
     if(oTextBox != null && oTextBox.value.search(regExp) != -1)
        return true;
    else
        return false;
}

function ValidateInternationalPhone(ClientID)
{
    var oTextBox = document.getElementById(ClientID);
    var regExp=regExpIntlPhone;
    var oTextBox = document.getElementById(ClientID);
     if(oTextBox != null && oTextBox.value.search(regExp) != -1)
        return true;
    else
        return false;
}

function ValidateCreditCard(ClientID)
{
    var oTextBox = document.getElementById(ClientID);
    var regExp=regExpIntlCC;
    var oTextBox = document.getElementById(ClientID);
     if(oTextBox != null && oTextBox.value.search(regExp) != -1)
        return true;
    else
        return false;
}

function ValidateZipCode(ClientID)
{
    var oTextBox = document.getElementById(ClientID);
    var regExp=regExpZip;
    var oTextBox = document.getElementById(ClientID);
     if(oTextBox != null && oTextBox.value.search(regExp) != -1)
        return true;
    else
        return false;
}

function ValidateZipCodeExtended(ClientID)
{
    var oTextBox = document.getElementById(ClientID);
    var regExp=regExpZipExtended;
    var oTextBox = document.getElementById(ClientID);
     if(oTextBox != null && oTextBox.value.search(regExp) != -1)
        return true;
    else
        return false;
}

function ValidateZipCodeAlphanumeric(ClientID)
{
    var oTextBox = document.getElementById(ClientID);
    var regExp=regExpZipAlphaNumeric;
    var oTextBox = document.getElementById(ClientID);
     if(oTextBox != null && oTextBox.value.search(regExp) != -1)
        return true;
    else
        return false;
}
///////////////////////////////////////////////////////////////////////////////////////////

function onKeyUpHandler(e)
{
	var iCounter = 0;
	//alert("keypress caught." + e.target.id);
	//alert("aMaskedControlIDs.length: " + aMaskedControlIDs.length);
	while ((e.target.id != aMaskedControlIDs[iCounter]) && (iCounter < aMaskedControlIDs.length))
	{
		iCounter ++
	}
			
		
	if (iCounter == aMaskedControlIDs.length)
	{
		return document.routeEvent(e);
	}
	else
	{
		var oSrcObj;
		oSrcObj = e.target;
		//alert("Source object:" + oSrcObj.value);
		//alert("key: " + e.which);
		formatToMask(oSrcObj, aMaskedControlMasks[iCounter], e.which);
		return false;
	}
		
} //end function onKeyUpHandler

function formatToMask(oTextBox, sMask, sKeyCode)
{
// + String.fromCharCode(window.event.keyCode)
	//alert ("oTextBox.value=" + oTextBox.value + ";");
	var sTxtValue = oTextBox.value;
	var sMaskedValue = "";
	var iTxtIndex = 0;
	var iMaskIndex = 0;
	var sMaskChar;
	var sTxtValueChar;
	
	//alert ("oTextBox.value: " + oTextBox.value);
	//alert ("oTextBox.maxLength: " + oTextBox.maxLength);
	
	// Stop from acting on backspace, and arrow key presses
	if ((sKeyCode != 8) && //Backspace
	    (sKeyCode != 37) && //left arrow
	    (sKeyCode != 39) && //right arrow
	    (sKeyCode != 36) && //home key
	    (sKeyCode != 46)) //delete key 
	{
		while ((iMaskIndex < sMask.length) && (iTxtIndex < sTxtValue.length))
		{
			sMaskChar = sMask.charAt(iMaskIndex);
			sTxtValueChar = sTxtValue.charAt(iTxtIndex);
			//alert("sMaskChar: " + sMaskChar + "\nsTxtValueChar: " + sTxtValueChar);
			switch (sMaskChar)
			{
				case "#" :
					var iCharCode = sTxtValueChar.charCodeAt(0);
					//Verify the current TxtValue character is a number.
					if ((iCharCode >= 48) && (iCharCode <= 57))
					{
						sMaskedValue = sMaskedValue + sTxtValueChar;
						
						// if the first 3 character matches "011" and "911" then reset the string
						if ((oTextBox.maxLength == 15) &&
						    (sMaskedValue.length > 2 &&
						    ((sMaskedValue.substring(0, 3) == "011") || (sMaskedValue.substring(0, 3) == "911"))))
		        {
	            sMaskedValue = "";
	            iMaskedIndex = 0;
	          }
						
						iMaskIndex++;
						iTxtIndex++;
					}
					//The character is not a number, so increment to find the next character that is a number in sTxtValue.
					else
					{
						iTxtIndex++;
					}
					break;
				case "*" :
					var iCharCode = sTxtValueChar.charCodeAt(0);
					//Verify the current TxtValue character is a letter or space or number.
					if ( ((iCharCode >= 97) && (iCharCode <= 122)) || ((iCharCode >= 65) && (iCharCode <= 90)) || (iCharCode == 32) || ( (iCharCode >= 48) && (iCharCode <= 57)))
					{
						sMaskedValue = sMaskedValue + sTxtValueChar;
						iMaskIndex++;
						iTxtIndex++;
					}
					//The character is not a letter, so increment to find the next character that is a letter in sTxtValue.
					else
					{
						iTxtIndex++;
					}
					break;
					case "A" :
					var iCharCode = sTxtValueChar.charCodeAt(0);
					//Verify the current TxtValue character is a letter or space.
					if (((iCharCode >= 97) && (iCharCode <= 122)) || ((iCharCode >= 65) && (iCharCode <= 90)) || (iCharCode == 32))
					{
						sMaskedValue = sMaskedValue + sTxtValueChar;
						iMaskIndex++;
						iTxtIndex++;
					}
					//The character is not a letter, so increment to find the next character that is a letter in sTxtValue.
					else
					{
						iTxtIndex++;
					}
					break;
				default :
				  
					sMaskedValue = sMaskedValue + sMaskChar;
					iMaskIndex++;
			}
		}
		
		oTextBox.value = sMaskedValue;
	}

}	

function removeAllMasks ()
{
	var oMaskedTextBox;
	var iCounter = 0;
	var sMask;
	//alert("removeAllMasks called.");
	
	for (var iCounter = 0; iCounter < aMaskedControlIDs.length; iCounter++)
	{
		oMaskedTextBox = document.getElementById(aMaskedControlIDs[iCounter]);
		sMask = aMaskedControlMasks[iCounter];
		oMaskedTextBox.value = unformatFromMask (oMaskedTextBox, sMask, HARMLESS_KEY_CODE);
	}
	
	return true;
}

function unformatFromMask (oTextBox, sMask, sKeyCode)
{
	var sUnmaskedValue = "";
	var sTextValue = oTextBox.value;
	var iMaskIndex = 0;
	var cMaskChar;
	var cTextValue;
	//Loop through the mask and when a wildcard is encountered, pull the
	// matching index character from the masked text string.
	while ( iMaskIndex < sMask.length )
	{
		cMaskChar = sMask.charAt(iMaskIndex);
		cTextValue = sTextValue.charAt(iMaskIndex);

		if ( ( cMaskChar == '#' ) || ( cMaskChar == 'A' ) || (cMaskChar=='*'))
		{
			sUnmaskedValue +=  cTextValue;
		}
		else //if mask has already been removed prevent skipping of 1st number on each postback
		{
		  if ( ((cMaskChar == '(') || (cMaskChar == ')') || (cMaskChar== '-')) && (!isNaN(cTextValue))   )
		  {
		    sUnmaskedValue +=  cTextValue;
		  }
		}
				
		iMaskIndex++;
	}

	return sUnmaskedValue;
}
