<!-- // start hiding code
/*
// Copyright 1999 MTI All Rights Reserved.
// File Name:		form.js
// Purpose:	This is a file of javascript code of common client side form functions.  
// Modification History
// 02/29/2000 000 -  Created By	Mike Benist (MEB)
// 08/01/2000 001 -  Added SetSelectArrayValue and SetSelIndexArrayValue functions from TGAAdmin scripts.
// 
*/

////////////////////////////FORM FUNCTIONS/////////////////////////////////
// This function changes the action property (destination) of the form
function ChangeDestination(oform,cDestination) {
	oform.action = cDestination
}

// This function will change the action of the form based on the
// value of the select list chosen
function GetSelAction(osel,oform) {
	oform.action = GetSelValue(osel)
}

///////////////////////////FORM FIELD FUNCTIONS////////////////////////////
// This function checks for the existence of an object on a form
function FieldExists(elm) {
	if (eval(elm)==null) {
		return false;
	} else {
		return true;
	}
}

// This function will return the object associated with a label
function GetObject(elm) {
	if (FieldExists(elm)) {
		return eval(elm);
	} else {
		return false;
	}
}

///////////////////////////TEXT BOX FUNCTIONS//////////////////////////////
// This function sets the value of a text box.
function SetValue(elm,strvalue){
	if (FieldExists(elm)) {
		oElm = eval(elm);
		oElm.value = strvalue;
	}
}
/****************************************************************/
// This function takes the value of a text box and makes it safe
// for executing a SQL statement.  Certain characters will cause a 
// SQL statement to fail for syntax error.
function makeSafe (elm) {
	elm.value = sqlSafe (elm.value);
}

/****************************************************************/
/* PURPOSE:  Since we are using the single tick mark as the
	string delimiter to construct our SQL queries, a string with
	a tick mark in it will cause a SQL error.  Therefore we replace
	all "'" with "''", which eliminates the possibility of a SQL error.
*/

function sqlSafe (s) {
	var new_s = s;
	new_s = replaceAll (new_s, "'", "|");
	new_s = replaceAll (new_s, "|", "''");
	new_s = replaceAll (new_s, "\"", "|");
	new_s = replaceAll (new_s, "|", "''");
	return new_s;
}

/****************************************************************/
function replaceAll (s, fromStr, toStr) {
	var new_s = s;
	for (i = 0; i < 100 && new_s.indexOf (fromStr) != -1; i++) {
		new_s = new_s.replace (fromStr, toStr);
	}
	return new_s;
}

///////////////////////////SELECT LIST FUNCTIONS//////////////////////////////
// This function will get the value of the selected item
// on a drop down list box
function GetSelValue(elm) {
	var choicenumber=0;
	var selobj=elm;
	choicenumber=selobj.selectedIndex;
	return selobj.options[choicenumber].value;
}

// This function sets a select control based on the text
function SetSelectText(SelElm,cValue) {
	if (FieldExists(SelElm)) {
		oSelElm = eval(SelElm);
		SetSelIndex(oSelElm,cValue)
	}
}

// This function sets a select control based on a value
function SetSelectValue(SelElm,cValue) {
	if (FieldExists(SelElm)) {
		oSelElm = eval(SelElm);
		SetSelIndexValue(oSelElm,cValue)
	}
}

// This function will get the text of the selected item
// on a drop down list box
function GetSelText(elm) {
	var choicenumber=0;
	var selobj=elm;
	choicenumber=selobj.selectedIndex;
	return selobj.options[choicenumber].text;
}

// This function will set the selected index of a drop down list
// box to a specified string if it is in the list
function SetSelIndex(elm,instring) {
	var i;
	oSel = elm;
	for (i=0;i<oSel.length;i++) {
		if (oSel.options[i].text==instring) {
			oSel.selectedIndex=i;
			break;
		}
	}
}

// This function will set the selected index of a drop down list
// box to a specified value if it is in the list
function SetSelIndexValue(elm,instring) {
	var i;
	oSel = elm;
	for (i=0;i<oSel.length;i++) {
		if (oSel.options[i].value==instring) {
			oSel.selectedIndex=i;
			break;
		}
	}
}

