var sRecipeDetail;

function searchontitle(v)
{
	var vItem = v.parentNode;
	if (vItem != null)
	{
		var vItem2 = vItem.parentNode;
		if (vItem2!=null)
		{
			var vItem3 = vItem2.parentNode;
			if (vItem3!=null)
			{
				var vItem4 = vItem3.previousSibling;
				if (vItem4!=null)
				{
					var vItem5 = vItem4.previousSibling;
					if (vItem5!=null)
					{
						var vItem6 = vItem5.childNodes[0];
						if (vItem6!=null)
						{
							var vItem7 = vItem5.childNodes[0].childNodes[0];
							if (vItem7!=null)
							{
								if (vItem7.nodeType == 3)
								{
									var vline =vItem7.nodeValue;
									if (vline.length && vline.length < 80)
									{
										location.href = vItem7.parentNode.href;
										return;
									}
								}
							}
						}
					}
				}

				var vItem4a = vItem3.parentNode;
				if (vItem4a != null)
				{
					var vItem5a = vItem4a.parentNode;
					if (vItem5a)
					{
						var vItem6a=vItem5a.childNodes[3].childNodes[0];
						if (vItem6a!=null)
						{
							if (vItem6a.nodeType == 3)
							{
								var vline =vItem6a.nodeValue;
								if (vline.length && vline.length < 80)
								{
//									location.href = vItem6a.parentNode.href;
									return;
								}
							}							
						}
					}
				}

				// maybe it's internet explorer?
				if (vItem4!=null)
				{
					var vItem5 = vItem4;
					if (vItem5!=null)
					{
						var vItem6 = vItem5.childNodes[0];
						if (vItem6!=null)
						{
							var vItem7 = vItem5.childNodes[0].childNodes[0];
							if (vItem7!=null)
							{
								if (vItem7.nodeType == 3)
								{
									var vline =vItem7.nodeValue;
									if (vline.length && vline.length < 80)
									{
										location.href = vItem7.parentNode.href;
										return;
									}
								}
							}
						}
					}
				}

				if (vItem4a != null)
				{
					var vItem5a = vItem4a.parentNode;
					if (vItem5a)
					{
						var vItem6a=vItem5a.childNodes[1].childNodes[0];
						if (vItem6a!=null)
						{
							if (vItem6a.nodeType == 3)
							{
								var vline =vItem6a.nodeValue;
								if (vline.length && vline.length < 80)
								{
//									location.href = vItem6a.parentNode.href;
									return;
								}
							}							
						}
					}
				}

			}
		}
	}

}

function FindSimilarTo(vline)
{
	var sdesc2 = "";
	for (var ii=0; ii<vline.length; ii++)
	{
		if (vline.charAt(ii)=='(') break;
		if (vline.charAt(ii)==' ') 
		 sdesc2 += '+';
		else
		 sdesc2 += vline.charAt(ii);
	}
	sdesc2+="+thai";

	var sURL;
	if (sdesc2.length)
	{
		sURL = "http://www.google.com/custom?domains=www.khiewchanta.com&q=";
		sURL +=sdesc2;
		sURL += "&sa=Google+Search&sitesearch=www.khiewchanta.com&client=pub-9188875818498283&forid=1&ie=ISO-8859-1&oe=ISO-8859-1&safe=active&flav=0000&sig=VJfbOPvyax10vevr&cof=GALT%3A%23666666%3BGL%3A1%3BDIV%3A%238FABBE%3BVLC%3A666666%3BAH%3Acenter%3BBGC%3AFFFFFF%3BLBGC%3A999999%3BALC%3A666666%3BLC%3A666666%3BT%3A000000%3BGFNT%3A666666%3BGIMP%3A666666%3BLH%3A50%3BLW%3A269%3BL%3Ahttp%3A%2F%2Fwww.khiewchanta.com%2Fimages%2Fbanner.gif%3BS%3Ahttp%3A%2F%2Fwww.khiewchanta.com%2F%3BFORID%3A1&hl=en";

		location.href = sURL;
	}

}

