// /root/js/ajax_draw.js
// Last Updated : 31 October 2007

// Functions to draw the search box on the page.

var validNums1 = '0123456789.tT'; 
var content;

// This creates the search box div and sets up the layout structure
function closeSearchBox()
{
	searchbox = document.getElementById("pane_search_extend");
	mastercontainervar = document.getElementById("pane_centre");	
	if(mastercontainervar==null)
	{
		document.body.removeChild(searchbox);
	}
	else
	{
		mastercontainervar.removeChild(searchbox);
	}
}
function closeExerciseBox()
{
	searchbox = document.getElementById("search_energy_out_pane");
	mastercontainervar = document.getElementById("pane_centre");	
	if(mastercontainervar==null)
	{
		document.body.removeChild(searchbox);
	}
	else
	{
		mastercontainervar.removeChild(searchbox);
	}
}
function getSearchOutBox()
{
	searchbox = document.getElementById("search_energy_out_pane");
	// If the searchbox doesn't already exist, then create it.
	// If it does exist, then make sure it's shown and remove the contents.
	mastercontainervar = document.getElementById("pane_centre");
	
	if(searchbox === null)
	{
		searchbox = document.createElement("div");
		searchbox.id = "search_energy_out_pane";
		//document.body.appendChild(searchbox);
		if(mastercontainervar==null)
		{
			document.body.appendChild(searchbox);
		}
		else
		{
			mastercontainervar.insertBefore(searchbox,mastercontainervar.childNodes[0]);
		}
	} 
	
	searchbox.innerHTML = '';
	//showHideLayers('pane_search_extend','show')
	return searchbox;	
	
}
function getSearchOutBox1()
{
	searchbox = document.getElementById("search_energy_out_pane");
	// If the searchbox doesn't already exist, then create it.
	// If it does exist, then make sure it's shown and remove the contents.
	mastercontainervar = document.getElementById("pane_centre");
	
	if(searchbox === null)
	{
		searchbox = document.createElement("div");
		searchbox.id = "search_energy_out_pane";
		//document.body.appendChild(searchbox);
		if(mastercontainervar==null)
		{
			document.body.appendChild(searchbox);
		}
		else
		{
			mastercontainervar.insertBefore(searchbox,mastercontainervar.childNodes[0]);
		}
	} 
	
	//searchbox.innerHTML = '';
	//showHideLayers('pane_search_extend','show')
	return searchbox;	
	
}

function getSearchBox(help)
{
	searchbox = document.getElementById("pane_search_extend");
	
	mastercontainervar = document.getElementById("pane_centre");	
	if(searchbox == null)
	{
		searchbox = document.createElement("div");
		searchbox.id = "pane_search_extend";
	// Search
		//if(mastercontainervar==null)
		//{
		//	document.body.removeChild(searchbox);
		//}
		//else
		//{ 
		//	mastercontainervar.removeChild(searchbox);
		//}		
		if(mastercontainervar==null)
		{
			document.body.appendChild(searchbox);
		}
		else
		{
			mastercontainervar.insertBefore(searchbox,mastercontainervar.childNodes[0]);
		}
	}
	searchbox.className='';
	searchbox.innerHTML = '';
	div10 = document.createElement("div");
	div10.id = "SearchHelp";

	if(help==null)
		div10.innerHTML ="<a href=javascript:popup('/popups/search_help.html','Help',600,520,'scrollbars=auto')><img src='/media/images/2/parts/search_help.gif'></a>";			
	else
		div10.innerHTML ="<a href=javascript:" + help + "><img src='/media/images/parts/search_help2.gif'></a>";			
	searchbox.appendChild(div10);

	//showHideLayers('pane_search_extend','show')
	return searchbox;	
}



function drawSearchBox(scroldivContent) {
	
	searchbox = getSearchBox();
	searchbox.className = 'c_search';

	// Main div
	div1    = document.createElement("div");
	div1.id = "pane_search_extend_content";
	
	// Vertical line
	div2    = document.createElement("div");
	div2.id = "pane_search_separator";
	
	// Content
	div3    = document.createElement("div");
	div3.id = "pane_centre_content";
	
	// Header - Title "Results > ", etc
	div4    = document.createElement("div");
	div4.id = "pane_centre_header";
	
	// Main body
	div5    = document.createElement("div");
	div5.id = "pane_centre_body";
	
	// Title "Text Search > ..." ,etc
	//div6    = document.createElement("div");
	//div6.id = "pane_centre_body_title";
	
	// Add in the text tags
	txt_lab = document.createElement("h1");
	txt_lab.innerHTML = bc_get();
	div4.appendChild(txt_lab);

	ax_texttagwithid(div5,"p","Click on a result below to display the serving sizes... or search again","moreTitle");
	// Close button
	closediv    = document.createElement("div");
	closediv.id = "pane_search_extend_close";
	if(ptrA >1)
	{ 
		ax_button(closediv,"javascript:bc_go_back();","/media/images/2/buttons/gobackup.gif")
	} 
	ax_button(closediv,"javascript:closeSearchBox();","/media/images/2/buttons/btnsearchclose.gif");

	// pane_search_extend
	// |- pane_search_extend_content
	//    |- pane_search_separator
	//    |- pane_center_content
	//       |- pane_center_header
	//       |- pane_center_body
	//       |  |- pane_center_body_title
	//       |  |- product_listing
	//       |  |- text
	//       |- pane_search_extend_close

	// Organize Structure
	img1 = document.createElement("img");
	img1.className="sectionSS";
	img1.src="/media/images/2/search/searchresults.gif"
	div1.appendChild(div2);
	div1.appendChild(div3);
	div3.appendChild(img1);
	div3.appendChild(div4);
	div3.appendChild(div5);
	//div5.appendChild(div6);	
	div3.appendChild(closediv);

	// Create the scrollable div
	content = createGenericScrollableDiv(div5,"searchproductlist","searchlisting","pink","150");
	// Parse the JSON object for the product listing
	scroldivContent(content,jsonObj.ajaxJSON);
	
	var manadd = 0;
	var j = jsonObj.ajaxJSON;
	
	for(i=0;i<j.length;i++){
		if(j[i].product_id<0) manadd = -1;
		
	}
	if(manadd<0){
		//Add in the text
		someText = document.createElement("p");
		someText.className = "pane_search_info";
		ax_texttag(someText,"p",ax_entity('&dagger;') + "Item manually added by you into your personal database.");
		ax_texttag(someText,"p","\u00a0" + "\u00a0" + "This cannot be viewed by other users.");
		div5.appendChild(someText);
	}
	
	// Add to page
	searchbox.appendChild(div1);
}

// This creates the search box div and sets up the layout structure
function drawSearchServingsBox(scroldivContent) {
	
	searchbox = getSearchBox();

	img1 = document.createElement("img");
	img1.className="sectionSS";
	img1.src="/media/images/2/search/searchresults.gif";

	// Main div
	div1    = document.createElement("div");
	div1.id = "pane_search_extend_content";
	
	// Vertical line
	div2    = document.createElement("div");
	div2.id = "pane_search_separator";
	
	// Content
	div3    = document.createElement("div");
	div3.id = "pane_centre_content";
	
	// Header - Title "Results > ", etc
	div4    = document.createElement("div");
	div4.id = "pane_centre_header";
	
	// Main body
	div5    = document.createElement("div");
	div5.id = "pane_centre_body";
	
	//div7    = document.createElement("div");
	//div7.id = "blank";
	//div7.innerHTML="<p>&nbsp;</p>"
	//div5.appendChild(div7);
	//ax_texttagwithid(div5,"p","","moreTitle");
	
	// Title "Text Search > ..." ,etc
	//div6    = document.createElement("div");
	//div6.id = "pane_centre_body_title";
	
	// Conversion Grid
	//div7    = document.createElement("div");
	//div7.id = "conversionGrid";	
	//div7.innerHTML="<a href=javascript:nu_create_window('400','420','/popups/conversion-grid.htm')>Conversion grid</a>";
	
	
	
	// Add in the text tags
	txt_lab = document.createElement("h1");
	txt_lab.innerHTML = bc_get();
	div4.appendChild(txt_lab);

	ax_texttagwithid(div5,"p","Now click on a serving size to add it to your diary...","moreTitle");
	// Close button
	closediv    = document.createElement("div");
	closediv.id = "pane_search_extend_close";
	if(ptrA > 1)
	{ 
		ax_button(closediv,"javascript:bc_go_back();","/media/images/2/buttons/gobackup.gif")
	}
	ax_button(closediv,"javascript:closeSearchBox()","/media/images/2/buttons/btnsearchclose.gif");
	
	
	// pane_search_extend
	// |- pane_search_extend_content
	//    |- pane_search_separator
	//    |- pane_center_content
	//       |- pane_center_header
	//       |- pane_center_body
	//       |  |- pane_center_body_title
	//       |  |- product_listing
	//       |  |- text
	//       |- pane_search_extend_close

	// Organize Structure
	div1.appendChild(div2);
	div1.appendChild(div3);
	div3.appendChild(img1);
	div3.appendChild(div4);
	//div3.appendChild(div7);
	div3.appendChild(div5);
		
	
	
	
	div3.appendChild(closediv);


	//divpl = document.createElement("div");
	//divpl.id = "product_listing";	

	// Create the scrollable div
	content = createGenericScrollableDiv(div5,"product_listing","","pink","150");

	if(currentpid>0)
	{
		Nutri = document.createElement("div");
		Nutri.id="nData" 

		Nutri.innerHTML ="<a href="+'"'+"javascript:ED_create('',currentpid);"+'"'+"id='Change'><img src='/media/images/2/buttons/amenddata.gif' alt='amend this data' style='position: absolute;top: 220px;left:0;'></a>";
		div5.appendChild(Nutri);
	}


	
	// Parse the JSON object for the product listing
	scroldivContent(content,jsonObj.ajaxJSON);
	
	/*
	someText = document.createElement("p");
	someText.className = "pane_search_info";
	ax_texttag(someText,"p",ax_entity('&dagger;') + "Item manually added by you into your personal database.");
	ax_texttag(someText,"p","\u00a0" + "\u00a0" + "This cannot be viewed by other users.");
	div5.appendChild(someText);*/
	
	// Add to page
	searchbox.appendChild(div1);
}