// 08/01/2000 001 -  Added SetSelectArrayValue and SetSelIndexArrayValue functions from TGAAdmin scripts.
// This function sets a select control based on a value and an array in the value attribute of the select control.
function SetSelectArrayValue(SelElm,cValue,nSubscript,cSplitChar) {
	if (FieldExists(SelElm)) {
		SetSelIndexArrayValue(SelElm,cValue,nSubscript,cSplitChar)
	}
} // end function SetSelectArrayValue(SelElm,cValue,nSubscript,cSplitChar)

// 08/01/2000 001 -  Added SetSelectArrayValue and SetSelIndexArrayValue functions from TGAAdmin scripts.
// This function will set the selected index of a select control to a specified value if 
// it is in the correct subscript of an array value.
function SetSelIndexArrayValue(elm,instring,nSubscript,cSplitChar) {
	var i;
	oSel = elm;
	for (i=0;i<oSel.length;i++) {
		ctemp = oSel.options[i].value
//		alert("ctemp "+ctemp);
//		alert("cSplitChar "+cSplitChar);
		arvalue = ctemp.split(cSplitChar);
//		alert("arvalue "+arvalue);
		cvalue = arvalue[nSubscript];
//		alert("cvalue "+cvalue);
		if (cvalue==instring) {
			oSel.selectedIndex=i;
			break;
		}
	}
} // end function SetSelIndexArrayValue(elm,instring,nSubscript)

///////////////////////////RADIO BUTTON FUNCTIONS//////////////////////////////
// This function gets the value of the selected radio button
function GetRadioValue(radioObject) {
	var value = null;
	var i;
	for (var i=0; i<radioObject.length; i++) {
		if (radioObject[i].checked) {
			value = radioObject[i].value
			break
		}
	}
	return value;
}

// This function sets a radio button based on a value
function SetRadioValue(radioObject,cValue) {
	for(j=0;j<radioObject.length;j++) {
		if (radioObject[j].value == cValue) {
			radioObject[j].click();
			break;
		}		
	}
}

///////////////////////////CHECK BOX FUNCTIONS//////////////////////////////
// This function sets a check box based on a value
function SetCheckBoxValue(CheckBoxObject,cValue) {
	if (CheckBoxObject.value == cValue) {
		CheckBoxObject.click();
	}		
}

// This function gets the value of the selected check box
function GetCheckBoxValue(CheckBoxObject) {
	var value = null;
	if (CheckBoxObject.checked) {
		value = CheckBoxObject.value;
	}
	return value;
}

// Purpose: to test an existing string for a specific format
// The format is based on a mask consisting of characters that
// represent different possible types of values
// * - repeats the following character
// # - is an integer
// ? - is an alpha character in upper or lower case
// ~ - is any character
// $ - are money characters including digits
// [] - is a set of optional characters
// {} - is a list of characters
// @ - is an email address
// other characters are literal
// a phone number in North America would be (###)###-####
// a zip code would be #####[-####]
// The length variable can be used if the entire string should be
// of a single type.  eg (ZIP,N,5) would validate a short zip code
// The required flag is optional.  When included the string can't
// be blank

// numeric characters
var numbers = "0123456789";
var upperalpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var loweralpha = "abcdefghijklmnopqrstuvwxyz"
var allalpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var special = "@~!#$%^&*()-_+=`|{}[];:',.<>/?"
var money = numbers + "$,.-+()"

