//		phoneValidator:	Input: string 'phone' containing the phone number to be validated
//						Return: false if there is an error, true otherwise
//		This function returns a boolean: false if there is an error
//		in the phone number string, true otherwise
function phoneValidator(phone)
{
	if ((phone == null) || (phone == ""))
		return false;
	
	if ((phone.indexOf('"') != -1) || (phone.indexOf("'") != -1))
		return false;

	var checkOK = "0123456789";
	
	switch(phone.length)
	{
		case 8:
			if (phone.charAt(3) != '-')
				return false;
			
			if ((checkOK.indexOf(phone.charAt(0)) == -1) || (checkOK.indexOf(phone.charAt(1)) == -1)
				|| (checkOK.indexOf(phone.charAt(2)) == -1) || (checkOK.indexOf(phone.charAt(4)) == -1)
				|| (checkOK.indexOf(phone.charAt(5)) == -1) || (checkOK.indexOf(phone.charAt(6)) == -1)
				|| (checkOK.indexOf(phone.charAt(7)) == -1))
				return false;

			return true;
		
		case 12:
			if ((phone.charAt(3) != '-') || (phone.charAt(7) != '-'))
				return false;
			
			if ((checkOK.indexOf(phone.charAt(0)) == -1) || (checkOK.indexOf(phone.charAt(1)) == -1)
				|| (checkOK.indexOf(phone.charAt(2)) == -1) || (checkOK.indexOf(phone.charAt(4)) == -1)
				|| (checkOK.indexOf(phone.charAt(5)) == -1) || (checkOK.indexOf(phone.charAt(6)) == -1)
				|| (checkOK.indexOf(phone.charAt(8)) == -1) || (checkOK.indexOf(phone.charAt(9)) == -1)
				|| (checkOK.indexOf(phone.charAt(10)) == -1) || (checkOK.indexOf(phone.charAt(11)) == -1))
				return false;

			return true;

		case 14:
			if ((phone.charAt(1) != '-') || (phone.charAt(5) != '-') || (phone.charAt(9) != '-'))
				return false;
			
			if ((checkOK.indexOf(phone.charAt(0)) == -1) || (checkOK.indexOf(phone.charAt(2)) == -1)
				|| (checkOK.indexOf(phone.charAt(3)) == -1) || (checkOK.indexOf(phone.charAt(4)) == -1)
				|| (checkOK.indexOf(phone.charAt(6)) == -1) || (checkOK.indexOf(phone.charAt(7)) == -1)
				|| (checkOK.indexOf(phone.charAt(8)) == -1) || (checkOK.indexOf(phone.charAt(10)) == -1)
				|| (checkOK.indexOf(phone.charAt(11)) == -1) || (checkOK.indexOf(phone.charAt(12)) == -1)
				|| (checkOK.indexOf(phone.charAt(13)) == -1))
				return false;

			return true;
		
		default:
			return false;
	}
}

//		emailValidator:	Input: string 'email' containing the email address to be validated
//						Return: false if there is an error, true otherwise
//		This function returns a boolean: false if there is an error
//		in the email address string, true otherwise
function emailValidator(email)
{

	if ((email == null) || (email == ""))
		return false;

	if ((email.indexOf('"') != -1) || (email.indexOf("'") != -1))
		return false;

	var atCharIndex = email.indexOf('@');
	var dotCharIndex = email.indexOf('.');

	if ((atCharIndex <= 0) || (atCharIndex == 0) || (dotCharIndex <= 2))
		return false;
		
	if ((email.indexOf('.com') == -1) && (email.indexOf('.org') == -1) && (email.indexOf('.net') == -1))
		return false;
	
	var checkOK = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789_-.";

	for(j = 0; j < atCharIndex; j++)
	{
		if (checkOK.indexOf(email.charAt(j)) == -1)
			return false;
	}

//	for(j = atCharIndex + 1; j < dotCharIndex; j++)
//	{
//		if (checkOK.indexOf(email.charAt(j)) == -1)
//			return false;
//	}

	return true;
}

//		zipValidator:	Input: string 'zip' containing the zip code to be validated
//						Return: false if there is an error, true otherwise
//		This function returns a boolean: false if there is an error
//		in the zip code string, true otherwise
function zipValidator(zip)
{
	if ((zip == null) || (zip == ""))
		return false;
	
	if ((zip.indexOf('"') != -1) || (zip.indexOf("'") != -1))
		return false;

	var checkOK = "0123456789";

	switch(zip.length)
	{
		case 5:
			for(j = 0; j < 5; j++)
			{
				if (checkOK.indexOf(zip.charAt(j)) == -1)
					return false;
			}
			return true;
			
		case 10:
			for(j = 0; j < 5; j++)
			{
				if (checkOK.indexOf(zip.charAt(j)) == -1)
					return false;
			}

			for(j = 6; j < 10; j++)
			{
				if (checkOK.indexOf(zip.charAt(j)) == -1)
					return false;
			}

			return true;

		default:
			return false;
	}
	
	return true;
}

