
setUserToken = function(currentPage ,redirectURL) {
	if (readCookie("currentUserToken")) {
		var pageUrl = "/registration/processlogin";
		
		$.ajax( {
					url : pageUrl,
					type : 'post',
					data : {
						"screen" : "updateToken",
						"loginklogid" : readCookie("userId"),
						"passwordklogid" :readCookie("currentUserToken"),
						"passmode":"T"
					},
					error : function(request, error) {
						alert('Internal Error Occured ');
					},
					success : function(userData) {
						
						var jsonObj = jQuery.parseJSON(userData);
						if (jsonObj.status == 'SUCCESS') {
							userToken = (jsonObj.userinfo != undefined) ? jsonObj.userinfo[0].userToken: "";
							if(userToken!=readCookie("currentUserToken")){
								  createCookie("currentUserToken",userToken,0,"localhost");
							}
						} else if(jsonObj.status == 'TOKEN_ERROR' && currentPage!=null && currentPage.indexOf("communitylogin") == -1 ) {
									
							        eraseCookieWithDomain("username",window.location.hostname); 
								    eraseCookieWithDomain("lastname",window.location.hostname);
								    eraseCookieWithDomain("profileId",window.location.hostname); 
									eraseCookieWithDomain("emailId",window.location.hostname);
								    //Clear cookie Remember me upon logout
								    eraseCookieWithDomain("remember_me",window.location.hostname);
								    eraseCookieWithDomain("rememberme_crdate",window.location.hostname);
								    //Used for Recipe
								    eraseCookieWithDomain("userId",window.location.hostname);
								    eraseCookieWithDomain("currentUserToken",window.location.hostname);
								    if(readCookie("username")){
								        document.getElementById("loggedin11").style.display = "block";
								        document.getElementById("notlogged11").style.display = "none";  
								    }
								    else
								    {
								        document.getElementById("notlogged11").style.display = "block";
								        document.getElementById("loggedin11").style.display = "none";
								    }
									window.location = redirectURL;
						}
					}
				});
	}
};

function addRecipe(recipeId, redirectUrl) {

	if (readCookie("profileId")) {
		var pageUrl = "/registration/processlogin";
	
		$.ajax( {
			url : pageUrl,
			type : 'post',
			data : {
				"screen" : "addRecipe",
				"profileId" : unescape(readCookie("profileId")),
				"userName":readCookie("username"),
				"key":unescape(readCookie("key")),				
				"recipeId" : recipeId,
				"loginklogid" : unescape(readCookie("userId")),
				"passwordklogid" : unescape(readCookie("currentUserToken")),
				"passmode":"T"
			},
			beforeSend : function() {
				$('#ajaxLoading').show();
			},
			error : function(request, error) {
				alert('Error - Some error occurred while adding recipe');
			},
			success : function(recipeinfo) {
				var jsonObj = jQuery.parseJSON(recipeinfo);
				if (jsonObj.status == 'SUCCESS') {
					// alert('Recipe is added successfully');
			location.href = redirectUrl + ".html";
		} else {
			alert('Some error occurred while adding recipe');
		}
	},
	complete : function(recipeinfo) {
		$('#ajaxLoading').hide();
	}
		});
	} else {
		location.href = "/communitylogin.html?nextUrl=" + redirectUrl + ".html";
	}
}

function addRecipeBoxComfortFoodMakevor(recipeId, redirectUrl) {
	if (readCookie("profileId")) {
		var pageUrl = "/registration/processlogin";
		$
				.ajax( {
					url : pageUrl,
					type : 'post',
					data : {
						"screen" : "addRecipe",
						"profileId" : unescape(readCookie("profileId")),
						"userName":readCookie("username"),
						"key":unescape(readCookie("key")),
						"recipeId" : recipeId,
						"loginklogid" : unescape(readCookie("userId")),
						"passwordklogid" : unescape(readCookie("currentUserToken")),
						"passmode":"T"
					},
					beforeSend : function() {
						document.getElementById("recipebox" + recipeId).innerHTML = "Processing...";
					},
					error : function(request, error) {
						alert('Error - Some error occurred while adding recipe');
					},
					success : function(recipeinfo) {
						var jsonObj = jQuery.parseJSON(recipeinfo);
						if (jsonObj.status == 'SUCCESS') {
							document.getElementById("recipebox" + recipeId).innerHTML = "In your recipe box";
						} else {
							document.getElementById("recipebox" + recipeId).innerHTML = "Unable to add recipe to box";
						}
					},
					complete : function(recipeinfo) {
						$('#ajaxLoading').hide();
					}
				});

	} else {

		location.href = "/communitylogin.html?nextUrl=" + redirectUrl + ".html";

	}

}