function reWriteMobileIngredients()
{
	var ilist = document.images;

	for(var imageindex = 0; imageindex < ilist.length; imageindex++) 
	{
		var imgsrc =ilist[imageindex].src;
		var iLen = imgsrc.length;
		var sfile = imgsrc.substr(iLen-13);

		if( sfile == "_See_More.gif") 
		{
			sRecipeDetail ="<table  width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">\n";

			var vIngredientLine = ilist[imageindex].parentNode.childNodes;
			var numberOfChildNodes = vIngredientLine.length;
			var Ingstr="";
			for(i=0;i<numberOfChildNodes;i++) 
			{
				var vText = vIngredientLine[i].innerText;
				if (vText ==  undefined)
					vText = vIngredientLine[i].textContent;

				if (vText !=  undefined)
					Ingstr += vText;
			};
			sRecipeDetail += "<tr bgcolor=\"#caddea\"><td colspan=\"4\" align=\"center\">"+Ingstr +"</td></tr>";

			sRecipeDetail += "<tr bgcolor=\"#caddea\"><td>Metric</td><td>US</td><td>Description</td><td></td></tr>";

			var vItem = ilist[imageindex].parentNode.nextSibling;
			for (var i=1; i<100;i++)
			{
				if (vItem != null)
				{
					vItem = vItem.nextSibling;
					if (vItem==null) break;
					if (vItem.nodeType == 3)
					{
						var vline =vItem.nodeValue;
						if (vline.length)
						{
							parseline(vline);
						}
					}
				}
				else break;
			} ;

			sRecipeDetail+="</table>";


			var itemToDelete = ilist[imageindex].parentNode.parentNode;
			itemToDelete.innerHTML = sRecipeDetail;

		}
	}
}


function gotoFull()
{
	var h2 = location.href;
	var hnew = h2.replace("mobile", "archives");
	location.href = hnew;
}

function gotoMiniAuto()
{
	if (document.referrer.indexOf('mobile')==-1)
	{
		if (screen.width>=10 && screen.width<800)
		{
			var h2 = location.href;
			var hnew = h2.replace("archives","mobile");
			location.href = hnew;
		}
	}
}

function showdetails(v)
{
	sRecipeDetail="<table  width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\"><tr bgcolor=\"#caddea\"><td>Metric</td><td>US Units</td><td>Description</td><td></td></tr>\n";

	var vItem = v.parentNode.nextSibling;
	for (var i=1; i<100;i++)
	{
		if (vItem != null)
		{
			vItem = vItem.nextSibling;
			if (vItem==null) break;
			if (vItem.nodeType == 3)
			{
				var vline =vItem.nodeValue;
				if (vline.length)
				{
					parseline(vline);
				}
			}
		}
		else break;
	} ;
	sRecipeDetail+="</table>";

	window.open("/detail.html", "mw");
}

function IsNumber(sText)
{
   var ValidChars = "0123456789. ";
   var Char;

	for (i = 0; i < sText.length; i++) 
	{ 
	 Char = sText.charAt(i); 
	 if (ValidChars.indexOf(Char) == -1) return false;
	}
   return true;
}

var vScalar = 1.0;
var sUnitName = "";
var bValidConv = false;

