
function ShowLink(theObject){
	
	if (document.getElementById (theObject.id).className == "LinkOn") {
		document.getElementById (theObject.id).className = "LinkOff"
	} else {
		document.getElementById (theObject.id).className = "LinkOn"
	}
}
function FrameLink(theFrame,theLink) {
//purpose - change page displayed in an iframe from a link on the parent frame(pass in the object)
	try {
		theFrame.location = theLink
	} catch (e) {
		alert("There was an error accessing that page.")
	}
}

function ToggleIt(theName, theSpan) {
//purpose - toggles display of detail data stored in a span underneath an HTML element
	theElement = document.getElementById(theName)
	if (theElement.style.display == "") {
		theElement.style.display = "none"
		try {
			theSpan.innerHTML = String(theSpan.innerHTML).replace("Hide","Details")
		} catch (e) {}
	} else {
		theElement.style.display = ""
		try {
			theSpan.innerHTML = String(theSpan.innerHTML).replace("Details","Hide")
		} catch (e) {}
	}
	//theElement.focus()
}

function ShowElement(theDiv, theTagValue) {
//purpose - toggles display of a div tag
//optional - theTagValue, if included, will check a "level" attribute and only change those values that match
	theDivs = document.all.tags("div")
	var theAttributeValue = false
	for (i=0; i<theDivs.length; i++) {
		if (theTagValue) {
			theAttributeValue = (theDivs[i].getAttribute("level"))?theDivs[i].getAttribute("level"):false
		}
		if (theAttributeValue) {
			if (theAttributeValue == theTagValue) {
				//alert(theDivs[i].id + "\ndisplay:" + theDivs[i].style.display)
				theDivs[i].id == theDiv?theDivs[i].style.display = "":(theDivs[i].style.display = "none")
			}
		} else {
			theDivs[i].id == theDiv?theDivs[i].style.display = "":(theDivs[i].style.display = "none")
		}
	}
}

function ShowChildElement(theDiv, theFrame) {
//purpose - toggles display of a div tag within an iframe
	theDivs = theFrame.document.all.tags("div")
	for (i=0; i<theDivs.length; i++) {
			theDivs[i].id == theDiv?theDivs[i].style.display = "":(theDivs[i].style.display = "none")
	}
}

function openWindow(theValue, theWidth, theHeight, theTitle) {
//purpose - opens new window with specified width and height
	window.open (theURL,(theTitle)?theTitle:'','width=' + theWidth + ',height=' + theHeight)
}


function CheckStringForNull(theString) {
	return (theString == null)?"":theString
}