//		modForm:	Input: array of field names from the form
//					Return: false if there is an error, true otherwise
//		This function returns a boolean: false if there is an error
//		in the appropriate string variable, true otherwise. It will also
//		create an alert with a message containing the error, if there is one
function modForm (formCheck)
{
	var fieldName, fieldValue, reqFields, reqFieldsAry;

	reqFields = formCheck._requiredFields.value
	reqFieldsAry = reqFields.split(",")
	reqFieldNames = formCheck._requiredFieldNames.value
	reqFieldNamesAry = reqFieldNames.split(",")
	
	for(i = 0; i < formCheck.elements.length; i++)
	{
		if (formCheck.elements[i].type == 'text' || formCheck.elements[i].type == 'password')
		{
			fieldName = formCheck.elements[i].name;
			fieldValue = formCheck.elements[i].value;
							
			var fieldNameLC = fieldName.toLowerCase();
			
			for(j = 0; j < reqFieldsAry.length; j++)
			{
				fieldShowName = reqFieldNamesAry[j]
				if (reqFieldsAry[j].toLowerCase() == fieldNameLC)
				{
					if ((fieldValue == null) || (fieldValue == ""))
					{
						alert("Please enter a value for the " + fieldShowName.toUpperCase() + " field.");
						formCheck.elements[i].focus();
						return false;
					}
					

					if (fieldNameLC.indexOf('phone') != -1)
					{
						if (phoneValidator(fieldValue) == false)
						{
							alert("Invalid phone number. Please use one of the following formats:\n\t1-234-567-8901\n\t123-456-7890\n\t123-4567");
							formCheck.elements[i].focus();
							return false;
						}
					}
					else if (fieldNameLC.indexOf('email') != -1)
					{
						if (emailValidator(fieldValue) == false)
						{
							alert("Invalid email address. Please use the 'name@domain' format, for example:\n\ttjones@rayindustries.org\n\tbillray89@myemaildomain.com\n\tthewiz@techwizards.net");
							formCheck.elements[i].focus();
							return false;
						}
					}
					else if (fieldNameLC.indexOf('zip') != -1)
					{
						if (zipValidator(fieldValue) == false)
						{
							alert("Invalid zip code. Please use the '01234' or '98765-1234' format.");
							formCheck.elements[i].focus();
							return false;
						}
					}
					else
					{
						// do nothing
					}
					j = reqFieldsAry.length;
				}
			}
		}
	}

	return true;
}

function checkWine (formCheck)
{
	formError = false;
	formString = "";
	if (formCheck.wine_name.value == "")
	{
		formError = true;
		formString = formString + "Missing wine name.\n";
	}
	if (formCheck.wine_year.value == "")
	{
		formError = true;
		formString = formString + "Missing wine year.\n";
	}
	if (formCheck.wine_varietal.value == "select")
	{
		formError = true;
		formString = formString + "Missing wine varietal.\n";
	}
	if (formCheck.wine_type.value == "select")
	{
		formError = true;
		formString = formString + "Missing wine type.\n";
	}
	if (formCheck.wine_price.value == "" || isNaN(formCheck.wine_price.value))
	{
		formError = true;
		formString = formString + "Missing or invalid wine price.\n";
	}
	if (formCheck.wine_submitterrating.value == ""  || isNaN(formCheck.wine_submitterrating.value) || parseFloat(formCheck.wine_submitterrating.value) > 10 || parseFloat(formCheck.wine_submitterrating.value) < 7)
	{
		formError = true;
		formString = formString + "Missing or invalid wine rating.\n";
	}
	if (formCheck.buyagain[0].checked != true && formCheck.buyagain[1].checked != true)
	{
		formError = true;
		formString = formString + "Missing buy again option.\n";
	}
	
	if (formError == true)
	{
		alert(formString);
		return false		
	}
	else
	{
		return true;
	}
}

function checkPairing (formCheck)
{
	formError = false;
	formString = "";
	if (formCheck.pairing_title.value == "")
	{
		formError = true;
		formString = formString + "Missing pairing title.\n";
	}

	if (formError == true)
	{
		alert(formString);
		return false		
	}
	else
	{
		return true;
	}
}