function IsUnits(sValue, sUnits, sLine)
{
	bValidConv = false;
	if (sUnits == null) return false;
	var vValue = sValue *1;
	var sText2 = sUnits.toLowerCase();
	var sLine2 = sLine.toLowerCase();
	if (sText2 == "litres" || sText2 == "litre" || sText2 == "ltr"  || sText2 == "ltrs")
	{
		sUnitName="Cups";
		vScalar = 4.23;
		bValidConv = true;
		return true;
	}
	if (sText2 == "ml" || sText2 == "mls")
	{
		if (vValue >114)
		{
			sUnitName="Cups";
			vScalar = 0.00422675282;
			bValidConv = true;
			return true;		
		} else if (vValue>45)
		{
			sUnitName="Fluid oz";
			vScalar =34/1000;
			bValidConv = true;
			return true;
		} else if (vValue>14)
		{
			sUnitName="Tablespoons";
			vScalar =0.0676280451;
			bValidConv = true;
			return true;		
		} else 
		{
			sUnitName="Teaspoons";
			vScalar = 0.202884135;
			bValidConv = true;
			return true;
		}
	}
	if (sText2=="gms" || sText2=="gm" || sText2=="grammes"  || sText2=="gram")
	{
		if (sLine2.indexOf("sugar")>=0)
		{
			if (vValue>50)
			{
				vScalar = 1/(90*2.36);
				sUnitName="Cups";
				bValidConv = true;
				return true;
			} else
			{
				vScalar = 1/(90/6.666);
				sUnitName="Tablespoons";
				bValidConv = true;
				return true;
			}
		}
		else if (sLine2.indexOf("starch")>=0  || ( (sLine2.indexOf("rice")>=0 || sLine2.indexOf("corn")>=0) && sLine2.indexOf("flour")>=0 ) )
		{
			if (vValue>50)
			{
				vScalar = 1/(55*2.36);
				sUnitName="Cups";
				bValidConv = true;
				return true;
			} else
			{
				vScalar = 1/(55/6.666);
				sUnitName="Tablespoons";
				bValidConv = true;
				return true;
			}
		}
		else if (sLine2.indexOf("flour")>=0)
		{
			if (vValue>50)
			{
				vScalar = 1/(70*2.36);
				sUnitName="Cups";
				bValidConv = true;
				return true;
			} else
			{
				vScalar = 1/(70/6.666);
				sUnitName="Tablespoons";
				bValidConv = true;
				return true;
			}
		}
		else if (sLine2.indexOf("coconut")>=0 && (sLine2.indexOf("dried") >=0 || sLine2.indexOf("dessicated") >=0 || sLine2.indexOf("desiccated") >=0  || sLine2.indexOf("shredded") >=0 ) )
		{
			if (vValue>50)
			{
				vScalar = 1/(40*2.36);
				sUnitName="Cups";
				bValidConv = true;
				return true;
			} else
			{
				vScalar = 1/(40/6.666);
				sUnitName="Tablespoons";
				bValidConv = true;
				return true;
			}
		}
		else
		{
			vScalar = 0.0352739619;
			sUnitName="oz";
			bValidConv = true;
			return true;
		}
	}
	if (sText2=="head" || sText2=="heads" || sText2=="clove" || sText2=="cloves" || sText2=="pieces" || sText2=="piece" || sText2=="slice" || sText2=="slices" ||  sText2=="teaspoon" ||  sText2=="teaspoons" ||  sText2=="tablespoon" ||  sText2=="tablespoons")
	{
		bValidConv = false;
		return true;
	}

	return false;
}

