var DefinitionViewType = "cc";
//status of the testament list to process
var sTestament = "OT";

// all translations for this view
var TranslationsList = "NKJV";

// Selected Bible Bersion
var BibleVersion = "NKJV";

//Scripture Regex
var PatterVerses = "((I+|1st|2nd|3rd|First|Second|Third|1|2|3)\s+)?(Genesis|Gen\.?|Exodus|Exod?|Ex\.?|Leviticus|Lev\.?|Levit?\.?|Numbers|Nmb\.?|Numb?\.?|Deuteronomy|Deut?\.?|Dt\.?|Joshua|Josh?\.?|Judges|Jdg\.?|Judg?\.?|Ruth|Ru\.?|Samuel|Sam\.?|Sml\.?|Kings|Kn?gs?\.?|Kin?\.?|Chronicles|Chr\.?|Chron\.?|Ezra|Ez\.?|Nehemiah|Nehem?\.?|Neh\.?|Esther|Esth?\.?|Job|Jb\.?|Psalms?|Ps?|Psa?\.?|Proverbs?|Prov?\.?|Ecclesiastes|Eccl?\.?|Songs? of Solomon|Song?\.?|Songs|Isaiah|Isa\.?|Jeremiah|Jer\.?|Jerem\.?|Lamentations|Lam\.?|Lament?\.?|Ezekiel|Ezek?\.?|Daniel|Dan\.?|Hosea|Hos\.?|Joel|Jo\.|Amos|Am\.?|Obadiah|Obad?\.?|Jonah|Jon\.|Micah|Mic\.?|Nahum|Nah\.?|Habakkuk|Hab\.?|Habak|Zephaniah|Zeph\.?|Haggai|Hagg?\.?|Zechariah|Zech?\.?|Malachi|Malac?\.?|Mal\.?|Mat{1,2}hew|Matt?\.?|Mark|Mr?k\.?|Luke|Lu?k\.?|John|Jh?n\.?|Jo\.|Acts?|Ac\.?|Romans|Rom\.?|Corinthians|Cor\.?|Corin\.?|Galatians|Gal\.?|Galat\.?|Ephesians|Eph\.?|Ephes\.?|Phil+ippians|Phili?\.?|Colossians|Col\.?|Colos\.?|Thessalonians|Thes?\.?|Timothy|Tim\.?|Titus|Tts\.?|Tit\.?|Philemon|Phil?\.?|Hebrews|Hebr?\.?|James|Jam\.?|Jms\.?|Peter|Pete?\.?|Jude|Ju\.?|Revelations?|Rev\.?|Revel\.?)?\s+\d{1,}(-\d{1,})?(\:\d{1,}-?,?\d{0,2}((,\s\d{1,})*)?)";

function SetBibleVersion(Version)
{
	BibleVersion = Version;
}

//get the number of chapters for a given book
function ShowBooks(Testament){
	sTestament = Testament;
	
	var otList  = document.getElementById("OTBibleBooks");
	var ntList  = document.getElementById("NTBibleBooks");
	
	if (Testament == "NT")
	{
		otList.style.display = 'none';
		ntList.style.display = 'block';
	}
	else
	{
		otList.style.display = 'block';
		ntList.style.display = 'none';
	}
}

//show processing message
function DisplaywProcessingMsg(divAction)
{
	var msgDiv = document.getElementById("ProcessingMessage")
	if (msgDiv != null)
	{
		if (divAction == "shw")
			msgDiv.style.display = 'block';
		else
			msgDiv.style.display = 'none';
	}
}

//get the number of chapters for a given book
function GetChapters(oBibleBooks, strTestament){
	sTestament = strTestament
	if (oBibleBooks.options[oBibleBooks.selectedIndex].value == "NA")
		alert("Please Select a Valid Book Name");
	else
	{
		//deselect the unused bible books dropdown
		var NoteSelectedBook  = document.getElementById((strTestament == "OT" ? "NTBibleBooks" : "OTBibleBooks"))
				NoteSelectedBook.selectedIndex = -1;
		
		// get the chapter count form the database
		BibleSearchMethods.GetChaptersVersesCount(BibleVersion, parseInt(oBibleBooks.options[oBibleBooks.selectedIndex].value), GetChapters_callback);
		
		//reset the chapters and the verses dropdown
		document.getElementById("BookChapters").options.length=0;
		document.getElementById("Verses").options.length=0;
	}
}

function GetChapters_callback(response){
	if (response.error != null)	{
		//error message
		alert("Error. [2001] \n" + response.request.responseText); 
	}
	else	{
		var dsChapters = response.value;
		if(dsChapters != null && typeof(dsChapters) == "object" && dsChapters.Tables != null)
		{
			var selChapters = document.getElementById("BookChapters");
			if (selChapters != null)
			{
				//clear the number of options
				selChapters.options.length = 0;
				for(var i=0; i < dsChapters.Tables[0].Rows.length; i++)
					selChapters.options[i] = new Option(dsChapters.Tables[0].Rows[i].Chapter, dsChapters.Tables[0].Rows[i].Verses);
			}		
		}
		else { 
			alert("Error. [3001] \n" + response.request.responseText); 
		}
	}
}

