
var UserBookStatusCodes = {
    0: "Not Read",
    1: "To be read",
    2: "Currently reading",
    3: "Read"
}

var current_user_id;
var current_series_id;

function checkAllRead(series_id, user_id)
{

	$j.ajax({
        type: "POST",
        url: "../BookDataService.asmx/userBooksRead",
        data: "{'series_id':'"+ series_id +"', 'user_id':'"+ user_id +"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

			var countBooks = 0;

			var $inputs = $j("input:hidden");
				
			$inputs.each(function() {
			if($j(this).attr("name") == "bookID")
			{
        					
    				countBooks++;
        					
    			}
    			});

			var books = (typeof data.d) == 'string' ? eval('(' + data.d + ')') : data.d;
			
			//alert(books.length);				
															
			if(books.length<countBooks)
				$j("#markAllRead").html("<a href='#' onClick='markAllRead("+ user_id +", "+ series_id +");'>Mark all read</a>");

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d);
        }

    }); 
	
	
} /* end checkAllRead */


function markAllRead(user_id, series_id) {
	$j.ajax({
		type: "POST",
		url: "/SaveDataService.asmx/MarkAllBooksReadInSeries",
		data: "{'user_id':'" + user_id + "','series_id':'" + series_id + "'}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(data) {
		  window.location.reload();
			

			//$j("#membersHaveRead").html(data.d);

		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			// typically only one of textStatus or errorThrown 
			// will have info
			//this; // the options for this ajax request
			alert(XMLHttpRequest.d);
		}

	});

}

function markAllRead_old(user_id, series_id)
{
	$j("#markAllRead").html("&nbsp;");
	
	var $inputs = $j("input:hidden");
				
			$inputs.each(function() {
			if($j(this).attr("name") == "bookID")
			{
        			
        			//alert($j(this).val());		
        									
    				MarkAsReadOnSeries($j(this).val(), user_id, series_id, "userFollowingBook");
        					
    			}
    			});
	
} /* end markAllRead */

function membersHaveRead(book_id)
{
	$j.ajax({
        type: "POST",
        url: "../BookDataService.asmx/membersHaveRead",
        data: "{'book_id':'"+ book_id +"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            //alert(data.d);

            $j("#membersHaveRead").html(data.d);

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d);
        }

    });
} /* end membersHaveRead */

function membersFollowSeries(series_id)
{
	$j.ajax({
        type: "POST",
        url: "../BookDataService.asmx/membersFollowSeries",
        data: "{'series_id':'"+ series_id +"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            //alert(data.d);

            $j("#membersFollowSeries").html(data.d);

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d);
        }

    });
} /* end membersFollowSeries */

function MarkAsUnread(book_id, user_id, series_id,panel) {

    $j.ajax({
        type: "POST",
        url: "SaveDataService.asmx/RemoveUserBook",
        data: "{'book_id':'" + book_id + "','user_id':'" + user_id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

            $j("#" + panel).html("<a href='#' style='font-size:11px;' onclick=\"MarkAsRead(" + book_id + "," + user_id + ","+ series_id +",'" + panel + "');$('rating_one').show();\">Mark as read</a>");

			$j("#wm_atag_"+ book_id +" img.watermark").remove();
			
			$j('#thebookcover').css({backgroundColor: '#ffffff'});
			
			membersHaveRead(book_id);
			
			//rating_one = new Control.Rating('rating_one',{value: $j("#ratingvalue").val(), rated: true});
		
		},
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d + " ERROR" + errorThrown + " TEXT" + textStatus);
            //alert(XMLHttpRequest.d);
        }

    });

} /* end MarkAsUnread */