//this created the selected exercise
function drawExerciseBoxSingle(description,kcal)
{
	searchbox = getSearchOutBox();	
	
	// Main div
	div1    = document.createElement("div");
	div1.id = "search_energy_out_pane_content";
	
	// Header - Title "Results > ", etc
	div4    = document.createElement("div");
	div4.id = "search_energy_out_pane_header";
	
	// Main body
	div5    = document.createElement("div");
	div5.id = "search_energy_out_pane_body";
	
	bc_text = document.createElement("p");
	bc_text.innerHTML = "";
	div5.appendChild(bc_text);
	//ax_texttag(div5,"p",textSearch);

	// Close button
	closediv    = document.createElement("div");
	closediv.id = "search_energy_out_close";
	ax_button(closediv,"javascript:closeExerciseBox();","/media/images/2/buttons/energyoutclose.gif");

	// out_pane (searchbox)
	// |- out_pane_content (div1)
	//    |- out_pane_header (div4)
	//    |- out_pane_body (div5)
	//        |- product_listing_energyout (content)
	//        |- pane_search_extend_close (closediv)

	// Organize Structure
	div1.appendChild(div4);
	div1.appendChild(div5);
	div5.appendChild(closediv);

	
	// Create the scrollable div
	content = createGenericScrollableDiv(div5,"product_listing_energyout","searchlisting","blue","140");
	
	
	txt_lab = document.createElement("div");
	txt_lab.id ="exercisetime";
	txt_lab.innerHTML = "<form name='searchresults' action='process.jsp?section=diary&action=' method='post'><input type='hidden' name='subaction' value='exercise'><input type='hidden' name='item' value=''><input type='hidden' name='kcal' value=''><input type='hidden' name='duration' value=''></form><table cellpadding='0' cellspacing='0' border='0'><tr class='titlerow'><td width='250' class=\"search_product_co1\"><p>Activity</p></td><td class=\"search_product_co1\"><p>Duration of Activity</p></td><td class=\"search_product_co1\"><p>kCal Required</p></td><td></td></tr><tr id='addTime1'><td class=\"search_product_co1\"><p>" + description + "</p></td><td class=\"search_product_co1\"><a href='javascript:exercise(\"" + description + "\"," + kcal/6 +",10)'>10 mins</a></td><td class=\"search_product_co1\"><p>" + Math.round(kcal/6) +"</p></td><td><a href='javascript:exercise(\"" + description + "\"," + kcal/6 +",10)'><img src='/media/images/2/buttons/addfood.gif' style='margin:2px;'></a></td></tr><tr id='addTime2'><td class=\"search_product_co1\">&nbsp;</td><td class=\"search_product_co1\"><a href='javascript:exercise(\"" + description + "\"," + Math.round(kcal/2) +",30)'>30 mins</a></td><td class=\"search_product_co1\"><p>" + kcal/2 + "</p></td><td><a href='javascript:exercise(\"" + description + "\"," + Math.round(kcal/2) +",30)'><img src='/media/images/2/buttons/addfood.gif' style='margin:2px;'></a></td></tr><tr id='addTime3'><td class=\"search_product_co1\">&nbsp;</td><td class=\"search_product_co1\"><a href='javascript:exercise(\"" + description + "\"," + kcal + ",60)'>60 mins</a></td><td class=\"search_product_co1\"><p>" + kcal + "</p></td><td><a href='javascript:exercise(\"" + description + "\"," + kcal + ",60)'><img src='/media/images/2/buttons/addfood.gif' style='margin:2px;'></a></td></tr><tr id='addTime4'><td class=\"search_product_co1\">&nbsp;</td><td class=\"search_product_co1\"><a href='javascript:showCustomBoxExercise()'>Add your own time</a></td><td class=\"search_product_co1\"><p>--</p></td><td></td></tr><tr id='newTime' style='display:none;'><td valign='top'><p>" + description + "</p></td><td colspan='2'><div id='customTimeExercise' style='display:none;'><form action='/process.jsp?section=diary&action=&amp;subaction=exercise' method='post' id='exerciseForm' style='height:60px' onsubmit='javascript:validateExerciseTime(this);'><p><input  type='hidden' id='item' name='item' value='" + description +"'/><input type='hidden' id='kcal' name='kcal'  value='" + kcal + "'/><input  type='hidden' id='kcaltemp' name='kcaltemp'  value='" + kcal + "'/><input style='font-size:11px;height:21px;' type='text' id='duration' name='duration' size='3' maxlength='3' />&nbsp;&nbsp;(in minutes)</p><p style='font-size: 9px;'><input type='image' src='/media/images/2/buttons/btnaddtime.gif' /></p></form></div></td></tr></table>";
	content.appendChild(txt_lab);
	
	// Parse the JSON object for the product listing
	//scroldivContent(content,jsonObj.ajaxJSON);
	
	// Add to page
	searchbox.appendChild(div1);
	
	

}

// This creates the exercise box div and sets up the layout structure
function drawExerciseBox(scroldivContent) {
	
	searchbox = getSearchOutBox();

	// Main div
	div1    = document.createElement("div");
	div1.id = "search_energy_out_pane_content";
	
	// Header - Title "Results > ", etc
	div4    = document.createElement("div");
	div4.id = "search_energy_out_pane_header";

	bc_text = document.createElement("h1");
	bc_text.innerHTML = bc_get_nolink();
	div4.appendChild(bc_text);



	// Main body
	div5    = document.createElement("div");
	div5.id = "search_energy_out_pane_body";
	
		
	//ax_texttag(div5,"p",textSearch);

	// Close button
	closediv    = document.createElement("div");
	closediv.id = "search_energy_out_close";
	ax_button(closediv,"javascript:closeExerciseBox();","/media/images/2/buttons/energyoutclose.gif");

	// out_pane (searchbox)
	// |- out_pane_content (div1)
	//    |- out_pane_header (div4)
	//    |- out_pane_body (div5)
	//        |- product_listing_energyout (content)
	//        |- pane_search_extend_close (closediv)

	// Organize Structure
	div1.appendChild(div4);
	div1.appendChild(div5);
	div5.appendChild(closediv);

	// Create the scrollable div
	content = createGenericScrollableDiv(div5,"product_listing_energyout","searchlisting","blue","140");
	
	// Parse the JSON object for the product listing
	scroldivContent(content,jsonObj.ajaxJSON);
	
	// Add to page
	searchbox.appendChild(div1);
}