function checkReg (formCheck)
{
	formError = false;
	formString = "";
	if (formCheck.reg_name.value == "" || formCheck.reg_confirmName.value == "")
	{
		formError = true;
		formString = formString + "Missing username.\n";
	}
	else if (formCheck.reg_name.value != formCheck.reg_confirmName.value)
	{
		formError = true;
		formString = formString + "Usernames do not match.\n";
	}
	
	
	
	if (formCheck.reg_email.value == "" || formCheck.reg_confirmEmail.value == "")
	{
		formError = true;
		formString = formString + "Missing email.\n";
	}
	else if (formCheck.reg_email.value != formCheck.reg_confirmEmail.value)
	{
		formError = true;
		formString = formString + "Emails do not match.\n";
	}	
	else if (emailValidator(formCheck.reg_email.value) == false || emailValidator(formCheck.reg_confirmEmail.value) == false)
	{
		formError = true;
		formString = formString + "Invalid email address.\n";
	}
	
		
	
	if (formCheck.reg_password.value == "" || formCheck.reg_confirmPassword.value == "")
	{
		formError = true;
		formString = formString + "Missing password.\n";
	}
	else if (formCheck.reg_password.value != formCheck.reg_confirmPassword.value)
	{
		formError = true;
		formString = formString + "Passwords do not match.\n";
	}	
	
	if (formCheck.birthmm.value == "" || formCheck.birthdd.value == ""  || formCheck.birthyyyy.value == "") 
	{
		formError = true;
		formString = formString + "Missing complete birth date.\n";
	}
	else
	{
		bMonth = parseInt(formCheck.birthmm.value);
		bDay =  parseInt(formCheck.birthdd.value);
		bYear =  parseInt(formCheck.birthyyyy.value);
		var myDate=new Date();
		myDate.setFullYear((bYear+21),(bMonth-1),bDay);
		var today = new Date();
		if (myDate>today)
		{
			formError = true;
			formString = formString + "You are NOT over 21. You must be over 21 to join this website.\n";
		}
	}
	
	
	if (formCheck.reg_firstName.value == "")
	{
		formError = true;
		formString = formString + "Missing first name.\n";
	}
	
	if (formCheck.reg_lastName.value == "")
	{
		formError = true;
		formString = formString + "Missing last name.\n";
	}

	if (formCheck.reg_state.options[formCheck.reg_state.selectedIndex].value != "" && formCheck.reg_zip.value == "")
	{
		formError = true;
		formString = formString + "Zip code is a required field for US and Canada.\n";
	}

	if (formCheck.reg_country.value == "" || formCheck.reg_country.value == "Country")
	{
		formError = true;
		formString = formString + "Missing country.\n";
	}
	if (formError == true)
	{
		alert(formString);
		return false		
	}
	else
	{
		return true;
	}
}
function checkEmailFormat(formCheck)
{
	formString = "";
	if (emailValidator(formCheck.email_address.value) == false)
	{
		alert('Invalid email address.');
		return false;
	}
	else
	{
		return true;
	}
	
}
function checkPassUpdate(formCheck)
{
	formError = false;
	formString = "";
	if (formCheck.acct_currentpw.value == "" || formCheck.acct_newpw.value == "" || formCheck.acct_confirmpw.value == "")
	{
		formError = true;
		formString = formString + "Missing one of the passwords.\n";
	}
	else if (formCheck.acct_newpw.value != formCheck.acct_confirmpw.value)
	{
		formError = true;
		formString = formString + "New passwords do not match.\n";
	}
	
	if (formError == true)
	{
		alert(formString);
		return false		
	}
	else
	{
		return true;
	}
}