function parseline(vLine)
{
	bValidConv = false;
	var vQuant="", vUnits="", vQuant2="", vUnits2="", vDescription="";
	var	vValue1, vValue2, vSep="", bMultiPart = false;

	var temp = new Array();
	temp = vLine.split(' ');
	
	for (var k=0; k<temp.length; k++)
	{
		if (temp[k].length && temp[k].charAt(0)==0)
		{
			temp[k] = temp[k].substring(1);
		}
	}

	var vFirst = new Array();
	vFirst = temp[0].split('/');

	if (vFirst.length >1)
	{
		bMultiPart = true;
		vSep = "/";
	}
	else
	{
		vFirst = temp[0].split('-');
		if (vFirst.length > 1)
		{
			bMultiPart = true;
			vSep = "-";		
		}
	}

	for (var m=0; m<vFirst.length; m++)
	{
		if (vFirst[m].length && vFirst[m].charAt(0)==0)
		{
			vFirst[m] = vFirst[m].substring(1);
		}
	}

	if (temp[0]=='pinch' || temp[0]=='Pinch')
	{
		vQuant = temp[0];
		for (j=1; j<temp.length; j++) { vDescription+=temp[j]; vDescription+=" ";};
		vUnits = "";
	}
	else if (temp[1]=='pinch' || temp[1]=='Pinch')
	{
		vQuant = temp[0]+ " "+ temp[1];
		for (j=2; j<temp.length; j++) { vDescription+=temp[j]; vDescription+=" ";};
		vUnits = "";
	}
	else if (vFirst.length>1 )
	{
		// multi part number
		if (IsNumber(vFirst[0]) && IsNumber(vFirst[1]) &&  IsUnits(vFirst[0], temp[1], vLine))
		{
			// recognized units
			if (bValidConv)
			{
				if (vSep=='-')
				{
					vQuant2 = Math.round(vFirst[0]*vScalar*10)/10;
					vQuant2 += "-";
					vQuant2 +=Math.round(vFirst[1]*vScalar*10)/10;
					vUnits2 = sUnitName;
				}
				else	// 1/2 etc.
				{
					if (vFirst[0]*vScalar*10/vFirst[1] < 10)
					{
						vQuant2 = Math.round(vFirst[0]*vScalar*100/vFirst[1] )/100;
					} else
					{
						vQuant2 = Math.round(vFirst[0]*vScalar*10/vFirst[1] )/10;
					}
					vUnits2 = sUnitName;
				}
			}
			{
				vQuant = temp[0];
				for (j=2; j<temp.length; j++) { vDescription+=temp[j]; vDescription+=" ";};
				vUnits = temp[1]
			}
		}
		else
		{
			vQuant = temp[0];
			vUnits = "";
			for (j=1; j<temp.length; j++) { vDescription+=temp[j]; vDescription+=" ";};
		}
	}
	else if ( IsUnits(temp[0], temp[1], vLine))
	{
		vQuant = temp[0];
		vUnits = temp[1];
		for (j=2; j<temp.length; j++) { vDescription+=temp[j]; vDescription+=" ";};
		if (bValidConv)
		{
			if (vQuant*vScalar*10 < 10)
			{
				vQuant2 = Math.round(vQuant*vScalar*100)/100;
			}
			else
			{
				vQuant2 = Math.round(vQuant*vScalar*10)/10;
			}
			vUnits2 = sUnitName;
		}
	}
	else if (IsNumber(temp[0]))
	{
		vQuant = temp[0];
		vUnits = "";
		
		// if this is garlic cloves, fix it up
		for (j=1; j<temp.length-1; j++)
		{
			if (temp[j].toLowerCase()=="garlic" && (temp[j+1].toLowerCase()=="clove" || temp[j+1].toLowerCase()=="cloves" ))
			{
				vUnits = temp[j+1];
				temp[j+1]="";
				break;
			}
		}
		
		for (j=1; j<temp.length; j++) { vDescription+=temp[j]; vDescription+=" ";};
	}
	else
	{
		for (j=0; j<temp.length; j++) { vDescription+=temp[j]; vDescription+=" ";};	
	}
	
	var sdesc3 = "";
	for (var kk=0; kk<vDescription.length; kk++)
	{
		if (vDescription.charAt(kk)=='(') break;
		 sdesc3 += vDescription.charAt(kk);
	}
	sRecipeDetail += "<tr  bgcolor=\"#ffffd9\"><td>"+vQuant+" "+vUnits+"</td><td>"+vQuant2+" "+vUnits2+"</td><td>"+sdesc3+"</td><td>";

	var sdesc2 = "";
	for (var ii=0; ii<vDescription.length; ii++)
	{
		if (vDescription.charAt(ii)=='(') break;
		if (vDescription.charAt(ii)==' ') 
		 sdesc2 += '+';
		else
		 sdesc2 += vDescription.charAt(ii);
	}

	if (sdesc2.length)
	{
		sRecipeDetail += "<a href=\"";
		sRecipeDetail += "http://www.khiewchanta.com/cgi-bin/mt-search.cgi?IncludeBlogs=1&search=";
		sRecipeDetail +=sdesc2;
		sRecipeDetail += "\"><img src=\"/sfor2.png\" width=\"23\" height=\"20\" border=\"0\"></a>";

//		sRecipeDetail += "http://www.google.com/custom?domains=www.khiewchanta.com&q=";
//		sRecipeDetail +=sdesc2;
//		sRecipeDetail += "&sa=Google+Search&sitesearch=www.khiewchanta.com&client=pub-9188875818498283&forid=1&ie=ISO-8859-1&oe=ISO-8859-1&safe=active&flav=0000&sig=VJfbOPvyax10vevr&cof=GALT%3A%23666666%3BGL%3A1%3BDIV%3A%238FABBE%3BVLC%3A666666%3BAH%3Acenter%3BBGC%3AFFFFFF%3BLBGC%3A999999%3BALC%3A666666%3BLC%3A666666%3BT%3A000000%3BGFNT%3A666666%3BGIMP%3A666666%3BLH%3A50%3BLW%3A269%3BL%3Ahttp%3A%2F%2Fwww.khiewchanta.com%2Fimages%2Fbanner.gif%3BS%3Ahttp%3A%2F%2Fwww.khiewchanta.com%2F%3BFORID%3A1&hl=en";
//		sRecipeDetail += "\"  target=\"_googlesearch\"><img src=\"/sfor2.png\" width=\"23\" height=\"20\" border=\"0\"></a>";
	}
	sRecipeDetail += "</td></tr>\n";

}


function CreateBookmarkLink(url) 
{
	title = "HRS Good Hotel Booker"; 

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); 
	}
	else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	}
}

