function note(lid, row) {
	var q = $("#m"+row).val();
	$("#li"+row).html("<img src=\"/images/loading.gif\" height=\"16\" width=\"16\" alt=\"\"/>");//fix
	$.ajax({
	type: 'GET',
	url: '/ajax/editnote.php',
	data:{l:lid, m:row, n:q,r:(new Date().getTime())},
			  dataType:'xml',
			  success: function(xml) {
				$(xml).find("re").each(function() {
					var error = $(this).find("err").text();
					if (error) {
						alert("noteReturned error: "+error);
					} else {
						var ro = $(this).find("ro").text();
						var li = $(this).find("li").text();
						$('#li'+ro).html("&nbsp;");
						var nt = $(this).find("cl").text();
						if (nt) { 
							$("#n" + ro).html("\""+nt+"\" [<a href=\"#\" onclick=\"showNote("+li+","+ro+",'"+nt+"');return false\">edit</a>] [<a href=\"#\" onclick=\"note("+li+","+ro+");return false\">clear</a>]");	
						} else {
							$("#n" + ro).html("[<a href=\"#\" onclick=\"showNote("+li+","+ro+",'');return false\">Add note<\/a>]");
						}
					}
				});
			  }
			});	
}

function search() {
	var q = $('#bk_search').val();
	//q = q.replace(/[\s]/g, ',');	//fix
	if(q != ""){
		$("#loading").html("<img src=\"/images/loading.gif\" height=\"16\" width=\"16\" alt=\"\"/>");
		$.ajax({
		type: 'GET',
		url: '/ajax/findbook.php',
		data:{gy:1, q:q, r:(new Date().getTime())},
				  dataType:'xml',
				  success: function(xml) {
					$("#loading").html("&nbsp;");
					$(xml).find("re").each(function()
					{
						var error = $(this).find("err").text();
						if (error) {
							alert("search error: "+error);
						} else {
						
var table = "<div style=\"width:600px;\"><div class=\"tabletop\"><div class=\"narrowcol\">&nbsp;</div><div class=\"widecol\">&nbsp;</div><div class=\"addcol\">+</div><div class=\"wishcol\">wish</div><div class=\"igcol\"></div></div>";//fix need
							var dex = 0;
							$(this).find("bk").each(function() {
										
										
	  var is = $(this).find("is").text();
	  var thisrow = "<div id=\"ep" + dex + "\" class=\"trow\"><div class=\"imgcol\">";
	  var li = "/book/a/b/" + is;//fix "a/b"
	  var id = parseInt($(this).find("id").text());
	  var ti = $(this).find("ti").text();
	  var im = $(this).find("im").text();
	  //if ($(this).find("im")) {
		//im = $(this).find("im").text();
	  if (im) {
		thisrow = thisrow + "<a rel=\"nofollow\" href=\"" + li + "\"><img src=\"http://ecx.images-amazon.com/images/I/" + im + "._SL75_.jpg\" alt=\"\" title=\"" + ti + "\" height=\"75\"/></a>";		
	  } else {
		thisrow = thisrow + "&nbsp;";
	  }
	  var au = $(this).find("au").text();
	  thisrow = thisrow + "<\/div><div class=\"widecol\"><a rel=\"nofollow\" href=\"" + li + "\">" + ti + "<br />" + au + "<\/a><\/div>";
		if (id == 0) {
		    thisrow = thisrow + "<div id=\"ia" + dex + "\" class=\"addcol\"><a href=\"#\" onclick=\"newBook('" + is + "',1," + dex + ");return false\">[+]</a><\/div><div id=\"iw" + dex + "\" class=\"wishcol\"><a href=\"#\" onclick=\"newBook('" + is + "',3," + dex + ");return false\">[w]</a><\/div><div class=\"igcol\"><\/div></div>";
		} else {
			//searched by isbn
			//fix
			/*if (tttt != 1) {
				alert("Should be only one result");//fix
			}*/
			thisrow = thisrow + "<div id=\"ra" + dex + "\" class=\"addcol\"><a href=\"#\" onclick=\"editBook(" + id + ",1," + dex + ");return false\">[+]</a><\/div><div id=\"rw" + dex + "\" class=\"wishcol\"><a href=\"#\" onclick=\"editBook("+id+",3," + dex + ");return false\">[w]</a><\/div><div class=\"igcol\"><\/div></div>";
		}
		table = table + thisrow;
		++dex;
									});
									
									if (dex == 0) {
									
	table = table + "<div class=\"trow\"><div class=\"imgcol\"><\/div><div class=\"widecol\">No titles found<\/div><div class=\"addcol\"><\/div><div class=\"wishcol\"><\/div><div class=\"igcol\"><\/div></div>";
									}
  $("#results").html(table + "</div>");
								}
							});
						  }
						});		
		
	} else {		
		alert("Enter a word from the book's title, author's name or ISBN number");
	}
}