function MarkAsUnread(book_id, user_id, series_id) {

	$j.ajax({
		type: "POST",
		url: "SaveDataService.asmx/RemoveUserBook",
		data: "{'book_id':'" + book_id + "','user_id':'" + user_id + "'}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(data) {

			//$j("#" + panel).html("<a href='#' style='font-size:11px;' onclick=\"MarkAsRead(" + book_id + "," + user_id + "," + series_id + ",'" + panel + "');$('rating_one').show();\">Mark as read</a>");

			$j("#wm_atag_" + book_id + " img.watermark").remove();

			$j('#thebookcover').css({ backgroundColor: '#ffffff' });

			membersHaveRead(book_id);

			//rating_one = new Control.Rating('rating_one', { value: $j("#ratingvalue").val(), rated: true });

		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			// typically only one of textStatus or errorThrown 
			// will have info
			//this; // the options for this ajax request
			//alert(XMLHttpRequest.d + " ERROR" + errorThrown + " TEXT" + textStatus);
			//alert(XMLHttpRequest.d);
		}

	});

} /* end MarkAsUnread */

function MarkAsUnreadOnSeries(book_id, user_id, series_id,panel) {

    $j.ajax({
        type: "POST",
        url: "SaveDataService.asmx/RemoveUserBook",
        data: "{'book_id':'" + book_id + "','user_id':'" + user_id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

            $j("#" + panel + "_" + book_id).html("<a href='#' style='font-size:12px;' onclick=\"MarkAsReadOnSeries(" + book_id + "," + user_id + ","+ series_id +",'" + panel + "');\">Mark as read</a>");

			$j("#wm_atag_"+ book_id +" img.watermark").remove();
			
			
		},
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d + " ERROR" + errorThrown + " TEXT" + textStatus);
            //alert(XMLHttpRequest.d);
        }

    });

} /* end MarkAsUnreadOnSeries */


function MarkAsUnreadOnSeries(book_id, user_id, series_id) {
//Used for new book status
    $j.ajax({
        type: "POST",
        url: "/SaveDataService.asmx/RemoveUserBook",
        data: "{'book_id':'" + book_id + "','user_id':'" + user_id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {

            //$j("#_" + book_id).html("<a href='#' style='font-size:12px;' onclick=\"MarkAsReadOnSeries(" + book_id + "," + user_id + "," + series_id + ",'" + panel + "');\">Mark as read</a>");
            //$j("#bookStausLink_" + book_id).html(GetUserBookStatusHTML(book_id,0,series_id));


            $j("#userFollowingBook_" + book_id).html(GetUserBookStatusHTML(book_id, 0, current_series_id));
            $j("#wm_atag_" + book_id + " img.watermark").remove();
            CancelUserBookStatus(book_id);

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d + " ERROR" + errorThrown + " TEXT" + textStatus);
            //alert(XMLHttpRequest.d);
        }

    });

} /* end MarkAsUnreadOnSeries */


function MarkAsRead(book_id, user_id, series_id, panel)
{
	$j.ajax({
        type: "POST",
        url: "SaveDataService.asmx/InsertUserBook",
        data: "{'book_id':'" + book_id + "','user_id':'" + user_id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

        	$j("#userFollowingBook").html("<a href='#' style='font-size:11px;' onClick='MarkAsUnread(\""+ book_id +"\", \""+ user_id +"\", \""+ series_id +"\" ,\"userFollowingBook\");'>Mark as unread</a>");

			$j('#thebookcover').css({backgroundColor: '#19BD19'});
			
			userBooksRead(series_id,user_id);
			
			membersHaveRead(book_id);
			
			setUserBookRating(book_id, user_id);

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {

            //alert(XMLHttpRequest.d + " ERROR" + errorThrown + " TEXT:" + textStatus);

        }

    });
} /* end MarkAsRead */

