function preLoadImages() {
	// Preloading the necessary images. CMS
	if (document.images)
	{
		// Load the logo image. CMS
		var logopic = new Image(200,66); 
		logopic.src = "images/dsslogo.gif";
		
		// Load the background images. CMS
		var backpagetitle = new Image(700,40); 
		backpagetitle.src = "images/bckpagetitle.jpg";
	
		// Load the body gradient. CMS
		var backgradient = new Image(25,700); 
		backgradient.src = "images/backgradient.jpg";
	
		// Load the main family photo. CMS
		var familypic = new Image(480,150); 
		familypic.src = "images/family.jpg";
	
		// Load the main green block image. CMS
		var greenblock = new Image(220,150); 
		greenblock.src = "images/family.jpg";
	}
}
function clickDesc(elemID)
{
	// Loop through all elements and close them first. Then open the requested element. CMS
	/*
	var loopDesc = 1;
	var elemName = elemID.split("_")[0];
	var elemNum = elemID.split("_")[1];
	var loopName = elemName + "_" + loopDesc;
	while (document.getElementById(loopName)) {
		if (loopName != elemID) {
			document.getElementById(loopName).style.display = "none";
		}
		loopDesc = loopDesc + 1;
		loopName = elemName + "_" + loopDesc;
	}
	*/
	var agency = document.getElementById(elemID);

	if(agency.style.display != "block"){
		agency.style.display = "block";
		// Hide the Mother and Her Children picture. CMS
		//document.getElementById('mainMotherPic').style.display = "none";
	}
	else{
		agency.style.display = "none";
		//document.getElementById('mainMotherPic').style.display = "block";
	}
	//matchColumnSize(); // Resize the columns if necessary. CMS
}
// Call the method to preload images.
preLoadImages();

// Global variables used to display the error message at the top of the window. CMS
var msgElem, winBody;

// Function to determine references to window objects so that the error message can be displayed at the top of the screen. CMS
function evalErrorMessage(messageID) {
	
	// Get the div tag that displays the messge. When the page is first requested, the div element is hidden. This will need to be
	// validated against a text-only browser. CMS
	msgElem = document.getElementById(messageID);
	
	// Reference to the documentElement object. CMS
	winBody = document.documentElement;
	
	// Set the positionElement() method to run every 100 milliseconds (1/10 of a second). CMS
	setInterval("positionElement()",100);
}

// Function to position the element in the top center of the screen. CMS 
function positionElement(){
	
	// Determine the width of the screen. Different browsers support different properties. CMS
	winWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;
	
	// Calculate the pixel location from the left to center the div tag in the window. I minus the 140 because of the width of the
	// left menu bar. CMS
	msgLoc = (((winWidth - 125) / 2) + 125) - (msgElem.clientWidth / 2);
	
	// Determine the location of the left point of the screen. CMS
	var dsocleft = winBody.scrollLeft;
	// Determine the upper position of the window relative to the document. CMS
	var dsoctop = winBody.scrollTop;
	
	// Position the note in the top left corner of the screen. CMS
	msgElem.style.left = parseInt(msgLoc) + "px";
	msgElem.style.top = parseInt(dsoctop) + 10 + "px";
}

// Function to hide the note when someone begins entering text. CMS
function hideNote() {
	
	// Hide the element from view. CMS
	msgElem.style.display = "none";
	
	// Erase the message so that it does not show up in text browsers. CMS
	msgElem.innerHTML = "";	
}