function showNote(lid, row, note) {
	var toshow;
	if (note == '') {
		var x = new Date();		
		toshow  = "Lent on "+(x.toDateString()).substring(4)+" to [name]";//fix		
	} else {
		toshow = note;
	}
	$('#n' + row).html("<form action=\"javascript:note("+lid+","+row+");\"><input id=\"m"+row+"\" type=\"text\" name=\"note\" value=\""+toshow+"\" maxlength=\"50\" size=\"40\"\/><input type=\"submit\" value=\"Note\"\/><\/form>");
}

function editBook(bid,action,rid) {
	var id;
	var id2;
	if ((action == 1) || (action == 2)) {
		id = "#ra" + rid;
		id2 = "#ia" + rid;
	} else if ((action == 3) || (action == 4)) {
		id = "#rw" + rid;
		id2 = "#iw" + rid;
	} else {
		id = "#ri" + rid;
		id2 = "#ii" + rid;
	}
	var ex;
	if ($(id)) {
		$(id).html("<img src=\"/images/loading.gif\" height=\"16\" width=\"16\" alt=\"\"/>");
		ex = 0;
	} else {
		$(id2).html("<img src=\"/images/loading.gif\" height=\"16\" width=\"16\" alt=\"\"/>");
		ex = 1;
	}
	$.ajax({
	type: 'GET',
	url: "/ajax/editbook.php",
	data:{m:rid, a:action, ex:ex, b:bid, r:(new Date().getTime())},
			  dataType:'xml',
			  success: function(xml) {
				stateChanged(xml, rid);
			  }
			});	
}

function newBook(bid,action,rid) {
	var id;
	if ((action == 1) || (action == 2)) {
		id = "#ia"+rid;
	} else if ((action == 3) || (action == 4)) {
		id = "#iw"+rid;
	} else {
		//id = "ri"+bid;
		alert("No ig");//fix
	}	
	$(id).html("<img src=\"/images/loading.gif\" height=\"16\" width=\"16\" alt=\"\"/>");
	$.ajax({
	type: 'GET',
	url: "/ajax/editbook.php",
	data:{m:rid, a:action, ex:1, i:bid, r:(new Date().getTime())},
			  dataType:'xml',
			  success: function(xml) {
				stateChanged(xml, rid);
			  }
			});
}

function stateChanged(xml, rid) {
	$(xml).find("re").each(function (){
		var error = $(this).find("err").text();
		if (error) {
			if (error == "Session ended") {
				window.location = "http:\/\/sharedbookshelves.com\/signout.php";
			} else {
				alert("editBook error: "+error);
			}
		} else {
			var countown = $(this).find("countown").text();
			if (countown) {
				$('#countown').html(countown);
			}
			var countwish = $(this).find("countwish").text();
			if (countwish) {
				$('#countwish').html(countwish);
			}
			var id = $(this).find("bid").text();
			var lid = $(this).find("lid").text();
			var ro = $(this).find("ro").text();
			var btn = $(this).find("btn").text();
			var ex = parseInt($(this).find("ex").text());
			var toa;
		  var tow;
		  var toi;
		  var noa;
	if (ex == 0) {
		toa = "#ra" + rid;
		tow = "#rw" + rid;
		toi = "#ri" + rid;
		noa = "#n" + rid;
	} else {
		toa = "#ia" + rid;
		tow = "#iw" + rid;
		toi = "#ii" + rid;
	}
  switch(parseInt(btn)) {
	case 1:		
		$(toa).html("<a href=\"#\" onclick=\"editBook("+id+","+btn+","+rid+");return false\">[+]</a>");
		$(tow).html("<a href=\"#\" onclick=\"editBook("+id+",3,"+rid+");return false\">[w]</a>");
		if ($(toi)) {
			$(toi).html("<a href=\"#\" onclick=\"editBook("+id+",5,"+rid+");return false\">[x]</a>");//fix there might not be an ignore column!
		}
		if ((ex == 0) && $(noa)) {
			$(noa).html("&nbsp;");
		}
	break;
	case 2:
		$(toa).html("<a href=\"#\" onclick=\"editBook("+id+","+btn+","+rid+");return false\">[-]</a>");
		$(tow).html("&nbsp;");
		if ($(toi)) {
			$(toi).html("&nbsp;");
		}
		if ((ex == 0) && $(noa)) {
			$(noa).html("[<a href=\"#\" onclick=\"showNote("+lid+","+rid+",'');return false\">Add note</a>]");
		}
	break;
	case 3:
		$(tow).html("<a href=\"#\" onclick=\"editBook("+id+","+btn+","+rid+");return false\">[w]</a>");
	break;
	case 4:
		$(tow).html("<a href=\"#\" onclick=\"editBook("+id+","+btn+","+rid+");return false\">[u]</a>");
	break;
	case 5:
		if ($(toi)) {
			$(toi).html("<a href=\"#\" onclick=\"editBook("+id+","+btn+","+rid+");return false\">[x]</a>");
		}
	break;
	case 6:
		if ($(toi)) {
			$(toi).html("<a href=\"#\" onclick=\"editBook("+id+","+btn+","+rid+");return false\">[y]</a>");
		}
	break;
	default:
		//fix error
	break;
  };
									
								}
							});
						  }
