
window.onload = initPage;

// variables whose values are passed from the aspx page
var isAuthenticated;
var existsInRecipeBox;
var RecipeShoppingListCount;

function initPage (){

	if (document.getElementById("recipeBtns") != undefined){

		var recipeBoxBtn = document.getElementById("recipeBtns").getElementsByTagName('span')[0];
		var shoppingListBtn = document.getElementById("recipeBtns").getElementsByTagName('span')[1];

		// authentication check
		if (isAuthenticated == false){
			// failed - hide the buttons
			recipeBoxBtn.style.display = 'none';
			shoppingListBtn.style.display = 'none';
		}
		else {
			document.getElementById("recipeBtns").style.display="block";
			// passed 
			if (existsInRecipeBox != undefined && existsInRecipeBox == true){

				// recipe previously added to recipe box
				// print msg and set class accordingly

				var throwawayNode = recipeBoxBtn.removeChild(recipeBoxBtn.childNodes[0]);

				recipeBoxBtn.appendChild(document.createTextNode('This recipe is in your Recipe Box'));
				recipeBoxBtn.className = 'successMsg2';
				recipeBoxBtn.onclick = '';
			}
			else {
				recipeBoxBtn.onclick = addToRecipeBox;
			}

			if (RecipeShoppingListCount != undefined && RecipeShoppingListCount > 0){

				// recipe ingredients previously added to recipe box
				// print msg and set class accordingly
				
				var ingredientListLength = document.getElementById("BlogPostContentArea").getElementsByTagName('li').length;	
				if (ingredientListLength == RecipeShoppingListCount){

					var throwawayNode2 = shoppingListBtn.removeChild(shoppingListBtn.childNodes[0]);

					shoppingListBtn.appendChild(document.createTextNode('Ingredients are in your Shopping List'));
					shoppingListBtn.className = 'successMsg';
					shoppingListBtn.onclick = "";
				}
				else {
					shoppingListBtn.onclick = addToCart;
				}
			}
			else {
				shoppingListBtn.onclick = addToCart;
			}
		}
		
		if (isAuthenticated == false){
			document.getElementById("ratingContainer").onclick = showRateMsg;
			document.getElementById("ratingContainer").className = "nonAuthenticated";
			document.getElementById("nonAuthMsgContainer").style.display = "block";
			
			
			
		}
	}
}


function showRateMsg (){
	Effect.Appear(document.getElementById("rateThisRecipeMsg"), { duration: 0.5 });
}

function hideRateMsg (){
	Effect.Fade(document.getElementById("rateThisRecipeMsg"), { duration: 0.5 });
}

function showDivs (showDivs) {

    if (showDivs == "RecentActivityRecipe"){
        document.getElementById("RecentActivityRecipe").style.display = "block";
        document.getElementById("RecentActivityProducts").style.display = "none";
        document.getElementById("RecentRecipes").className = "mostViewedActive";
        document.getElementById("RecentProducts").className = "";
    }
    else {
        document.getElementById("RecentActivityProducts").style.display = "block";
        document.getElementById("RecentActivityRecipe").style.display = "none";
        document.getElementById("RecentProducts").className = "mostCommentedActive";
        document.getElementById("RecentRecipes").className = "";
    }
}

function showCommentForm() {
	document.getElementById("CommonFormArea").style.display = "block";
}


