/**
 * 気になるリストに追加
 */

/**
 * 商品を「気になるリスト」に追加する。
 */
function addToFavoriteList(productId, cdId) {

	var cbParam = new Array();

	// ログイン済みの場合はそのまま登録する。
	
	name = "kakehashi_autologin=";
		
	var curCookie = document.cookie+";";
	var start = curCookie.indexOf(name);
	if (start != -1) {
		end = curCookie.indexOf(";",start);
		loginValue = unescape(curCookie.substring(start + name.length, end));
	}

	if ( loginValue ) {
		_addToFavoriteList(productId);
		return;
	}

	// ログインページへ遷移
	location.href="https://kakereco.com/login.php?cd_id=" + cdId;

}


function _addToFavoriteList(productId) {
				
		$.ajax({
			url: 'api.php',
			type: 'GET',
			data: {
				'cmd': 'add_mylist',
				'product_id': productId
			},
			dataType: 'json',
			timeout: 5000,
			complete: function(data) {
	
				eval(data.responseText);

				if ( add_mylist['result'] != 'OK' ) {

						alert(add_mylist['message']);
						return;
				
				}

				$('p#stock_'+productId).html('<button class=\"btn btn-large disabled\"><i class=\"icon-bookmark icon-white\"></i> リストに追加しました。</button>');


			}

		});

}


function deleteFavoriteList(productId, cdId) {

	// ログイン済みの場合はそのまま削除する。

	name = "kakehashi_autologin=";
		
	var curCookie = document.cookie+";";
	var start = curCookie.indexOf(name);
	if (start != -1) {
		end = curCookie.indexOf(";",start);
		loginValue = unescape(curCookie.substring(start + name.length, end));
	}
		
	if ( loginValue ) {
		_deleteFavoriteList(productId);
		return;
	}

	// ログインページへ遷移
	location.href="https://kakereco.com/login.php?cd_id=" + cdId;

}


function _deleteFavoriteList(productId) {

		$.ajax({
			url: 'api.php',
			type: 'GET',
			data: {
				'cmd': 'delete_mylist',
				'product_id': productId
			},
			dataType: 'json',
			timeout: 5000,
			complete: function(data) {
			
				eval(data.responseText);
				
				if ( delete_mylist['result'] != 'OK' ) {

						alert(delete_mylist['message']);
						return;
				
				}

				$('p#stock_'+productId).html('<a href="#" class=\"btn disabled\"><i class=\"icon-bookmark icon-white\"></i> 削除しました。</a>');


			}

		});

}