//get the number of chapters for a given book
function GetVerses(oChapters) {
	if ( parseInt(oChapters.options[oChapters.selectedIndex].value) > 0 ){
		var selVerses = document.getElementById("Verses")
		if (selVerses != null) {	
			//clear the number of options first
			selVerses.options.length = 0;
			for(var i=0; i < parseInt(oChapters.options[oChapters.selectedIndex].value); i++)
				selVerses.options[i] = new Option(i+1, i+1);
		}
	}
}

//Handles the browser button click
function RetrieveSelectedVerses()
{
	var selVersion = document.getElementById("Translations"); 
	var selBooks   = document.getElementById(sTestament + "BibleBooks"); 
	var selChapter = document.getElementById("BookChapters");
	var selVerses  = document.getElementById("Verses");

	//check for required items
	var sMessage = "";
	if (selBooks.selectedIndex  == -1)
		 sMessage += "Bible Book";
	else if (selChapter.selectedIndex == -1)
		sMessage += "Chapter";
		
	if (sMessage == "")
	{
	
		if (selVerses.selectedIndex > -1)
		{
			//display processing message
			DisplaywProcessingMsg('shw');
	
			//capture selected verses
			var SelectedVerses = "";
			
			//get the book id and the chapter
			var SelectedBookandChapter = selBooks.options[selBooks.selectedIndex].value + "0" + selChapter.options[selChapter.selectedIndex].text;
		
			for (var i=0; i<selVerses.length; i++)
			  if (selVerses.options[i].selected)
				SelectedVerses += SelectedBookandChapter + "0" + (i+1) + ",";
		
			//add the 0 to the end of the string to account for the extra comma [,]
			SelectedVerses += "0";
			
			// Execute the request
			BibleSearchMethods.GetVerseListVersioned(TranslationsList, SelectedVerses, GetVerseListVersioned_callback); 
		}
		else
		{
		    //alert(TranslationsList + ":" + selBooks.options[selBooks.selectedIndex].value + ":" + selChapter.options[selChapter.selectedIndex].value)
			BibleSearchMethods.GetVerseListVersionedChapter(TranslationsList, selBooks.options[selBooks.selectedIndex].value , selChapter.options[selChapter.selectedIndex].value, GetVerseListVersioned_callback); 
			//GetVerseListVersionedChapter(TranslationsList, selBooks.options[selBooks.selectedIndex].value , selChapter.options[selChapter.selectedIndex].value);
		}
	}
	else
	{
		alert("Please Select a \"" + sMessage + "\"");
	}
	return false;
}

function GetVerseListVersioned_callback(response)
{
	if (response.error != null)	{
		//error message
		alert("Error. [2001] \n" + response.request.responseText); 
	}
	else
	{ 
		var dsChapters = response.value;
		if(dsChapters != null && typeof(dsChapters) == "object" && dsChapters.Tables != null)	{
			//get a reference to the bible books and the chapters
				var selBooks   = document.getElementById(sTestament + "BibleBooks")
				var selChapter = document.getElementById("BookChapters");
				
				for(var i=0; i < dsChapters.Tables[0].Rows.length; i++)
				{
					//Display the book and chapter information
					wH(dsChapters.Tables[0].Rows[i].Version, DefinitionViewType, dsChapters.Tables[0].Rows[i].TableVerses);
					
					wH(dsChapters.Tables[0].Rows[i].Version, DefinitionViewType,"<h4 style=\"margin-bottom: 2px;\">" + 
					selBooks.options[selBooks.selectedIndex].text + " " + selChapter.options[selChapter.selectedIndex].text + 
					"  <sup>" + (sTestament) + "</sup></h4>");
				}
				//hide processing message
				DisplaywProcessingMsg("hde");
		}
		else{ 
			alert("Error. [3001] \n" + response.request.responseText); 
		}
	}
}

//Get a list of scriptures by reference id
function GetVerseListVersionedChapter(TranslationsList, SelectedBook, SelectedChapter)
{
	//Execute the callback method
	BibleSearchMethods.GetVerseListVersionedChapter(TranslationsList, SelectedBook, SelectedChapter, GetVerseListVersioned_callback); 
}

function GetVerseListVersionedChapter_callback(response)
{
	if (response.error != null)	{
		//error message
		alert("Error. [2001] \n" + response.request.responseText); 
	}
	else
	{ 
		var dsChapters = response.value;
		if(dsChapters != null && typeof(dsChapters) == "object" && dsChapters.Tables != null)	{
			//get a reference to the bible books and the chapters
				var selBooks   = document.getElementById(sTestament + "BibleBooks")
				var selChapter = document.getElementById("BookChapters");
				
				for(var i=0; i < dsChapters.Tables[0].Rows.length; i++)
				{
					//Display the book and chapter information
					wH(dsChapters.Tables[0].Rows[i].Version, DefinitionViewType, dsChapters.Tables[0].Rows[i].TableVerses);
					
					wH(dsChapters.Tables[0].Rows[i].Version, DefinitionViewType,"<h4 style=\"margin-bottom: 2px;\">" + 
					selBooks.options[selBooks.selectedIndex].text + "  " + 
					"<sup>" + (sTestament) + "</sup></h4>");
				}
		}
		else{ 
			alert("Error. [3001] \n" + response.request.responseText); 
		}
	}
}

function ClearScriptureReader()
{
	var aTrans = TranslationsList.split(","); //["BBE", "NIV", "KJV", "WEB", "YLT"];		
		for (var i=0; i<aTrans.length; i++){
			wH(aTrans[i], "cl", "");
		}
}