function Validator() {//theForm
//theForm is the actual form, NOT THE NAME OF THE FORM!!
	theForm = document.dform	
	//VALIDATION LEGEND
	var chkRequired = /R/					//check for Required value(has to have a value)
	var chkSelectRequired = /SC/			//check for a select box to have an option selected other than ""(empty string)
	var chkDate = /D/						//check for valid date format
	var chkTime = /T/						//check for valid time format
	var chkNumber = /N/						//field can only contain valid numbers
	var chkEmail = /E/						//valid email format
	var chkPhone = /P/						//valid phone number format
	var chkZip = /Z/						//valid zip code format(5 digits. with optional "-" and 4 more digits
	var chkSingleQuotes = /Q/				//check for single quotes
											//	- checks unless you add in the Q
	var chkHTML = /H/						//check for invalid html tags("<input", "<meta") 
											//	- checks unless you add in the H
	var chkCompareDate1 = /C1a/				//Compare Date to C2a
	var chkCompareDate2 = /C2a/				//Compare Date to C1a
	var chkCompareDate3 = /C1b/				//Compare Date to C2b
	var chkCompareDate4 = /C2b/				//Compare Date to C1b
	var chkSwitch = /x/						//if (x) present compare both date ranges to each other...
	var chkGreaterThanToday = /Gt/			//check for date > today(return error if not)
	var chkLessThanToday = /Lt/				//check for date < today(return error if not)
	var chkGreaterThanEqualToday = /Ge/		//check for date >= today(return error if not)
	var chkLessThanEqualToday = /Le/		//check for date <= today(return error if not)
	var chkValueGroup = /Gr/				//Check to see if at least one value out of the group has a value
	var chkForTrue = /true/
	var chkStringMatch = /M/				//check for 2 strings Matching
		
	var err = ""
	var theGroupValues = ""
	
	var theMatch1 = ""
	var theMatch2 = ""
	var theStringName1 = ""
	var theStringName2 = ""

	for (i=1; i < theForm.length ;i++){
		
		var theString = theForm(i).getAttribute("validate")
		var theFriendlyName = theForm(i).getAttribute("friendly")
		var theExclude = theForm(i).getAttribute("exclude")
		
		theFieldName = (theForm(i).getAttribute("friendly") == null)?theForm(i).name:theForm(i).getAttribute("friendly")
			with (theForm(i)) {
				if (theExclude != "Y") {
					// Required Validation 
					if (chkRequired.exec(theString)){ 
						if(value == "") {
							err += CheckStringForNull(theFieldName + " cannot be null or empty.\n")
						}
					}
					
					// Required Select Box Choice 
					if (chkSelectRequired.exec(theString)){ 
						if(theForm(i).options[theForm(i).selectedIndex].value == "") {
							err += CheckStringForNull("You must choose an option for " + theFieldName + ".\n")
						}
					}

					// Date Check Validation
					if (chkDate.exec(theString)) {
						theRE =/^\d{1,2}[\.\/]\d{1,2}[\.\/]\d{4}$/
						if (value) {
							if (!theRE.test(value)) {
								err += CheckStringForNull(theFieldName + " is not a valid date. \nDates must be in the format m/d/yyyy.\n\n")
							}
						}
					}
					
					// Time Check Validation
					if (chkTime.exec(theString)) {
						theRE =/\d{1,2}:\d{2}:\d{2}\s[AM|PM]{1}/i
						if (value) {
							if (!theRE.test(value)) {
								CheckStringForNull(err += theFieldName + " is not a valid time. \nTimes must be in the format h:mm:ss AM/PM\n\n")
							}
						}
					}
					
					// Valid Numbers Only Validaion
					if (chkNumber.exec(theString)){
						if (isNaN(value)) {
							err += CheckStringForNull(theFieldName + " must be a valid number.\n")
						}
					}
					
					// Email
					if (chkEmail.exec(theString)) {
						theRE =/^([\w\.]+)@([A-Za-z0-9_.]+)\.(\w{2,4})$/gi
						if (value) {
							if (!theRE.test(value)) {
								err += CheckStringForNull(theFieldName + " must be a valid email.\n")
							}
						}
					}

					// Phone Number
					if (chkPhone.exec(theString)) {
						theRE = /^[19]{0,1}[-./]{0,1}(\d{0}|[\(]{0,1}\d{3}[\)]{0,1})[-./]{0,1}\d{3}[-./]{0,1}\d{4}$/ // /([19])([\.-])(\()(\d{3})(\))([-\.])((\d{3}[-.]d{4})||(d{7}))/
						if (value) {
							if (!(theRE.test(value))) {
								err += CheckStringForNull(theFieldName + " must be a valid phone number format.\n")
							}
						}
					}
					//Zip
					if (chkZip.exec(theString)) {
						theRE = /^\d{5}(|\d{4}|\-\d{4})$/i
						if (value) {
							if (!(theRE.test(value))) {
								err += CheckStringForNull(theFieldName + " must be a valid zip code format.\n")
							}
						}
					}
					
					//Single quotes
					if (!chkSingleQuotes.exec(theString)) {
						theRE = /\'/
						if ((value)&&(name != "ID")) {
							if (theRE.test(value)) {
								err += CheckStringForNull(theFieldName + " must not contain single quotes.\n")
							}
						}
					}	

					//HTML
					if (!chkHTML.exec(theString)) {
						theRE = /\<(input|meta)/i
						if (value) {
							if (theRE.test(value)) {
								err += CheckStringForNull(theFieldName + " must not contain any of the following HTML tags.\n\t - <input>\n\t - <meta>\n")
							}
						}
					}	
					
					//check for strings Matching
					if (chkStringMatch.exec(theString)) {
						if (theMatch1 == "") {
							theMatch1 = value
						} else if ((theMatch2 == "")&&(theMatch1 != "")) theMatch2 = value

						if (theStringName1 == "") {
							theStringName1 = theFieldName
						} else if ((theStringName2 == "")&&(theStringName1 != "")) theStringName2 = theFieldName
					}		

					// Set Date Values for Comparison
					if (chkCompareDate1.exec(theString)){
						var Date1 = new Date(value)
						var Name1 = theFieldName
					}	
					if (chkCompareDate2.exec(theString)) {
						var Date2 = new Date(value)
						var Name2 = theFieldName
					}
					if (chkCompareDate3.exec(theString)) {
						var Date3 = new Date(value)
						var Name3 = theFieldName
					}	
					if (chkCompareDate4.exec(theString)) {
						var Date4 = new Date(value)
						var Name4 = theFieldName
					}
					/*if (!chkValueGroup.exec(theString)) {
						var theGroupValues
						theGroupValues += checked + " "
					}*/
					
					//today date checks
					var Today = new Date()
					Today.setHours(0)
					Today.setMinutes(0, 0, 0)
					if (chkGreaterThanToday.exec(theString)) {
						var theDate = new Date(value)
						if (theDate <= Today) err += CheckStringForNull(theFieldName + " must be greater than today's date.\n")
					}
					if (chkLessThanToday.exec(theString)) {
						var theDate = new Date(value)
						if (theDate >= Today) err += CheckStringForNull(theFieldName + " must be less than today's date.\n")
					}
					if (chkGreaterThanEqualToday.exec(theString)) {
						var theDate = new Date(value)
						if (theDate < Today) err += CheckStringForNull(theFieldName + " must be greater than or equal to today's date.\n")
					}
					if (chkLessThanEqualToday.exec(theString)) {
						var theDate = new Date(value)
						if (theDate > Today) err += CheckStringForNull(theFieldName + " must be less than or equal to today's date.\n")
					}
					
									
			} //With
		}//if
	} //For Loop

			if ((theMatch1 != theMatch2)&&(theMatch1 != "")) err += theStringName1 + " and " + theStringName2 + " must match."

			try {
				if (Date1 > Date2){
					CheckStringForNull(err += Name1 + " must be less than " + Name2 + ".\n")
				}
				if (Date3 > Date4){
					CheckStringForNull(err += Name3 + " must be less than " + Name4 + ".\n")
				}
				if (chkSwitch){
					if (Date1 > Date3){
						CheckStringForNull(err += Name1 + " cannot be greater than " + Name3 + ".\n")
					}
					if (Date2 < Date4){
						CheckStringForNull(err += Name2 + " cannot be greater than " + Name4 + ".\n")
					}
				}	
			} catch (e) {
				//if the date validation fails, just turn it over to the server error 
				//handling. Just do nothing.
			}

			if ((theGroupValues>'')&&(chkForTrue.exec(theGroupValues))) {
				CheckStringForNull(err += "You must select at least one of the group options.")
			}

	if (err != '') {
		alert(err)		
		return false	
	} else {
	//document.forms[0].submit()
		return true
	}
}//function