function MarkAsReadOnSeries(book_id, user_id, series_id, panel)
{
	$j.ajax({
        type: "POST",
        url: "SaveDataService.asmx/InsertUserBook",
        data: "{'book_id':'" + book_id + "','user_id':'" + user_id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

        		$j("#userFollowingBook_" + book_id).html("<a href='#' style='font-size:12px;' onClick='MarkAsUnreadOnSeries(\""+ book_id +"\", \""+ user_id +"\", \""+ series_id +"\" ,\"userFollowingBook\");'>Mark as unread</a>");

			$j("#wm_atag_" + book_id).append('<img src="http://fictfact.s3.amazonaws.com/images/greencheck.png" border="0" class="watermark" alt="Read Book" />');

			userFollowingSeriesOnSeries(series_id, user_id);

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {

            //alert(XMLHttpRequest.d + " ERROR" + errorThrown + " TEXT:" + textStatus);

        }

    });
} /* end MarkAsReadOnSeries */

function userBooksRead(series_id, user_id)
{
	$j.ajax({
        type: "POST",
        url: "../BookDataService.asmx/userBooksRead",
        data: "{'series_id':'"+ series_id +"', 'user_id':'"+ user_id +"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

			var books = (typeof data.d) == 'string' ? eval('(' + data.d + ')') : data.d;
							
			for (var i = 0; i < books.length; i++) {
						$j("#wm_atag_" + books[i].book_id).append('<img src="http://fictfact.s3.amazonaws.com/images/greencheck.png" border="0" class="watermark" alt="Read Book" />');
	        }

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d);
        }

    }); 
} /*end userBooksRead*/

function userFollowingBook(book_id, user_id, series_id)
{
	$j.ajax({
        type: "POST",
        url: "../BookDataService.asmx/userFollowingBook",
        data: "{'book_id':'"+ book_id +"', 'user_id':'"+ user_id +"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            if(data.d=="1"){
               $j("#userFollowingBook").html("<a href='#' style='font-size:11px;' onClick='MarkAsUnread(\""+ book_id +"\", \""+ user_id +"\", \""+ series_id +"\" ,\"userFollowingBook\");'>Mark as unread</a>");

			   $j('#thebookcover').css({backgroundColor: '#19BD19'});
			
			   setUserBookRating(book_id, user_id);

			} else {
				$j("#userFollowingBook").html("<a href='#' style='font-size:11px;' onClick='MarkAsRead(\""+ book_id +"\", \""+ user_id +"\", \""+ series_id +"\" ,\"userFollowingBook\");'>Mark as read</a>");

				$j('#thebookcover').css({backgroundColor: '#ffffff'});					
			}

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d);
        }

    });
} /* end userFollowingBook */

function userFollowingBookOnSeries(book_id, user_id, series_id) {
    
    current_series_id = series_id;
    current_user_id = user_id;

    $j.ajax({
        type: "POST",
        url: "/SaveDataService.asmx/ReturnUserBookStatus",
        data: "{'user_id':'" + user_id + "', 'book_id':'" + book_id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {

            var current_user_status = data.d;
            
            if (current_user_status == 3) { //User has read book so don't show anything
                //$j("#userFollowingBook_" + book_id).html("<a href='#' style='font-size:12px;' onClick='MarkAsUnreadOnSeries(\""+ book_id +"\", \""+ user_id +"\", \""+ series_id +"\" ,\"userFollowingBook\");'>Mark as unread</a>");

            } else {

                //$j("#userFollowingBook_" + book_id).html("<a href='#' style='font-size:12px;' onClick='MarkAsReadOnSeries(\""+ book_id +"\", \""+ user_id +"\", \""+ series_id +"\" ,\"userFollowingBook\");'>Mark as read</a>");
                
                $j("#userFollowingBook_" + book_id).html(GetUserBookStatusHTML(book_id, current_user_status, series_id));
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            alert(XMLHttpRequest.responseText);
        }

    });
} /* end userFollowingBookOnSeries */

function userFollowingSeries(book_id, series_id, user_id)
{				
	$j.ajax({
        type: "POST",
        url: "../BookDataService.asmx/userFollowingSeries",
        data: "{'series_id':'"+ series_id +"', 'user_id':'"+ user_id +"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            if(data.d=="1"){
               $j("#userFollowingSeries").html("<a href='#' style='font-size:11px;' onClick='userUnfollowSeries("+ book_id +", "+ series_id +","+ user_id +")'>Unfollow series</a>");

			} else {
				$j("#userFollowingSeries").html("<a href='#' style='font-size:11px;' onClick='userFollowSeries("+ book_id +", "+ series_id +", "+ user_id +");'>Follow series</a>");
				
				$j("#userFollowingBook").html("");
			}

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d);
        }

    });
	
} /* end userFollowingSeries */