// This creates the search box div for categories and sets up the layout structure
function drawSearchBoxCategory(leftContent, rightContent, jsonChildren, jsonPopular) {
	
	searchbox = getSearchBox();

	img1 = document.createElement("img");
	img1.className="sectionSS";
	img1.src="/media/images/2/search/searchresults.gif"
	// Main div
	div1    = document.createElement("div");
	div1.id = "pane_search_extend_content";
	
	// Content
	div3    = document.createElement("div");
	div3.id = "pane_centre_content";
	
	// Header - Title "Results > ", etc
	div4    = document.createElement("div");
	div4.id = "pane_centre_header";
	
	// Main body
	div5    = document.createElement("div");
	div5.id = "pane_centre_body";
	
	// Title "Text Search > ..." ,etc
	//div6    = document.createElement("div");
	//div6.id = "pane_centre_body_title";
	
	// leftContent
	div7    = document.createElement("div");
	div7.id = "moreCategories";
	
	// Verticle Line 2
	div8    = document.createElement("div");
	div8.id = "pane_search_separator1";
	
	// rightContent
	div9    = document.createElement("div");
	div9.id = "popularChoices";
	
	// Add in the text tags	
	txt_lab = document.createElement("h1");
	txt_lab.innerHTML = bc_get();
	div4.appendChild(txt_lab);
	
	//ax_texttag(div4,"h1","Results > " + breadCrumb);
	//ax_texttag(div6,"p"," ");

	// Close button
	closediv    = document.createElement("div");
	closediv.id = "pane_search_extend_close";
	if(ptrA >1)
	{ 
		ax_button(closediv,"javascript:bc_go_back();","/media/images/2/buttons/gobackup.gif")
	}
	ax_button(closediv,"javascript:closeSearchBox()","/media/images/2/buttons/btnsearchclose.gif");


	// Structure	
	// pane_search_extend
	// |- pane_search_extend_content
	//    |- pane_search_separator
	//    |- pane_center_content
	//       |- pane_center_header
	//       |- pane_center_body
	//       |  |- pane_center_body_title
	//	 |  |- moreCategories
	//	 |  |  |-table
	//	 |  |- popularChoices
	//       |  |  |- product_listing
	//       |  |- text
	//       |- pane_search_extend_close

	// Organize Structure
	
	div1.appendChild(div3);
	div3.appendChild(img1);
	div3.appendChild(div4);
	div3.appendChild(div5);	
	//div5.appendChild(div6);
	div5.appendChild(div7);
	div5.appendChild(div8);
	div5.appendChild(div9);
	
	div3.appendChild(closediv);
	
	// Add in the title text
	ax_texttagwithid(div7,"p","Click on a category below...","moreTitle");

	
	// Add in the title text
	ax_texttagwithid(div9,"p","Top choices in this category are...","popTitle");


	// Create the scrollable div
	content = createGenericScrollableDiv(div9,"product_listing","searchlisting","pink","150");
	
	content1 = createGenericScrollableDiv(div7,"product_listing","searchlisting_morecat","pink","150");
	
	// Parse the JSON object for the cateories
	leftContent(content1,jsonChildren.ajaxJSON);
	
	// Parse the JSON object for the product listing
	rightContent(content,jsonPopular.ajaxJSON);
	
	// Add to page
	searchbox.appendChild(div1);
}
function cgd_top()
{
	cbDiv2 = document.createElement("b");
	cbDiv2.className = "cTop";	
	cbDiv3 = document.createElement("b");
	cbDiv3.className = "cb1";	
	cbDiv4 = document.createElement("b");
	cbDiv4.className = "cb2";	
	cbDiv5 = document.createElement("b");
	cbDiv5.className = "cb3";	
	cbDiv6 = document.createElement("b");
	cbDiv6.className = "cb4";	
	cbDiv2.appendChild(cbDiv3);
	cbDiv2.appendChild(cbDiv4);
	cbDiv2.appendChild(cbDiv5);
	cbDiv2.appendChild(cbDiv6);
	return cbDiv2;	
}
function cgd_bottom()
{
	// Create new divs, for the curved borders
	cbDiv8 = document.createElement("b");
	cbDiv8.className = "cBottom";	
	cbDiv9 = document.createElement("b");
	cbDiv9.className = "cb1";	
	cbDiv10 = document.createElement("b");
	cbDiv10.className = "cb2";	
	cbDiv11 = document.createElement("b");
	cbDiv11.className = "cb3";	
	cbDiv12 = document.createElement("b");
	cbDiv12.className = "cb4";

	// Now put the structure together
	cbDiv8.appendChild(cbDiv12);
	cbDiv8.appendChild(cbDiv11);
	cbDiv8.appendChild(cbDiv10);
	cbDiv8.appendChild(cbDiv9);

	return cbDiv8;
}
function createGenericDiv(parent, divname, contentname) {
	// Create the container div
	div = document.createElement("div");
	div.id = divname;
	
		// Create the container div
	div = document.createElement("div");
	div.id = divname;
	
	// Create new divs, for the curved borders
	cbDiv2 = cgd_top();
	cbDiv8 = cgd_bottom();
	cbDiv1 = document.createElement("div");
	cbDiv1.className = "cProductBox";	

	cbDiv7 = document.createElement("div");
	cbDiv7.className = "cBoxContent";
	
	// Now put the structure together
	cbDiv1.appendChild(cbDiv2);

		
	returndiv = document.createElement("div");
	if (contentname == "")
	{
		returndiv.id = "productbox";
		
	}
	else
	{
		returndiv.id = "productbox_" + contentname;
		
	}

	cbDiv7.appendChild(returndiv);	
	
	cbDiv1.appendChild(cbDiv7);
	cbDiv1.appendChild(cbDiv8);
	
	// Create add weight div and table	
	div.appendChild(cbDiv1);
	// Append container div to the parent
	parent.appendChild(div);
	
	return returndiv; // Return the scrollable div

}
// This creates a generic scrollable div and will put it into the parent.
function createGenericScrollableDiv(parent, divname, contentname, color, height) {
	
	// Create the container div
	div = document.createElement("div");
	div.id = divname;
	
	// Create new divs, for the curved borders
	
	cbDiv1 = document.createElement("div");
	cbDiv1.className = "cProductBox";	
	
	cbDiv7 = document.createElement("div");
	cbDiv7.className = "cBoxContent";
	
	cbDiv2 = cgd_top();
	cbDiv8 = cgd_bottom();
	
	cbDiv1.appendChild(cbDiv2);
		
	returndiv = document.createElement("div");
	if (contentname == "")
	{
		returndiv.id = "productbox";
		returndiv.style.overflow = "auto"; // Scrollable
	}
	else
	{
		returndiv.id = "productbox_" + contentname;
		returndiv.style.overflow = "auto"; // Scrollable
	}
	returndiv.style.height = height + "px"; // Set Height	
	
	cbDiv7.appendChild(returndiv);	

	cbDiv1.appendChild(cbDiv7);
	cbDiv1.appendChild(cbDiv8);
	
	// Create add weight div and table	
	addwghtdiv = document.createElement("div");
	addwghtdiv.id="enterspecificweight";
	addwghtdiv.innerHTML = addwghtdiv.innerHTML + "<table style='width: 310px;margin-left:17px;' border='0' cellpadding='0' cellspacing='0'><tr><td colspan='3' style='padding-bottom:5px;'>Enter exact weight of ingredient&nbsp;&nbsp;&nbsp;&nbsp;(<a href=javascript:nu_create_window('400','420','/popups/conversion-grid.htm')>Conversion grid</a>)</td></tr><tr><td style='width:70px'><input maxlength='6' size='6' type='text' name='aWeight' id='weightportion'></td><td style='width:50px;'><select name='aMeasure' id='weightmeasure'><option value='g'>g</option><option value='ml'>ml</option><option value='oz'>oz</option></select></td><td align='right'><input name='btnaddweight' src='/media/images/2/buttons/addweight.gif' type='image' onclick='javascript:getData()'></td></tr></table>";

	cbDiv1.appendChild(addwghtdiv);	
	div.appendChild(cbDiv1);
/*	
	if (contentname == ""){
	Nutri = document.createElement("div");
	Nutri.id="nData" 

	Nutri.innerHTML ="<p style='padding-left: 10px; padding-top: 0px;'><a href="+'"'+"javascript: popup('/Action?section=diary&action=dataerror&id= ','Help',570,360,'scrollbars=no');"+'"'+"style='font-size: 11px; font-weight: normal;' id='Change'>Click here to let us know a change in nutrition data</a></p>";
	cbDiv1.appendChild(Nutri);
	}
*/	
	// Append container div to the parent
	parent.appendChild(div);
	
	return returndiv; // Return the scrollable div
}


// Puts the information into the combo box for foodswap
function fsComboBox(content) {
	if (content == fsFlavourList) {
		cmb = document.getElementById("flavour");
	} else if (content == fsServingList) {
		cmb = document.getElementById("servingsize");
	} else if (content == fsBrandList) {
		cmb = document.getElementById("brandid");
	}
	
	// Clear the list except for the first entry
	for (var i = cmb.options.length - 1; i >= 1; i--){
		cmb.options[i] = null;
  	}
	cmb.selectedIndex = 0;
	
	// Put content into the combobox
	content(1, jsonObj.ajaxJSON, cmb);
}

// Get the Absolute Position of an object
function _getAbsolutePosition(obj) {
	var x = 0;
	var y = 0;
	var parent = obj;
  
	while (parent) {
		x += parent.offsetLeft;
		y += parent.offsetTop - parent.scrollTop;
		parent = parent.offsetParent;
	}

	return [x, y];
}
// For IE
function drawsetFloat(obj, direction) {
	obj.style.cssFloat = direction;
	obj.style.styleFloat = direction;
}


