function valLogin() {
		$('#u').css('background-color','#ffffff').css('color','#000000');
		$('#p').css('background-color','#ffffff').css('color','#000000');
		var hasError = false;
 
		var u = $("#u").val();
		if (u == '') {
			$("#u").css('background-color','#333333').css('color','#ffffff');
			hasError = true;
		}
		
		var p = $("#p").val();
		if (p == '') {
			$("#p").css('background-color','#333333').css('color','#ffffff');
			hasError = true;
		}
		
		if (hasError == false) {
		$('#u').attr('disabled','disabled');
		$('#p').attr('disabled','disabled');
		$('#submit').hide();
		$('#forgotLink').hide();
		$('#log_load').fadeIn();
		$.post('login.php', {
			uname: $('#u').val(),
			pword: $('#p').val()
		},
			function(data) {
				if (data == 'success') {
					location.href = 'index.php';
					return false;
				}
				if (data == 'fail_exist') {
					//$.facebox("<div style='width:350px'><span><img src='images/login_head.png' border='0' /></span><hr size='2' color='000000' width='100%' align='left'><div id='success_p' style='width:350px;padding:10px 0px 10px 0px;'><span style='font-size:8pt;display:block;' class='fail'>Your username or password are incorrect.</span></div></div>");
					//setTimeout(function() {$.facebox.close();},2000);
					alert("Your username or password are incorrect! Please try again.");
					//location.href = 'login_attempt.php';
					$('#u').attr('disabled','');
					$('#p').attr('disabled','');
					$('#log_load').hide();
					$('#submit').fadeIn();
					$('#forgotLink').fadeIn();
					return false;
				}
			}
		);
		
		return false;
		
		}
	
	return false;
}

function valSearch(main) {
	var e = window.event;
	if (e.type == 'submit' || e.button == 0) {
		var string = $("#string").val();
		if (string == 'Enter title or ISBN...') {
			$('#string').css('background-color','#ffffff').css('color','#878787');
		} else if (string == 'Enter a title...') {
			$('#string').css('background-color','#ffffff').css('color','#878787');
		} else if (string == 'Enter an ISBN...') {
			$('#string').css('background-color','#ffffff').css('color','#878787');
		} else {
			$('#string').css('background-color','#ffffff').css('color','#000000');
		}
		var hasError = false;
		
		var isAdv = $("#isAdv").val();
		if (isAdv == 'true') {
			var type = $("#type").val();
		
			if (string === '' || string == 'Enter title or ISBN...' || string == 'Enter a title...' || string == 'Enter an ISBN...') {
				//if (type == 't') {$.facebox("<div style='width:350px'><span><img src='images/search_head.png' border='0' /></span><hr size='2' color='000000' width='100%' align='left'><div id='success_p' style='width:350px;padding:10px 0px 10px 0px;'><span style='font-size:8pt;display:block;' class='fail'>Please enter a title to search.</span></div></div>");}
				//if (type == 'i') {$.facebox("<div style='width:350px'><span><img src='images/search_head.png' border='0' /></span><hr size='2' color='000000' width='100%' align='left'><div id='success_p' style='width:350px;padding:10px 0px 10px 0px;'><span style='font-size:8pt;display:block;' class='fail'>Please enter an ISBN number to search.</span></div></div>");}
				//setTimeout(function() {$.facebox.close();},2000);
				if (type == 't') alert("Please enter a title to search!");
				if (type == 'i') alert("Please enter an ISBN number to search!");
				hasError = true;
			}
		
			if (hasError === false) {
				if (type == 't') {
					stringIndex = string.indexOf(" ");
					while (stringIndex != -1) {
						string = string.replace(" ","+");
						stringIndex = string.indexOf(" ");
					}
					if (main) {
						location.href='search.php?t='+string+'&exact=true';
					} else {
						location.href='search.php?t='+string;
					}
					return false;
				}
				if (type == 'i') {
					$.post('check_isbn.php', {
						type: 'search',
						string: $('#string').val()
					},
						function(data) {
							if (data == 'sent') {
								location.href='search.php?i='+string;
								return false;
							}
							if (data == 'fail') {
								//$.facebox("<div style='width:350px'><span><img src='images/search_head.png' border='0' /></span><hr size='2' color='000000' width='100%' align='left'><div id='success_p' style='width:350px;padding:10px 0px 10px 0px;'><span style='font-size:8pt;display:block;' class='fail'>Your ISBN is invalid. Please try again.</span></div></div>");
								//setTimeout(function() {$.facebox.close();},3000);
								alert("Your ISBN is invalid. Please try again.");
								return false;
							}
						}
					);
				}
			return false;
			}
		} else {
			if (string === '' || string == 'Enter title or ISBN...' || string == 'Enter a title...' || string == 'Enter an ISBN...') {
				//$.facebox("<div style='width:350px'><span><img src='images/search_head.png' border='0' /></span><hr size='2' color='000000' width='100%' align='left'><div id='success_p' style='width:350px;padding:10px 0px 10px 0px;'><span style='font-size:8pt;display:block;' class='fail'>We need something to search!</span></div></div>");
				//setTimeout(function() {$.facebox.close();},3000);
				alert("We need something to search!");
				hasError = true;
			}
		
			if (hasError === false) {
				$.post('check_isbn.php', {
					type: 'check',
					string: $('#string').val()
				},
					function(data) {
						if (data == 'isbn') {
							location.href='search.php?i='+string;
						}
						if (data == 'title') {
							stringIndex = string.indexOf(" ");
							while (stringIndex != -1) {
								string = string.replace(" ","+");
								stringIndex = string.indexOf(" ");
							}
							if (main) {
								location.href='search.php?t='+string+'&exact=true';
							} else {
								location.href='search.php?t='+string;
							}
						}
					}
				);
			}
			return false;
		}
	return false;
	}
}

