function validateForm() {

	var list = getElement('isbnNumbers').value.trim();

	if (list == "") { alert('You must enter at least one ISBN or EAN to run a search.'); return false;}

	return true;	
}

var firstSearch = true;
$(document).ready(function(e) {
	
//	$('#slideshow').cycle({
//		fx: 'fade'
//	});

	// It's possible, with caching, that the images could
	//  be loaded *before* this code runs.  So:
	var is_image_loaded = function(img) {
		// IE
		if(!img.complete) {
			return false;
		}
		// Others
		if(typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
			return false;
		}
		return true;
	};
	
	var first_slide = $('#slideshow img:first');
	if (is_image_loaded(first_slide.get(0))) {
		$('#loading').hide();
		$('#slideshow').cycle({fx: 'fade'});
	} else {
		first_slide.load(function(e) {
			$('#loading').hide();
			$('#slideshow').fadeIn().cycle({fx: 'fade'});
		});
		$('#slideshow').hide();
		// I'll handwave the display of a loading indicator, since
		// it's covered in the tutorial linked in the question.
		$('#loading').show();
	}	
	
	$('#socialSearchBar .search').focus(function(e) {
		if (firstSearch) $(this).val('');
		firstSearch = false;
	});
	
	$('#frmSiteSearch').submit(function(e) {
		if (isISBN($('#socialSearchBar .search').val())) {			
			alert('The seach bar is for searching the site. If you wish to sell a book please type the ISBN(s) into the box below!');
			return false;
		}
		
		return true;
	});
});

function isISBN(val) {
	
	// Set default variables and cleanup ISBN
	var isbn10exp = /^\d{9}[0-9X]$/;
	var isbn13exp = /^\d{13}$/;
	var total     = 0;

		// Get candidate ##
		isbnnum = val.replace(/[-\s]/g,"").toUpperCase();
		isbnlen = isbnnum.length;		
		total   = 0;
		
		// Check if this is either a valid 10 or 13 character ISBN
		if (!(isbn10exp.test(isbnnum)) && !(isbn13exp.test(isbnnum))) {
			return false;
		}

		// Validate & convert a 10-digit ISBN
		if (isbnlen == 10) {

			// Test for 10-digit ISBNs:
			// Formulated number must be divisible by 11
			// 0234567899 is a valid number
			for (var x=0; x<9; x++) {
				total = total+(isbnnum.charAt(x)*(10-x));
			}

			// check digit
			z = isbnnum.charAt(9);
			if (z == "X") { z = 10; }

			// validate ISBN
			if ((total+z*1) % 11 != 0) { // modulo function gives remainder
				return false;
			}
			
			return true; // is valid ISBN-10
		}
		
		// Validate & convert a 13-digit ISBN
		else {
			// Test for 13-digit ISBNs
			// 9780234567890 is a valid number
			for (var x=0; x<12; x++) {
				if ((x % 2) == 0) { y = 1; }
				else { y = 3; }
				total = total+(isbnnum.charAt(x)*y);
			}

			// check digit
			z = isbnnum.charAt(12);

			// validate ISBN
			if ((10 - (total % 10)) % 10 != z) { // modulo function gives remainder
				return false;
			}			
			
			return true; 

		}
		

	return true;
		
}// end isISBN


function toggleAnswer(id) { $('#faq_' + id).slideToggle(); }
