
/***********************************************/
function clearDivs(){
	document.getElementById('homepage').innerHTML="";
	document.getElementById('freetext').innerHTML="";
	document.getElementById('displayCategory').innerHTML="";
	document.getElementById('recipeindex').innerHTML="";
	document.getElementById('displayTitle').innerHTML="";
	document.getElementById('displaySource').innerHTML="";
	document.getElementById('displayIngredients').innerHTML="";
	document.getElementById('displayDirections').innerHTML="";
	document.getElementById('displayNote').innerHTML="";
	document.getElementById('displayVariation').innerHTML="";
	document.getElementById('printrecipe').innerHTML="";

} /** end function clearDivs
/***********************************************/



/***********************************************/

var xmlhttp

function getXMLDoc(url)
{
/** code for Mozilla, etc.  **/

if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange=xmlhttpChange;
  xmlhttp.open("GET",url,false);
  xmlhttp.send(null);
  }
/** code for IE  **/
else if (window.ActiveXObject)
  {
  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    if (xmlhttp)
    {
    xmlhttp.onreadystatechange=xmlhttpChange;
    xmlhttp.open("GET",url,false);
    xmlhttp.send();
    }
  }

return xmlhttp.responseXML;
}  /** end function getXMLDoc**/

/***********************************************/


/***********************************************/
function xmlhttpChange()
{
/** if xmlhttp shows "loaded"  **/
if (xmlhttp.readyState==4)
  {
  /**  if "OK"  **/	
  if (xmlhttp.status==200)
    {

    }
  else
    {
    alert("Problem retrieving XML data "+xmlhttp.status);
    }
  }
}  /** end function xmlhttpChange  **/

/***********************************************/

/***********************************************/
function getRecipeList(recipeFileName)
{
	/**
	This function displays a list of recipe titles drawn from the recipes contained in the xml file recipeFileName.
	It also displays the recipe category at the top of the page.
	**/

	var xmlDocumentObject, allRecipesNodes, currentNode
	var counter, numChildNodes, foundCategory
	var numRecipes, recipeTitle, recipeListText
	
	/** Clear the area where the recipes will go  ***/
	clearDivs();

	/*** Get reference to XML doc ***/
	xmlDocumentObject=getXMLDoc(recipeFileName);  

	/*** Get all recipe title nodes ***/
	allRecipesNodes=xmlDocumentObject.getElementsByTagName("title");
	
	/** Find the category **/
	currentNode=allRecipesNodes[0].parentNode;
	numChildNodes=currentNode.childNodes.length;
	foundCategory=false;
	counter=0;
	/*** Find the category node ***/
	do {
		if (currentNode.childNodes[counter].nodeName== "category"){
		foundCategory=true;
		} else  {
		counter++;
		}
	}while ((counter<numChildNodes) && (foundCategory==false))

	if (foundCategory==true){
		document.getElementById('displayCategory').innerHTML=currentNode.childNodes[counter].firstChild.nodeValue+"<br/>";
		} else { 	
		document.getElementById('displayCategory').innerHTML="";
	}


	/* Assemble the list of recipe titles  */
	numRecipes=allRecipesNodes.length;
	
	recipeListText ="";
	recipeTitle="";
	for (var counter=0; counter<numRecipes; counter++){
		recipeTitle=allRecipesNodes[counter].firstChild.nodeValue;
		/*** create string eg <a href="#" onClick='showRecipe("Sugar Cookies","cookies.xml")'>Sugar Cookies</a><br/>   ***/
		var param1="&quot;" ;
		param1=param1 + escape(recipeTitle);
		param1=param1 + "&quot;";
		var param2="&quot;" + recipeFileName + "&quot;";
		var params=param1 + ", " + param2;
		recipeListText += "<a href='#' onClick='showRecipe(" + params +")' class='recipeIndexTitle'>" + recipeTitle + "</a><br/>";
		}
	
	/*** Recipe title list has been assembled - change HTML display element ***/
	document.getElementById('recipeindex').innerHTML=recipeListText;
		
		
}  /*** end function getRecipeList ***/
/***********************************************/


