/***************************
* Copyright (c) GrocerySavings.ca
* All Rights Reserved.
*
* Author: Ben Cho
* Website: grocerysavings.ca, localgrocerydeals.com
* CopyRight: 2007
*
* Functions responsible validating forms that will be submitted
* Functions responsible for validating fields on these forms
***************************************/

	// Global ERROR messages
	var ERROR_FIELD_COLOR = "red";
	var ERROR_MSG_PASSWORD_LENGTH = " length must be between 6 and 10 characters long. Please try another.";

	var ERROR_MSG_EMAIL_SUBJECT = "Subject heading must be filled out.";
	var ERROR_MSG_EMAIL_BODY = "Email message must be filled out."

	var ERROR_MSG_REQUIRED_EMAIL_ADDRESS = "Please enter your email address.";
	var ERROR_MSG_INVALID_EMAIL_ADDRESS = "Your email address entered is invalid. Please try again.";

	var ERROR_MSG_REQUIRED_NAME = "Please enter your name.";
	var ERROR_MSG_REQUIRED_FAV_GROCERY_STORE = "Please select your favorite grocery store.";
	var ERROR_MSG_REQUIRED_SHOP_PER_WEEK = "Please select how many times a week you go grocery shopping."
	var ERROR_MSG_REQUIRED_SPEND_PER_WEEK = "Please select your how much you spend a week on groceries."
	var ERROR_MSG_REQUIRED_GENDER = "Please select your gender."
	var ERROR_MSG_REQUIRED_AGE_GROUP = "Please select your age group.";
	var ERROR_MSG_REQUIRED_POSTAL_CODE = "Please enter your postal code.";
	var ERROR_MSG_INVALID_POSTAL_CODE = "The postal code entered is invalid. The proper format is M4M1C3. Please try again.";
	var ERROR_MSG_REQUIRED_USER_NAME = "Please a user name to use on the forums.";

	var ERROR_MSG_INVALID_EMAIL_RECIPIENT = "The recipient's email address entered is invalid.";
	var ERROR_MSG_REQUIRED_EMAIL_RECIPIENT = "Please enter the recipient's email address.";

	var ERROR_MSG_REQUIRED_TYPE_IN_GROCERY_LIST = "You have not entered any groceries. Please enter your grocery list into the text area."	
	var ERROR_MSG_REQUIRED_TYPE_IN_GROCERY_LIST_E_GROCERY_LIST = ERROR_MSG_REQUIRED_TYPE_IN_GROCERY_LIST + "\n Make sure that you have saved your grocery list by clicking 'Save >>'";

	function getErrorMsgPasswordLength(message)
	{
		return "Your " + message + ERROR_MSG_PASSWORD_LENGTH;
	}

	function changeColor(field)
	{
		field.style.color = ERROR_FIELD_COLOR;
	}

	/* ======================== START of validation functions ===================*/

	/*********************
	** check the specified field for empty string and test null
	**********************/
