/*************************************
 *  メニューを評価する
 *************************************/
var menuEvaluation_idle = true;

/**
 *  クチコミ評価の追加
 */
function getMenuEvaluationString(value_)
{
    switch (value_)
    {
        case 1:
            return "参考になった";
        case -1:
            return "参考にならなかった";
    }
}

/**
 *  メニューを評価して、ページをリロードする。
 */
function addMenuEvaluation(userId_,
                           tenantId_,
                           menuId_, 
                           value_,
                           elementId_)
{
	if (menuEvaluation_idle) 
	{
		menuEvaluation_idle = false;

		jQuery.post("/service/index.php",
	  		{ 	
	  			action: "add_menu_evaluation",
				user_id: userId_,
				tenant_id: tenantId_,
				menu_id: menuId_,
				value: value_
			},
	  		function(json)
	  		{
				menuEvaluation_idle = true;
				
				var data = eval('(' + json + ')');
				
				if (data["code"] == 0)
				{
					var jsonButtons = getElement_MenuEvaluation_AlreadyEvaluated(tenantId_, menuId_);
					
					var buttons = eval('(' + jsonButtons + ')');
					
					if (buttons["code"] == 0)
					{
						jQuery("#" + elementId_ + " .buttons").html(buttons["pane"]);
					}
					else
					{
						alert("エレメントの取得時に問題が起こりました。");
					}
				}
				else
				{
					alert("登録時に問題が起こりました。");
				}
	  		},
	  		"JSON");
	}
}

/**
 *  メニューの評価を削除する
 */
function removeMenuEvaluation(userId_,
                              tenantId_,
                              menuId_,
                              elementId_)
{
	if (!window.confirm('評価を削除しますがよろしいですか？'))
	{
		return 0;
	}

	if (menuEvaluation_idle) 
	{
		menuEvaluation_idle = false;

		jQuery.post("/service/index.php",
	  		{ 	
	  			action: "remove_menu_evaluation",
	  			user_id: userId_,
				tenant_id: tenantId_,
				menu_id: menuId_
			},
	  		function(json)
	  		{
				menuEvaluation_idle = true;
				
				var data = eval('(' + json + ')');
				
				if (data["code"] == 0)
				{
					alert("あなたの評価は削除されました。");
					
					var jsonButtons = getElement_MenuEvaluationButtons(tenantId_, menuId_);
					
					var buttons = eval('(' + jsonButtons + ')');
					
					if (buttons["code"] == 0)
					{
						jQuery("#" + elementId_ + " .buttons").html(buttons["pane"]);
					}
					else
					{
						alert("エレメントの取得時に問題が起こりました。");
					}
				}
				else
				{
					alert("削除時に問題が起こりました。");
				}
	  		},
	  		"JSON");
	}
}

/**
 *  適切なメニュー評価ボタンを取得する
 */
function getElement_MenuEvaluationButtons(tenantId_, menuId_)
{
	return 	jQuery.ajax({
				url: "/service/index.php",
				data:{	action : "get_element_menu_evaluation_buttons",
						tenant_id : tenantId_,
						menu_id : menuId_},
				async: false
			}).responseText;
}

/**
 *  適切なメニュー評価ボタンを取得する
 */
function getElement_MenuEvaluation_AlreadyEvaluated(tenantId_, menuId_)
{
	return 	jQuery.ajax({
				url: "/service/index.php",
				data:{	action : "get_element_menu_evaluation_already_evaluated",
						tenant_id : tenantId_,
						menu_id : menuId_},
				async: false
			}).responseText;
}