// General function to validate forms. The names of the fields must correspond with a valid[field name]() method listed. There could be
// a way to expand the regular expression so that we can help the user know exactly where the error is occurring. CMS
function validateForm(formToValidate,emailRequired) {

	// If the form sent is undefined or is invalid, then show a message to the user. CMS
	if (!formToValidate) {
		alert("The form submitted is invalid.");
		return false;
	}
	
	// Loop through the form elements and clear any message. CMS
	for (i = 0; i < formToValidate.length; i++) {
		msgElemName = formToValidate.elements[i].name + 'Message';
		if (document.getElementById(msgElemName)) {
			var elemChange = document.getElementById(msgElemName);
			elemChange.innerHTML = "";
			elemChange.style.display = "none";
		}
	}

	// Create a new array to hold the messages that need to be told to the user. CMS
	var userMsg = new Array(formToValidate.length);
	var validForm = true;
	
	// Loop through the form and validate the data sent in the form fields. CMS
	for (i = 0; i < formToValidate.length; i++) {
		switch (formToValidate.elements[i].name) {
		case "message":
			// Validate using the validateMessage function because it simply checks for some text. CMS
			var validEntry = validateMessage(formToValidate.elements[i].value);
			if (!validEntry) {
				userMsg[i] ="Please provide a message.";
				validForm = false;
			}
			break;
		case "fullName":
			var validEntry = validateFirstName(formToValidate.elements[i].value);
			if (!validEntry) {
				userMsg[i] = "Please provide your name.";
				validForm = false;
			}
			break;
		case "firstName":
			var validEntry = validateFirstName(formToValidate.elements[i].value);
			if (!validEntry) {
				userMsg[i] = "Please provide a valid first name.";
				validForm = false;
			}
			break;
		case "lastName":
			var validEntry = validateLastName(formToValidate.elements[i].value);
			if (!validEntry) {
				userMsg[i] = "Please provide a valid last name.";
				validForm = false;
			}
			break;
		case "emailAddress":
			// If the email address is required or there is an entry then validate it. CMS
			if (emailRequired || formToValidate.elements[i].value.length > 0 || formToValidate.confirmEmailAddress.value.length > 0) {
				var validEntry = validateEmailAddress(formToValidate.elements[i].value);
			}
			else
				var validEntry = true;
			
			if (!validEntry) {
				userMsg[i] = "Please enter a valid email address.";
				validForm = false;
			}
			break;
		case "confirmEmailAddress":
			// If the email address is required or there is an entry for the confirm email then validate it. CMS
			if (emailRequired || formToValidate.elements[i].value.length > 0 || formToValidate.emailAddress.value.length > 0)
				var validEntry = validateConfirmEmailAddress(formToValidate.elements[i].value, formToValidate.emailAddress.value);
			else
				var validEntry = true;
				
			if (!validEntry) {
				userMsg[i] = "Please confirm the email address. The email address and the confirm email address must match exactly.";
				validForm = false;
			}
			break;
		case "userName":
			var validEntry = validateUserName(formToValidate.elements[i].value);
			if (!validEntry) {
				userMsg[i] = "Please enter a valid user name. User names must be at least 6 characters long and can contain only letters, numbers, and underscores.";
				validForm = false;
			}
			break;
		case "password":
			var validEntry = validatePassword(formToValidate.elements[i].value);
			if (!validEntry) {
				userMsg[i] = "Please enter a valid password. Passwords must be at least 7 characters long and must have at least one letter and one number.";
				validForm = false;
			}
			break;
		case "confirmPassword":
			var validEntry = validateConfirmPassword(formToValidate.elements[i].value, formToValidate.password.value);
			if (!validEntry) {
				userMsg[i] = "Your confirmed password does not match your original password. Please confirm your password.";
				validForm = false;
			}
			break;
		case "secretQuestion":
			var validEntry = validateSecretQuestion(formToValidate.elements[i].value);
			if (!validEntry) {
				userMsg[i] = "Please select a secret question.";
				validForm = false;
			}
			break;
		case "secretAnswer":
			var validEntry = validateSecretAnswer(formToValidate.elements[i].value, formToValidate.password.value);
			if (!validEntry) {
				userMsg[i] = "Please provide an answer to your secret question. The answer to the secret question must be at least 3 characters long and cannot be the same as the password.";
				validForm = false;
			}
			break;
		case "phoneNumber":
			if (formToValidate.elements[i].value != '') {
				var validEntry = validatePhoneNumber(formToValidate.elements[i].value);
				if (!validEntry) {
					userMsg[i] = "Please enter a valid phone number.";
					validForm = false;
				}
			}
			break;
		case "middleInitial":
			var validEntry = validateMIddleInitial(formToValidate.elements[i].value);
			if (!validEntry) {
				userMsg[i] = "Please enter a valid middle initial.";
				validForm = false;
			}
			break;
		case "birthDate":
			var validEntry = validateBirthDate(formToValidate.elements[i].value);
			if (!validEntry) {
				userMsg[i] = "Please enter a valid birth date.";
				validForm = false;
			}
			break;
		case "date":
			var validEntry = validateDate(formToValidate.elements[i].value);
			if (!validEntry) {
				userMsg[i] = "Please enter a valid date.";
				validForm = false;
			}
			break;
		}
	}

	var msgList = "";
	var innerHtmlValid = false;
	
	// Loop through the message array and display the messages to the user. CMS
	for (i = 0; i < userMsg.length; i++) {
		if (userMsg[i] != "" && userMsg[i]) {
			msgElemName = formToValidate.elements[i].name + 'Message';
			if (document.getElementById(msgElemName)) {
				var elemChange = document.getElementById(msgElemName);
				elemChange.innerHTML = "<img src='images/error.gif' alt='Error' style='vertical-align: middle; margin: 0px 4px 3px 0px;' /><span style='color:black;'>" + userMsg[i] + "</span>";
				elemChange.style.display = "block";
				innerHtmlValid = true;
			}
			// Concatenate a message to the current list of messages for the user to be displayed in an alert() box. CMS
			else msgList = userMsg[i] + "\n";
		}
	}
	

	if (!validForm) {
		//if (!innerHtmlValid)
			// Display the concatenation of messages for the user. CMS
			//alert(msgList);
		//else {
			// Set the error message text for the user. CMS
			//msgElem.innerHTML = "<div id=\"mainErrorMessageText\"><div style=\"color: white; background-color: #cc6600; border: 1px solid black; padding: 3px; text-align: center;\">There is a problem with one or more of the entries.<br />Please correct them before continuing.</div></div>";
			// msgElem.innerHTML = "<div id=\"mainErrorMessageText\" style=\"display: inline; float: left;\"><div style=\"color: black;\">There is a problem with one or more of the entries. Please correct them before continuing.</div></div><div style=\"display: inline; float: right; padding-bottom: 2px; vertical-align: top;\"><a style=\"border: 1px solid black; padding: 1px 4px; font-size: .6em; color: black; text-decoration: none;\" class=\"blankAnchor\" title=\"Click here to close this window.\" onClick=\"javascript: document.getElementById('mainErrorMessage').style.display = \'none\';\">X</a></div><div style=\"clear: both; text-align: center; padding: 10px 0px 4px 0px;\"><a style=\"font-size: .8em;\" class=\"blankAnchor\" title=\"Click here to close this window.\" onClick=\"javascript: document.getElementById('mainErrorMessage').style.display = \'none\';\">Close this window.</a></div>";
			
			// Display an error message to the user. CMS
			//msgElem.style.display = 'block';
		//}
		
		// Return false to prevent the form from being submitted. CMS
		return false;
	}
	
	// Set the validated hidden field to true to tell ColdFusion that the form has been successfully validated. CMS
	// I have removed this hidden field because validating again with ColdFusion should not produce significant performance problems
	// when submitting the form. Also, this will ensure that the data was validated even if JavaScript has been turned off. Even
	// though this was the one of the purposes of using the hidden field. This functionality may change in the future. CMS
	// formToValidate.validated.value = "true";
	
	// Return true to tell the onSubmit() event to submit the form. CMS
	return true;
}