//	function validateRequired(field, fieldDescription, alerttxt)	
	function validateRequired(field, alerttxt)
	{
		with (field)
		{
			if (isEmpty(value))
			{
				alert(alerttxt);
//				changeColor(fieldDescription);
				return false;
			}
			else 
			{
				return true;
			}
		}
	}

	/*********************
	** check the specified drop down element to see if the an option was selected
	** -1 implies that the default option was selected, therefore the user did not select an option
	**********************/
	function validateRequiredDropDown(field, alerttxt)
	{
		with (field)
		{
			if (field.value == -1)
			{
				alert(alerttxt);
				return false;
			}
			else 
			{
				return true;
			}
		}
	}

	/*********************
	** check the entered field's length
	**********************/
	function validateMaxLength(field, textName, maxLength)
	{
		with (field)
		{
			var trimmedFieldValue = trimString(value);

			if (trimmedFieldValue.length > maxLength)
			{
				alert(textName + " cannot be more than " + maxLength + " characters.");
				return false;
			}
			else 
			{
				return true;
			}
		}
	}

	/*********************
	** check to make sure that the email entered is of proper format, includes "@" and "."
	**********************/
	function validateEmail(field, alerttxt)
	{
		with (field)
		{
			apos=value.indexOf("@")
			dotpos=value.lastIndexOf(".")
			if (apos<1||dotpos-apos<2) 
			{
				alert(alerttxt);
				return false;
			}
			else 
			{
				return true;
			}
		}
	}

	/*********************
	** check the specified password field, password length must be betweeen 6 and 10 characters
	**********************/
	function validatePassword(field, alerttxt)
	{
		with (field)
		{
			var trimmedFieldValue = trimString(field.value);
			if (trimmedFieldValue.length < 6 || trimmedFieldValue.length > 14)
			{
				alert(alerttxt);
				return false;
			}
			else 
			{
				return true;
			}
		}
	}

	/*********************
	** check to make sure that the values entered in the two given fields are the same
	*********************/
	function validateRetype(aVar, aVarRetype, message)
	{
		if (aVar.value == aVarRetype.value)
		{
			return true;
		}
		alert(message);
		return false;
	}

	/*********************
	** validate format of postal code
	*********************/
	function validatePostalCode(aVar)
	{
		var myReg = new RegExp("^[a-zA-Z]\\d[a-zA-Z]\\d[a-zA-Z]\\d$");

		var postalCode = aVar.value;
		// get rid of any space between postal code parts
		postalCode = postalCode.replace(" ", "");
		// get rid of any - between postal code parts
		postalCode = postalCode.replace("-", "");

		if (myReg.test(postalCode))
		{
			return true;
		}

		alert(ERROR_MSG_INVALID_POSTAL_CODE);
		return false;
	}

	/****************************************************
	** default.asp AND eGroceryList.asp
	** validates that the user has selected at least one grocery store to be included in their search
	** @param	pageName	the name of the page since it could be default.asp or eGroceryList.asp
	****************************************************/
	function atLeastOneStore(pageName)
	{
		var GROCERY_STORE_SELECTION_INDEX_NAME = "#groceryStoreSelection"
		var loblaws = document.getElementById('loblawsCB').checked;
		var nofrills = document.getElementById('nofrillsCB').checked;
		var foodbasics = document.getElementById('foodbasicsCB').checked;
//		var dominion = document.getElementById('dominionCB').checked;
		var pricechopper = document.getElementById('pricechopperCB').checked;

		// determine which stores are checked
		if (loblaws == true) return true;
		if (nofrills == true) return true;
		if (foodbasics == true) return true;
//		if (dominion == true) return true;
		if (pricechopper == true) return true;

		// no stores were selected by the user
		alert("You have not selected any grocery stores to be included in your search. Please make your selection at the 'Grocery store selection' section.");
		// focus on the grocery store selection after this message
		window.open(pageName + GROCERY_STORE_SELECTION_INDEX_NAME, "_self")

		return false;
	}

	/* ======================== END of validation functions ===================*/


// TODO: delete
	// check login password at the beginning, -------delete later
	function checkAuthorization()
	{
		var message = "Please enter your login name and password";
		var returnValue = prompt(message);

		if (returnValue == "bunbao")
		{
			window.location="default.asp";
		}
		else
		{
			window.location="t.html";
		}
	}

	/* ======================== START of FORM validations ===================*/
// TODO: delete
	/*********************
	** loginGrocerySavings.asp - validates login process
	** check to see that the username and password values are not empty
	** @returns	returns whether the username and password values are filled in
	*********************/
	function checkLogin(myForm)
	{
		// dom element id name in the loginGrocerySavings.asp page for displaying error messages
		var DOM_LOGIN_MSG = "loginMsg";
		var tag = window.document.getElementById(DOM_LOGIN_MSG);

		// grab the username from the login form on the left
		if (myForm.username.value == "")
		{
			tag.innerHTML = "Please enter your Username.";
			return false;
		} 
		// grab the username's password from the login form on the left
		else if(myForm.password.value == "")
		{
			tag.innerHTML = "Please enter your Password.";
			return false;
		}

		return true;
	}

