KEY_BACKSPACE = 8;
KEY_ZERO = 48;
KEY_NINE = 57;
KEY_ENTER = 13;
WHICH_TAB = 0;
KEY_TAB = 9;

//LENGTH_DATE = 6;
MAX_LENGTH_DATE = 10;

LENGTH_PHONE = 8;
MAX_LENGTH_PHONE = 12;

//Array of counters used to count the number of chars in each textbox.
m_charCount = new Array();

//Array of flags used to suppress keystrokes and box selection.
m_suppressSelect = new Array();
m_keepKeyPress = new Array();

function popupWin()
{
	this.outputWindow = window.open("", "tinyWindow"); 
}


function initializeCounters (e, txt)
{
	this.m_charCount[txt.id] = 0;
//this.outputWindow.document.writeln('initcounter'+this.m_charCount[txt.id]+'<br/>');	
	this.m_suppressSelect[txt.id] = false;
	this.m_keepKeyPress[txt.id] = true;
}

function autoTab(e, txt, length, nextElem)
{
	var nextTxt = document.getElementById(nextElem);
	if (nextTxt != null) {
		if (document.all) { // Internet Explorer
			if (e.keyCode != 0 && e.keyCode != KEY_BACKSPACE) {
				this.m_charCount[txt.id] = this.m_charCount[txt.id] + 1;
				if (this.m_charCount[txt.id] == length + 1) { // Internet Explorer puts the key in next text box
					this.m_suppressSelect[nextTxt.id] = true;
					nextTxt.value = "";
					nextTxt.focus();
					this.m_charCount[nextTxt.id] = 1;
				} else if (e.keyCode == KEY_BACKSPACE) {
					this.m_charCount[txt.id] = this.m_charCount[txt.id] - 1;
				}
			}
		}
		else if (document.getElementById) { // DOM2 Browsers: Netscape 6+, Mozilla, FireFox
			if (this.m_keepKeyPress[txt.id] && e.which != KEY_BACKSPACE) {
				this.m_charCount[txt.id] = this.m_charCount[txt.id] + 1;
				if (this.m_charCount[txt.id] == length) { // DOM2 Browsers put the key in the current box
					this.m_suppressSelect[nextTxt.id] = true;
					nextTxt.value = "";
					nextTxt.focus();
					this.m_charCount[nextTxt.id] = 0;
				} else if (e.which == KEY_BACKSPACE) {
					this.m_charCount[txt.id] = this.m_charCount[txt.id] - 1;
				}
			}
		}
	}
}

function selectText(e, txt)
{
	if (!this.m_suppressSelect[txt.id]) {
		txt.select();
		this.m_charCount[txt.id] = 0;
	//this.outputWindow.document.writeln('selecttext'+this.m_charCount[txt.id]+'<br/>');	
	}
	this.m_suppressSelect[txt.id] = false;	
}

function catchBackspace (e, txt) // For IE Only
{
	if(e){ // if the event object is present
		e = e // var e = event
	}
	else {
		e = window.event // else e = window.event for IE
	}
	if (document.all){ // Internet Explorer
		if (e.keyCode == KEY_BACKSPACE) {
			if(this.m_charCount[txt.id]){
				this.m_charCount[txt.id] = this.m_charCount[txt.id] - 1;
	//this.outputWindow.document.writeln('catchBackspace if'+this.m_charCount[txt.id]+'<br/>');	
			}else{
				this.m_charCount[txt.id] = 0;
			//this.outputWindow.document.writeln('catchBackspace else'+this.m_charCount[txt.id]+'<br/>');	
			}
		}
	}
}

function maskNumeric(e, txt)
{
//this.outputWindow.document.writeln('masknum'+'<br/>');
	if (document.all){ // Internet Explorer
		if (e.keyCode != KEY_BACKSPACE && e.keyCode != KEY_ENTER && (e.keyCode < KEY_ZERO || e.keyCode > KEY_NINE)) {
			e.keyCode = 0; // Cancel the Key Press (Internet Explorer)	
		}
	}
	else if (document.getElementById){ // DOM2 Browsers: Netscape 6+, Mozilla, FireFox
		if (e.which != KEY_BACKSPACE && e.which != KEY_ENTER && !(e.which == WHICH_TAB && e.keyCode == KEY_TAB) && (e.which < KEY_ZERO || e.which > KEY_NINE)) {
			this.m_keepKeyPress[txt.id] = false;  // Cancel the Key Press (DOM2 Browsers)
			e.preventDefault();
			return false;
		}
		else {
			this.m_keepKeyPress[txt.id] = true; // Valid numeric, Keep Key Press (DOM2 Browsers)
		}
	}
}

