var BookOfTheWord = new Array(
	"Genesis",
	"Exodus",
	"Leviticus",
	"Numbers",
	"Deuteronomy",
	"Joshua",
	"Judges",
	"1Samuel",
	"2Samuel",
	"1Kings",
	"2Kings",
	"Psalms",
	"Isaiah",
	"Jeremiah",
	"Lamentations",
	"Ezekiel",
	"Daniel",
	"Hosea",
	"Joel",
	"Amos",
	"Obadiah",
	"Jonah",
	"Micah",
	"Nahum",
	"Habakkuk",
	"Zephaniah",
	"Haggai",
	"Zechariah",
	"Malachi",
	"Matthew",
	"Mark",
	"Luke",
	"John",
	"Revelation"
);

var BookSize = new Array(
	 50,
	 40,
	 27,
	 36,
	 34,
	 24,
	 21,
	 31,
	 24,
	 22,
	 25,
	150,
	 66,
	 52,
	  5,
	 48,
	 12,
	 14,
	  3,
	  9,
	  1,
	  4,
	  7,
	  3,
	  3,
	  3,
	  2,
	 14,
	  4,
	 28,
	 16,
	 24,
	 21,
	 22
);


function RandomChapter(form)
{
	// Select a random book
	var n = BookSize.length;
	var chapcnt = 0;
	var i;
	for (i = 0; i < n; i++)
		chapcnt += BookSize[i];
	var r = Math.floor(Math.random()*chapcnt)%chapcnt + 1;
	for (i = 0; i < n; i++)
	{
		if (r <= BookSize[i])
		{
			var Book = BookOfTheWord[i];
			var j;
			for (j = 0; j < form.Book.length; j++)
			{
				form.Book[j].selected = (form.Book[j].value == Book);
			}
			form.Chapter.value = r;
			return true;
		}
		r -= BookSize[i];
	}
	return false;
}	
function Read()
{
	if (!ValidRef(document.selection))
		return false;
	var loc = "/cgi-bin/Read?Book="+document.selection.Book.value+"&Chapter="+document.selection.Chapter.value;
	document.selection.action = loc;
	return true;
}	
function Study()
{
	if (!ValidRef(document.selection))
		return false;
	var loc = "/cgi-bin/Study?Book="+document.selection.Book.value+"&Chapter="+document.selection.Chapter.value;
	document.selection.action = loc;
	return true;
}	


// This assumes there are form fields named Book, Chapter, and Verse:
//    <select name=Book>               With an option naming each book
//                                     (the name is the "value")
//    <input type=text name=Chapter>   The chapter number
//    <input type=text name=Verse>     The verse number (optional)
//
// If there is no Verse field, a verse number in the input will be
//   ignored.
//
// This routine examines the Chapter value, or if it is blank or all
//   digits, the Verse value.  If it looks like it starts with a book
//   name, it breaks the value down and distributes it to the three
//   fields.
// The input book name must be a proper prefix of exactly one of the
//   <option> values in the Book <select>.
//
// Normally this would be called from the FORM tag:
//     <form ... onsubmit="return ValidRef(this)">
// Alert pop-ups describe any errors that are found.