function checkEmailUpdate(formCheck)
{
	formError = false;
	formString = "";

	if (formCheck.acct_currentEmail.value == "" || formCheck.acct_newEmail.value == "" || formCheck.acct_confirmEmail.value == "")
	{
		formError = true;
		formString = formString + "Missing one of the emails.\n";
	}
	else if (emailValidator(formCheck.acct_currentEmail.value) == false || emailValidator(formCheck.acct_newEmail.value) == false || emailValidator(formCheck.acct_confirmEmail.value) == false)
	{
		formError = true;
		formString = formString + "Invalid email address.\n";
	}
	else if (formCheck.acct_newEmail.value != formCheck.acct_confirmEmail.value)
	{
		formError = true;
		formString = formString + "Emails do not match.\n";
	}	
		
	
	if (formError == true)
	{
		alert(formString);
		return false		
	}
	else
	{
		return true;
	}
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function checkRecipe (formCheck)
{
	formError = false;
	formString = "";
	if (formCheck.recipe_name.value == "")
	{
		formError = true;
		formString = formString + "Missing recipe name.\n";
	}
	/*
	if (trim(formCheck.recipe_dishtype.options[formCheck.recipe_dishtype.selectedIndex].text) == "Any")
	{
		formError = true;
		formString = formString + "Missing recipe dish type.\n";
	}
	if (trim(formCheck.recipe_mainingredient.options[formCheck.recipe_mainingredient.selectedIndex].text) == "Any")
	{
		formError = true;
		formString = formString + "Missing recipe main ingredient.\n";
	}
	if (trim(formCheck.recipe_preptype.options[formCheck.recipe_preptype.selectedIndex].text) == "Any")
	{
		formError = true;
		formString = formString + "Missing recipe prep type.\n";
	}
	if (trim(formCheck.recipe_cuisinetype.options[formCheck.recipe_cuisinetype.selectedIndex].text) == "Any")
	{
		formError = true;
		formString = formString + "Missing recipe cuisine type.\n";
	}
	*/
	if (formCheck.recipe_ingredients.value == "")
	{
		formError = true;
		formString = formString + "Missing recipe ingredients.\n";
	}
	if (formCheck.recipe_directions.value == "")
	{
		formError = true;
		formString = formString + "Missing recipe directions.\n";
	}
	if (formCheck.recipe_preptime.value == "")
	{
		formError = true;
		formString = formString + "Missing recipe prep time.\n";
	}
	if (formCheck.recipe_totaltime.value == "")
	{
		formError = true;
		formString = formString + "Missing recipe total time.\n";
	}
	
	if (formCheck.recipe_servings.value == "" || isNaN(formCheck.recipe_servings.value))
	{
		formError = true;
		formString = formString + "Missing or invalid recipe servings.\n";
	}
	if (formCheck.recipe_submitterrating.value == ""  || isNaN(formCheck.recipe_submitterrating.value) || parseFloat(formCheck.recipe_submitterrating.value) > 10 || parseFloat(formCheck.recipe_submitterrating.value) < 7)
	{
		formError = true;
		formString = formString + "Missing or invalid recipe rating.\n";
	}
	if (formCheck.recipe_makeagain[0].checked != true && formCheck.recipe_makeagain[1].checked != true)
	{
		formError = true;
		formString = formString + "Missing make again option.\n";
	}
	
	if (formError == true)
	{
		alert(formString);
		return false		
	}
	else
	{
		return true;
	}
}
function openUploadWindow(uploadFile)
{
	uploadWin = window.open(uploadFile,'uploadWindow', 'left=20,top=20,width=328,height=173,toolbar=0,resizable=0,statusbar=0');

}
function checkRecipeRating(formCheck)
{
	formError = false;
	formString = "";
	if (formCheck.recipe_comments.value == "")
	{
		formError = true;
		formString = formString + "Missing recipe review comments.\n";
	}
	
	if (formError == true)
	{
		alert(formString);
		return false		
	}
	else
	{
		return true;
	}
}
function checkPairingItems(formCheck)
{
	formError = false;
	formString = "";
	checkedItems=0;
	if (eval(formCheck.recipeId))
	{
		if(formCheck.recipeId.length)
		{
			for (i = 0; i < formCheck.recipeId.length; i++)
			{
				if (formCheck.recipeId[i].checked==true)
				{ 
					checkedItems++;
				}
			}
			if (checkedItems == 0)
			{
				formError = true;
				formString = formString + "Please select at least one recipe.\n";
			}	
		}
		else if (formCheck.recipeId.checked == false)
		{
				formError = true;
				formString = formString + "Please select at least one recipe.\n";
		}
	}
	else
	{
		formError = true;
		formString = formString + "You need to add recipes to your favorites in order to create a pairing.\n";
	}
	checkedItems=0;
	if (eval(formCheck.wineId))
	{
		if(formCheck.wineId.length)
		{
			for (i = 0; i < formCheck.wineId.length; i++)
			{
				if (formCheck.wineId[i].checked==true)
				{ 
					checkedItems++;
				}
			}
			if (checkedItems == 0)
			{
				formError = true;
				formString = formString + "Please select at least one wine.\n";
			}	
		}
		else if (formCheck.wineId.checked == false)
		{
				formError = true;
				formString = formString + "Please select at least one wine.\n";
		}
	}
	else
	{
		formError = true;
		formString = formString + "You need to add wines to your favorites in order to create a pairing.\n";
	}
	
	if (formError == true)
	{
		alert(formString);
		return false		
	}
	else
	{
		return true;
	}
}
function checkAccountInfo(formCheck)
{
	formError = false;
	formString = "";
	if (formCheck.mem_firstname.value == "")
	{
		formError = true;
		formString = formString + "First name is a required field.\n";
	}
	
	if (formCheck.mem_lastname.value == "")
	{
		formError = true;
		formString = formString + "Last name is a required field.\n";
	}
	
	if (formCheck.mem_state.options[formCheck.mem_state.selectedIndex].value != "" && formCheck.mem_zip.value == "")
	{
		formError = true;
		formString = formString + "Zip code is a required field for US and Canada.\n";
	}
	
	if (formError == true)
	{
		alert(formString);
		return false		
	}
	else
	{
		return true;
	}
}
