function clear(field) {
	field.value = "";
}

function popBigWindow(url) {
	return popWindowXY(url, 1000, 700);
}

function popWindowTrue(url) {
	popWindow(url);
	return true;
}

function popWindow(url) {
	return popWindowXY(url, 600, 600);
}

function popWindowXY(url, width, height) {
	var popupWindow = window.open(url, "editPop", "width=" + width + ",height=" + height + ",resizable=1,scrollbars=1");
	popupWindow.focus();
	return false;
}

// form validation

function checkEmail(email) {
	var emailRegExp = /^[^@]+@[^@]+\.[^@]+$/;
	return emailRegExp.test(email);
}

function checkName(name) {
	var nameRegExp = /^.+ .+$/;
	return nameRegExp.test(name);
}

function checkString(string) {
	return string.length > 1;
}

function checkZipCode(zipCode) {
	var zipCodeRegExp = /^[0-9]{5}$/;
	return zipCode == "" || zipCodeRegExp.test(zipCode);
}

function checkState(state, country) {
	return state.length == 2 || country != "United States";
}

function checkDate(month, day) {
	return month < 11 || day <= 4;
}

function checkUrl(url) {
	var urlRegExp = /^http:\/\/[\w\.\-]+\/.+/;
	return url == "" || urlRegExp.test(url);
}

function checkPassword(password) {
	var passwordRegExp = /^\w{5,20}$/;
	return passwordRegExp.test(password);
}

function checkRadio(radio) {
	for (var i = 0; i < radio.length; i++)
		if (radio[i].checked)
			return true;
	return false;
}

function verifyUser(myform, requirePassword) {
	var goodName = checkName(myform.name.value);
	var goodEmail = checkEmail(myform.email.value);
	var goodReferredByName = myform.referredByName.value == "" || checkName(myform.referredByName.value);
	var goodReferredByEmail = myform.referredByEmail.value == "" || checkEmail(myform.referredByEmail.value);
	if (goodName && goodEmail && goodReferredByName && goodReferredByEmail)
		return verifyNewPassword(myform, requirePassword);
	else {
		var problems = "Your submission was invalid:";
		if (!goodName) problems += "\nYou must enter your first and last name.";
		if (!goodEmail) problems += "\nYou did not enter a valid email address.";
		if (!goodReferredByName) problems += "\nIf you enter the person who referred you, you must supply both first and last name.";
		if (!goodReferredByEmail) problems += "\nIf you enter the person who referred you, you must supply a valid email address.";
		alert(problems);
		return false;
	}
}

function verifyResetPassword(myform) {
	var goodEmail = checkEmail(myform.email.value);
	var confirmEmail = myform.email.value == myform.emailConfirm.value;
	if (goodEmail && confirmEmail)
		return true;
	else {
		var problems = "Your submission was invalid:";
		if (!goodEmail) problems += "\nYou did not enter a valid email address.";
		if (!confirmEmail) problems += "\nThe email addresses you entered did not match.";
		alert(problems);
		return false;
	}
}	

function verifyNewPassword(myform, requirePassword) {
	var goodPassword = checkPassword(myform.password.value) || (!requirePassword && myform.password.value == '');
	var confirmPassword = myform.password.value == myform.passwordConfirm.value;
	var goodGender = checkRadio(myform.gender);

	if (goodPassword && confirmPassword && goodGender)
		return true;
	else {
		var problems = "Your submission was invalid:";
		if (!goodPassword) problems += "\nYour password must be at least 5 characters and no more than 20 characters and it can only contain letters, numbers, and underscores.";
		if (!confirmPassword) problems += "\nThe passwords you entered did not match.";
		if (!goodGender) problems += "\nYou did not specify male or female.";
		alert(problems);
		return false;
	}
}

function verifyAddPerson(myform, personType) {
	var goodName = checkName(myform.name.value);
	var goodEmail = checkEmail(myform.email.value);
	var confirmEmail = myform.email.value == myform.emailConfirm.value;

	if (goodName && goodEmail && confirmEmail)
		return true;
	else {
		var problems = "Your submission was invalid:";
		if (!goodName) problems += "\nYou must enter the first and last name of the " + personType + ".";
		if (!goodEmail) problems += "\nYou did not enter a valid email address for the " + personType + ".";
		if (!confirmEmail) problems += "\nThe email addresses you entered did not match.";
		alert(problems);
		return false;
	}
}

function verifySignIn(myform) {
	var goodEmail = checkEmail(myform.email.value);
	var goodPassword = (myform.password.value != "");
	if (goodEmail && goodPassword)
		return true;
	else {
		var problems = "Invalid login attempt:";
		if (!goodEmail) problems += "\nYou did not enter a valid email address.";
		if (!goodPassword) problems += "\nYou did not enter a password.";
		alert(problems);
		return false;
	}
}

function verifyDinner(myform, newUser) {
	var goodName = checkString(myform.dinnerName.value);
	var goodCity = checkString(myform.city.value);
	var goodState = checkState(myform.state.value, myform.country.options[myform.country.selectedIndex].value);
	var goodZipCode = checkZipCode(myform.zipCode.value);
	var goodDate = checkDate(myform.month.options[myform.month.selectedIndex].value, myform.day.options[myform.day.selectedIndex].value);
	var goodImage = checkUrl(myform.image.value);
	if (goodName && goodCity && goodState && goodZipCode && goodDate && goodImage) {
		if (newUser)
			return verifyUser(myform, true);
		else
			return true;
	}
	else {
		var problems = "Your submission was invalid:";
		if (!goodName) problems += "\nYou must enter a name for the dinner.";
		if (!goodCity) problems += "\nYou must enter a valid city.";
		if (!goodState) problems += "\nYou must enter a valid state.";
		if (!goodZipCode) problems += "\nYou must enter a valid ZIP code or leave this field blank.";
		if (!goodDate) problems += "\nYou must enter a date no later than November 4th.";
		if (!goodImage) problems += "\nIf you enter an image, it must be a valid URL (e.g. http://www.site.com/images/picture.jpg).";
		alert(problems);
		return false;
	}
}