function deleteRecipe(recipeId, currentPagePath) {

	var pageUrl = "/registration/processlogin";
	$.ajax( {
		url : pageUrl,
		type : 'post',
		data : {
			"screen" : "deleteRecipe",
			"profileId" : unescape(readCookie("profileId")),
			"recipeId" : recipeId,
			"loginklogid" : unescape(readCookie("userId")),
			"passwordklogid" : unescape(readCookie("currentUserToken")),
			"passmode":"T"
		},
		beforeSend : function() {
			$('#ajaxLoading').show();
		},
		error : function(request, error) {
			alert('Some error occurred while deleting recipe');
		},
		success : function(recipeinfo) {
			getRecipes(currentPagePath);
		},
		complete : function(recipeinfo) {
			$('#ajaxLoading').hide();
		}
	});

}

function getRecipes(currentPagePath) {
	var profileId = readCookie("profileId");
	var pageUrl = "/registration/processlogin";
	$.ajax( {
		url : pageUrl,
		type : 'post',
		data : {
			"screen" : "getRecipes",
			"profileId" : unescape(readCookie("profileId")),
			"loginklogid" : unescape(readCookie("userId")),
			"passwordklogid" : unescape(readCookie("currentUserToken")),
			"passmode":"T"
		},
		beforeSend : function() {
			$('#ajaxLoading').show();
		},
		error : function(request, error) {
			alert('Some error occurred while adding recipe');
		},
		success : function(recipeinfo) {
			var jsonObj = jQuery.parseJSON(recipeinfo);
			var processUrl = currentPagePath + ".recipelist.html";
			if (jsonObj.status == 'SUCCESS') {
				if (jsonObj.recipes.length > 0) {
					$.ajax( {
						url : processUrl,
						type : 'get',
						data : {
							"ids" : jsonObj.recipeIds
						},
						beforeSend : function() {
							$('#ajaxLoading').show();
						},
						error : function(request, error) {
							// alert('Some error occurred while adding recipe');
					},
					success : function(msg) {
						$('#divRecipes1').html(msg);
					},
					complete : function() {
						$('#ajaxLoading').hide();
					}
					});
				}
			}
		},
		complete : function() {
			$('#ajaxLoading').hide();
		}
	});

}

function isRecipeAssociated(currentPagePath, pRecipeId) {
	var isAssociated = false;
	var pageUrl = "/registration/processlogin";
	$.ajax( {
				url : pageUrl,
				type : 'post',
				data : {
					"screen" : "getRecipes",
					"profileId" : unescape(readCookie("profileId")),
					"recipeId" : pRecipeId,
					"loginklogid" : unescape(readCookie("userId")),
					"passwordklogid" : unescape(readCookie("currentUserToken")),
					"passmode":"T"
				},
				beforeSend : function() {
					$('#ajaxLoading').show();
				},
				error : function(request, error) {
					// alert('Some error occurred while adding recipe');
				},
				success : function(recipeinfo) {
					var jsonObj = jQuery.parseJSON(recipeinfo);
					var processUrl = currentPagePath + ".recipelist.html";
					if (jsonObj.status == 'SUCCESS') {
						if (jsonObj.recipes.length > 0) {
							for ( var i = 0; i < jsonObj.recipes.length; i++) {
								if (jsonObj.recipes[i].recipeId == pRecipeId) {
									$('#recipebox').html('<li class="rbottom">In your recipe box</li>');
									isAssociated = true;
									break;
								}
							}
						}
					}
				},
				complete : function() {
					if (isAssociated) {
						$('#recipebox').html(
								'<li class="rbottom">In your recipe box</li>');
					}
					$('#ajaxLoading').hide();
				}
			});
}