function userFollowingSeriesOnSeries(series_id, user_id)
{				
	$j.ajax({
        type: "POST",
        url: "../BookDataService.asmx/userFollowingSeries",
        data: "{'series_id':'"+ series_id +"', 'user_id':'"+ user_id +"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            if(data.d=="1"){
               $j("#userFollowingSeries").html("<a href='#' style='font-size:12px;' onClick='userUnfollowSeriesOnSeries("+ series_id +","+ user_id +")'>Unfollow series</a>");
               PopulateUserListContainer("userlistcontainer", false);
			} else {
				$j("#userFollowingSeries").html("<a href='#' style='font-size:12px;' onClick='userFollowSeriesOnSeries("+ series_id +", "+ user_id +");'>Follow series</a>");
			}
			
			membersFollowSeries(series_id);

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d);
        }

    });
	
} /* end userFollowingSeriesOnSeries */

function userUnfollowSeries(book_id, series_id, user_id)
{
	 
	if (confirm("Are you sure you want to remove? This can not be undone. You will loose all ratings, book reads, and all related information.")) {
        $j.ajax({
            type: "POST",
            url: "SaveDataService.asmx/RemoveFollowSeries",
            data: "{'series_id':'" + series_id + "','username':'"+ $j("#username").val() +"'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {

                var tmpObj = $j(".watermark");

				for(var i=0;i<tmpObj.length;i++)
				{
					tmpObj[i].remove();
				}

				//$j("#userFollowingBook").html("<a href='#' onClick='MarkAsRead(\""+ book_id +"\", \""+ user_id +"\", \""+ series_id +"\" ,\"userFollowingBook\");'>Mark as read</a>");
				$j("#userFollowingBook").html("");

				$j('#thebookcover').css({backgroundColor: '#ffffff'});

				$j("#userFollowingSeries").html("<a href='#' style='font-size:11px;' onClick='userFollowSeries("+ book_id +", "+ series_id +", "+ user_id +");'>Follow series</a>");

				membersHaveRead(book_id);

				membersFollowSeries(series_id);

            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                // typically only one of textStatus or errorThrown 
                // will have info
                //this; // the options for this ajax request
                //alert(XMLHttpRequest.d + " " + errorThrown);
                //alert(XMLHttpRequest.d);
            }

        });
    } /* end confirm if */
	
} /* end userUnfollowSeries */

function userUnfollowSeriesOnSeries(series_id, user_id)
{
	
	if (confirm("Are you sure you want to remove? This can not be undone. You will loose all ratings, book reads, and all related information.")) {
        $j.ajax({
            type: "POST",
            url: "SaveDataService.asmx/RemoveFollowSeries",
            data: "{'series_id':'" + series_id + "','username':'"+ $j("#username").val() +"'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {

				$j("#userFollowingSeries").html("<a href='#' style='font-size:12px;' onClick='userFollowSeriesOnSeries("+ series_id +", "+ user_id +");'>Follow series</a>");
				
				HideUserListContainer("userlistcontainer");
				
				var $inputs = $j("input:hidden");
				
				$inputs.each(function() {
				if($j(this).attr("name") == "bookID")
				{
        					userFollowingBookOnSeries($j(this).val(), user_id, series_id);
        					
        					$j("#wm_atag_" + $j(this).val() +" .watermark").remove();
        					
        			}
        			});

                /*var tmpObj = $j(".watermark");

				for(var i=0;i<tmpObj.length;i++)
				{
					tmpObj[i].hide();
				}*/

				membersFollowSeries(series_id);

            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                // typically only one of textStatus or errorThrown 
                // will have info
                //this; // the options for this ajax request
                //alert(XMLHttpRequest.d + " " + errorThrown);
                //alert(XMLHttpRequest.d);
            }

        });
    } /* end confirm if */
	
} /* end userUnfollowSeriesOnSeries */