/***********************************************/
function  showRecipe(recipeTitle, recipeFileName)	
{
	/**
	This function displays one entire recipe
	recipeTitle is the title of a recipe to be displayed
	recipeFileName is the xml recipe file
	*/

	var xmlDocumentObject
	var allRecipesNodes, currentNode, ingredientNode, directionNode
	var recipeSource, recipeIngredients, recipeDirections, recipeNote
	var numRecipes, counter, foundIndex, numChildNodes, variationcounter, numingredients, numsteps
	var foundRecipe
	
	recipeTitle=unescape(recipeTitle);
	recipeSource="";
	recipeIngredients="";
	recipeDirections="";
	recipeNote="";
	recipeVariation="";

	/***  Get reference to XML doc  ***/
	xmlDocumentObject=getXMLDoc(recipeFileName);

	/*** Find the recipe title in the xml document by getting all the 
	title nodes and then searching for it
	***/
	allRecipesNodes=xmlDocumentObject.getElementsByTagName("title");
	numRecipes=allRecipesNodes.length;
	counter=0;
	foundRecipe=false;
	/*** Loop through recipe title nodes until recipeTitle is found   ***/
	do {
		if (allRecipesNodes[counter].firstChild.nodeValue == recipeTitle){
		foundRecipe=true;
		} else  {
		counter++;
		}
	}while ((counter<numRecipes) && (foundRecipe==false))
	


	if (foundRecipe==false) {

		document.getElementById('displayTitle').innerHTML="Recipe not found";

		} else {

		/***	Assemble recipe  ***/
	
		foundIndex=counter;
		currentNode=allRecipesNodes[foundIndex].parentNode;
		variationcounter=0;
		numChildNodes=currentNode.childNodes.length;
		for (var k=0; k<numChildNodes; k++) 
			{
			switch (currentNode.childNodes[k].nodeName) {		
				case "source": 
					if (currentNode.childNodes[k].firstChild.nodeValue != "") {
						recipeSource="Source: " + currentNode.childNodes[k].firstChild.nodeValue+ "<br/><br/>";
					}  
					break;
				case "note": 
					if (currentNode.childNodes[k].firstChild.nodeValue != "") {
						recipeNote="Note: " + currentNode.childNodes[k].firstChild.nodeValue;
					} 
					break;
				case "variation":
					if (currentNode.childNodes[k].firstChild.nodeValue != "") {
					variationcounter=variationcounter+1;
					recipeVariation=recipeVariation+"Variation "+variationcounter+": " + currentNode.childNodes[k].firstChild.nodeValue+"<br/><br/>";
					}
					break;
				case "ingredients":
					{
					ingredientNode=currentNode.childNodes[k];
					numingredients=ingredientNode.childNodes.length;
					for (var m=0; m<numingredients; m++) {
					if (ingredientNode.childNodes[m].nodeName=="item")
					{recipeIngredients=recipeIngredients+ingredientNode.childNodes[m].firstChild.nodeValue+"<br/>";
					}
	
					}  /** for (var m=0; m<numingredients; m++) **/
					break;
					} /**  case "ingredients":   **/
				case "directions":
					{
					directionNode=currentNode.childNodes[k];
					numsteps=directionNode.childNodes.length;
					for (var n=0; n<numsteps; n++) {
					if (directionNode.childNodes[n].nodeName=="step")
					{recipeDirections=recipeDirections+directionNode.childNodes[n].firstChild.nodeValue+"<br/><br/>";
					}
	
					}  /** for (var n=0; n<numsteps; n++)  **/
					break;
					}  /** case "directions":   **/
				
				
	
}  /**  end switch  **/
		} /** end for k **/
	
				
		
	/*** Recipe elements have been assembled - change HTML display 	elements ***/
	document.getElementById('displayTitle').innerHTML=recipeTitle + "<br/>";
	document.getElementById('displaySource').innerHTML=recipeSource;
	document.getElementById('displayIngredients').innerHTML=recipeIngredients + "<br/><br/>";
	document.getElementById('displayDirections').innerHTML=recipeDirections;
	document.getElementById('displayNote').innerHTML=recipeNote;
	document.getElementById('displayVariation').innerHTML=recipeVariation;

	/*** Add a printable view button for the recipe***/
	document.getElementById('printrecipe').innerHTML="<a href='#' onClick='printRecipe()'>Print Recipe</a><br/>";

	}	/**  end if (foundRecipe==false) else **/
}  /***  end function showRecipe   ***/
/***********************************************/



/***********************************************/
function printRecipe()	
{
	/**
	This function creates a printable view of the currently displayed recipe
	*/

	var recipewindow, printText, links

	/**Open a new window  **/
	recipewindow=window.open('','recipewin','scrollbars=yes resizable=yes menubar=yes ');
	
	/**Set up print and close window links in the new window **/
	links="<a href='#' onClick='window.print()'>Print</a>&nbsp;&nbsp;&nbsp;";	
	links=links + "<a href='#' onClick='window.close()'>Close</a><br/><br/>";	

	/** Set up html doc info including stylesheet and send to new window **/
	printText="<html><head><link rel='stylesheet' type='text/css' href='cookbook.css'></link><title>A Recipe from Carol's Cookbook</title></head><body>";
	printText=printText+links;
	printText=printText+document.getElementById('recipedisplay').innerHTML;
	printText=printText+"</body></html>";	
	recipewindow.document.write(printText);
	recipewindow.document.close();

	
}/***  end function printRecipe   ***/
/***********************************************/


/***********************************************/


function showAboutSite (){
	clearDivs();
	document.getElementById('freetext').innerHTML="<br/>Welcome to my personal recipe collection.<br/><br/>Unless noted otherwise, none of these recipes are my original creations. Sources are listed when available.<br/><br/>Happy cooking!";
} /** end function showAboutSite ***/
/***********************************************/

