var DefinitionViewType = "cl";

//Client name of the bible list control
var MaxUserTranslations;

//Client name of the bible list control
var BibleListName = "kjv,nkjv,niv,ylt";

var DefinitionList = "Naves,Eastons,Isbe,Amtract,Smith,Torrey";

var urlId = null;

//last word selected - tracks duplicate clicks
var LastWordId = -1;

function ClearText() {
	wH("divContent", "clear", "");
	wH("dvScriptures", "clear", "");
}

function GetNamesByletter(){
	var Let1 = document.getElementById("Letter1");
	var Let2 = document.getElementById("Letter2");
	var sLetter = "aa";
	
	if (Let1 != null && Let2 != null)
	  sLetter = Let1.options[Let1.selectedIndex].value + Let2.options[Let2.selectedIndex].value;
	//display processing message
	DisplaywProcessingMsg('shw', "Loading Name list ..."); 
	BibleNamesMethods.GetBibleNamesByLetter(sLetter, GetBibleNamesByLetter_callback);
}

//Get a list of scriptures by reference id
function GetBibleNamesByLetter_callback(response)
{
	if (response.error != null)	
	{
		//error message
		alert("Error. [2001] \n" + response.request.responseText); 
	}
	else	
	{
		var dsWords = response.value;
		
		if(dsWords != null && typeof(dsWords) == "object" && dsWords.Tables != null)
		{
			var ShowWords = document.getElementById("WordList");
			ShowWords.options.length=0;
			
			if (ShowWords != null)
			{
				//clear the number of options
				ShowWords.options.length = 0;
				
				for(var i=0; i < dsWords.Tables[0].Rows.length; i++)
				  ShowWords.options[i] = new Option(dsWords.Tables[0].Rows[i].name, dsWords.Tables[0].Rows[i].id);
			}
			//display processing message
	        DisplaywProcessingMsg('hde');		
		}
		else if (parseInt(dsWords.Tables[0].Rows.length) > 0 )
		{
		  alert ("Zero Records were found");
		}
		else 
		{ 
		  alert("Error. [3001] \n" + response.request.responseText); 
		}
	}
}


function GetNamesByWildCard(){
	var WildCard = document.getElementById("txtWildcard");
	if (WildCard != null)
	{
	  if (WildCard.value.length > 0 )
	  {//display processing message
	    DisplaywProcessingMsg('shw', "Loading Name list ...");
	    BibleNamesMethods.GetBibleNamesByWildcard(WildCard.value, GetBibleNamesByWildcard_callback);
	  }
	  else
	    alert("Please enter a search string.");
	}
}


//Get a list of scriptures by reference id
function GetBibleNamesByWildcard_callback(response)
{
	if (response.error != null)	
	{
		//error message
		alert("Error. [2001] \n" + response.request.responseText); 
	}
	else	
	{
		var dsWords = response.value;
		
		if(dsWords != null && typeof(dsWords) == "object" && dsWords.Tables != null)
		{
		
			var ShowWords = document.forms(0).WordList			//getElementById("WordList");
			ShowWords.options.length=0;
			
			if (ShowWords != null)
			{
				//clear the number of options
				ShowWords.options.length = 0;
				for(var i=0; i < dsWords.Tables[0].Rows.length; i++)
				  ShowWords.options[i] = new Option(dsWords.Tables[0].Rows[i].name, dsWords.Tables[0].Rows[i].id);
			}	
			//display processing message
	        DisplaywProcessingMsg('hde');	
		}
		else if (parseInt(dsWords.Tables[0].Rows.length) > 0 )
		{
		  alert ("Zero Records were found");
		}
		else 
		{ 
		  alert("Error. [3001] \n" + response.request.responseText); 
		}
	}
}
//get the number of chapters for a given book
function GetBibleNamesById(){
   if (urlId == null)
   {
      var ShowWords = document.getElementById("WordList");
      if (ShowWords.selectedIndex >= 0)
      {
        if (ShowWords.options[ShowWords.selectedIndex].value != LastWordId)
        {
          LastWordId = ShowWords.options[ShowWords.selectedIndex].value;
            
          var BibleTranslation = "KJV";
        	
          // translation list
          var BibleList = document.getElementById(BibleListName);
          if (BibleList != null)
	          BibleTranslation = BibleList.options[BibleList.selectedIndex].value;
        	
          //display processing message
          DisplaywProcessingMsg('shw', "Loading Name definition and Scriptures");
          BibleNamesMethods.GetBibleNamesById(ShowWords.options[ShowWords.selectedIndex].value, GetBibleNamesById_callback);
          document.getElementById("divContent").style.display = "block";
          document.getElementById("spScriptures").style.display = "block";
          document.getElementById("trHelp").style.display = 'none';
          document.getElementById("hlpImage").src = "images/treeview/click-down.gif"
          document.getElementById("hlpImage").alt = "Show help";
		    }
	    }
	    else
	     alert("Please select a word")
	  }
	  else
	  {
	    //display processing message
      DisplaywProcessingMsg('shw', "Loading Name definition and Scriptures");
      BibleNamesMethods.GetBibleNamesById(urlId, GetBibleNamesById_callback);
      document.getElementById("divContent").style.display = "block";
      document.getElementById("spScriptures").style.display = "block";
      document.getElementById("trHelp").style.display = 'none';
      document.getElementById("hlpImage").src = "images/treeview/click-down.gif"
      document.getElementById("hlpImage").alt = "Show help";
	  }
}