//autosuggest
		function format(book) {
			var otherInfo = book.other;
			var otherSplit = otherInfo.split("|");
			var otherAuthor = otherSplit[0];
			if (otherAuthor.length > 35) {
				otherAuthor = otherAuthor.slice(0,35) + '...';
			}
			var otherISBN = otherSplit[1];
			return book.title + "<br><span class='suggestAuthor'>" + otherAuthor + "&nbsp;&nbsp;(ISBN: <b>" + otherISBN + "</b>)</span>";
		}
		function suggest() {
			$("#string").autocomplete("suggest.php",
				{
					width: 320,
					minChars: 3,
					max: 5,
					selectFirst: false,
					scroll: false,
					matchContains: "word",
					dataType: "json",
					parse: function(data) {
						return $.map(data, function(row) {
							var other = row.other;
							var otherSplit = other.split("|");
							var ISBN = otherSplit[1];
							return {
								data: row,
								value: row.title,
								result: row.title + "[" + ISBN + "]"
							}
						});
						
					},
					formatItem: function(item) {
						return format(item);
					}
				}
			).result(function() {
				return valSearch('auto');
			});
		}
function twitTip() {
$('#twitter').qtip({
				content: {
					url: 'twitter.php'
				},
				position: {
					corner: {
						target: 'topLeft',
						tooltip: 'bottomRight'
					},
					adjust: {
						x: -5,
						y: -15
					}
				},
				style: {
					width: 450,
					padding: 0,
					margin: 0,
					background: '#ffffff'
				},
				tip: 'bottomRight',
				show: { when: { event: 'click' }},
				hide: false
});
}
function closeTwit() {
	$('#twitter').qtip("hide");
}

function getMostRecent() {
	updateNums();
	$.post('most_recent.php', {
		curMostRec: $('#curMostRec').val()
	},
		function (data) {
			if (data != 'bad') {
				var mostRecData = data.split("|");
				var mostRec = mostRecData[0];
				var title = mostRecData[1];
				var price = mostRecData[2];
				var klass = mostRecData[3];
				$('#curMostRec').val(mostRec);
				$('#mostRec').hide();
				$('#mostRec').html('<a href="search.php?book_uniq=' + mostRec + '" style="text-decoration:underline;color:#777777;"><i><b>' + title + '</b></i> @ <b>$' + price + '</b> (' + klass + ')</a>').fadeIn();
			}
		}
	);
	setTimeout("getMostRecent()",5000);
}

function updateNums() {
	$.post('get_unread_messages.php',
		function (data) {
			if (data != 'bad') {
				//alert(data);
				var data_split = data.split("|");
				$('#unread_num').text(data_split[0]);
				if (data_split[0] > 0) {
					$('#unread_num').css('font-weight','bold');
				} else {
					$('#unread_num').css('font-weight','normal');
				}
				$('#wish_num').text(data_split[1]);
			}
		}
	);
	//setTimeout("updateMessages('"+uniq_id+"')",1000);
}