function removeDiaryServingsBox() 
{
	exqu = document.getElementById("extendedQuanBox");
	if(!exqu) return;


	document.body.removeChild(exqu);

}
function drawDiaryServingsBox(functiontorun,obj,diaryobject) 
{
	if(!obj) return ;

	var pos = getAbsolutePosition(obj);

	removeDiaryServingsBox();
	
	div = document.createElement("div");
	div.className = "quanBox";
	div.id = "extendedQuanBox";
	
	
	div.style.top = (pos[1] ) + "px";
	//setFloat(titleDiv, "left");
	div.style.left = (pos[0] +103) + "px";
  
	// This is a hack to ensure proper layout.  Most browsers seem to have trouble handling the below
	div.style.width = 170 + "px";
	//setFloat(div, "left");
	if(diaryobject.productid==-1)
		functiontorun(div,null,diaryobject);
	else
		functiontorun(div,jsonObj.ajaxJSON,diaryobject);
	document.body.appendChild(div);
	
}

function MA_addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
function MA_create(section,id){

	if(section=='recipe')
	{
		if(sectionID==0)
		{
			alert("Please make sure you have selected a recipe first.");
			return false;
		}
	}

	
	searchbox = getSearchBox("popup('/popups/manadd_help.html','MAHelp',600,520,'scrollbars=yes')");
	searchbox.className="MA";	
	div1 = document.createElement("div");
	div1.id="pane_centre_content";

	div2 = document.createElement("div");
	// Create the form
 	customForm = document.createElement("form");
	customForm.setAttribute("action","/Action?section=" + section + "&action=manualadd&subaction=manualadd&" + id);
	customForm.setAttribute("method","post");
	customForm.name="ma";
	customForm.id="ma_";
	
	//MA_addEvent(customForm,"submit", MA_validate);

	div1.appendChild(customForm);
	
	div3 = document.createElement("div");
	div3.id ="Options";
	div3.innerHTML=MA_getButtons('js1(1,0)','js1(1,1)','closeSearchBox()');
	
	content = createGenericDiv(div2,"manualadd1","MA");
	
	returndiv.innerHTML="<table border='0' cellpadding='0' cellspacing='0' id='data' class='manadd'><tbody><tr class='dbl'><td class='col1'>Product Title</td><td colspan='3'><input type='text' name='txttitle' id='txttitle' value='&lt;brand&gt;' maxlength='20' onFocus='MA_change(this);' tabindex='1001'/>&nbsp;<input type='text' name='txtdescription' id='txtdescription' maxlength='50' value='&lt;description&gt;' onFocus='MA_change(this)'; tabindex='1002'/></td><td class='col5'>Barcode</td><td><input type='text' name='txtbarcode' id='txtbarcode' value='&lt;full barcode&gt;' onFocus='MA_change(this);' maxlength='15' tabindex='1003' /></td></tr><tr class='dbl'><td class='col1'>Serving Size</td><td colspan='3'><input name='txtsrvsize' id='txtsrvsize' value='&lt;e.g. per 1/2 pizza&gt;' tabindex='1004' onFocus='MA_change(this);' maxlength='50'/></td>" + MA_getFive(0,1005,'/popups/manadd_help.html#five') + "</tr><tr class='dbl'><td class='col1'>This serving<br>provides</td><td class='col2'><input class='dataentry'  type='text' name='txtkcal' tabindex='1006' id='txtkcal' maxlength='5' onkeypress='return nu_validateKeyPress(event,validInt)'; />&nbsp;kcal</td><td class='col3'>fat</td><td class='col4'><input class='dataentry' tabindex='1007'  maxlength='5' type='text' name='txtfat' id='txtfat'  onkeypress='return nu_validateKeyPress(event,validNums);' />&nbsp;g</td>" + MA_getAlcohol(0,1008,'/popups/manadd_help.html#alcohol') + "</tr>" + MA_getCommon('','','','','','') +"</tbody></table><input type='hidden' name='saveonly' value='0'>";
	
	img = document.createElement("img");
	img.className="sectionMA";
	img.src="/media/images/2/search/manualadd.gif";
	
	customForm.appendChild(img);
	customForm.appendChild(div2);
	customForm.appendChild(div3);
	
	searchbox.appendChild(div1);	

}


function ED_create(section,id){

	arraylist = jsonObj.ajaxJSON;
	var pdescription='';
	var pkcal='';var pfatg;var pp;var psf;var pc;var pf;var ps;var psa;var pfive=0;var pal=0;
 	if(arraylist.length>0)
	{
		pdescription=arraylist[0].product_description;		
		pkcal = Math.round(arraylist[0].kcal);		
		pfatg = MA_fix(arraylist[0].fatg);	
		pp = MA_fix(arraylist[0].p);
		psf = MA_fix(arraylist[0].sf);
		pc = MA_fix(arraylist[0].c);
		pf = MA_fix(arraylist[0].f);
		ps = MA_fix(arraylist[0].s);
		psa = MA_fix(arraylist[0].sa);
		pfive = arraylist[0].five; 
		pal = arraylist[0].alcohol;
		
	}
	
	searchbox = getSearchBox("popup('/popups/edit_help.html','Help',600,520,'scrollbars=yes')");
	searchbox.className="MA ED";	
	div1 = document.createElement("div");
	div1.id="pane_centre_content"; 

	div2 = document.createElement("div");
	// Create the form
 	customForm = document.createElement("form");
	customForm.setAttribute("action","/Action?section=" + section + "&action=ed&subaction=error&" + id);
	customForm.setAttribute("method","post");
	customForm.name="ma";
	customForm.id="ma_";
	
	div1.appendChild(customForm);
	
	div3 = document.createElement("div");
	div3.id ="Options";
	div3.innerHTML=MA_getButtons('js1(2,0)','js1(2,1)','SearchServings(' + id +',' + currentwiz +')');
	
	content = createGenericDiv(div2,"manualadd1","ED");
	
	var combo="<select onchange='js2(this)' name='srvsize' id='srvsize' tabindex='1001'>";
	var addweight=false;
	for(x=0; x<arraylist.length; x++) 
	{
		if(arraylist[x].serving_formula!='add weight')
			combo =combo + "<option value='" + arraylist[x].factor + "'>" + arraylist[x].serving_formula + "</option>";
		else
			addweight=true;
			
	}
	if(addweight)
		combo = combo + "<option value='100'>Per 100g/ml</option>";
	else
		combo = combo + "<option value='1000'>Per 100g/ml</option>";
             
                
         combo = combo + '</select>';
	
	
	returndiv.innerHTML="<table border='0' cellpadding='1' cellspacing='0' id='data'><tbody><tr class='dbl'><td class='col1'>Serving Size</td><td colspan='3'>" + combo + "</td>" + MA_getFive1(pfive,1002,'/popups/manadd_help.html#five') + "</tr><tr class='dbl'><td class='col1'>energy</td><td class='col2'><input class='dataentry' tabindex='1003' type='text' name='txtkcal' id='txtkcal' maxlength='5' onkeypress='return nu_validateKeyPress(event,validInt)'; value='" + pkcal +  "'/>&nbsp;kcal</td><td class='col3'>fat</td><td class='col4'><input tabindex='1004' class='dataentry'  maxlength='5' type='text' name='txtfat' id='txtfat'  onkeypress='return nu_validateKeyPress(event,validNums);' value='" + pfatg +  "'/>&nbsp;g</td>" + MA_getAlcohol(pal,1005,'/popups/manadd_help.html#alcohol') + "</tr>" + MA_getCommon(pp,psf,pc,pf,ps,psa) +"</tbody></table><input type='hidden' name='saveonly' value='0'>";
	
	img = document.createElement("img");
	img.className="section";
	img.src="/media/images/2/search/editdata.gif";
	
	h2 = document.createElement("h2");
	h2.className="foodtitle";
	h2.innerHTML=pdescription;	
	
	customForm.appendChild(img);
	customForm.appendChild(h2);
	customForm.appendChild(div2);
	customForm.appendChild(div3);
	
	searchbox.appendChild(div1);	

}
function MA_getButtons(fsaveadd,fsaveonly,fcancel)
{
  
 return "<a href='javascript:" + fsaveadd + "'><img border='0' src='/media/images/2/buttons/saveadd.gif' /></a><a href='javascript:" + fsaveonly + "'><img border='0' src='/media/images/2/buttons/saveonly.gif' /></a><a href='javascript:" + fcancel + "'><img src='/media/images/2/buttons/btnsearchclose.gif' border='0'></a>"	
}
function MA_getFive(f, tabindex,help)
{
return "<td class='col5'>5-a-day<br />portion</td><td class='col6'><select name='fiveaday' id='fiveaday' tabindex='" + tabindex + "'><option value='0'>none</option><option value='0.5'>&#189;</option><option value='1'>1</option><option value='1.5'>1 &#189;</option><option value='2'>2</option><option value='2.5'>2 &#189;</option><option value='3'>3</option><option value='3.5'>3 &#189;</option><option value='4'>4</option><option value='4.5'>4 &#189;</option><option value='5'>5</option></select>&nbsp;<a href="+'"'+"javascript:popup('" + help + "','Help',600,520,'scrollbars=yes')"+'"'+"><img src='/media/images/search/5aday.gif' align='absmiddle' /></a></td>";
}
/*
function MA_getFive1(f, tabindex,help)
{
	if(f<0) return "<td></td><td></td>";
	return "<td colspan='2'><table border='0' cellpadding='0' cellspacing='0'><tr id='fiveaday_row1' ><td colspan='2'><p class='analysis'>This serving provides <label id='fiveaday_data'>" + MA_fix(f) + "</label> units of your five a day. Click <a href='#' onclick='return MA_fiveshow(1)'>here</a> to change</p></td></tr><tr style='display:none' id='fiveaday_row2'>" + MA_getFive(f,tabindex,help) + "</tr></table></td>";
}
*/