// Function to validate the message. CMS
// Need to allow any characters for regular expression. CMS
function validateMessage (userName) {
	// User regular expression to validate the user name. CMS
	var re = new RegExp(/[\w\W]+/);
	return re.test(userName);
}

// Function to validate the password. CMS
function validateUserName (userName) {
	// User regular expression to validate the user name. CMS
	var re = new RegExp(/^(\w){6,48}$/);
	return re.test(userName);
}

// Function validates the confirmation email address. It must match the email address exactly. CMS
function validateConfirmPassword (confirmPassword, password) {
	if (confirmPassword != password || !validatePassword(confirmPassword))
		return false;
	return true;
}

// Function to validate the password. CMS
function validatePassword (password) {
	// User regular expression to validate the password. It must be at least 7 characters long and contain one letter and one number. CMS
	var re = new RegExp(/^(?=.*\d)(?=.*[A-Za-z]).|(?=.*\d){7,48}$/);
	return re.test(password);
}

// Function to validate the answer to the secret question. CMS
function validateSecretAnswer (secretAnswer, password) {
	// If the length is less than 3 or the secret answer is the same as the password, then return false. CMS
	if (secretAnswer.length < 3 || secretAnswer == password)
		return false;
	return true;
}

// Function to validate the secret question selection. CMS
function validateSecretQuestion (secretQuestionNum) {
	if (secretQuestionNum <= 0)
		return false;
	return true;
}

// Function validates the confirmation email address. It must match the email address exactly. CMS
function validateConfirmEmailAddress (confirmEmailAddress, emailAddress) {
	if (confirmEmailAddress != emailAddress || !validateEmailAddress(confirmEmailAddress))
		return false;
	return true;
}

// Validate email addresses in a form. CMS
function validateEmailAddress (emailAddress) {
	// Using regular expression to validate email addresses. CMS
	var re = new RegExp(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/);
	return re.test(emailAddress);
}

// Validate first names in a form. This function could be expanded further to capitalize the name properly. We may need to consider if 
// other languages will be supported. CMS  
function validateFirstName (firstName) {
	var re = new RegExp(/^[A-Za-z ]+$/);
	return re.test(firstName);
}

// Validate last names in a form. This function could be expanded further to capitalize the name properly. We may need to consider if 
// other languages will be supported. CMS 
function validateMiddleInitial (middleInitial) {
	var re = new RegExp(/^[A-Za-z]?$/);
	return re.test(middleInitial);
}