function CheckMask(inelm, maskelm, msgObject, labelelm) {
	var i = 0	
	var m = 0
	var lstatus = true
	var lisRepeat = false
	var lisOptional = false
	var lisAnything = false
	var lisEmail = false
	var lisReq = false
	var leom = false
	var leos = false
	var nsearch = 0
	var cvaliddata = ""
	var cmaskstring = maskelm.value
	var cmaskChar = ""
	var cinChar = ""
	var cinstring = inelm.value
	var clabelstring = labelelm.value
	var cdetailError = ""

	// See if you are done
	// You are done when you have a true status,
	// you are at the end of the instring,
	// and you are at the end of the maskstring
	do { 
		// if you are not at the end of the mask string get the next character
		if (!leom) {
			cmaskChar=cmaskstring.substring(m,m+1);
			if (cmaskChar=="") { // If there wasn't anything to get you are at the end of the string
				leom=true;
			} else {
				switch (cmaskChar) {
					case "{" : // it is the beginning of a list
					cvaliddata = ""; // initialize the valid data to blank
					m++;
					for (; m != cmaskstring.length; m++) {
						if (cmaskstring.substring(m,m+1) == "}") {
							break
						}
						cvaliddata = cvaliddata + cmaskstring.substring(m,m+1)
					}
					break
					
					case "~" : // anything is ok 
					lisAnything=true;
					break
					
					case "?" : // looking for a letter
					cvaliddata = upperalpha + loweralpha;
					break
					
					case "#" : // looking for a number
					cvaliddata = numbers;
					break
					
					case "$" : // looking for a number
					cvaliddata = money;
					break
					
					case "@" : // looking for an email address
					if (i<1) {
						lisEmail = true;
						cvaliddata = "@";
					}
					break
					
					case "*" : // this is the repeat character
					lisRepeat = true;
					m++; // go to the next mask character
					continue // This starts it back at the top
					break
					
					case "[" : // this is the optional character
					lisOptional = true;
					m++; // go to the next mask character
					continue // This starts it back at the top
					break
					
					case "]" : // this is the end of optional
					lisOptional = false;
					m++; // go to the next mask character
					continue // This starts it back at the top
					break
					
					default : // when no special mask characters are there
					cvaliddata = cmaskChar;
					break
				}
			}
		}
		// if you are at the beginning of the mask and it is not optional the field is required.
		// if there is no mask this function shouldn't be called
		if (m==0 && !lisOptional) { 
			cdetailError = "The "+clabelstring + " is required."+"<br>";
			lisReq=true;
		}
		// if you are not at the end of the instring get the next character
		if (!leos) {
			cinChar=cinstring.substring(i,i+1);
			if (cinChar=="") { // If there wasn't anything to get you are at the end of the string
				leos=true;
			}
		}
		
		// check for error conditions and if there aren't any
		// then go ahead and compare the strings
		// First error condition is that you are at the end of the
		// mask and you still have a string to compare (instring too long)
		if ((leom==true) && (leos==false)) {
			if (lisRepeat) { // you may not have a problem if you are repeating
				i++; // See if incrementing the string gets you to the end
				continue ; // will try the condition next
			} 
			cdetailError = cdetailError+"The "+clabelstring+" is too long. Enter it as "+GetMaskDesc(cmaskstring)+"<br>";
			lstatus = false; // kill the loop you have an error
			continue ; // will try the condition next
		}
		// Second error condition is that you are at the end of the 
		// string and you are not at the end of the mask
		if ((leos==true) && (leom==false)) {
			if (lisRepeat) { // you may not have a problem if you are repeating
				lisRepeat = false; // you are no longer repeating
				m++; // See if incrementing the mask gets you to the end
				continue ; // will try the condition next
			} 
			if (lisEmail) { // you may not have a problem if it is email (special case of repeat)
				if (lisOptional) {
					return (lstatus);
				} else {
					cdetailError = cdetailError+"The "+clabelstring+" is an invalid e-mail address.  Must be at least a@b.c"+"<br>"
					lstatus=false; // Houston we have a problem!
					continue ;  // will try the condition next
				}
			} else { // it is not a repeat or an email
				if (!lisOptional) { // it is not optional so you are too short
					if (lisReq && cinstring!="") {
						cdetailError = cdetailError+"The "+clabelstring+" is too short. Enter it as "+GetMaskDesc(cmaskstring)+"<br>"; 
					} else {
						cdetailError = cdetailError+"Enter the "+clabelstring+" as "+GetMaskDesc(cmaskstring)+"<br>"; 
					}
					lstatus = false; // kill the loop you have an error	
					continue ;  // will try the condition next
				}
			}
		}	
		// compare the strings
		nsearch = cvaliddata.indexOf(cinChar);
		if (nsearch == -1) { // if the search fails
			// Check to see if you are on a repeating validation
			// if so you have to see if the next one fails before 
			// you blow the whistle
			if (lisAnything) { 
				i++;
				if (!lisRepeat) { // If you are not repeating also increment the mask
					m++;
				}
				continue // go on to the next
			}
			if (lisRepeat) {
				lisRepeat = false; // you are no longer repeating
				m++; // go to the next character in the mask
				continue // go on to the next
			}
			if (lisOptional) { // if they are optional it might be ok
				m++; // go to the next character in the mask
				continue // go on to the next
			}
			if (lisEmail) { // if this is an email address it might be ok
				if (cinChar==" ") {
					cdetailError = cdetailError+"The "+clabelstring+" does not allow spaces."+"<br>";
					// without a continue here it is an error condition
				} else { // You haven't found the "@" yet
					i++; // go to the next character in the string
					continue // go on to the next
				}
			}
			lstatus=false; // Houston we have a problem!
			if (cinChar==" ") {
				cdetailError = cdetailError+"The "+clabelstring+" does not allow a space in position # "+(i+1)+". Enter it as "+GetMaskDesc(cmaskstring)+"<br>";
			} else {
				cdetailError = cdetailError+"The "+clabelstring+" does not allow the character, " + cinChar + 
				", in position # "+(i+1)+". Enter it as "+GetMaskDesc(cmaskstring)+"<br>";
			}
		}		
		if (lisEmail) { // if you are looking at an email and the
			// search character is the "@" it can't be the first character
			if (cvaliddata=="@") {
				if (i+1==cinstring.length) {
					cdetailError = cdetailError+"The "+clabelstring+" '@' can't be the last character.  Must be at least a@b.c"+"<br>"
					lstatus=false; // Houston we have a problem!
					continue;
				} else {
					i++; // it should have at least one character before the "."
					cvaliddata = ".";
					continue ;
				}
			} else if (cvaliddata==".") { // you are almost done with email 
				if (i+1==cinstring.length) {  // you are at the end of the string and it is a period
					cdetailError = cdetailError+"The "+clabelstring+" does not allow a '.' in the last position."+"<br>"
					lstatus=false; // Houston we have a problem!
					continue ;
				} else {  // you have found a period after the "@" so it should pass
					return (lstatus); // Yeah you are done with email
				}
			} // end of "." period conditional
		} // end of lisEmail conditional 
		// increment the counters before trying again
		if (!lisRepeat) { // only update the mask counter if you are not repeating
			m++; // mask counter			
		}
		i++; // instring counter
	} while (((leos == false) || (leom == false)) && (lstatus==true)); // end of do while
	if (lstatus==false) {
		if (msgObject != null) {
			msgObject.value = msgObject.value + cdetailError + "<br>";
		}
	}
	return (lstatus);
}