function MA_getFive1(f, tabindex,help)
{
	if(f<0) return "<td></td><td></td>";
	return "<td colspan='2'><table border='0' cellpadding='0' cellspacing='0' style='width:200px;'><tr id='fiveaday_row1' ><td><p class='analysis'><span style='font-weight:bold;color:#33CC33;'>5-a-day</span> Provides <label id='fiveaday_data'>" + MA_fix(f) + "</label> units per serving. Click <a href='#' onclick='return MA_fiveshow(1)'>here</a> to change</p></td><td><img src='/media/images/search/5aday.gif' /></td></tr><tr style='display:none' id='fiveaday_row2'>" + MA_getFive(f,tabindex,help) + "</tr></table></td>";
}

function MA_fiveshow(vis)
{
	 
	if(vis==1)
	{
		var obj = document.getElementById("fiveaday_row2");
		if(obj) 
			obj.style.display='block';
		var obj = document.getElementById("fiveaday_row1");
		if(obj) 
			obj.style.display='none';
	}
	else
	{
		var obj = document.getElementById("fiveaday_row1");
		if(obj) 
			obj.style.display='block';
		var obj = document.getElementById("fiveaday_row2");
		if(obj) 
			obj.style.display='none';
	}
	return false;
}
function MA_getAlcohol(a, tabindex,help)
{
	if(a<0) return "<td class='col5'></td><td class='col6'></td>";
	return "<td class='col5'>Alcohol</td><td class='col6'><input tabindex='" + tabindex + "' class='dataentry' type='text' name='txtalcohol' id='txtalcohol' maxlength='3' onkeypress='return nu_validateKeyPress(event,validNums);' value='" + MA_fix(a) + "'/>&nbsp;units&nbsp;<a href="+'"'+"javascript:popup('/popups/manadd_help.html#alcohol','Help',600,520,'scrollbars=yes')"+'"'+"><img src='/media/images/search/alcohol.gif' align='absmiddle' style='margin-left:12px;'/></a></td>";
}
function MA_getCommon(p,sf,c,f,s,sa)
{
 return "<tr><td colspan='6'><img src='/media/images/2/search/optionalhdr.gif' /></td></tr><tr><td class='col1'><b>protein</b></td><td><input class='dataentry' type='text' name='txtprotein' id='txtprotein' maxlength='5' onkeypress='return nu_validateKeyPress(event,validNums1);' value='" + p + "' tabindex='1009'/> g</td><td class='col3'><b>sat fat</b></td><td><input class='dataentry' type='text' name='txtsatfat' id='txtsatfat' maxlength='5' tabindex='1012' onkeypress='return nu_validateKeyPress(event,validNums1);' value='" + sf + "'/> g</td><td rowspan='3' colspan='2'><p class='analysis'	>Adding this data will help<br />us provide more nutritional<br />analysis of your food diary</p></td></tr><tr><td class='col1'><b>carbs</b></td><td><input tabindex='1010' class='dataentry' type='text' name='txtcarbs' id='txtcarbs' maxlength='5' onkeypress='return nu_validateKeyPress(event,validNums1);' value='" + c + "' /> g</td><td class='col3'><b>fibre</b></td><td><input  class='dataentry' type='text' name='txtfibre' id='txtfibre' maxlength='5' onkeypress='return nu_validateKeyPress(event,validNums1);' value='" + f + "' tabindex='1013'/> g</td></tr><tr><td class='col1'><b>sugars</b></td><td><input class='dataentry' type='text' name='txtsugars' id='txtsugars' maxlength='5' onkeypress='return nu_validateKeyPress(event,validNums1);' value='" + s + "' tabindex='1011'/> g</td><td class='col3'><b>sodium</b></td><td><input class='dataentry' type='text' name='txtsodium' id='txtsodium' maxlength='5' onkeypress='return nu_validateKeyPress(event,validNums1);' value='" + sa + "' tabindex='1014' /> g</td></tr>";

}
function MA_fix(f)
{
 if(f<0)
 	return "";
 return (1 * f).toFixed(1);	
}


function MA_change(con)
{
	var str=con.value;
	var pos=str.indexOf("<");
	
	if(pos=='0')
		con.value='';
}