function userFollowSeries(book_id, series_id, user_id)
{
	
	$j.ajax({
        type: "POST",
        url: "SaveDataService.asmx/SetFollowSeries",
        data: "{'series_id':'" + series_id + "','username':'"+ $j("#username").val() +"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

            $j("#userFollowingBook").html("<a href='#' style='font-size:11px;' onClick='MarkAsRead(\""+ book_id +"\", \""+ user_id +"\", \""+ series_id +"\" ,\"userFollowingBook\");'>Mark as read</a>");

			$j("#userFollowingSeries").html("<a href='#' style='font-size:11px;' onClick='userUnfollowSeries("+ book_id +", "+ series_id +","+ user_id +")'>Unfollow series</a>");

			membersHaveRead(book_id);

			membersFollowSeries(series_id);

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d + " ERROR: " + errorThrown);
            //alert(XMLHttpRequest.d);
        }

    });
	
} /* end userFollowSeries */

function userFollowSeriesOnSeries(series_id, user_id)
{

    $j.ajax({
        type: "POST",
        url: "SaveDataService.asmx/SetFollowSeries",
        data: "{'series_id':'" + series_id + "','username':'" + $j("#username").val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {



            //$j("#userFollowingBook").html("<a href='#' style='font-size:12px;' onClick='MarkAsReadOnSeries(\""+ book_id +"\", \""+ user_id +"\", \""+ series_id +"\" ,\"userFollowingBook\");'>Mark as read</a>");

            $j("#userFollowingSeries").html("<a href='#' style='font-size:12px;' onClick='userUnfollowSeriesOnSeries(" + series_id + "," + user_id + ")'>Unfollow series</a>");
            PopulateUserListContainer("userlistcontainer", false);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d + " ERROR: " + errorThrown);
            //alert(XMLHttpRequest.d);
        }

    });
	
} /* end userFollowSeries */

function returnUserName(user_id)
{	
				
	$j.ajax({
        type: "POST",
        url: "../UserDataService.asmx/returnUserName",
        data: "{'user_id':'"+ user_id +"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            $j("#username").attr("value", data.d);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d);
        }

    });
	
} /* end returnUserName */

function setUserBookRating(book_id, user_id)
{
	
	$j.ajax({
        type: "POST",
        url: "../UserDataService.asmx/returnUserBookRating",
        data: "{'book_id':'"+ book_id +"', 'user_id':'"+ user_id +"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

            if(data.d==""){
			    //rating_one = new Control.Rating('rating_one',{value: 0, rated: false});
			
				//rating_one.observe('afterChange',function(new_value){
				//UpdateUserBookRating(book_id, user_id, new_value);
			    //});
			
			} else {
				//rating_one = new Control.Rating('rating_one',{value: data.d, rated: true});
			}

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d);
        }

    });
	
} /* end setUserBookRating */