function ValidRef(form)
{
	var Letters = "abcdefghijklmnopqrstuvwxyz";
	var Digits = "0123456789";

	// Shift input to lowercase for comparison
	value = String(form.Chapter.value.toLowerCase());
	// Strip leading spaces
	while (value.length && value.substr(0,1) == " ")
		value = value.substr(1);
	if (form.Verse)
	{
		var tryverse = true;
		if (value.length != 0)
		{
			// Strip trailing spaces
			while (value.substr(value.length-1) == " ")
				value = value.substr(0,value.length-1);
			// Only try the verse if the chapter is all digits
			for (i = 0; i < value.length; i++)
				if (Digits.indexOf(value.substr(i,1)) < 0)
					tryverse = false;
		}
		if (tryverse)
		{
			// Shift input to lowercase for comparison
			var tmpvalue = form.Verse.value.toLowerCase();
			// Strip leading spaces
			while (tmpvalue.length && tmpvalue.substr(0,1) == " ")
				tmpvalue = tmpvalue.substr(1);
			// If it looks like there is a book or chapter
			//    supplied here, use it
			if (Letters.indexOf(tmpvalue.substr(0,1)) >= 0 ||
			    Letters.indexOf(tmpvalue.substr(1,1)) >= 0 ||
			    tmpvalue.indexOf(":") > 0 ||
			    tmpvalue.indexOf(".") > 0)
				value = tmpvalue;
		}
	}
	if (value.length == 0) // All spaces
	{
		if (form.Verse)
			alert("No chapter or verse supplied");
		else
			alert("No chapter supplied");
		return false;
	}
	// Strip trailing spaces
	while (value.substr(value.length-1) == " ")
		value = value.substr(0,value.length-1);

//alert("Comparison value: \""+value+"\"");
	// If the first or second character is a letter, assume a book
	//  was entered
	var found = -1;
	if (Letters.indexOf(value.substr(0,1)) >= 0 || (value.length > 1 && Letters.indexOf(value.substr(1,1)) >= 0))
	{
		// Use up to the first space as the book
		i = value.indexOf(" ");
		var book = value.substr(0,i);
		var isabbrev = 0;
		if (book.substr(i-1,1) == ".")
		{
			isabbrev = 1;
			book = book.substr(0,i-1);
		}

		// The rest is chapter:verse (strip extra spaces)
		value = value.substr(i+1);
		while (value.substr(0,1) == " ")
			value = value.substr(1);

		// The input book must match the beginning of exactly one of
		//   the books in the list
		var match = 0;     // The index of the first book that matches
		var matches = "";  // A list of the matching books
		found = 0;         // The number of matches
		for (i = 0; i < form.Book.length; i++)
		{
			//alert("Comparing \""+book+"\" with \""+form.Book[i].value.toLowerCase().substr(0,book.length)+"\"");
			if (form.Book[i].value.length >= (book.length+isabbrev))
			{
				if (book == form.Book[i].value.toLowerCase().substr(0,book.length))
				{
					if (found == 0)
					{
						match = i;
						matches = form.Book[i].value;
					}
					else
						matches = matches+", "+form.Book[i].value;
					found++;
				}
			}
		}

		if (found == 0)
		{
			if (isabbrev)
				alert("\""+book+".\" is not an abbreviation for a book in the list");
			else
				alert("The book \""+book+"\" was not found in the list");
			return false;
		}
		if (found > 1)
		{
			alert("Need more input.\n  The book \""+book+"\" matches: "+matches);
			return false;
		}
		form.Book[match].selected = true;
	}

	// Now the book is taken care of.  value will have only chapter:verse left.
	var chapter = String(value);
	var verse = "";

	// Look for a colon or dot separator in the input
	//   even if no verse is expected.
	var where = value.indexOf(":");
	if (where < 0)
		where = value.indexOf(".");

	// assume verse is optional
	if (where >= 0)
	{
		chapter = value.substr(0,where);
		verse = value.substr(where+1);
	}

	for (i = 0; i < chapter.length; i++)
	{
		if (Digits.indexOf(chapter.substr(i,1)) < 0)
		{
			if (form.Verse)
				alert("Unexpected character in chapter number\n  Use digits only, please.\n  Use a colon or period to separate chapter and verse");
			else
				alert("Unexpected character in chapter number\n  Use digits only, please.");
			return false;
		}
	}
	form.Chapter.value = chapter;
	if (form.Verse)
	{
		// If the input being processed had no book nor colon, and
		//   there is a Verse field, the verse must not be changed.
		if (found < 0 && where < 0)
			verse = form.Verse.value;

		for (i = 0; i < verse.length; i++)
		{
			if (Digits.indexOf(verse.substr(i,1)) < 0)
			{
				alert("Unexpected character in verse number\n  Use digits only, please");
				return false;
			}
		}
		form.Verse.value = verse;
	}
	return true;
}