function MA_validate(mode,formname,saveonly){

	var retval = true;
	var f = MA_getForm(formname);
	
	if(!f)
	{
		alert('Unsupported');	
		return;
	}
	f.saveonly.value=saveonly;
	if(mode==1)
	{
		if (f.txttitle.value=='' || f.txttitle.value=='<brand>'){
			f.txttitle.value = '';
		}
	}
	if(mode==1)
	{
		if(retval)
		{
			if (f.txtdescription.value=='' || f.txtdescription.value=='<description>'){
				alert('Please enter a product description.');
				retval= false;
			}
		}
	}
	if(mode==1)
	{
		if (f.txtbarcode.value=='<full barcode>'){
			f.txtbarcode.value = '';
			if(retval){
			if (f.txtdescription.value=='' || f.txtdescription.value=='<description>'){
				alert('Please enter a product description.');
				retval= false;
			}
			}		
		}	
	}
/*	
	if(mode==1)
	{
		if(retval){	
			var barcode= f.txtbarcode.value;
			if (barcode.length > 0) 
			{
				if(!(barcode.length==8 || barcode.length==11 || barcode.length==12 || barcode.length==13))
				{
					alert("Please enter a valid barcode");
					retval= false;
				}
			}
		}
	}
*/	
	if(mode==1)
	{
		if(retval)
		{
			if (f.txtsrvsize.value=='' || f.txtsrvsize.value=='<e.g. per 1/2 pizza>'){
				alert('Please enter a serving size.');
				retval= false;
			}
		}
	}
	if(retval)
	{
		if (f.txtkcal.value==''){
			alert('Please enter a kcal value.');
			retval= false;
		}
	}
	if(retval)
	{
		if (f.txtfat.value==''){
			alert('Please enter a fat value.');
			retval= false;
		}
	}
	
	if(retval)
	{
		kcalval = f.txtkcal.value;
		fatval = f.txtfat.value;
		
		if(kcalval<(fatval*9)){
			alert("Every gram of Fat must contain at least 9 Kcal. Based on your fatg figure the energy should be at least " + Math.round((fatval*9)) + "kcal.");
			retval = false;
		}
	}	
	
	if(!retval)
	{
		return;
	}
	if(mode==1)
		f.submit();
	else
		//we nned to do ajax stuff
	{
	var url = "/DataAction?section=product&action=change&id=" + currentpid;
	
	var opt = f.srvsize.options[f.srvsize.selectedIndex];
	
	
	var s = "&ss=" + escape(opt.text) + "&txtkcal=" + f.txtkcal.value + "&txtfat="+ f.txtfat.value + "&srvsize=" + opt.value + "&txtprotein=" + f.txtprotein.value + "&txtsatfat=" + f.txtsatfat.value + "&txtcarbs=" + f.txtcarbs.value + "&txtfibre=" + f.txtfibre.value + "&txtsugars=" + f.txtsugars.value + "&txtsodium=" + f.txtsodium.value + "&saveonly=" + f.saveonly.value; 

	if(f.fiveaday)
	{
		if(f.fiveaday.selectedIndex>-1)
		{
			s = s + "&fiveaday=" + 	 f.fiveaday.value;
		}
	}
	if(f.txtalcohol)
	{
		s = s+"&txtalcohol=" + f.txtalcohol.value;	
	}
	if(sectionID>0)
	{
		s = s+"&recipeid=" + sectionID;
	}
	//currentpid = productid;
	ajax_request(url + s, js3, null );
	
	}
}
function js3(response, args)
{
		if(sectionID>0)
			//recipe redirect
			window.location.href="/Action?section=recipe&action=view&recipeid=" + sectionID;
		else //diary		
			window.location.href="/Action?section=diary";

}
function js1(mode,saveonly)
{

	if(mode==1) //ma
	{
		MA_validate(mode,'ma',saveonly);
	}
	else if(mode==2)
	{
		MA_validate(mode,'ma',saveonly);
	}
		
}
function js2(obj)
{
	var pp;var psf;var pc;var pf;var ps;var psa;var pfive;
	var found = false;
         arraylist = jsonObj.ajaxJSON;

	
	for(x=0; x<arraylist.length; x++) 
	{
		if(obj.value==arraylist[x].factor)
		{
			found = true;
                        if(obj.value==100)
			{
				pkcal = Math.round(arraylist[x].kcal/100 * arraylist[x].factor_100);		
				pfatg = MA_fix(arraylist[x].fatg/100 * arraylist[x].factor_100);		
				pp = MA_fix(arraylist[x].p/100 * arraylist[x].factor_100);
				psf = MA_fix(arraylist[x].sf/100 * arraylist[x].factor_100);
				pc = MA_fix(arraylist[x].c/100 * arraylist[x].factor_100);
				pf = MA_fix(arraylist[x].f/100 * arraylist[x].factor_100);
				ps = MA_fix(arraylist[x].s/100 * arraylist[x].factor_100);
				psa = MA_fix(arraylist[x].sa/100 * arraylist[x].factor_100);
				pfive = MA_fix(arraylist[x].five/100 * arraylist[x].factor_100); 
				pal = MA_fix(arraylist[x].alcohol/100 * arraylist[x].factor_100);
			}
			else
			{
				pkcal = Math.round(arraylist[x].kcal);		
				pfatg = MA_fix(arraylist[x].fatg);		
				pp = MA_fix(arraylist[x].p);
				psf = MA_fix(arraylist[x].sf);
				pc = MA_fix(arraylist[x].c);
				pf = MA_fix(arraylist[x].f);
				ps = MA_fix(arraylist[x].s);
				psa = MA_fix(arraylist[x].sa);
				pfive = MA_fix(arraylist[x].five); 
				pal = MA_fix(arraylist[x].alcohol);
			}
			x = arraylist.length;
		}
	}
        if(!found & obj.value==1000)
	{
                pkcal = 0;		
                pfatg = 0;		
                pp = 0;
                psf = 0;
                pc = 0;
                pf = 0;
                ps = 0;
                psa = 0;
                pfive = 0; 
                pal = 0;

        }
        
         
         
	var f = MA_getForm('ma');
	if(f)
	{
		f.txtkcal.value=pkcal;
		f.txtfat.value=pfatg;
		f.txtprotein.value=pp;
		f.txtsatfat.value=psf;
		f.txtcarbs.value=pc; 
		f.txtfibre.value=pf;
		f.txtsugars.value=ps;
		f.txtsodium.value=psa;
		if(f.fiveaday)
			f.fiveaday.selectedIndex=-1;
		MA_fiveshow(0);
		var fi = document.getElementById('fiveaday_data');
		if(fi)
		{
			fi.innerHTML =pfive;	
		}
		if(f.txtalcohol)
			f.txtalcohol.value=pal;
	}
	else
	{
		alert('Unsupported');
	}
}
function MA_getForm(formname)
{
	var f = document.forms[formname];
	if(!f)
		var f = document.getElementById(formname + '_');	
	return f;
}
function MA_validateKeyPress(e, validSet) 
{ 
    var key; 
    var keychar; 
         
    if(window.event || !e.which) // IE 
        key = e.keyCode; // IE 
    else if(e) 
        key = e.which;   // Netscape 
    else 
        return true;     // no validation 
	if(key<32) return true;
    keychar = String.fromCharCode(key); 
    validSet += String.fromCharCode(8); 

	
    if (validSet.indexOf(keychar) < 0) 
      return false; 

    return true; 
} 
function RecipeCategoriesCallBack(response, args){

	jsonObj = response.text.parseJSON();
	help = "popup('/popups/search_myrecipes.html','Help',600,520,'scrollbars=yes')";
	searchbox = getSearchBox(help);

	img1 = document.createElement("img");
	img1.className="sectionSS";
	img1.src="/media/images/2/search/searchrecipes.gif"
	img2 = document.createElement("img");
	img2.className="iconaddrcps";
	img2.src="/media/images/diary/icon_myrecipes.gif";		
	
	// Main div
	div1    = document.createElement("div");
	div1.id = "pane_search_extend_content";
	
	// Content
	div3    = document.createElement("div");
	div3.id = "pane_centre_content";
	
	// Header - Title "Results > ", etc
	div4    = document.createElement("div");
	div4.id = "pane_centre_header";
	
	// Main body
	div5    = document.createElement("div");
	div5.id = "pane_centre_body";
	
	div6 = document.createElement("div");
	div6.id = "moreCategories";	
	
	p1 = document.createElement("p");
	p1.id="moreTitle";
	p1.innerHTML="Click on an occasion below...";


	div6.appendChild(p1);
	div5.appendChild(div6);
	
	// Create new divs, for the curved borders
	cbDiv2 = cgd_top();
	cbDiv8 = cgd_bottom();
	cbDiv1 = document.createElement("div");
	cbDiv1.className = "cProductBox";		

	cbDiv7 = document.createElement("div");
	cbDiv7.className = "cBoxContent";
	
	div8 = document.createElement("div");
	div8.style.height = "150px";
	
	//top
	cbDiv1.appendChild(cbDiv2);
	
	RecipeOccasions(jsonObj.ajaxJSON,div8);
	
	cbDiv7.appendChild(div8);
	cbDiv1.appendChild(cbDiv7);
	
	//bottom
	cbDiv1.appendChild(cbDiv8)
	
	
	div6.appendChild(cbDiv1)
	
	closediv    = document.createElement("div");
	closediv.id = "pane_search_extend_close";
	addnewrec   = document.createElement("div");
	addnewrec.id = "pane_search_add_new_recipe";	
	addnewrec.innerHTML='<a href="/process.jsp?section=recipe&action=add"><img src="/media/images/2/buttons/mymeals_add.gif"></a>';
	
	if(ptrA >1)
	{ 
		ax_button(closediv,"javascript:bc_go_back();","/media/images/2/buttons/gobackup.gif")
	}
	ax_button(closediv,"javascript:closeSearchBox()","/media/images/2/buttons/btnsearchclose.gif");	
	
	
	div1.appendChild(div3);
	div3.appendChild(img1);
	div3.appendChild(img2);
	div3.appendChild(div4);
	div3.appendChild(div5);			
	div3.appendChild(addnewrec);
	div3.appendChild(closediv);
	
	searchbox.appendChild(div1);
}

function RecipeOccasions(jsonObj){
	table = document.createElement("table");
	table.id="occasionslist";
		
	if (jsonObj.length > 0){

		for(i=0;i<jsonObj.length;i++){
	
			var x=table.insertRow(-1)
			var td=x.insertCell(0);
			var str;
			var getJsonObj;
			var ucJsonObj;
			var lcJsonObj;
			var getOccasion;
			
			if (jsonObj[i].occ != ''){
				getJsonObj = jsonObj[i].occ;
				ucJsonObj = getJsonObj.charAt(0);
				ucJsonObj = '<span style="text-transform:uppercase;">'+ucJsonObj+'</span>';
				lcJsonObj = getJsonObj.substr(1);
				getOccasion = ucJsonObj+lcJsonObj;
				
				str='<a href="javascript:SearchRecipeCategories(\''+jsonObj[i].occ+'\');">'+getOccasion+'</a> ('+jsonObj[i].count+')';
			}
			else{
				str='<a href="javascript:SearchRecipeCategories(\'non\');">No Occasion</a> ('+jsonObj[i].count+')';
			}
	
			td.className = "recipecat";
			td.innerHTML = str;	
		}

	}
		
	else{
		var x=table.insertRow(-1)
		var td=x.insertCell(0);
		var str='You currently have no recipes in this folder. Please click below to create a new meal or recipe.';
		td.className = "recipecat";
		td.innerHTML = str;		
	}

	div8.appendChild(table);
	return div8;
}