// This function will take the data from the database and apply
// formatting based on a mask.  The mask cannot contain conditional
// formatting.  # is a number and ? is a letter.  All other characters
// are considered formatting.
function AddMaskFormat(elm, maskelm) {
	var cInString = elm.value;
	var cMaskString = maskelm.value;
	var i,m;  // i is the instring counter, m is the mask counter
	var cMaskChar, cInStringChar;
	var nMaskLength = cMaskString.length;
	var nInStringLength = cInString.length;
	var nDataLength = nMaskLength;
		// first check to see if the incoming data is the right length
	for (m = 0; m < (nMaskLength -1); m++) {
		cMaskChar=cMaskString.substring(m,m+1);
		if ((cMaskChar != "?") && (cMaskChar != "#")) {
			nDataLength--  
		}
	}
	if (nDataLength != nInStringLength) { // There is a problem
		// so give them the original value and force an edit
		// on their part to correct the data.
		return cInString;
	} else {
		// format the data
		i = 0;
		for (m = 0; m < (nMaskLength -1); m++) {
			cMaskChar=cMaskString.substring(m,m+1);
			if ((cMaskChar != "?") && (cMaskChar != "#")) {
				cInString=cInString.substring(0,i)+cMaskChar+cInString.substring(i,nMaskLength-1)
			}
			i++;
		}
	}
	return cInString;
}

// This function will take the formatted data from the web page and 
// strip the formatting based on a mask.  The mask cannot contain 
// conditional formatting.  # is a number and ? is a letter.  All 
// other characters are considered formatting.
function StripMaskFormat(elm, maskelm) {
	var cInString = elm.value;
	var cMaskString = maskelm.value;
	var i,m;  // i is the instring counter, m is the mask counter
	var cMaskChar, cInStringChar;
	var nMaskLength = cMaskString.length;
	var nInStringLength = cInString.length;
	var nDataLength = nMaskLength;
	// format the data
	i = 0;
	for (m = 0; m < (nMaskLength-1); m++) {
		cMaskChar=cMaskString.substring(m,m+1);
		if ((cMaskChar != "?") && (cMaskChar != "#")) {
			if (cInString.substring(m,m+1)==cMaskChar) {
				cInString=cInString.substring(0,i)+cInString.substring(i+1,nMaskLength)
				i--;
			}
		} 
		i++;
	}
	return cInString;
}

// end hiding code
// -->