function fetchBarnesLocal()
{
	var zipcode = $j("#localsearch").val();
	var ean = $j('#isbn13').val();
	
	var tmpObj = $j("#localUL li");

	for(var i=0;i<tmpObj.length;i++)
	{
		tmpObj[i].remove();
	}
	
	$j.ajax({
        type: "POST",
        url: "../FindLocalBookService.asmx/fetchBarnesLocal",
        data: "{'zipcode':'"+ zipcode +"', 'ean':'"+ ean +"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
	
	        var stores = (typeof data.d) == 'string' ? eval('(' + data.d + ')') : data.d;
							
			for (var i = 0; i < stores.length; i++) {
					//alert(stores[i].storeName);
					$j("#localUL").append("<li><a onClick='$j(\"#add_"+ i +"\").show();'><img src='http://search.barnesandnoble.com/favicon.ico' width='16' height='16' border='0'> "+ stores[i].storeName +"</a> - In Stock - "+ stores[i].viewMap +"</li>");
					
					$j("#localUL").append("<li style='display:none;margin-left:20px;' id='add_"+ i +"'><a href='http://maps.google.com/?q="+ stores[i].address +"+"+ stores[i].city +"+"+ stores[i].state +"+"+ stores[i].zip +"' target='_whole'>"+ stores[i].address +" "+ stores[i].city +" "+ stores[i].state +" "+ stores[i].zip +"</a></li>");
			
			}
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            $j("#localUL").append("<li>No copies were found for your zipcode.</li>");
        }

    });
} /*  end fetchBarnesLocal */

function addTag(bookID, tagType)
{
	var stringSplit = CSVToArray($j('#addinputbox').val(), " ");//$('addinputbox').value.split(" ");
	
	stringSplit = stringSplit[0];
	
	var tmpObj = $j("#tagTable a");
	
	for(var i=0;i<stringSplit.length;i++)
	{
		
		
		var itshere = -1;
		
		for(var z=0;z<tmpObj.length;z++)
		{
			//alert(tmpObj[z].innerHTML);
		
			if(tmpObj[z].innerHTML == stringSplit[i])
			{
				itshere = 1;
				//alert('itshere!');
			}
		
		}//end for
		
		if(itshere == -1)
		{
			if(tmpObj.length==0){
				$j("#tagTable").append("<a href='search/"+ tagType +"Tags.aspx?term=" + stringSplit[i] +"' id='tag_a_"+ stringSplit[i].replace(" ", "_") +"' style='display:none;'>" + stringSplit[i] + "</a>");
			} else {
				$j("#tagTable").append(", " + "<a href='search/"+ tagType +"Tags.aspx?term=" + stringSplit[i] +"' id='tag_a_"+ stringSplit[i].replace(" ", "_") +"' style='display:none;'>" + stringSplit[i] + "</a>");
			}
			
			$j.ajax({
		        type: "POST",
		        url: "../BookDataService.asmx/saveBookTag",
		        data: "{'id':'"+ bookID +"', 'tag':'"+ stringSplit[i] +"', 'type':'"+ tagType +"', 'username':'"+ $j("#username").val() +"'}", //"id="+ bookID +"&tag="+ stringSplit[i] +"&type=" + tagType, //
				contentType: "application/json; charset=utf-8",
		        dataType: "json",
		        success: function(data) {

			        
			
		        },
		        error: function(XMLHttpRequest, textStatus, errorThrown) {
		            // typically only one of textStatus or errorThrown 
		            // will have info
		            //this; // the options for this ajax request
					//alert(errorThown);
		        }

		    });
			
			$j("#tag_a_" + stringSplit[i].replace(" ", "_")).fadeIn("slow");
			
						
		} /* end itshere == -1 */
		
		/*var el = $('taglist');
		var ret = "";
		var itshere = -1;
		el.immediateDescendants().find(function(num, index) {
		    var parseArray = num.innerHTML.split(">");
		
			parseArray = parseArray[1].split("<");
			
			ret = parseArray[0];
		
			if(ret == stringSplit[i])
			{
				itshere = 1;
			}
		
		});
		
		
		if(itshere == -1)
		{
			var a = new Element('a', { href: 'search/'+ tagType +'Tags.aspx?term=' + stringSplit[i] }).update(stringSplit[i]);
		
			var li = new Element('li', { 'class': 'module-list-item'}).insert(a);
	
				var url = "/proxy/jsonaddtag.aspx";
				var pars = 'id='+ id +'&tag='+ stringSplit[i] +'&type=' + tagType;

				new Ajax.Request(url,
				  {
					parameters: pars,
				    method:'get',
				    onSuccess: function(transport){
				      var response = transport.responseText || "0";
					}
				  });
	
				$('taglist').insert(li);
		} //end if
		*/
		//alert(stringSplit[i]);

	}//end for
	
	$j("#addinputbox").attr("value", "");

} /* end function addTag */