// TODO: delete
	/*********************
	** profile.asp - validates logout process
	** Process logout by prompting a confirmation box to the user.
	** Okay logs the user out
	*********************/
	function checkLogout(myForm)
	{
		var name = confirm("Are you sure you want to logout?")
		if (name == true)
		{
			return true;;
		}
		return false;		
	}


	/*********************
	** Contact.asp - validates the fields on this form
	** validate the fields in the contact form
	*********************/
	function validateContactForm(thisform)
	{
		with (thisform)
		{
			if (validateEmail(emailFrom, "Your email address entered is invalid, please click on 'use default' if you would not like to supply your email.") == false)
			{
				emailFrom.focus();
				return false;
			}
			else if (validateRequired(subject, ERROR_MSG_EMAIL_SUBJECT) == false)
			{
				subject.focus();
				return false;
			}
			else if (validateRequired(message, ERROR_MSG_EMAIL_BODY) == false)
			{
				message.focus();
				return false;
			}
		}
		return true;
	}

	/*********************
	** sendProblem.asp - validates the fields on this form
	** validate the fields in the send problem form
	*********************/
	function validateFormSendProblem(thisForm)
	{
		with (thisForm)
		{
			if (validateRequired(emailFrom, ERROR_MSG_REQUIRED_EMAIL_ADDRESS) == false)
			{
				emailFrom.focus();
				return false;
			}
			else if (validateEmail(emailFrom, ERROR_MSG_INVALID_EMAIL_ADDRESS) == false)
			{
				emailFrom.focus();
				return false;
			}
			else if (validateRequired(subject, ERROR_MSG_EMAIL_SUBJECT) == false)
			{
				subject.focus();
				return false;
			}
			else if (validateRequired(message, ERROR_MSG_EMAIL_BODY) == false)
			{
				message.focus();
				return false;
			}
		}
		return true;
	}

	/*********************
	** Recommend.asp - validates the fields on this form
	** validate the fields in the "email site to a friend" form in the recommend page
	*********************/
	function validateRecommendForm(thisform)
	{
		var MEMBER_NOT_LOGGED_IN = 1;
		var memberID = document.getElementById("memberID");

		with (thisform)
		{
			if (validateRequired(recipientEmail, ERROR_MSG_REQUIRED_EMAIL_RECIPIENT) == false)
			{
				recipientEmail.focus();
				return false;
			}
			else if (validateEmail(recipientEmail, ERROR_MSG_INVALID_EMAIL_RECIPIENT) == false)
			{
				recipientEmail.focus();
				return false;
			}
			else if (validateRequired(senderName, ERROR_MSG_REQUIRED_NAME) == false)
			{
				senderName.focus();
				return false;
			}
			else if (validateRequired(senderEmail, ERROR_MSG_REQUIRED_EMAIL_ADDRESS) == false)
			{
				senderEmail.focus();
				return false;
			}
			else if (validateEmail(senderEmail, ERROR_MSG_INVALID_EMAIL_ADDRESS) == false)
			{
				senderEmail.focus();
				return false;
			}
		}
		return true;
	}

	/*********************
	** printGroceryList.asp
	** validate the fields needed to send the generated grocery list via email to someone
	*********************/
	function validateSendEmailWithGroceryList(thisForm)
	{
		with (thisForm)
		{
			if (validateRequired(emailFrom, ERROR_MSG_REQUIRED_EMAIL_ADDRESS) == false)
			{
				emailFrom.focus();
				return false;
			}
			else if (validateEmail(emailFrom, ERROR_MSG_INVALID_EMAIL_ADDRESS) == false)
			{
				emailFrom.focus();
				return false;
			}
			else if (validateRequired(emailTo, ERROR_MSG_REQUIRED_EMAIL_RECIPIENT) == false)
			{
				emailTo.focus();
				return false;
			}
			else if (validateEmail(emailTo, ERROR_MSG_INVALID_EMAIL_RECIPIENT) == false)
			{
				emailTo.focus();
				return false;
			}
/*
not sure if you need a subject and name of the sender
			else if (validateRequired(subject, ERROR_MSG_EMAIL_SUBJECT) == false)
			{
				subject.focus();
				return false;
			}
*/
		}
		return true;
	}

	var ERROR_ALERT_COLOR = "orange";
	var NO_ERROR_COLOR = "black";

	// set the color of an element to error color
	function setDescriptionErrorColor(domElement)
	{
//		domElement.style.color = ERROR_ALERT_COLOR;
		domElement.className = "error";
	}

	// set the color of an element to no error (white)
	function setDescriptionNoErrorColor(domElement)
	{
//		domElement.style.color = NO_ERROR_COLOR;
		domElement.className = "";
	}

	// clear all error colors 
	function clearAllDescriptionErrorColor(thisForm)
	{
		var fNameDescr = document.getElementById("fNameDescr");
		var favoriteGroceryStoreDescr = document.getElementById("favoriteGroceryStoreDescr");
		var shoppingPerWeekDescr = document.getElementById("shoppingPerWeekDescr");
		var spendingPerWeekDescr = document.getElementById("spendingPerWeekDescr");
		var genderDescr = document.getElementById("genderDescr");
		var ageDescr = document.getElementById("ageDescr");
		var postalCodeDescr = document.getElementById("postalCodeDescr");
		var emailDescr = document.getElementById("emailDescr");
		var usernameDescr = document.getElementById("usernameDescr");
		var retypeEmailDescr = document.getElementById("retypeEmailDescr");
		var aPasswordDescr = document.getElementById("aPasswordDescr");
		var retypePasswordDescr = document.getElementById("retypePasswordDescr");

		with (thisForm)
		{
			setDescriptionNoErrorColor(fNameDescr);
			setDescriptionNoErrorColor(favoriteGroceryStoreDescr);
			setDescriptionNoErrorColor(shoppingPerWeekDescr);
			setDescriptionNoErrorColor(spendingPerWeekDescr);
			setDescriptionNoErrorColor(genderDescr);
			setDescriptionNoErrorColor(ageDescr);
			setDescriptionNoErrorColor(postalCodeDescr);
			setDescriptionNoErrorColor(emailDescr);
			setDescriptionNoErrorColor(usernameDescr);
			setDescriptionNoErrorColor(retypeEmailDescr);
			setDescriptionNoErrorColor(aPasswordDescr);
			setDescriptionNoErrorColor(retypePasswordDescr);
		}
	}

	/*********************
	** Register.asp
	** validate the fields in the "registration" form in the registration page
	*********************/
	function validateRegisterForm(thisForm)
	{
		// clear all error colors first, then assign color based on errors
		clearAllDescriptionErrorColor(thisForm);

		var fNameDescr = document.getElementById("fNameDescr");
		var favoriteGroceryStoreDescr = document.getElementById("favoriteGroceryStoreDescr");
		var shoppingPerWeekDescr = document.getElementById("shoppingPerWeekDescr");
		var spendingPerWeekDescr = document.getElementById("spendingPerWeekDescr");
		var genderDescr = document.getElementById("genderDescr");
		var ageDescr = document.getElementById("ageDescr");
		var postalCodeDescr = document.getElementById("postalCodeDescr");
		var emailDescr = document.getElementById("emailDescr");
		var usernameDescr = document.getElementById("usernameDescr");
		var retypeEmailDescr = document.getElementById("retypeEmailDescr");
		var aPasswordDescr = document.getElementById("aPasswordDescr");
		var retypePasswordDescr = document.getElementById("retypePasswordDescr");

		with (thisForm)
		{
			if (validateRequired(fName, ERROR_MSG_REQUIRED_NAME) == false)
			{
				setDescriptionErrorColor(fNameDescr);
				fName.focus();
				return false;
			}
			else if (validateRequiredDropDown(favoriteGroceryStore, ERROR_MSG_REQUIRED_FAV_GROCERY_STORE) == false)
			{
				setDescriptionErrorColor(favoriteGroceryStoreDescr);
				favoriteGroceryStore.focus();
				return false;
			}
			else if (validateRequiredDropDown(shoppingPerWeek, ERROR_MSG_REQUIRED_SHOP_PER_WEEK) == false)
			{
				setDescriptionErrorColor(shoppingPerWeekDescr);
				shoppingPerWeek.focus();
				return false;
			}
			else if (validateRequiredDropDown(spendingPerWeek, ERROR_MSG_REQUIRED_SPEND_PER_WEEK) == false)
			{
				setDescriptionErrorColor(spendingPerWeekDescr);
				spendingPerWeek.focus();
				return false;
			}
			else if (validateRequiredDropDown(gender, ERROR_MSG_REQUIRED_GENDER) == false)
			{
				setDescriptionErrorColor(genderDescr);
				gender.focus();
				return false;
			}
			else if (validateRequiredDropDown(age, ERROR_MSG_REQUIRED_AGE_GROUP) == false)
			{
				setDescriptionErrorColor(ageDescr);
				age.focus();
				return false;
			}

			else if (validateRequired(postalCode, ERROR_MSG_REQUIRED_POSTAL_CODE) == false)
			{
				setDescriptionErrorColor(postalCodeDescr);
				postalCode.focus();
				return false;
			}
			else if (validatePostalCode(postalCode) == false)
			{
				setDescriptionErrorColor(postalCodeDescr);
				postalCode.focus();
				return false;
			}
			else if (validateRequired(username, ERROR_MSG_REQUIRED_USER_NAME) == false)
			{
				setDescriptionErrorColor(usernameDescr);
				username.focus();
				return false;
			}
			else if (validateRequired(email, ERROR_MSG_REQUIRED_EMAIL_ADDRESS) == false)
			{
				setDescriptionErrorColor(emailDescr);
				email.focus();
				return false;
			}
			else if (validateEmail(email, ERROR_MSG_INVALID_EMAIL_ADDRESS) == false)
			{
				setDescriptionErrorColor(emailDescr);
				email.focus();
				return false;
			}
			else if (validateRetype(email, retypeEmail, "The retype email and original email do not match.") == false)
			{
				setDescriptionErrorColor(retypeEmailDescr);
				retypeEmail.focus();
				return false;
			}
			if (validateRequired(aPassword, "Please enter your password.") == false)
			{
				setDescriptionErrorColor(aPasswordDescr);
				aPassword.focus();
				return false;
			}
			else if (validatePassword(aPassword, "Your password length must be between 6 and 14 characters long. Please try another.") == false)
			{
				setDescriptionErrorColor(aPasswordDescr);
				aPassword.focus();
				return false;
			}
			else if (validateRetype(aPassword, retypePassword, "The retype password and original password do not match.") == false)
			{
				setDescriptionErrorColor(retypePasswordDescr);
				retypePassword.focus();
				return false;
			}
			else if (disclaimer.checked == false)
			{
				alert("You must agree to the Terms of Use and Privacy Policy in order to register.")
				disclaimer.focus();
				return false;
			}
		}

		// hash the password
		var pw = document.getElementById('aPassword');
		// get the password for sha256 before md5 hashing it
		var forumPassword = pw.value;
		var hash = hex_md5(pw.value);
		pw.value = hash;

		// hash the forum password
		var domForumPassword = document.getElementById('forumPassword');
		domForumPassword.value = SHA256(forumPassword);

		return true;
	}

	/*********************
	** loginGrocerySavings.asp
	** validate the email field in the "loginGrocerySavings.asp" page where to send forgotten password
	*********************/
	function validateForgottenPW(thisform)
	{
		var domElement;
		with (thisform)
		{
			if (validateRequired(email, ERROR_MSG_REQUIRED_EMAIL_ADDRESS) == false)
			{
				email.focus();
				return false;
			}
			else if (validateEmail(email, ERROR_MSG_INVALID_EMAIL_ADDRESS) == false)
			{
				email.focus();
				return false;
			}
			domElement = email;
		}
		return true;
	}
	
	/*********************
	** eGroceryList.asp
	** validates to see if there is a saved typed in grocery list before the user can do a quicksearch
	*********************/
	function validateOption1QuickSearch(aForm)
	{
		with (aForm)
		{
			if (validateRequired(typedInGroceryList, "You have not entered your grocery list, please create a grocery list by either typing it in or uploading a file and then clicking save.") == false)
			{
				return false;
			}
		}
		return true;
	}

	/*********************
	** eGroceryList.asp
	** validates to see if there is a saved general grocery list before the user can do a quicksearch
	*********************/
	function validateOption2QuickSearch(aForm)
	{
		with (aForm)
		{
			if (validateRequired(searchByKeywords, "You have not selected any groceries, please make your selection by clicking on your desired groceries and clicking save") == false)
			{
				return false;
			}
		}
		return true;
	}

	/*********************
	** validate that user has typed in a grocery name to search that is at least 3 characters long
	*********************/
	function validateGroceryPriceBookHistory()
	{
		enteredGroceryName = document.getElementById("enteredGroceryName");
		
		if (validateRequired(enteredGroceryName, "You have not entered a grocery to search, please enter a single grocery to search.") == false)
		{
			enteredGroceryName.focus();
			return false;
		}

		var groceryNameLength = enteredGroceryName.value.length;
		if (groceryNameLength < 3)
		{
			alert("The grocery name entered contains " + groceryNameLength + " character(s).  Please provide a grocery name with at least 3 characters.");
			enteredGroceryName.focus();
			return false;
		}

		// XSS prevention
		enteredGroceryName.value = removeBad(trimString(enteredGroceryName.value));
		enteredGroceryName.value

		return true;
	}

	/*********************
	** validate that user has typed in a grocery name to search that is at least 3 characters long
	*********************/
	function validateCanadaOnlineGroceryPriceBook(thisForm)
	{
		enteredGroceryName = document.getElementById("enteredGroceryName");

		with (thisForm)
		{
			if (validateRequiredDropDown(province, "Please select a province") == false)
			{
				province.focus();
				return false;
			}

			else if (validateRequired(enteredGroceryName, "You have not entered a grocery to search, please enter a single grocery to search.") == false)
			{
				enteredGroceryName.focus();
				return false;
			}

			var groceryNameLength = enteredGroceryName.value.length;
			if (groceryNameLength < 3)
			{
				alert("The grocery name entered contains " + groceryNameLength + " character(s).  Please provide a grocery name with at least 3 characters.");
				enteredGroceryName.focus();
				return false;
			}

			// XSS prevention
			enteredGroceryName.value = removeBad(trimString(enteredGroceryName.value));
			enteredGroceryName.value

			return true;
		}
	}

	/* ======================== END of form validations ===================*/


	/* ======================== START of MEMBER form validations ===================*/

	/*********************
	** emailPreferences.asp 
	** validate the update email preferences Form in the user's profile
	*********************/
	function validateFormUpdateEmailPreferences(thisForm)
	{
		with (thisForm)
		{
			// if email is empty then do not check the format of it
//@@			if (trimString(newEmail.value) != "")
			if (!(isEmpty(newEmail.value)))
			{
				if (validateEmail(newEmail, "The new email that you've entered is invalid.") == false)
				{
					newEmail.focus();
					return false;
				}
				else if (validateRetype(newEmail, confirmNewEmail,  "The retype email and original email do not match.") == false)
				{
					confirmNewEmail.focus();
					return false;
				}
			}
		}

		return true;
	}

	/*********************
	** personalInformation.asp 
	** validate the fields on this page
	*********************/
	function validateFormPersonalInformation(thisForm)
	{
		with (thisForm)
		{
			if (validateRequired(firstName, ERROR_MSG_REQUIRED_NAME) == false)
			{
				firstName.focus();
				return false;
			}
			else if (validateRequiredDropDown(favoriteGroceryStore, ERROR_MSG_REQUIRED_FAV_GROCERY_STORE) == false)
			{
				favoriteGroceryStore.focus();
				return false;
			}
			else if (validateRequiredDropDown(shoppingPerWeek, ERROR_MSG_REQUIRED_SHOP_PER_WEEK) == false)
			{
				shoppingPerWeek.focus();
				return false;
			}
			else if (validateRequiredDropDown(spendingPerWeek, ERROR_MSG_REQUIRED_SPEND_PER_WEEK) == false)
			{
				spendingPerWeek.focus();
				return false;
			}
			else if (validateRequiredDropDown(gender, ERROR_MSG_REQUIRED_GENDER) == false)
			{
				gender.focus();
				return false;
			}
			else if (validateRequiredDropDown(age, ERROR_MSG_REQUIRED_AGE_GROUP) == false)
			{
				age.focus();
				return false;
			}
			else if (validateRequired(postalCode, ERROR_MSG_REQUIRED_POSTAL_CODE) == false)
			{
				postalCode.focus();
				return false;
			}
			else if (validatePostalCode(postalCode) == false)
			{
				postalCode.focus();
				return false;
			}
		}
		return true;
	}


	/*********************
	** changePassword.asp 
	** validate the change password Form in the user's profile
	*********************/
	function validateFormChangePassword(thisForm)
	{
		with (thisForm)
		{
			if (validateRequired(oldPassword, "Please enter your old password.") == false)
			{
				oldPassword.focus();
				return false;
			}
			else if (validatePassword(oldPassword, getErrorMsgPasswordLength("old password")) == false)
			{
				oldPassword.focus();
				return false;
			}
			else if (validateRequired(password, "Please enter your new password.") == false)
			{
				password.focus();
				return false;
			}
			else if (validatePassword(password, getErrorMsgPasswordLength("new password")) == false)
			{
				password.focus();
				return false;
			}
			else if (validateRequired(confirmPassword, "Please re-enter your new password.") == false)
			{
				confirmPassword.focus();
				return false;
			}
			else if (validateRetype(password, confirmPassword,  "The new password and retyped password do not match.") == false)
			{
				confirmPassword.focus();
				return false;
			}
		}

		// hash the new password
		var oldPw = document.getElementById('oldPassword');
		var hash = hex_md5(oldPw.value);
		oldPw.value = hash;

		// hash the new password
		var pw = document.getElementById('password');
		// get the password for sha256 before md5 hashing it
		var forumPassword = pw.value;
		var hash = hex_md5(pw.value);
		pw.value = hash;

		// hash the forum password
		var domForumPassword = document.getElementById('forumPassword');
		domForumPassword.value = SHA256(forumPassword);

		return true;
	}

	/*********************
	** eGroceryList.asp AND default.asp - validates the text area field
	*********************/
	function validateTypeInGroceryList(whichPage, domElement)
	{
		var DEFAULT = 1;
		var E_GROCERY_LIST = 2;

		// handle error msg differently depending on which page we are validating
		if (whichPage == DEFAULT)
		{
			if (validateRequired(domElement, ERROR_MSG_REQUIRED_TYPE_IN_GROCERY_LIST) == false)
			{
				domElement.focus();
				return false;
			}

		}
		else
		{
			if (validateRequired(domElement, ERROR_MSG_REQUIRED_TYPE_IN_GROCERY_LIST) == false)
			{
				domElement.focus();
				return false;
			}
		}

		// validate the entered groceries to see if they contain any single quotes (')
		// more than a single quote would be a vialotion
		var tempArray = domElement.value.split(" ");
		if (tempArray.length > 0)
		{
			for (x=0; x<tempArray.length; x++)
			{
//				var index1 = tempArray[x].indexOf('\'');
//				var index2 = tempArray[x].indexOf('\'', index1 + 1);
				var index = tempArray[x].indexOf("\'\'");
				if (index != -1)
				{
					alert("The grocery list entered is invalid, a grocery cannot contain more than two single quotes (\'). Please try again.");
					domElement.focus();
					return false;
				}
			}
		}

		// validate that each entered grocery is at least 2 character long
		var tempArray = domElement.value.split(",");
		var tempGrocery = "";
		var lastGroceryArrayIndex = tempArray.length - 1;

		for (x=0; x<tempArray.length; x++)
		{
			tempGrocery = trimString(tempArray[x]);

			// check to see if this is the last grocery in the entered grocery list
			if (x == lastGroceryArrayIndex)
			{
				// check to see if user accidentally added an ending ","
				if (tempGrocery == "")
				{
//alert(domElement.value);
					break;
				}
			}

			// check grocery name lengths, must be > 2
			if (tempGrocery.length < 2)
			{			
				alert("The grocery list contains invalid grocery names, a grocery name must be at least 2 characters long. Please try again.");
				domElement.focus();
				return false;
			}
		}
		
		return true;
	}

	/*********************
	** eGroceryList.asp - validates option 1 form (typing in grocery list)
	** calls validateTypeInGroceryList() to validate the text area field
	*********************/
	function validateFormTypeInGroceryList(thisForm)
	{
		with (thisForm)
		{
			if (validateTypeInGroceryList(2, typeInGroceryList) == false)
				return false;
		}
		return true;
	}

	/*********************
	** eGroceryList.asp - validates option 1 by uploaded file containing grocery list
	** check to see that the a path of the file exists
	*********************/
	function validateFormUploadedGroceryList(thisForm)
	{
		with (thisForm)
		{
			var uploadedFilePath = groceryListFile.value;

			// check that the file path is not empty
			if (trimString(uploadedFilePath) == "")
			{
				alert("You have not specified the location of your grocery list file. Please locate your grocery list file by clicking on 'Browse'.");				
				document.getElementById('groceryListFile').focus();
				return false;
			} 

			// regular expression to test that the user has selected a file on their hard drive
			// eg. C:
			var filePathRegex = /[A-Za-z]+:/;
			if (!uploadedFilePath.match(filePathRegex))
			{
				alert("You need to specify the location of your grocery list file on your hard drive. Please locate your grocery list file by clicking on 'Browse'.");
				return false;
			}
		}
		return true;
	}

	function validateBlogForm(thisForm)
	{
		with (thisForm)
		{
			if (validateRequired(name, "Please fill in your name.") == false)
			{
				name.focus();
				return false;
			}
			else if (validateMaxLength(name, "Your name", 30) == false)
			{
				name.focus();
				return false;
			}
			else if (validateMaxLength(title, "The title", 50) == false)
			{
				title.focus();
				return false;
			}
			else if (validateRequired(comment, "Please fill in your comment.") == false)
			{
				comment.focus();
				return false;
			}
			else if (validateMaxLength(comment, "Your comment", 2000) == false)
			{
				comment.focus();
				return false;
			}

			addComment.value = "true";
			return true;
		}
	}

	/* ======================== END of MEMBER form validations ===================*/