//Get a list of scriptures by reference id
function GetBibleNamesById_callback(response)
{
	if (response.error != null)	{
		//error message
		alert("Error. [2001] \n" + response.request.responseText); 
	}
	else	
	{
		var dsWords = response.value;
		
		var ShowWords = document.getElementById("WordList");
		if(dsWords != null )
		{
	    var results = dsWords.split("|");
	    wH("divContent", DefinitionViewType, "<div class=\"BibleName\">" + results[0] + "</div>" + results[1] + "<br />");
	    if(results[2] != "0" ) GetBibleNamesScriptures(results[2], results[0]);
		}
		//display processing message
	  DisplaywProcessingMsg('hde');
	}
}

//get the number of chapters for a given book
function GetBibleNamesScriptures(refIds, bname){
	//display processing message
	DisplaywProcessingMsg('shw');
	BibleNamesMethods.GetVerseListByRefID(BibleTranslations, refIds, bname, GetBibleNamesScriptures_callback);
}

//Get a list of scriptures by reference id
function GetBibleNamesScriptures_callback(response)
{
	if (response.error != null)	{
		//error message
		alert("Error. [2001] \n" + response.request.responseText); 
	}
	else	
	{
		var dsScriptures = response.value;
		if(dsScriptures != null && typeof(dsScriptures) == "object" && dsScriptures.Tables != null)	
		{
		    //get a reference to the bible books and the chapters
			for(var i=0; i < dsScriptures.Tables[0].Rows.length; i++)
			{
			   //Display the book and chapter information
				wH(dsScriptures.Tables[0].Rows[i].Version.toLowerCase(), DefinitionViewType, dsScriptures.Tables[0].Rows[i].TableVerses + "<hr>");
			}
			//display processing message
	        DisplaywProcessingMsg('hde');
		}
		else { 
			alert("Error. [3001] \n" + response.request.responseText); 
		}
	}
}

//get the number of chapters for a given book
function GetScriptures(){
	var Scripture = document.getElementById(SearchFieldName);
	if (Scripture != null)
	{
		if (Scripture.value.length > 3 && Scripture.value != "Enter Scripture here.")
		{
			var scrips = Scripture.value;
			//display processing message
	        DisplaywProcessingMsg('shw');
			BibleSearchMethods.GetScripturesById(BibleTranslations, Scripture.value, GetScripturesFromText_callback);
		}
		else
			alert("Please enter a scripture");
	}
	else
		alert("Errors occured");
}

//Get a list of scriptures by reference id
function GetScripturesFromText_callback(response){
	if (response.error != null)	{
		//error message
		alert("Error. [2001] \n" + response.request.responseText); 
	}
	else	{
		var dsScriptures = response.value;
		if(dsScriptures != null && typeof(dsScriptures) == "object" && dsScriptures.Tables != null)	
		{
			//get a reference to the bible books and the chapters
			for(var i=0; i < dsScriptures.Tables[0].Rows.length; i++)
			{
				//Display the book and chapter information
				wH(dsScriptures.Tables[0].Rows[i].Version, DefinitionViewType, dsScriptures.Tables[0].Rows[i].TableVerses);
			}
			
			//display processing message
	        DisplaywProcessingMsg('hde');
		}
		else { 
			alert("Error. [3001] \n" + response.request.responseText); 
		}
	}
}

//clears selected scripture text
function ClearScriptureReader()
{
  var aTrans = BibleTranslations.split(",");
	for (var i=0; i<aTrans.length; i++){
		wH(aTrans[i], "cl", "");
	}
}

function ReadScripture(Scripture)
{
	var ScriptureList = document.getElementById("txtScriptures");
	ScriptureList.value = Scripture;
	
}