//------------------------------------------------------------------------------------------------
//12-20-2004: Addition to accomodate adding '/' to a date entered into a single textbox...KAH

///<Summary>
///This function will format a given textbox into the date format of mm/dd/yyyy.
///By format we mean that the function will insert '/' between numeric keypresses in the textbox.
//Once the maximum length of 10 characters is reached, the user cannot enter anymore text.
//They can however select the textbox and type over the characters.
///</Summary>
///<param>e - the onfocus event</param>
///<param>txt - the textbox user is typing in.</param>
function dateKeyHandler(e, txt)
{
	var length = 2;
	var midlength = 5;
			if (document.all) { // Internet Explorer
				if (e.keyCode != 0 && e.keyCode != KEY_BACKSPACE) {
					if (this.m_charCount[txt.id] < MAX_LENGTH_DATE){
						this.m_charCount[txt.id] = this.m_charCount[txt.id] + 1;
					}
					if (this.m_charCount[txt.id] == length + 1 || this.m_charCount[txt.id] == midlength + 1) { // Internet Explorer puts the key in next text box
						this.m_suppressSelect[txt.id] = true;
						txt.value = txt.value + "/";
						this.m_charCount[txt.id] = this.m_charCount[txt.id] + 1;	
					}
				} 
				else if (e.keyCode == KEY_BACKSPACE) {
						alert("Should not fire. IE");
					this.m_charCount[txt.id] = this.m_charCount[txt.id] - 1;	
				}
			} 
			else if (document.getElementById) { // DOM2 Browsers: Netscape 6+, Mozilla, FireFox
				if (this.m_keepKeyPress[txt.id] && e.which != KEY_BACKSPACE) {
					if (this.m_charCount[txt.id] == length || this.m_charCount[txt.id] == midlength) { // DOM2 Browsers put the key in the current box
						this.m_suppressSelect[txt.id] = true;
						txt.value = txt.value + "/";
						this.m_charCount[txt.id] = this.m_charCount[txt.id] + 1;
						//this.outputWindow.document.writeln('js_SingleTBFormatDate Added slash:'+this.m_charCount[txt.id]+'<br/>');	
					}
					if (this.m_charCount[txt.id] < MAX_LENGTH_DATE){
						this.m_charCount[txt.id] = this.m_charCount[txt.id] + 1;
					}
				} 
				else if (e.which == KEY_BACKSPACE) {
					this.m_charCount[txt.id] = this.m_charCount[txt.id] - 1;
					//this.outputWindow.document.writeln('js_SingleTBFormatDate backspace'+this.m_charCount[txt.id]+'<br/>');		
				}				
			}
}
//-----------------------------------------------------------------------------------------------------

///<Summary>
///This function will format a given textbox into the date format of mm/dd/yyyy.
///By format we mean that the function will insert '/' between numeric keypresses in the textbox.
//Once the maximum length of 10 characters is reached, the user cannot enter anymore text.
//They can however select the textbox and type over the characters.
///</Summary>
///<param>e - the onfocus event</param>
///<param>txt - the textbox user is typing in.</param>
function js_SingleTBFormatPhone(e, txt)
{
	var length = 3;
	
	if(txt.value.length < MAX_LENGTH_PHONE) {
	
		if (txt.value.length <= LENGTH_PHONE) {
			if (document.all) { // Internet Explorer
				if (e.keyCode != 0) {
					this.m_charCount[txt.id] = this.m_charCount[txt.id] + 1;
					if (this.m_charCount[txt.id] == length + 1) { // Internet Explorer puts the key in next text box
						this.m_suppressSelect[txt.id] = true;
						txt.value = txt.value + "-";
						this.m_charCount[txt.id] = 1;
					}
				}
			}
			else if (document.getElementById) { // DOM2 Browsers: Netscape 6+, Mozilla, FireFox
				if (this.m_keepKeyPress[txt.id]) {
					this.m_charCount[txt.id] = this.m_charCount[txt.id] + 1;
					if (this.m_charCount[txt.id] == length) { // DOM2 Browsers put the key in the current box
						this.m_suppressSelect[txt.id] = true;
						txt.value = txt.value + "-";
						this.m_charCount[txt.id] = 0;
	//this.outputWindow.document.writeln('js_SingleTBFormatPhone if2'+this.m_charCount[txt.id]+'<br/>');	
					}
				}
			}
		}
	}
	else {
		txt.maxLength = MAX_LENGTH_PHONE;
	}
	
}