// Validate last names in a form. This function could be expanded further to capitalize the name properly. We may need to consider if 
// other languages will be supported. CMS
function validateLastName (lastName) {
	var re = new RegExp(/^[A-Za-z'?]{2,}$/);
	return re.test(lastName);
}

// Validate telephone number in a form. CMS
function validatePhoneNumber (phoneNumber) {
	/*
		Valid phone numbers contain the following numbers:
		NPA = [2-9][0-8][0-9] 
		Nxx = [2-9][0-9][0-9] 
		Station = [0-9][0-9][0-9][0-9]
		*Information was provided by the North American Numbering System.
	*/
	
	// Phone numbers are validated in the 123-456-7890 format. We may want to make this more dynamic. This function could be expanded
	// further to properly format the phone number as needed. CMS
	var re = new RegExp(/^[2-9][0-8][0-9]-[2-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$/);
	return re.test(phoneNumber);
}

// Validate birthdates in a form. Birthdates are validated to prevent dates in the future. CMS
function validateBirthDate (birthDate, getObject) {
	// If we want the object, then return it.
	if (getObject) {
		//alert ('Returning an object. CMS ');
		return validateDate(birthDate, getObject);
	}
	
	// If we just want to validate the date, then return boolean. CMS 
	else if (!getObject && !validateDate(birthDate, getObject)) {
		//alert ('NOT Returning an object. CMS ');
		return false;
	}
		
	else {
		var currentDate = new Date();
		var birthDate = new Date(birthDate);
		// If the current date minus the birth date is less than or equal to zero than the user has entered a birthdate in the future.
		// This is not a valid birth date. CMS
		if (currentDate - birthDate <= 0)
			return false;
	}
	
	return true;
}

// Validate general dates for formatting only. CMS
function validateDate (dateToValidate, getObject) {
	// getObject is boolean value. Send true to have this function return a date object instead of boolean. CMS
	// Regular Expression to validate the format of the date. The following formats are acceptable:
	//		01/01/1970			(mm/dd/yyyy)
	//		1/1/1970			(m/d/yyyy)
	//		01.01.1970			(mm.dd.yyyy)
	//		1.1.1970			(m.d.yyyy)
	// Other dates that we may want to validate in the future include:
	//		01.01.70			(mm.dd.yy)
	//		01/01/70			(mm/dd/yy)
	//		01-Jan-1970			(dd-mmm-yyyy)
	//		Jan-01-1970			(mmm-dd-yyyy)
	//		January 01, 1970	(mmm dd, yyyy)
	// CMS  ^\d{1,2}\/\d{1,2}\/\d{4}$ 
	var re = new RegExp(/^[0-1]*[0-9]{1}[\/.]{1}[0-3]*[0-9]{1}[\/.]{1}[0-9]{4}$/); 	// This regular expression may be modified to
																					// validate all aspects of the date. Or it may
																					// replace the code written below, but that
																					// would need to be investigated. CMS
	// Validate against the regular expression above. CMS
	if (!re.test(dateToValidate))
		return re.test(dateToValidate);

	// Validate to make sure that the numbers are valid. For example, make sure there is no more than 12 months, etc.
	else {
		// Assign the date to a new JavaScript Date object. This will convert any invalid dates to real dates. For example, if the
		// date '15/1/2006' is enetered, then JS will covnert that to March 1, 2007. This is good because then we can simply compare
		// the date information sent from the user and determine if it is different. If it is, then JavaScript had to convert it and
		// thus it is an invalid date. CMS
		var re = new RegExp(/[\/.]/); 	// Use regular expression to split the users date into three pieces. CMS
		var tmpArray = dateToValidate.split(re);
		
		var tmpDa		= tmpArray[1];
		var tmpMonth	= tmpArray[0];
		var tmpYear		= tmpArray[2];
		
		// Using regular expression to convert any periods (.) to forward slashes (/) in the date. JS won't interprate the date unless
		// it uses forward slashes. CMS
		var reSlash = new RegExp(/[.]/g);
		var objDateToValidate = new Date(dateToValidate.replace(reSlash, "/"));
		
		var objDay = objDateToValidate.getDate();
		var objMonth = objDateToValidate.getMonth() + 1; // Add one because JS months begin with 0 (January = 0). CMS
		var objYear = objDateToValidate.getFullYear();
		
		// Perform the validation. Basically, I'm comparing the separate elements day, month, and year from the user against the
		// day, month, and year from the JS date object. If any are different, then the date is invalid. This is because JS will
		// attempt to convert the date to a valid date as explained above. CMS
		if (tmpDay != objDay || tmpMonth != objMonth || tmpYear != objYear)
			return false;
	}
	return true;
}


