// This function is for the menus in the drinks.php file (drinks.php)
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

// This function is going to check that the required fields are not empty and the email should be a valid email
// This is used in the Submit Drink Recipe (submit_drink.php)
function checkSubmit( myForm )
{
    // This checks to make sure you add a drink name
    if( myForm.drinkName.value == "" )
	  {
		    alert( "Please Enter A Drink Name!" );
		    myForm.drinkName.focus();
		    myForm.drinkName.select();
		    return false;
	  }
		// This checks to make sure you add the ingredients
		if( myForm.ingredients.value == "" )
	  {
		    alert( "Please Enter The Ingredients!" );
		    myForm.ingredients.focus();
		    myForm.ingredients.select();
		    return false;
	  }
		// This checks to make sure you add the instructions
		if( myForm.instructions.value == "" )
	  {
		    alert( "Please Enter The Instructions!" );
		    myForm.instructions.focus();
		    myForm.instructions.select();
		    return false;
	  }
		// This checks to make sure you add a category
		if( myForm.category.value == "" )
	  {
		    alert( "Please Select A Category!" );
		    myForm.category.focus();		    
		    return false;
	  }
		// This checks to make sure you add a glass
		if( myForm.glass.value == "" )
	  {
		    alert( "Please Select A Glass!" );
		    myForm.glass.focus();		   
		    return false;
	  }
	  
	  // This is going to check that you answered the question
	  if( myForm.question.value == "" )
	  {
		    alert( "Please Answer the Question!" );
		    myForm.question.focus();
		    myForm.question.select();
		    return false;
	  }	
		
    // This is going to check that the email is a valid email
  	re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	  // If the email address field is not empty make sure it is a valid email
	  if( myForm.email.value != "" )
	  {
		    // If the email address is valid do nothing
		    if( re.test( myForm.email.value ) ) 
		    {
		        // Do nothing
		    }
				else
				{
				    alert( "Invalid Email Address!\n\nExample: user@provider.com\n\nMake sure there are no spaces" );
			      	myForm.email.focus();
			      	myForm.email.select();
			      	return false;
				}
	  }	  
		
		// Everything when OK
		return true;
}

// This checks that the form is complete (email_recipe.php)
function checkEmailRecipe( myForm )
{
	// This is going to check that the email is a valid email
  	re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	
	// The email should not be empty
	if( myForm.recipient.value == "" )
	{
		alert( "Please Enter a Recipient's Email!" );
		myForm.recipient.focus();
		myForm.recipient.select();
		return false;
	}
	
	// If the email address field is not empty make sure it is a valid email
	if( myForm.recipient.value != "" )
	{
		// If the email address is valid do nothing
		if( re.test( myForm.recipient.value ) ) 
		{
			// Do nothing
		}
		else
		{
			alert( "Invalid Email Address!\n\nExample: user@provider.com\n\nMake sure there are no spaces" );
			myForm.recipient.focus();
			myForm.recipient.select();
			return false;
		}
	}	
	  
	// This makes sure there is a sender's name
	if( myForm.name.value == "" )
	{
		    alert( "Please Enter Your Name!" );
		    myForm.name.focus();
		    myForm.name.select();
		    return false;
	}
	
	// The email should not be empty  
	if( myForm.email.value == "" )
	{
		alert( "Please Enter Your Email!" );
		myForm.email.focus();
		myForm.email.select();
		return false;
	}
	  
	  // If the email address field is not empty make sure it is a valid email
	if( myForm.email.value != "" )
	{	
		// If the email address is valid do nothing
		if( re.test( myForm.email.value ) ) 
		{
			// Do nothing
		}
		else
		{
			alert( "Invalid Email Address!\n\nExample: user@provider.com\n\nMake sure there are no spaces" );
			myForm.email.focus();
			myForm.email.select();
			return false;
		}
	}	
	  
	// This is going to check that you answered the question
	if( myForm.question.value == "" )
	{
		alert( "Please Answer the Question!" );
		myForm.question.focus();
		myForm.question.select();
		return false;
	}
	
	// This checks to make sure that the email used in the recipient is not the same as the sender
	if( myForm.recipient.value == myForm.email.value )
	{
		alert( "The Recipient's Email is the same as Your Email!" );
		myForm.email.focus();
		myForm.email.select();
		return false;
	}
	
	// Everything when OK
    return true;
}

// ----------- My Favorites --------------------