function SearchRecipeCategoryCallBack(response, args){

	jsonObj = response.text.parseJSON();
	help = "popup('/popups/search_myrecipes.html','Help',600,520,'scrollbars=yes')";
	searchbox = getSearchBox(help);

	img1 = document.createElement("img");
	img1.className="sectionSS";
	img1.src="/media/images/2/search/searchrecipes.gif"
	img2 = document.createElement("img");
	img2.className="iconaddrcps";
	img2.src="/media/images/diary/icon_myrecipes.gif";		
	
	// Main div
	div1    = document.createElement("div");
	div1.id = "pane_search_extend_content";
	
	// Content
	div3    = document.createElement("div");
	div3.id = "pane_centre_content";
	
	// Header - Title "Results > ", etc
	div4    = document.createElement("div");
	div4.id = "pane_centre_header";
	
	// Main body
	div5    = document.createElement("div");
	div5.id = "pane_centre_body";
	
	div6 = document.createElement("div");
	div6.id = "searchproductlist";	
	
	p1 = document.createElement("p");
	p1.id="moreTitle";
	p1.innerHTML="Click on a recipe below...";


	div5.appendChild(p1);
	div5.appendChild(div6);
	
	// Create new divs, for the curved borders
	cbDiv2 = cgd_top();
	cbDiv8 = cgd_bottom();
	cbDiv1 = document.createElement("div");
	cbDiv1.className = "cProductBox";		

	cbDiv7 = document.createElement("div");
	cbDiv7.className = "cBoxContent";
	
	div8 = document.createElement("div");
	div8.style.height = "150px";
	
	//top
	cbDiv1.appendChild(cbDiv2);
	
	recipeTable(jsonObj.ajaxJSON,div8);
	
	cbDiv7.appendChild(div8);
	cbDiv1.appendChild(cbDiv7);
	
	//bottom
	cbDiv1.appendChild(cbDiv8)
	
	
	div6.appendChild(cbDiv1)
	
	addnewrec   = document.createElement("div");
	addnewrec.id = "pane_search_add_new_recipe";	
	addnewrec.innerHTML='<a href="/process.jsp?section=recipe&action=add"><img src="/media/images/recipe/btn_rec_add.gif"></a>';	
	closediv    = document.createElement("div");
	closediv.id = "pane_search_extend_close";
	if(ptrA >1)
	{ 
		ax_button(closediv,"javascript:bc_go_back();","/media/images/2/buttons/gobackup.gif")
	}
	ax_button(closediv,"javascript:closeSearchBox()","/media/images/2/buttons/btnsearchclose.gif");	
	
	
	div1.appendChild(div3);
	div3.appendChild(img1);
	div3.appendChild(img2);
	div3.appendChild(div4);
	div3.appendChild(div5);			
	div3.appendChild(addnewrec);
	div3.appendChild(closediv);
	
	searchbox.appendChild(div1);
	
}

function SearchFavouritesCallBack(response, args){

	jsonObj = response.text.parseJSON();
	help = "popup('/popups/search_myfavourites.html','Help',600,520,'scrollbars=yes')";
	searchbox = getSearchBox(help);

	img1 = document.createElement("img");
	img1.className="sectionSS";
	img1.src="/media/images/2/search/searchfavs.gif"
	img2 = document.createElement("img");
	img2.className="iconaddfavs";
	img2.src="/media/images/2/diary/icon_myfavs.gif";		
	
	// Main div
	div1    = document.createElement("div");
	div1.id = "pane_search_extend_content";
	
	// Content
	div3    = document.createElement("div");
	div3.id = "pane_centre_content";
	
	// Header - Title "Results > ", etc
	div4    = document.createElement("div");
	div4.id = "pane_centre_header";
	
	// Main body
	div5    = document.createElement("div");
	div5.id = "pane_centre_body";
	
	div6 = document.createElement("div");
	div6.id = "searchproductlist";	
	
	p1 = document.createElement("p");
	p1.id="moreTitle";
	p1.innerHTML="Click on an item below...";


	div5.appendChild(p1);
	div5.appendChild(div6);
	
	// Create new divs, for the curved borders
	cbDiv2 = cgd_top();
	cbDiv8 = cgd_bottom();
	cbDiv1 = document.createElement("div");
	cbDiv1.className = "cProductBox";		

	cbDiv7 = document.createElement("div");
	cbDiv7.className = "cBoxContent";
	
	div8 = document.createElement("div");
	
	//top
	cbDiv1.appendChild(cbDiv2);
	
	favouritesTable(jsonObj.ajaxJSON,div8);

	cbDiv7.appendChild(div8);
	cbDiv1.appendChild(cbDiv7);
	
	//bottom
	cbDiv1.appendChild(cbDiv8)
	
	
	div6.appendChild(cbDiv1)
	
	closediv    = document.createElement("div");
	closediv.id = "pane_search_extend_close";
	if(ptrA >1)
	{ 
		ax_button(closediv,"javascript:bc_go_back();","/media/images/2/buttons/gobackup.gif")
	}
	ax_button(closediv,"javascript:closeSearchBox()","/media/images/2/buttons/btnsearchclose.gif");	
	
	
	div1.appendChild(div3);
	div3.appendChild(img1);
	div3.appendChild(img2);
	div3.appendChild(div4);
	div3.appendChild(div5);			
	div3.appendChild(closediv);
	
	searchbox.appendChild(div1);
	
}

function SearchManAddCallBack(response, args){

	jsonObj = response.text.parseJSON();
	help = "popup('/popups/search_manadd.html','Help',600,520,'scrollbars=yes')";
	searchbox = getSearchBox(help);

	img1 = document.createElement("img");
	img1.className="sectionSS";
	img1.src="/media/images/2/search/searchmanadd.gif"
	
	img2 = document.createElement("img");
	img2.className="iconmanadd";
	img2.src="/media/images/diary/icon_manadd.gif";	
	
	// Main div
	div1    = document.createElement("div");
	div1.id = "pane_search_extend_content";
	
	// Content
	div3    = document.createElement("div");
	div3.id = "pane_centre_content";
	
	// Header - Title "Results > ", etc
	div4    = document.createElement("div");
	div4.id = "pane_centre_header";
	
	// Main body
	div5    = document.createElement("div");
	div5.id = "pane_centre_body";
	
	div6 = document.createElement("div");
	div6.id = "searchproductlist";	
	
	p1 = document.createElement("p");
	p1.id="moreTitle";
	p1.innerHTML="Click on a product below...";


	div5.appendChild(p1);
	div5.appendChild(div6);
	
	// Create new divs, for the curved borders
	cbDiv2 = cgd_top();
	cbDiv8 = cgd_bottom();
	cbDiv1 = document.createElement("div");
	cbDiv1.className = "cProductBox";		

	cbDiv7 = document.createElement("div");
	cbDiv7.className = "cBoxContent";
	
	div8 = document.createElement("div");
	
	//top
	cbDiv1.appendChild(cbDiv2);
	
	manaddTable(jsonObj.ajaxJSON,div8);
	
	cbDiv7.appendChild(div8);
	cbDiv1.appendChild(cbDiv7);
	
	//bottom
	cbDiv1.appendChild(cbDiv8)
	
	
	div6.appendChild(cbDiv1)
	
	closediv    = document.createElement("div");
	closediv.id = "pane_search_extend_close";
	if(ptrA >1)
	{ 
		ax_button(closediv,"javascript:bc_go_back();","/media/images/2/buttons/gobackup.gif")
	}
	ax_button(closediv,"javascript:closeSearchBox()","/media/images/2/buttons/btnsearchclose.gif");	
	
	
	div1.appendChild(div3);
	div3.appendChild(img1);
	div3.appendChild(img2);
	div3.appendChild(div4);
	div3.appendChild(div5);			
	div3.appendChild(closediv);
	
	searchbox.appendChild(div1);
	
}

function recipeTable(jsonObj,div8){

	returnTable = document.createElement("table"); 
	returnTable.style.width="465px";
	returnTable2 = document.createElement("table"); 
	returnTable2.style.width="465px";	
	div = document.createElement("div"); 
	div.style.width="490px";
	div.style.height="135px";
	div.style.overflow = "auto";
	
	tbody = document.createElement("tbody");
	thead = document.createElement("thead")
	tr = document.createElement("tr")
	td1 = document.createElement("td")
	td1a = document.createElement("td")
	td1.className = "foodtype";
	td1.innerHTML="Description";
	td1a.innerHTML="Serving size";
	td1a.className = "servings"	
	td2 = document.createElement("td")
 	td2.innerHTML="kCal";
	td2.className = "kcal";
	td3 = document.createElement("td");
	td3.innerHTML="Fat(g)";
	td3.className = "fatg";


	if(jsonObj.length<1){
		td1.innerHTML='<span style="font-weight:normal;">There are no recipes for this occasion.<br><br>Please <a href="/process.jsp?section=recipe&action=add">click here</a> to create a recipe, or <a href="javascript:SearchRecipeMain(22613, '+"'My Recipes'"+');">select a different category</a>.</span>';
		tr.appendChild(td1);
		tbody.appendChild(tr);
		returnTable.appendChild(tbody);		
	}
	else{
	
		for(i=0;i<jsonObj.length;i++){
			var x=returnTable2.insertRow(-1)		
			if(jsonObj[i].recipe_name){
				var pkcal = Math.round(jsonObj[i].kcalperserving);  
				var pfatg = MA_fix(jsonObj[i].fatgperserving);	
				
				var td=x.insertCell(0);
				var str='<a href="javascript:SearchRecipeServings('+"'"+jsonObj[i].recipe_id+"'"+')">'+jsonObj[i].recipe_name;+'</a>'
				td.className = "foodtype";
				td.innerHTML = str;
			
				var td=x.insertCell(1);
				td.className = "servings";
				td.innerHTML = jsonObj[i].serving_formula;

				var td=x.insertCell(2);
				td.className = "kcal";
				td.innerHTML = pkcal;
				
				var td=x.insertCell(3);
				td.className = "fatg";
				td.innerHTML = pfatg;
			}
	
		}	
	
		tr.appendChild(td1);
		tr.appendChild(td1a);
		tr.appendChild(td2);
		tr.appendChild(td3);
		thead.appendChild(tr);
		returnTable.appendChild(thead);
	}
	div8.appendChild(returnTable);
	div.appendChild(returnTable2);
	div8.appendChild(div);

	return div8;

}

