
var isbnUtils           = {};
isbnUtils.valid         = {};
isbnUtils.inValid       = {};
isbnUtils.proto         = location.protocol;
isbnUtils.host          = location.host;
isbnUtils.base          = isbnUtils.proto + '//' + isbnUtils.host;
isbnUtils.searchStopped = false;

// Check The List Of Input ISBN's
isbnUtils.checkList = function ( listId, errorId ) {

    // Clear out the isbn arrays
    isbnUtils.invalid = [];
    isbnUtils.valid   = [];

    // Reset searchStopped to false
    isbnUtils.searchStopped = false;

    // Get the list
    var list = jQuery("#listId");

    // Set errorId to default of 'error_box'
    if( typeof errorId == 'undefined' ) {
	errorId = 'error_box';
    }

    // Clear the error box
    jQuery("#errorId").css ({ display: "none" });
    jQuery("#errorId").empty();

    // Remove all bad chars and replace with a space
    var isbn_string = isbnUtils.removeBadChars( list.attr("value") );

    // Split the items into an array
    var isbns = isbn_string.split( " " );

    // Remove elements that are empty strings or new lines
    isbns.remove( "" );
    isbns.remove( "\n" );

    // Return if the list is empty
    if( isbns.length < 1 ) {

	// Erase any garbage left in the list
	list.value = "";

	// Set search stopped var
	isbnUtils.searchStopped = true;

	return false;
    }

    // Check the length of the isbns
    isbnUtils.checkLength( isbns );

    // Set the list value to the valid list of isbns
    list.value = isbnUtils.valid.join( "\n" );

    // Display error box if invalid isbns were found
    if( isbnUtils.invalid.length > 0 ) {

	// Build the flagged invalid isbn array
	var flagged = [];

	flagged = isbnUtils.invalid.map( function( item, index ) {
		return "*" + item;
	    } );

	// Add the invalid isbns to the bottom of the list
	if( isbnUtils.valid.length < 1 ) {
	    list.value = flagged.join( "\n" );
	} else {
	    list.value = list.value + "\n" + flagged.join( "\n" );
	}

	// Build the error list html array
	var invalid_html = [];

	invalid_html = isbnUtils.invalid.map( function( item, index ) {
		return "<li>" + item + "</li>";
	    } );

	// Display the error list
	jQuery("#errorId").html("<li>Found invalid ISBNs.</li>");
	// add this line back for feedback in the error message about which ISBNs       + invalid_html.join( " " );
	jQuery("#errorId").css({ display: "block" });

	isbnUtils.searchStopped = true;

	return false;
    }

    return true;
};

// Clean Up The Input String
isbnUtils.removeBadChars = function ( isbns ) {

    // Remove dashes
    isbns = isbns.replace( /-/g, "" );

    // Remove non-isbn characters
    isbns = isbns.replace( /[^0-9Xx]+/g, " " );

    // Remove all whitespace
    isbns = isbns.replace( /(\n|\t| )/g, " " );

    // And Return
    return isbns;
};

// Make Sure We're 10 or 13
isbnUtils.checkLength = function ( isbns ) {
    isbns.each( function( item, index ) {
	    if( item.length == 13 || item.length == 10 ) {
		isbnUtils.valid.include( item );
	    } else {
		isbnUtils.invalid.include( item );
	    }
	});
};

// Used To Be In textbooks.js and a couple other spots.
function isbnsearch () {

    var isbns = document.isbngroupsearch.isbn.value;
    isbns = isbns.replace(/-/g, "");
    isbns = isbns.replace(/[^0-9Xx]+/g, ", ");
    isbns = isbns.replace(/, $/, "");

    if (document.isbngroupsearch.isbn.value.length === 0) {
	alert("Aw....Please enter an ISBN!");
	return(false);
    }

    var tmp = isbns.split(", ");
    for (var i = 0; i < tmp.length; i++) {
	if (!(tmp[i].length == 10 || tmp[i].length == 13 )) {
	    alert("Sorry, but all ISBN's must be either 10 or 13 digits long and have spaces between each one.\n\nPlease double check your ISBN's and click on \"Search\" again.\n");
	    return(false);
	}
    }

    document.isbngroupsearch.isbn.value = isbns;

    return(true);
}