// This functions adds a new recipe to the cookie
function addFavorite( drinkID )
{
	favorites = getCookie( 'favorites' );
	
	// Check for the Favorites cookie
	if( favorites != null && favorites != "" )
	{
		// Check for a duplicate drink in the Favorites cookie
		if( !findDrink( favorites, drinkID ) )
		{
			// If there is a cookie, add a comman and the drink ID
			favorites = favorites + "," + drinkID;
			// Set the new cookie with the new drink
			setFavorites( favorites );
		}
		else
		{
			alert( "This drink is already in your Favorites." );
		}
	}
	// If there is not Favorites Cookie, add one with the new drink
	else
  	{
		// Set the cookie with this drink (This is the first drink)
		setFavorites( drinkID );		
  	}
}

// This looks for a drink. Returns true if found the drink in the cookie, else false
function findDrink( favorites, drinkID )
{
	var found = false;
	// This splits the drinks and puts them in an array
	drinks = favorites.split( "," );
	
	// Check the whole array for a duplicate drink
	for( var i = 0; i < drinks.length; i++ )
	{
		if( drinks[ i ] == drinkID )
		{
			found = true;
		}
	}
	
	return found;	
}

// This function set the cookie with the favorites
function setFavorites( favorites )
{
	// This is the expiration date
	var expire = new Date();
	// The cookie should expire in four years
	expire.setFullYear( expire.getFullYear() + 4 );
	// We set the cookie with the name of favorites
	document.cookie = "favorites=" + escape( favorites ) + ";expires=" + expire.toUTCString();
	// Notify the user
	alert( "Your Favorites have been updated!" );
}

function deleteFavorites()
{
	// The favorites should be empty
	var favorites = "";
	// This is the expiration date
	var expire = new Date();
	// The date of expire should be yesterday
	expire.setDate( expire.getDate() - 1 );
	// We delete the cookie
	if( document.cookie != "" && confirm( "Do you want to delete your Favorites?" ) )
	{
		document.cookie = "favorites=" + favorites + ";expires=" + expire.toUTCString();
		// Reload the page so the drinks disappear
		window.location.reload();
	}
		
}

// This gets the cookie value of the given name. It not found, it returns the empty string
function getCookie( c_name )
{
	if( document.cookie.length > 0 )
    {
		c_start = document.cookie.indexOf( c_name + "=" );
		if( c_start != -1 )
		{
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf( ";", c_start );
			if( c_end == -1 )
			{
				c_end=document.cookie.length;
			}
			return unescape( document.cookie.substring( c_start, c_end ) );
    	}
  	}
	return "";
}

// This function finds the selected and deletes them
function deleteSelected( thisForm )
{
	// Get the list of drinks to delete
	var deleteList = drinksSelected( thisForm );
	
	// If the delete list is not empty
	if( deleteList.length > 0 )
	{
		// Get the favorites cookie
		favorites = getCookie( 'favorites' );
		
		// Check for the Favorites cookie
		if( favorites != null && favorites != "" )
		{
			// This splits the drinks in the cookie and puts them in an array
			drinks = favorites.split( "," );			
			// Go thru the drinks and if found remove them
			newDrinks = removeDrinks( drinks, deleteList );
			// Make the new cookie. This converts the array into a string to be stored in a cookie
			newFavorites = newDrinks.toString();
			// Check to make sure that the favorites is not empty
			if( newFavorites != null && newFavorites != "" )
			{
				// Set the new cookie
				setFavorites( newFavorites );
			}
			// If we deleted all the favorites, remove the cookie
			else
			{
				// This removes the cookie
				deleteFavorites();
			}
			// Reload the page so the drinks disappear
			window.location.reload();
			// Uncheck all the checkboxes
			uncheckAll( document.favoritesForm )				
		}
	}
}

// This function removes drinks from the cookie array
// Input: Takes the cookie array (drinks) and the delete List array (deleteList)
// Returns the new array with the remaining favorite drinks
function removeDrinks( drinks, deleteList )
{
	// Go thru the drinks and if found remove them
	for( var j = 0; j < deleteList.length; j++ )
	{
		for( var k = 0; k < drinks.length; k++ )
		{
			if( deleteList[ j ] == drinks[ k ] )
			{
				// This removes one element in the array
				drinks.splice( k, 1 );
			}
		}
	}
	
	return drinks;
}

// This function returns an array with the selected drinks
function drinksSelected( thisForm )
{
	var drinkList = new Array();
	var count = 0;
	
	// This creates the array with the drinks to delete
	for( var i = 0; i < thisForm.elements.length; i++ )
	{
		if( thisForm.elements[ i ].checked == true )
		{
			drinkList[ count ] = thisForm.elements[ i ].value;
			count++;
		}
	}
	
	return drinkList;
}

// This checks to make sure that at least one drink is selected
function checkSelected( thisForm )
{
	var drinkList = new Array();
	
	drinkList = drinksSelected( thisForm );
	
	if( drinkList.length > 0 )
	{
		return true;
	}
	else
	{
		alert( "Please Select Some Drink Recipes to Print!" );
		return false;
	}
}