function addToRecipeBox () {

	var uri = appPath + '/MyStuff/setRecipeBox.aspx?Action=add&postId=' + postId;
	
	var request = (typeof(XMLHttpRequest) != "undefined") ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
	request.open("GET", uri, false);

	var theButton = document.getElementById("recipeBtns").getElementsByTagName('span')[0];
	var throwawayNode = theButton.removeChild(theButton.childNodes[0]);

	theButton.appendChild(document.createTextNode('Recipe has been added'));
	theButton.className = 'successMsg2';
	
	request.send(null);
				
	if(!request.getResponseHeader("Date"))
	{
		var cached = request;

		request = (typeof(XMLHttpRequest) != "undefined") ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");

		var ifModifiedSince = cached.getResponseHeader("Last-Modified");
		ifModifiedSince = (ifModifiedSince) ? ifModifiedSince : new Date(0); // January 1, 1970

		request.open("GET", uri, false);

		request.setRequestHeader("If-Modified-Since", ifModifiedSince);

		request.send("");
		if (request.status == 304)
		{
			request = cached;
		}
	}
	            var tag_url="http://fls.doubleclick.net/activityi;src=1278944;type=jimmy799;cat=addto101;ord=1;num="+Math.floor(Math.random()*999999);
				if(document.getElementById("DCLK_FLDiv")){var flDiv=document.getElementById("DCLK_FLDiv");}
				else{var flDiv=document.body.appendChild(document.createElement("div"));flDiv.id="DCLK_FLDiv";flDiv.style.display="none";}
				var DCLK_FLIframe=document.createElement("iframe");
				DCLK_FLIframe.id="DCLK_FLIframe_"+Math.floor(Math.random()*999999);
				DCLK_FLIframe.src=tag_url;
				flDiv.appendChild(DCLK_FLIframe);
}


function addToCart (e) {

	var ingredientContent = "";
	

	// NOTE! this assumes that any '<li>s' within the blog post body are individual ingredients.
	var ingredientItemList = document.getElementById("BlogPostContentArea").getElementsByTagName('li');

	if (ingredientItemList != undefined){

		var delimiter = "";

		for (var i=0;i<ingredientItemList.length;i++){
			ingredientContent = ingredientContent + delimiter + trim(ingredientItemList[i].innerHTML);
			
			if (delimiter == "") delimiter = "|";
		}

		if (ingredientContent != ""){
		
			ingredientContent = replaceVars(ingredientContent);
			ingredientContent = encodeURI(ingredientContent); 

			var uri = appPath + '/MyStuff/setIngredients.aspx?Action=add&postId=' + postId + '&ingredientString=' + ingredientContent;

			var request = (typeof(XMLHttpRequest) != "undefined") ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
			request.open("GET", uri, false);

			request.send(null);

			if(!request.getResponseHeader("Date"))
			{
				var cached = request;

				request = (typeof(XMLHttpRequest) != "undefined") ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");

				var ifModifiedSince = cached.getResponseHeader("Last-Modified");
				ifModifiedSince = (ifModifiedSince) ? ifModifiedSince : new Date(0); // January 1, 1970

				request.open("GET", uri, false);

				request.setRequestHeader("If-Modified-Since", ifModifiedSince);

				request.send("");
				if (request.status == 304)
				{
					request = cached;
				}
			}

			var theButton = document.getElementById("recipeBtns").getElementsByTagName('span')[1];
			var throwawayNode = theButton.removeChild(theButton.childNodes[0]);

			theButton.appendChild(document.createTextNode('Items have been added'));
			theButton.className = 'successMsg';
		}
	}
            	var tag_url="http://fls.doubleclick.net/activityi;src=1278944;type=jimmy799;cat=addto232;ord=1;num="+Math.floor(Math.random()*999999);
				if(document.getElementById("DCLK_FLDiv")){var flDiv=document.getElementById("DCLK_FLDiv");}
				else{var flDiv=document.body.appendChild(document.createElement("div"));flDiv.id="DCLK_FLDiv";flDiv.style.display="none";}
				var DCLK_FLIframe=document.createElement("iframe");
				DCLK_FLIframe.id="DCLK_FLIframe_"+Math.floor(Math.random()*999999);
				DCLK_FLIframe.src=tag_url;
				flDiv.appendChild(DCLK_FLIframe);
          
}

// gets a reference to the clicked item
function getTarget(e,element)
{
	if (!e) var e = window.event;
	if (e.target) var tg = e.target;
	else if (e.srcElement) var tg = e.srcElement;
	while (tg.nodeName != element) // Safari
		tg = tg.parentNode;
		
	// returns the item represented by var tg
	return tg;
}

function replaceVars(str){
	str = str.replace(","," _comma");
	str = str.replace("&"," _amp");
	return str;
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}