function favouritesTable(jsonObj){
	returnTable = document.createElement("table"); 
	returnTable.style.width="465px";
	returnTable2 = document.createElement("table"); 
	returnTable2.style.width="465px";	
	div = document.createElement("div"); 
	div.style.width="490px";
	div.style.height="135px";
	div.style.overflow = "auto";	
	
	tbody = document.createElement("tbody");
	thead = document.createElement("thead")
	tr = document.createElement("tr")
	td1 = document.createElement("td")
	td1a = document.createElement("td")
	td1.innerHTML="Description";
	td1.className = "foodtype";
	td1a.innerHTML="Serving size";
	td1a.className = "servings"
	td2 = document.createElement("td")
 	td2.className = "kcal";
	td2.innerHTML="kCal";
	td3 = document.createElement("td");
	td3.className = "fatg";
	td3.innerHTML="Fat(g)";

	if(jsonObj.length<1){
		td1.innerHTML='<span style="font-weight:normal;">You currently have no Favourites in this folder. To add a food to your Favourites simply click on <img src="/media/images/lowres/fav.gif" /> next to a food entry in your diary.</span>';
		tr.appendChild(td1);
		tbody.appendChild(tr);
		returnTable.appendChild(tbody);		
	}	
	else{
		for(i=0;i<jsonObj.length;i++){
			var x=returnTable2.insertRow(-1)				
			
			if(jsonObj[i].favourites_id){
				var pkcal = Math.round(jsonObj[i].kcal);  
				var pfatg = MA_fix(jsonObj[i].fatg);
				var td=x.insertCell(0);
				var str='<a href="javascript:SearchFavouritesServings('+"'"+jsonObj[i].favourites_id+"'"+')">'+jsonObj[i].product_description;+'</a>'
				td.className = "foodtype";
				td.innerHTML = str;
			
				var td=x.insertCell(1);
				td.className = "servings";
				td.innerHTML = jsonObj[i].serving_size_description;

				var td=x.insertCell(2);
				td.className = "kcak";
				td.innerHTML = pkcal;
				
				var td=x.insertCell(3);
				td.className = "fatg";
				td.innerHTML = pfatg;
			}
	
		}	
	
		tr.appendChild(td1);
		tr.appendChild(td1a);
		tr.appendChild(td2);
		tr.appendChild(td3);
		thead.appendChild(tr);
		returnTable.appendChild(thead);
	}
	
	div8.appendChild(returnTable);
	div.appendChild(returnTable2);
	div8.appendChild(div);
	
	return div8;

}

function manaddTable(jsonObj){
	returnTable = document.createElement("table"); 
	returnTable.style.width="465px";
	returnTable = document.createElement("table"); 
	returnTable.style.width="465px";
	returnTable2 = document.createElement("table"); 
	returnTable2.style.width="465px";	
	div = document.createElement("div"); 
	div.style.width="490px";
	div.style.height="135px";
	div.style.overflow = "auto";	
	
	tbody = document.createElement("tbody");
	thead = document.createElement("thead")
	tbody = document.createElement("tbody");
	tr = document.createElement("tr")
	td1 = document.createElement("td")
	td1a = document.createElement("td")
	td1.innerHTML="Description";
	td1.className = "foodtype";
	td1a.innerHTML="Serving size";
	td1a.className = "servings";
	td2 = document.createElement("td")
 	td2.innerHTML="kCal";
	td2.className = "kcal";
	td3 = document.createElement("td")
	td3.innerHTML="Fat(g)";
	td3.className = "fatg";

	if(jsonObj.length<1){
		td1.innerHTML='<span style="font-weight:normal;">You currently have no manually added items in your folder.<br>Please <a href="javascript:MA_create('+"'diary'"+');">click here</a> to create a new item.</span>';
		tr.appendChild(td1);
		tbody.appendChild(tr);
		returnTable.appendChild(tbody);		
	}	
	else{
		for(i=0;i<jsonObj.length;i++){
			var x=returnTable2.insertRow(-1)				

			if(jsonObj[i].user_product_id){
				var pkcal = Math.round(jsonObj[i].kcal);  
				var pfatg = MA_fix(jsonObj[i].fatg);
				var td=x.insertCell(0);
				var str='<a href="javascript:SearchManAddServings('+"'"+jsonObj[i].user_product_id+"'"+')">'+jsonObj[i].product_description;+'</a>'
				td.className = "foodtype";
				td.innerHTML = str;
				
				var td=x.insertCell(1);
				td.className = "servings";
				td.innerHTML = jsonObj[i].serving_formula;

				var td=x.insertCell(2);
				td.className = "kcal";
				td.innerHTML = pkcal;
				
				var td=x.insertCell(3);
				td.className = "fatg";
				td.innerHTML = pfatg;
			}
	
		}	
	
		tr.appendChild(td1);
		tr.appendChild(td1a);
		tr.appendChild(td2);
		tr.appendChild(td3);
		thead.appendChild(tr);
		returnTable.appendChild(thead);
	}
	div8.appendChild(returnTable);
	div.appendChild(returnTable2);
	div8.appendChild(div);
	
	return div8;

}


/* updater functions */
function updateSearchList(key,desc){
	searchbox = getSearchOutBox1();
	var url = "Action?section=diary&action=search&subaction=exercise&desc="+desc+"&key="+key;

	callUpdaterURL2(url,searchbox.id,displaySearch)
	
	//return false;
	
}

function exerciseBL(key,desc){
	searchbox = getSearchOutBox1();

	var url = "Action?section=diary&action=search&subaction=exerciseBL&key="+key+"&desc="+desc;
	callUpdaterURL2(url,searchbox.id,displaySearch)
	
	//return false;
}

function exerciseActivity(key,desc,kph){
	searchbox = getSearchOutBox1();

	var url = "Action?section=diary&action=search&subaction=exerciseActivity&key="+key+"&desc="+desc+"&kph="+kph;	
	
	callUpdaterURL2(url,searchbox.id,displaySearch)
	//return false;	
}

function displaySearch(){
	//alert("success");	
}

function quantityBox (obj,exid,diaryid,duration,kcal,view){
	
	if(!obj) return ;
	var pos = getAbsolutePosition(obj);
	removeDiaryServingsBox();
	
	div = document.createElement("div");
	div.className = "quanBox";
	div.id = "extendedQuanBox";	
	
	div.style.top = (pos[1] ) + "px";
	div.style.left = (pos[0] +103) + "px";
	div.style.width = 170 + "px";

	document.body.appendChild(div);	
	
	var url = "/ajax/diary/view/includes/quantityBox.jsp?exid="+exid+"&diaryid="+diaryid+"&duration="+duration+"&kcal="+kcal+"&view="+view;
	var id="extendedQuanBox";
	
	callUpdaterURL(url,id,displaySearch)
	
	return false;

}

function changeExercise(duration,kcal,diaryid){
	
	var f;
	f = document.forms["exerciseUpdate"];
	f.duration.value=duration;
	f.kcal.value=kcal;
	f.diaryid.value=diaryid;
	f.submit();

}

function productSearch(p){
	
	var desc = p.txt_productsearch.value;
	if(section=='recipe')
	{
		if(sectionID==0)
		{
			alert("Please make sure you have selected a recipe first.");
			return false;
		}
	}
	if (desc.length < 3)
	 {
		alert("Please enter 3 or more characters to perform a search");
		return false;
	 } 

	searchbox = getSearchBox();
	
	var url = "Action?section=diary&action=search&subaction=product&desc="+escape(desc);
	callUpdaterURL2(url,searchbox.id,displaySearch)
	
	return false;	
}

function productAutoSearch(p){
	
	var desc = p;


	if(section=='recipe')
	{
		if(sectionID==0)
		{
			alert("Please make sure you have selected a recipe first.");
			return false;
		}
	}
	
	var pl = $("searchproductlist");
	if(pl)
	{
		var url = "Action?section=diary&action=search&subaction=productL&desc="+escape(desc);
		callUpdaterURL2(url,pl.id,displaySearch)
	}
	else
	{
		searchbox = getSearchBox();
		var url = "Action?section=diary&action=search&subaction=product&desc="+escape(desc);
		callUpdaterURL2(url,searchbox.id,displaySearch)
	}
	return false;	
}


function productItem(key,wizid){
	
	var url = "Action?section=diary&action=search&subaction=productItem&desc="+escape(desc)+"&key="+key;
	
	return false;
}






