function HideUserListContainer(containerName) {
    $j("#" + containerName).html("");
   
}

function PopulateUserListContainer(containerName, isEdit) {


    $j.ajax({
        type: "POST",
        url: "SaveListService.asmx/GetUserListsWithSeriesCheck",
        data: "{'user_id':'" + user_id + "', 'series_id':'" + series_id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

            var lists = data.d;
            var str = "";
            var seriesInListCount = 0;
            if (lists.length > 0) {
                str = str + " <img src='/images/tinyleaf.jpg' style='padding:0 0 0 0;margin:0 0 0 0;' border='0'/> <b>In your lists:</b>";

                if (isEdit) {
                    str = str + " (<a href='javascript:function(){return false;}' onclick='PopulateUserListContainer(\"userlistcontainer\", false);'>done</a>)";
                } else {
                    str = str + " (<a href='javascript:function(){return false;}' onclick='PopulateUserListContainer(\"userlistcontainer\", true);'>edit</a>)";
                }
               

                str = str + "<div style='margin-left:10px;margin-top:7px;' >";
                if (isEdit) {
                    for (var i = 0; i < data.d.length; i++) {

                        str = str + "<input type='checkbox' id='chklist_" + lists[i].listID + "'";
                        if (lists[i].isSeriesInList) {
                            str = str + " checked ";
                            seriesInListCount++;
                        }
                        str = str + " onclick='SaveListChange(" + lists[i].listID + ");'";
                        //str = str + "> <a href='listdetail.aspx?list_id=" + lists[i].encodedID + "'>" + lists[i].listName + "</a><br>";
                        str = str + "> <a href='/list/" + user_name + "/" + ConvertToURLString(lists[i].listName) + "'>" + lists[i].listName + "</a><br>";
                    }
                } else {

                    for (var i = 0; i < data.d.length; i++) {


                        if (lists[i].isSeriesInList) {
                            seriesInListCount++;
                            // str = str + "<a href='listdetail.aspx?list_id=" + lists[i].encodedID + "'>" + lists[i].listName + "</a><br>";
                            str = str + "<a href='/list/" + user_name + "/" + ConvertToURLString(lists[i].listName) + "'>" + lists[i].listName + "</a><br>";
                           

                        }
                    }
                    if (seriesInListCount == 0) {
                        str = str + "Series not in list.";
                    }
                }
                str = str + "</div>";
            }
            $j("#" + containerName).html(str);

            //alert(str);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {

            alert(XMLHttpRequest.responseText);
        }

    });

}

function SaveListChange(list_id) {
    if ($j("#chklist_" + list_id + ":checked").val() != null) {
        SaveSeriesToList(list_id);
    } else {
        DeleteSeriesFromList(list_id);
    }
}


