var content_wishlist;
// wishlis is also used as a separate form, where init is not needed.
var runWishlistInit = true;

function isISBN(isbn) {
	var stripped = isbn.replace(/[^0-9]/gi, '');
	var length = stripped.length;
	var sum = 0;
	
	if( length == 10 ) {
		// ISBN-10
		for(var i = 0 ; i < 10 ; i++ ) {
			sum += ( 10 - i ) * ( stripped.substr(i, 1) * 1 );
		}
		var remainder = sum % 11;
		return remainder == 0 ;
	} else
	if( length == 13 ) {
		// ISBN-13
		for(var i = 0; i < 12; i++ ) {
			var j = i % 2;
			var digit = stripped.substr(i, 1) * 1;
			sum += (j == 0 ? digit : 3 * digit);
		}
		var remainder = sum % 10 ;
		var checkdigit = remainder == 0 ? 0 : 10 - remainder ;
		var lastdigit = stripped.substr(stripped.length-1, 1);
		return ( lastdigit == checkdigit ) ;
	} else {
		// Invalid
		return false;
	}
}


function WishlistInit() {
	if (runWishlistInit) {
		content_wishlist = new Fx.Slide('content-wishlist');
		content_wishlist.hide();
	}
}

setTimeout("WishlistInit()", 1000);

function ShowWishlist() {
	$('content-wishlist-thanx').style.display = "none";
	$('content-wishlist-form').style.display = "";
	$('content-wishlist').style.display = "";
	content_wishlist.slideIn();
	return false;
}

function HideWishlist() {
	content_wishlist.slideOut();
	setTimeout('$("content-wishlist").style.display = "none";', 300);
}

function SendContentWish() {
	if (!isEmail($("Email").value)) {
		alert($("ErrEmail").innerHTML);
		$("Email").focus();
		return false;
	}
	if ($("Name").value == '') {
		alert($("ErrName").innerHTML);
		$("Name").focus();
		return false;
	}
	if (!isISBN($("ISBN").value)) {
		alert($("ErrISBN").innerHTML);
		$("ISBN").focus();
		return false;
	}

	new Request({
		url : url + "ajax_post_content_wish.jsp",
		data : {
			"Name" : $("Name").value,
			"Email" : $("Email").value,
			"Bookname" : $("Bookname").value,
			"Publisher" : $("WishPublisher").value,
			"ISBN" : $("ISBN").value.replace(/[^0-9]/gi, ''),
			"ShopID" : $("ShopID").value
			},
		onSuccess : ContentWishThanx
	}).send();

}

function ContentWishThanx() {
	$('content-wishlist-form').style.display = "none";
	$('content-wishlist-thanx').style.display = "";
}


function PrefillWishlist(publisher, book, isbn) {
	$("WishPublisher").value = publisher;
	$("Bookname").value = book;
	$("ISBN").value = isbn;
	ShowWishlist();
	return false;
}


