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 running = false;
$(document).ready(function(e) {
	
	$("#ticker").jStockTicker({interval: 45});
	$('#ticker').removeClass('hidden');

//	$('#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').show().cycle({fx: 'fade'});
		running = true;
	} else {
		first_slide.load(function(e) {
			running = true;
			$('#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();

		// IE sometimes doesn't start with the .load callback...this will force the
		// slideshow to start after 5 seconds if it hasn't started already		
		window.setTimeout(function() {
			if (!running) {
				running = true;
				$('#loading').hide();
				$('#slideshow').fadeIn().cycle({fx: 'fade'});
			}			
		},5000);
	}	

	
	$('#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


var signingUp = false;
function signupNL() {

	if (signingUp == true) 
	{
		alert('Your email address is already in our list!');	
		return false;
	}

	var em = $('input[name=newsletterEmail]',$('#frmNewsletterSignup')).val();
	
	if (em == '') 
	{
		alert('Please provide email address!');	
		return false;
	}
	
	signingUp = true;
	var opts = {
		'AJAX_IWF_CALLER' : true,
		'op'    : 'Newsletter Signup',
		'email' : em
	};

	$.post('pages/ajax_actions/account_processing_ajax.php',opts,function(data) {
		
		switch(data.success) {
			case 1:	
				signingUp = true;
				$('input[name=newsletterEmail]',$('#frmNewsletterSignup')).val('');
				alert('Your email address has been added to our newsletter mailing list!');
			break;
			
			case -1:
				signingUp = false;
				alert('Your email address is already on our list!');	
			break;
			
			case -2:
				signingUp = false;
				alert('Your must enter a valid email address');								
			break;
			
		}
	},'json');
	
	return false;
	
}// end signupNL