function SaveSeriesToList(list_id) {
    $j.ajax({
        type: "POST",
        url: "SaveListService.asmx/SaveSeriesToList",
        data: "{'list_id':'" + list_id + "','series_id':" + series_id + ",'user_id':'" + user_id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {


        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {

            alert("ERROR:" + XMLHttpRequest.responseText);
        }

    });
}

function DeleteSeriesFromList(list_id) {
    $j.ajax({
        type: "POST",
        url: "SaveListService.asmx/DeleteSeriesFromList",
        data: "{'list_id':'" + list_id + "', 'item_id':'" + series_id + "', 'user_id':'" + user_id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {


        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {

            alert("ERROR:" + XMLHttpRequest.responseText);
        }

    });
}


function GetUserBookStatusHTML(book_id, current_status_code, series_id) {
    var str;
    var current_status_string;

    if (current_status_code == 0) {
        current_status_string = "Mark book as:";
    } else {
        current_status_string = UserBookStatusCodes[current_status_code];
    }



    str = "<a href='javascript:function(){return false;}' onclick='GetUserBookStatusOptionsHTML(" + book_id + "," + current_status_code + "," + series_id + ");' id='bookStausLink_" + book_id + "'>" + current_status_string + "</a>";
    str = str + " <a href='javascript:function(){return false;}' id='MarkBookAsbtn' onclick='GetUserBookStatusOptionsHTML(" + book_id + "," + current_status_code + "," + series_id + ");'><img src='/images/down_off.png' class='downImage' border='0' style='vertical-align:middle' /></a>";
    str = str + "<div id='divBookStatus_" + book_id + "'></div>";

    return str;
}

function GetUserBookStatusOptionsHTML(book_id, current_status_code, series_id) {
    var str = "";
    str = str + "<div id='userbookoptions' class='userbookoptions'><span style='color:666666; font-size:10px;'>Mark book as:</span>";
    str = str + "     <ul id='userBookOptionsList'  >";
    if (current_status_code != 1) {
        str = str + "      <li class='userListItem' id='li_status_1'><a href='javascript:function(){return false;}' onclick=\"SetUserBookStatusOnSeries(" + book_id + ",1,'To be read');\" class='userListItem'>To be read</a></li>";
    }
    if (current_status_code != 2) {
        str = str + "      <li class='userListItem' id='li_status_2'><a href='javascript:function(){return false;}' onclick=\"SetUserBookStatusOnSeries(" + book_id + ",2,'Currently Reading');\"  class='userListItem'>Currently reading</a></li>";
    }
    if (current_status_code != 3) {
        str = str + "      <li class='userListItem' id='li_status_3'><a href='javascript:function(){return false;}' onclick=\"SetUserBookStatusOnSeries(" + book_id + ",3,'Read');\"  class='userListItem'>Read</a></li>";
    }
    if (current_status_code > 0) {
        str = str + "      <li class='userListItem' id='li_status_0'><a href='javascript:function(){return false;}' onclick=\"MarkAsUnreadOnSeries(" + book_id + "," + current_user_id + "," + current_series_id + "); \"  class='userListItem'>Not read</a></li>";
    }

    str = str + "   </ul>";
    str = str + " <a href='javascript:function(){return false;}' onclick='CancelUserBookStatus(" + book_id + ");'>Cancel</a> ";

    str = str + " </div>";
  
    $j("div#divBookStatus_" + book_id).html(str);

   
}

function SetUserBookStatusOnSeries(book_id, status_id, status_desc) {
    $j("#userSelectedStatus").html(status_desc);
    
    HideUserBookStatusMenuOnSeries();

    

    $j.ajax({
        type: "POST",
        url: "/SaveDataService.asmx/SaveUserBookStatus",
        data: "{'user_id':" + current_user_id + ",'book_id':" + book_id + ",'status_code':" + status_id + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {

         
            if (status_id == 3) {
           
                $j("#wm_atag_" + book_id).append('<img src="http://fictfact.s3.amazonaws.com/images/greencheck.png" border="0" class="watermark" alt="Read Book" />');

                userFollowingSeriesOnSeries(current_series_id, user_id);
               
            } else {

                $j("#wm_atag_" + book_id + " img.watermark").remove();
            }
            

            $j("#userFollowingBook_" + book_id).html(GetUserBookStatusHTML(book_id, status_id, current_series_id));

 

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            //alert(XMLHttpRequest.d + " " + errorThrown);
            //alert(XMLHttpRequest.responseText);

        }

    });


}
function HideUserBookStatusMenuOnSeries() {
    $j("#userbookoptions").html("");


}

function CancelUserBookStatus(book_id) {
    $j("div#divBookStatus_" + book_id).html("");
}