var httpObject = null;
//keyword_div is the input box for the keyword search
//TargetSearchDiv is the div element used to house the final search results
function search(keyword_div, TargetSearchDiv)
{
//clear innerHTML of search results window in order to clear old search results
if(document.getElementById('search_results'))
{
var ResultsDiv = document.getElementById('search_results');
ResultsDiv.parentNode.removeChild(ResultsDiv);
}
//if there is no keyword search present
if(keyword_div.value.length == 0)
var val = "No Keyword Chosen";
else
{
var val = keyword_div.value;
}
//check which filters were selected and store them in an array
var SelectedFilters = new Array();
var x = 0;
for(i = 0; i < document.recipe_search_form.recipe_filter.length; i++)
{
if(document.recipe_search_form.recipe_filter[i].checked == true)
{
SelectedFilters[x] = document.recipe_search_form.recipe_filter[i].value;
x++;
}
}
//create new div to add to the search results window
var newdiv = document.createElement('div');
newdiv.setAttribute('id',"search_results");
//string ultimately returned to PHP script
var search_string = "";
//populate the new div with the relevant data and add to page
var MySearchText = "
Try broadening your search.";
MySearchText = MySearchText + "
";
//MySearchText = MySearchText + "
";
newdiv.innerHTML = MySearchText + newdiv.innerHTML;
TargetSearchDiv.appendChild(newdiv);
}
}
}
//hide all other center div elements and show the search results
show_search_recipes();
}
function show_recipe_list()
{
document.getElementById('recipe_list').style.visibility='visible';
document.getElementById('add_recipe').style.visibility='hidden';
document.getElementById('search_recipes').style.visibility='hidden';
document.getElementById('shopping_list').style.visibility='hidden';
document.getElementById('recipe_card').style.visibility='hidden';
document.getElementById('edit_recipe').style.visibility='hidden';
}
function show_add_recipe()
{
document.getElementById('recipe_list').style.visibility='hidden';
document.getElementById('add_recipe').style.visibility='visible';
document.getElementById('search_recipes').style.visibility='hidden';
document.getElementById('shopping_list').style.visibility='hidden';
document.getElementById('recipe_card').style.visibility='hidden';
document.getElementById('edit_recipe').style.visibility='hidden';
}
function show_search_recipes()
{
document.getElementById('recipe_list').style.visibility='hidden';
document.getElementById('add_recipe').style.visibility='hidden';
document.getElementById('search_recipes').style.visibility='visible';
document.getElementById('shopping_list').style.visibility='hidden';
document.getElementById('recipe_card').style.visibility='hidden';
document.getElementById('edit_recipe').style.visibility='hidden';
}
function show_shopping_list()
{
document.getElementById('recipe_list').style.visibility='hidden';
document.getElementById('add_recipe').style.visibility='hidden';
document.getElementById('search_recipes').style.visibility='hidden';
document.getElementById('shopping_list').style.visibility='visible';
document.getElementById('recipe_card').style.visibility='hidden';
document.getElementById('edit_recipe').style.visibility='hidden';
}
function show_recipe_card_window()
{
document.getElementById('recipe_list').style.visibility='hidden';
document.getElementById('add_recipe').style.visibility='hidden';
document.getElementById('search_recipes').style.visibility='hidden';
document.getElementById('shopping_list').style.visibility='hidden';
document.getElementById('recipe_card').style.visibility='visible';
document.getElementById('edit_recipe').style.visibility='hidden';
}
function show_edit_recipe()
{
document.getElementById('recipe_list').style.visibility='hidden';
document.getElementById('add_recipe').style.visibility='hidden';
document.getElementById('search_recipes').style.visibility='hidden';
document.getElementById('shopping_list').style.visibility='hidden';
document.getElementById('recipe_card').style.visibility='hidden';
document.getElementById('edit_recipe').style.visibility='visible';
}
function load_recipe(RecipeName)
{
//alert(RecipeName);
//clear innerHTML of recipe card in the case of a previous recipe already being shown
if(document.getElementById('returned_recipe_card'))
{
//console.log(document.getElementById('returned_recipe_card').innerHTML);
var RecipeDiv = document.getElementById('returned_recipe_card');
RecipeDiv.parentNode.removeChild(RecipeDiv);
}
//obtain target div housing the dynamic recipe content
var TargetRecipeDiv = document.getElementById('dynamic_recipe_card_content');
//create new div to add to the recipe card window
var NewRecipeDiv = document.createElement('div');
NewRecipeDiv.setAttribute('id',"returned_recipe_card");
//obtain recipe information from database
httpObject = getHTTPObject();
if (httpObject != null)
{
httpObject.open("GET", "return_recipe.php?recipeName=" + RecipeName, true);
httpObject.send(null);
httpObject.onreadystatechange = function()
{
if(httpObject.readyState == 4 && httpObject.status == 200)
{
var recipearray = new Array();
var ingarray = new Array();
var filterarray = new Array();
var requestResponse = httpObject.responseText;
alert("LOAD: " + requestResponse);
// Anything that is echoed in search_recipe.php will be shown here.
if(requestResponse == ";;;;")
{
//alert(RecipeName + " not found in database.");
errormsg(document.getElementById('errorbar'),RecipeName + " not found in database");
show_recipe_list();
}
else
{
recipearray = requestResponse.split(";");
ingarray = recipearray[2].split(":");
filterarray = recipearray[4].split(",");
var MyRecipeText = "
";
//for loop structure is temporary (THIS ONLY WORKS WHEN THERE IS A TRAILING COMMA IN THE FILTER LIST)
for(var x = 1; x < filterarray.length; x++)
{
MyRecipeText = MyRecipeText + "
";
}
}
else
{
MySearchText += " You have no recipes! Type in a rcipe name and click on 'Add Recipe' to get started.";
}
MySearchText = MySearchText + "";
newdiv.innerHTML = MySearchText;
TargetDiv.appendChild(newdiv);
show_recipe_list();
}
}
}
//php is called here to load all recipes owned by user currently logged in
}
function clear_shopping_list()
{
var targetdiv = document.getElementById('dynamic_shopping_list_content');
targetdiv.innerHTML = "";
}
function build_shopping_list()
{
var build_string = "";
var anchors = document.getElementById('dynamic_shopping_list_content').getElementsByTagName('a');
for (var i=0; i < anchors.length; i++)
{
build_string += anchors[i].innerHTML;
if(i != anchors.length-1)
build_string += ",";
}
//waffles:butter,Canned Goods;waffles:flour,Dry Goods;waffles:salt,Produce;tensileb:find,Produce;tensileb:tin,Produce;tensileb:roof,Canned Goods;tensileb:rusting,Canned Goods;tensileb:perfect,Produce;
httpObject = getHTTPObject();
if (httpObject != null)
{
httpObject.open("GET", "shopping_list.php?list=" + build_string, true);
httpObject.send(null);
httpObject.onreadystatechange = function()
{
//add_recipe_output();
//return_shopping_list();
if(httpObject.readyState == 4 && httpObject.status == 200)
{
var requestResponse = httpObject.responseText;
// Anything that is echoed in search_recipe.php will be shown here.
alert(requestResponse);
var div = document.getElementById('dynamic_finished_shopping_list_content');
div.innerHTML = "";
var categories = [ "Bakery", "Baking", "Bulk", "Canned Goods", "Chips/Crackers", "Dairy", "Deli", "Ethnic", "Fish", "Frozen", "Meat", "Pasta", "Produce", "Other" ];
var bakery = new Array();
var baking = new Array();
var bulk = new Array();
var cannedgoods = new Array();
var chips = new Array();
var dairy = new Array();
var deli = new Array();
var ethnic = new Array();
var fish = new Array();
var frozen = new Array();
var meat = new Array();
var pasta = new Array();
var produce = new Array();
var other = new Array();
var recipes = requestResponse.split(';');
for(var i = 0; i < recipes.length-1; i++)
{
//console.log(recipes[i]);
var recipe = recipes[i].split(':');
var ing = recipe[1].split(',');
//console.log(ing[0] + " " + ing[1]);
if(ing[1] == categories[0])
bakery.push(ing[0]);
else if(ing[1] == categories[1])
baking.push(ing[0]);
else if(ing[1] == categories[2])
bulk.push(ing[0]);
else if(ing[1] == categories[3])
cannedgoods.push(ing[0]);
else if(ing[1] == categories[4])
chips.push(ing[0]);
else if(ing[1] == categories[5])
dairy.push(ing[0]);
else if(ing[1] == categories[6])
deli.push(ing[0]);
else if(ing[1] == categories[7])
ethnic.push(ing[0]);
else if(ing[1] == categories[8])
fish.push(ing[0]);
else if(ing[1] == categories[9])
frozen.push(ing[0]);
else if(ing[1] == categories[10])
meat.push(ing[0]);
else if(ing[1] == categories[11])
pasta.push(ing[0]);
else if(ing[1] == categories[12])
produce.push(ing[0]);
else if(ing[1] == categories[13])
other.push(ing[0]);
}
div.innerHTML += "
Your Shopping List
";
if(bakery.length > 0)
{
div.innerHTML += "Bakery";
for(var i = 0; i < bakery.length; i++)
{
div.innerHTML += "
" + other[i];
}
//div.innerHTML += "";
}
div.innerHTML += " ";
show_shopping_list();
}
}
}
}
function pop_up(URL)
{
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=1000,left = 340,top = -100');");
}
function add_another_ingredient(targetform)
{
//pick location to insert form elements
if(targetform == 'edit')
targetdiv = document.getElementById('edit_ingredients_form');
else if(targetform == 'add')
targetdiv = document.getElementById('add_ingredients_form');
//create the input box element and give it the appropriate name
myText = document.createElement('INPUT');
if(targetform == 'edit')
myText.setAttribute('name','EditIngredientName');
else if(targetform == 'add')
myText.setAttribute('name','AddIngredientName');
//create the space to go between the input box and the select element
mySpace = document.createTextNode(' ');
//create the select element
mySelect = document.createElement('select');
if(targetform == 'edit')
mySelect.setAttribute('name','EditIngredientCategory');
else if(targetform == 'add')
mySelect.setAttribute('name','AddIngredientCategory');
//select option 'SELECT CATEGORY'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','SELECT CATEGORY');
thetext = document.createTextNode('SELECT CATEGORY');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Bakery'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Bakery');
thetext = document.createTextNode('Bakery');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Baking'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Baking');
thetext = document.createTextNode('Baking');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Bulk'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Bulk');
thetext = document.createTextNode('Bulk');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Canned Goods'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Canned Goods');
thetext = document.createTextNode('Canned Goods');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Chips/Crackers'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Chips/Crackers');
thetext = document.createTextNode('Chips/Crackers');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Dairy'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Dairy');
thetext = document.createTextNode('Dairy');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Deli'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Deli');
thetext = document.createTextNode('Deli');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Ethnic'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Ethnic');
thetext = document.createTextNode('Ethnic');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Fish'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Fish');
thetext = document.createTextNode('Fish');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Frozen'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Frozen');
thetext = document.createTextNode('Frozen');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Meat'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Meat');
thetext = document.createTextNode('Meat');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Pasta'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Pasta');
thetext = document.createTextNode('Pasta');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Produce'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Produce');
thetext = document.createTextNode('Produce');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//select option 'Other'
theoption = document.createElement('OPTION');
theoption.setAttribute('value','Other');
thetext = document.createTextNode('Other');
theoption.appendChild(thetext);
mySelect.appendChild(theoption);
//actual insertion of form elements
targetdiv.appendChild(myText);
targetdiv.appendChild(mySpace);
targetdiv.appendChild(mySelect);
}
function launch_add_recipe()
{
clear_add_form();
document.add_recipe_form.recipe_name.value = document.getElementById('initial_recipe_name').value;
show_add_recipe();
}
function add_recipe(add_form)
{
//this string will hold the string that will be sent to the PHP script
//it contains all of the information about the recipe to be added
var add_recipe_string = "";
var num_of_ingredients = 0;
var abortadd = false;
//extract recipe name from form
if(validated(add_form.recipe_name.value,'nosemicolon') == true)
add_recipe_string += add_form.recipe_name.value + ";";
else
{
//alert("Recipe Name cannot be empty or contain the \", ; :\" symbols.");
errormsg(document.getElementById('errorbar'),"Recipe Name cannot be empty or contain the ';' symbol.");
return false;
}
//extract recipe description from form
if(validated(add_form.description.value,'nosemicolon') == true)
add_recipe_string += add_form.description.value + ";";
else
{
//alert("Recipe Description cannot be empty or contain the \", ; :\" symbols.");
errormsg(document.getElementById('errorbar'),"Recipe Description cannot be empty or contain the ';' symbol.");
return false;
}
//extract ingredient list from form
var numinputs = 0;
var div = document.getElementById('add_ingredients_form')
if(div.childNodes.length >= 6)
{
//count number of input elements in child nodes
for(var x = 0; x < div.childNodes.length; x++)
{
if(div.childNodes[x].nodeName=="INPUT")
{
numinputs++;
}
}
//console.log(numinputs);
var index = 0;
for(var i = 0; i < div.childNodes.length; i++)
{
if (div.childNodes[i].nodeName=="INPUT")
{
//console.log(div.childNodes[i].value + "\t\t\t[" + div.childNodes[i+2].value + "]");
if(validated(div.childNodes[i].value,'everything') == true)
{
add_recipe_string += div.childNodes[i].value + ",";
add_recipe_string += div.childNodes[i+2].value;
if(index != numinputs-1) //if this comparison is wrong, we will miss ingredients
add_recipe_string += ":";
else
{
add_recipe_string += ";";
//extract instructions from form
var pre_instructions = add_form.instructions.value;
var instructions = pre_instructions.replace(new RegExp( "\\n", "g"),' ');
if(validated(instructions,'nosemicolon') == true)
add_recipe_string += instructions + ";";
else
{
//alert("Recipe Instructions cannot be empty or contain the \"; :\" symbols.");
errormsg(document.getElementById('errorbar'),"Recipe Instructions cannot be empty or contain the ';' symbol.");
return false;
}
//extract filter data from form
for(i = 0; i < add_form.Filter.length; i++)
{
if(add_form.Filter[i].checked == true)
{
add_recipe_string += add_form.Filter[i].value + ",";
}
}
//send add_recipe_string to PHP
httpObject = getHTTPObject();
if (httpObject != null)
{
httpObject.open("GET", "add_recipe.php?addString=" + add_recipe_string, true);
httpObject.send(null);
httpObject.onreadystatechange = function()
{
if(httpObject.readyState == 4 && httpObject.status == 200)
{
var requestResponse = httpObject.responseText;
// Anything that is echoed in add_recipe.php will be shown here.
errormsg(document.GetElementById('errorbar'),requestResponse);
load_recipe(add_form.recipe_name.value);
}
}
}
return false;
}
}
else
{
//alert("Ingredients cannot be empty or contains \", ; :\" symbols.");
errormsg(document.getElementById('errorbar'),"Ingredients cannot be empty or contains \", ; :\" symbols.");
//abortadd = true;
return false;
}
index++;
}
}
}
else
{
//alert("Must have at least two recipe ingredients.");
errormsg(document.getElementById('errorbar'),"Must have at least two recipe ingredients.");
//abortadd = true;
return false;
}
}
function clear_add_form()
{
var form = document.add_recipe_form;
form.recipe_name.value = "";
form.description.value = "Enter Description Here...";
document.getElementById('add_ingredients_form').innerHTML = "";
form.instructions.value = "Enter Instructions Here...";
for(var i = 0; i < form.Filter.length; i++)
{
form.Filter[i].checked = false;
}
}
function launch_edit_recipe(recipe_data)
{
var recipearray = recipe_data.split(";");
var ingarray = recipearray[2].split(":");
var filterarray = recipearray[4].split(",");
targetform = window.document.edit_recipe_form;
targetform.recipe_name.value = recipearray[0];
targetform.description.value = recipearray[1];
clear_form(targetform,'edit');
var ingnames = document.getElementsByName('EditIngredientName');
var ingcats = document.getElementsByName('EditIngredientCategory');
for(var i = 0; i < ingarray.length; i++)
{
var ingsubarray = ingarray[i].split(",");
add_another_ingredient('edit');
ingnames[i].value = ingsubarray[0];
ingcats[i].value = ingsubarray[1];
}
var pre_instructions = recipearray[3];
var instructions = pre_instructions.replace(new RegExp(" ","g"),"\n");
targetform.instructions.value = instructions;
/*for(var i = 0; i < targetform.Filter.length; i++)
{
//console.log(targetform.Filter[i].value);
}*/
for(var i = 0; i < filterarray.length; i++)
{
//targetform.Filter[i].value = filterarray[i];
if(filterarray[i] == 'Veggie')
targetform.Filter[0].checked = true;
else if(filterarray[i] == 'Spicy')
targetform.Filter[1].checked = true;
else if(filterarray[i] == 'Kosher')
targetform.Filter[2].checked = true;
else if(filterarray[i] == 'Snack')
targetform.Filter[3].checked = true;
else if(filterarray[i] == 'Breakfast')
targetform.Filter[4].checked = true;
else if(filterarray[i] == 'Lunch')
targetform.Filter[5].checked = true;
else if(filterarray[i] == 'Dinner')
targetform.Filter[6].checked = true;
else if(filterarray[i] == 'Dessert')
targetform.Filter[7].checked = true;
}
show_edit_recipe();
}
function clear_form(targetform,targettype)
{
//targetform.recipe_name.value = "";
//targetform.description.value = "";
if(targettype == 'edit')
document.getElementById('edit_ingredients_form').innerHTML = "";
if(targettype == 'add')
document.getElementById('add_ingredients_form').innerHTML = "";
//targetform.instructions.value = "";
/*for(var i = 0; i < targetform.Filter.length; i++)
{
targetform.Filter[i].checked = false;
}*/
}
function edit_recipe(edit_form)
{
//this string will hold the string that will be sent to the PHP script
//it contains all of the information about the recipe to be edited
var edit_recipe_string = "";
var num_of_ingredients = 0;
//extract recipe name from form
if(validated(edit_form.recipe_name.value,'nosemicolon') == true)
edit_recipe_string += edit_form.recipe_name.value + ";";
else
{
//alert("Recipe Name cannot be empty or contain the ';' symbols.");
errormsg(document.getElementById('errorbar'),"Recipe Name cannot be empty or contain the ';' symbol.");
return false;
}
//extract recipe description from form
if(validated(edit_form.description.value,'nosemicolon') == true)
edit_recipe_string += edit_form.description.value + ";";
else
{
//alert("Recipe Description cannot be empty or contain the ';' symbol.");
errormsg(document.getElementById('errorbar'),"Recipe Description cannot be empty or contain the ';' symbol.");
return false;
}
//extract ingredient list from form
var numinputs = 0;
var div = document.getElementById('edit_ingredients_form')
if(div.childNodes.length >= 6)
{
//count number of input elements in child nodes
for(var x = 0; x < div.childNodes.length; x++)
{
if(div.childNodes[x].nodeName=="INPUT")
{
numinputs++;
}
}
//console.log(numinputs);
var index = 0;
for(var i = 0; i < div.childNodes.length; i++)
{
if (div.childNodes[i].nodeName=="INPUT")
{
//console.log(div.childNodes[i].value + "\t\t\t[" + div.childNodes[i+2].value + "]");
if(validated(div.childNodes[i].value,'everything') == true)
{
edit_recipe_string += div.childNodes[i].value + ",";
edit_recipe_string += div.childNodes[i+2].value;
if(index != numinputs-1) //if this comparison is wrong, we will miss ingredients
edit_recipe_string += ":";
else
{
edit_recipe_string += ";";
//extract instructions from form
var pre_instructions = edit_form.instructions.value;
var instructions = pre_instructions.replace(new RegExp( "\\n", "g"),' ');
if(validated(instructions,'nosemicolon') == true)
edit_recipe_string += instructions + ";";
else
{
//alert("Recipe Instructions cannot be empty or contain the \"; :\" symbols.");
errormsg(document.getElementById('errorbar'),"Recipe Instructions cannot be empty or contain the ';' symbol.");
return false;
}
//extract filter data from form
for(i = 0; i < edit_form.Filter.length; i++)
{
if(edit_form.Filter[i].checked == true)
{
edit_recipe_string += edit_form.Filter[i].value + ",";
}
}
//HI ADRIAN
//AS YOU CAN SEE, I COPIED/MODIFIED THE ADD RECIPE CODE INTO THIS EDIT RECIPE FUNCTION
//I'M NOT SURE HOW YOU WANT TO HANDLE THIS ON THE PHP SIDE SO I LEFT THE PHP FUNCTION CALL ALONE
//THIS FUNCTION SHOULD UPDATE/EDIT THE RECIPE, NOT ADD, THE CODE IS JUST COPIED OVER FROM THE ADD_RECIPE FUNCTION FOR NOW
//send edit_recipe_string to PHP
httpObject = getHTTPObject();
if (httpObject != null)
{
httpObject.open("GET", "edit_recipe.php?editString=" + edit_recipe_string, true);
httpObject.send(null);
httpObject.onreadystatechange = function()
{
//load_recipe(edit_form.recipe_name.value);
if(httpObject.readyState == 4 && httpObject.status == 200)
{
var requestResponse = httpObject.responseText;
alert(requestResponse);
//Anything that is echoed in search_recipe.php will be shown here.
show_recipe_card_window();
}
}
}
return false;
}
}
else
{
//alert("Ingredients cannot be empty or contains \", ; :\" symbols.");
errormsg(document.getElementById('errorbar'),"Ingredients cannot be empty or contains \", ; :\" symbols.");
return false;
}
index++;
}
}
}
else
{
//alert("Must have at least two recipe ingredients.");
errormsg(document.getElementById('errorbar'),"Recipe must contain must at least two ingredients.");
return false;
}
}
function validated(targetstring,testtype)
{
//console.log(targetstring);
if(targetstring != "")
{
if(testtype == "instructions")
{
var badcharfound = false;
if(targetstring.indexOf(';') != -1)
badcharfound = true;
if(targetstring.indexOf(':') != -1)
badcharfound = true;
if(badcharfound != true)
return true;
else
return false;
}
if(testtype == "everything")
{
var badcharfound = false;
if(targetstring.indexOf(';') != -1)
{
badcharfound = true;
}
if(targetstring.indexOf(':') != -1)
badcharfound = true;
if(targetstring.indexOf(',') != -1)
badcharfound = true;
if(badcharfound != true)
return true;
else
return false;
}
if(testtype == "nosemicolon")
{
var badcharfound = false;
if(targetstring.indexOf(';') != -1)
{
badcharfound = true;
}
if(badcharfound != true)
return true;
else
return false;
}
}
else
return false;
}
function delete_recipe(RecipeName)
{
var answer = confirm("Are you sure you want to delete '" + RecipeName + "'");
if(answer)
{
//PHP will pick up RecipeName here for deletion
httpObject = getHTTPObject();
if (httpObject != null)
{
httpObject.open("GET", "delete_recipe.php?recipeName=" + RecipeName, true);
httpObject.send(null);
httpObject.onreadystatechange = function()
{
if(httpObject.readyState == 4 && httpObject.status == 200)
{
var requestResponse = httpObject.responseText;
errormsg(document.getElementById('errorbar'),requestResponse);
setTimeout('show_recipe_list()','3000');
}
}
}
show_recipe_list();
}
else
//alert(RecipeName + " will not be deleted.");
errormsg(document.getElementById('errorbar'),RecipeName + " will not be deleted.");
}
function add_to_shopping_list(RecipeName)
{
var found = false;
var anchors = document.getElementById('dynamic_shopping_list_content').getElementsByTagName('a');
for(var i = 0; i < anchors.length; i++)
{
if(RecipeName == anchors[i].innerHTML)
found = true;
}
if(found != true)
document.getElementById('dynamic_shopping_list_content').innerHTML = document.getElementById('dynamic_shopping_list_content').innerHTML + "" + RecipeName + " ";
else
{
//alert("'" + RecipeName + "' already added to shopping list.");
var errorstring = "'" + RecipeName + "' already added to list.";
errormsg(document.getElementById('errorbar'),errorstring);
}
}
function errormsg(targetdiv,errorstring)
{
//console.log(targetdiv);
//console.log(errorstring);
//working code (commented out for testing)
//errordiv = document.createElement('div');
//errordiv.setAttribute('id','error');
//errordiv.setAttribute('class','errormsg');
//errordiv.innerHTML = errorstring
//targetdiv.appendChild(errordiv);
//setTimeout('removediv(document.getElementById("error"))','3000');
prevcontents = targetdiv.innerHTML;
//console.log(prevcontents);
targetdiv.innerHTML = "
" + errorstring + "
";
//console.log(prevcontents);
setTimeout('reseterrordiv(prevcontents)','3000');
}
function reseterrordiv(prevcontents)
{
//console.log(prevcontents);
document.getElementById('errorbar').innerHTML = prevcontents;
}
function removediv(targetdiv)
{
targetdiv.parentNode.removeChild(targetdiv);
}
function log_in(login_form)
{
//alert("Logged in as: " + login_form.user.value + ".");
errormsg(document.getElementById('errorbar'),"Logged in as: " + login_form.user.value + ".");
location.href = 'demo.php';
//pass login_info to PHP
var log_in_string = login_form.user.value + "," + login_form.pass.value;
//console.log(log_in_string);
}
function register(register_form)
{
//php code inserted here to create new account
//alert("Account " + register_form.user.value + " created successfully. Logging in....");
errormsg(document.getElementById('errorbar'),"Account " + register_form.user.value + " created successfully. Logging in....");
location.href = 'demo.php';
if(register_form.pass1.value == register_form.pass2.value)
{
//pass registration info to PHP
var reg_string = register_form.user.value + "," + register_form.pass1.value;
//console.log(reg_string);
}
}
function getHTTPObject()
{
if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest)
return new XMLHttpRequest();
else{
alert("Your browser does not support AJAX.");
return null;
}
}
function add_recipe_output()
{
// This IF blocks makes sure that the request has been completed.
if(httpObject.readyState == 4 && httpObject.status == 200)
{
var requestResponse = httpObject.responseText;
// Anything that is echoed in add_recipe.php will be shown here.
alert(requestResponse);
}
}
function search_recipe_output(TargetSearchDiv)
{
// This IF blocks makes sure that the request has been completed.
if(httpObject.readyState == 4 && httpObject.status == 200)
{
var requestResponse = httpObject.responseText;
// Anything that is echoed in search_recipe.php will be shown here.
//alert(requestResponse);
TargetSearchDiv.innerHTML += " Returned Results:
" + requestResponse + "
";
}
}
function return_recipe_output()
{
// This IF blocks makes sure that the request has been completed.
if(httpObject.readyState == 4 && httpObject.status == 200)
{
var requestResponse = httpObject.responseText;
// Anything that is echoed in search_recipe.php will be shown here.
//alert(requestResponse);
}
}
function return_shopping_list()
{
// This IF blocks makes sure that the request has been completed.
if(httpObject.readyState == 4 && httpObject.status == 200)
{
var requestResponse = httpObject.responseText;
//Anything that is echoed in search_recipe.php will be shown here.
//alert(requestResponse);
}
}
Array.prototype.inArray = function (value)
// Returns true if the passed value is found in the
// array. Returns false if it is not.
{
var i;
for (i=0; i < this.length; i++)
{
// Matches identical (===), not just similar (==).
if (this[i] === value)
{
return true;
}
}
return false;
};
function pickabackground()
{
var ranNum= Math.floor(Math.random()*2);
//console.log(ranNum);
if(ranNum == 0)
{
document.body.style.backgroundImage = "url(18.jpg)";
document.body.style.background="background-image: url('18.jpg') no-repeat";
}
if(ranNum == 1)
{
document.body.style.backgroundImage = "url(13.jpg)";
document.body.style.background="background-image: url('13.jpg') no-repeat";
}
}