// This code is not copyrighted.
// VERSION 1.1

var titleText1 = "Click to read in the New World Translation (pop-up window)\" onmouseover=\"window.status='View";
var titleText2 = "in the New World Translation';return true\" onmouseout=\"window.status='';";

function cite(scripture) {

	var originalScripture = scripture;
	
	
	document.write("<span class=\"scriptureLink\">");


	if(scripture.split(',').length > 1) {
	
		// HANDLING MULTIPLE VERSES AND ADDITIONAL CHAPTER AND VERSES
		
		scriptures = scripture.split(',');
		var currentChapter = "";
		
		// Use a loop to go through each verse
		
		for(loopCounter=0; loopCounter < scriptures.length; loopCounter++) {
		
			//document.write(scriptures[loopCounter]);
			
			if(loopCounter == 0) {
			
				// HANDLING BOOK CHAPTER:VERSE
				if(scriptures[0].split(':').length > 2) {

					displayCrossChapterVerses(scriptures[0], true);
					
					currentChapter = scriptures[0].split('-')[1].split(':')[0];
					
				} else {
				
					document.write("<a title=\"" + titleText1 + " " + scriptures[0] + " " + titleText2 + "\" href=\"javascript:LookUp('" + scriptures[0] + "');\">" + scriptures[0] + "</a>");
				
					currentChapter = scriptures[0].split(' ')[1].split(':')[0];
				
				}
				
			} else {
			
				// HANDLING ADDITIONAL CHAPTER:VERSE-CHAPTER-VERSE
				if(scriptures[loopCounter].split(':').length > 2) displayCrossChapterVerses(scriptures[0].split(' ')[0] + scriptures[loopCounter], false);
			
				// HANDLING ADDITIONAL CHAPTER:VERSE
				else if(loopCounter != 0 && scriptures[loopCounter].split(':').length > 1) {
					
					document.write("<a title=\"" + titleText1 + " " + scriptures[0].split(' ')[0] + scriptures[loopCounter] + " " + titleText2 + "\" href=\"javascript:LookUp('" + scriptures[0].split(' ')[0] + " " + scriptures[loopCounter].split(' ')[1] + "');\">" + scriptures[loopCounter].replace(' ','') + "</a>");
					
					currentChapter = scriptures[loopCounter].split(' ')[1].split(':')[0];
					
				} else {
			
					// HANDLING ADDITIONAL VERSE
					document.write("<a title=\"" + titleText1 + " " + scriptures[0].split(' ')[0] + " " + scriptures[0].split(' ')[1].split(':')[0] + ":" + scriptures[loopCounter].replace(' ','') + " " + titleText2 + "\" href=\"javascript:LookUp('" + scriptures[0].split(' ')[0] + " " + currentChapter + ":" + scriptures[loopCounter].replace(' ','') + "');\">" + scriptures[loopCounter].replace(' ','') + "</a>");
					
				}
		
			}
			
			if(loopCounter < scriptures.length - 1) document.write(", ");
		
		} // End of loop
		
	
	} else {
	
		if(scripture.split(':').length > 2) displayCrossChapterVerses(scripture, true);
	
		// HANDLING NORMAL CITATIONS
		else document.write("<a title=\"" + titleText1 + " " + scripture + " " + titleText2 + "\" href=\"javascript:LookUp('" + scripture + "');\">" + scripture + "</a>");
	
	
	}
	
	document.write("</span>");
	
}

function LookUp(scripture) {

	bookName = "";
	chapterNumber = "";
	verseNumber = "";

	// Detecting format of scripture

	var book_pattern = /^[a-z]+$/i;
	var book_verse_pattern = /^[a-z]+ [0-9abc]+$/i;
	var book_chapter_colon_verse_pattern = /^[a-z]+ [0-9]{1,3}:[0-9abc]{1,3}$/i;
	var book_chapter_colon_verse_hyphen_verse_pattern = /^[a-z]+ [0-9]{1,3}:[0-9abc]{1,3}-[0-9abc]{1,3}$/i;
	var book_chap_chapter_pattern = /^[a-z]+ chapter [0-9]+$/i;
	
	// Detecting format for 1st, 2nd, and 3rd books
	
	var n_book_pattern = /^[0-9nrdthsi]+ [a-z]+$/i;
	var n_book_verse_pattern = /^[0-9nrdthsi]+ [a-z]+ [0-9abc]+$/i;
	var n_book_chapter_colon_verse_pattern = /^[0-9nrdthsi]+ [a-z]+ [0-9]{1,3}:[0-9abc]{1,3}$/i;
	var n_book_chapter_colon_verse_hyphen_verse_pattern = /^[0-9nrdthsi]+ [a-z]+ [0-9]{1,3}:[0-9abc]{1,3}-[0-9abc]{1,3}$/i;
	var n_book_chap_chapter_pattern = /^[0-9nrdthsi]+ [a-z]+ chapter [0-9]+$/i;
	
	// Detecting "Psalm #"
	
	var psalm_chapter_pattern = /^psalm(s){0,1} [0-9]+$/i;
	
	// Detecting "#th Psalm"
	
	var number_psalm_pattern = /^[0-9]{1,3}[nthrdst]{1,2} psalm$/i;
	
	
	// Format "BOOK"
	
	if(scripture.match(book_pattern)) {
	
		//alert("Matched as BOOK");
		
		bookName = scripture;
		chapterNumber = "1";
		verseNumber = "1";
		
	}
	
	
	// Format "BOOK VERSE"

	if(scripture.match(book_verse_pattern)) {
	
		//alert("Matched as BOOK VERSE");
		
		bookName = scripture.split(' ')[0];
		chapterNumber = "1";
		verseNumber = scripture.split(' ')[1];
		
	}
	
	
	// Format "BOOK CHAPTER:VERSE"
	
	if(scripture.match(book_chapter_colon_verse_pattern)) {
		
		//alert("Matched as BOOK CHAPTER:VERSE");
		
		bookName = scripture.split(' ')[0];
		chapterNumber = scripture.split(' ')[1].split(':')[0];
		verseNumber = scripture.split(' ')[1].split(':')[1];
		
	}
	
	
	// Format "BOOK CHAPTER:VERSE-VERSE"
	
	if(scripture.match(book_chapter_colon_verse_hyphen_verse_pattern)) {
	
		//alert("Matched as BOOK CHAPTER:VERSE-VERSE");
		
		bookName = scripture.split(' ')[0];
		chapterNumber = scripture.split(' ')[1].split(':')[0];
		verseNumber = scripture.split(' ')[1].split(':')[1];
		
	}
	
	
	// Format "BOOK chapter CHAPTER"
	
	if(scripture.match(book_chap_chapter_pattern)) {
	
		//alert("Matched as BOOK chapter CHAPTER");
		
		bookName = scripture.split(' ')[0];
		chapterNumber = scripture.split(' ')[2];
		verseNumber = "1";
		
	}
	
	
	// Format "N BOOK"
	
	if(scripture.match(n_book_pattern)) {
	
		//alert("Matched as N BOOK");
		
		bookName = scripture.split(' ')[0] + " " + scripture.split(' ')[1];
		chapterNumber = "1";
		verseNumber = "1";
		
	}
	
	
	// Format "N BOOK VERSE"

	if(scripture.match(n_book_verse_pattern)) {
	
		//alert("Matched as N BOOK VERSE");
		
		bookName = scripture.split(' ')[0] + " " + scripture.split(' ')[1];
		chapterNumber = "1";
		verseNumber = scripture.split(' ')[2];
		
	}
	
	
	// Format "N BOOK CHAPTER:VERSE"
	
	if(scripture.match(n_book_chapter_colon_verse_pattern)) {
		
		//alert("Matched as N BOOK CHAPTER:VERSE");
		
		bookName = scripture.split(' ')[0] + " " + scripture.split(' ')[1];
		chapterNumber = scripture.split(' ')[2].split(':')[0];
		verseNumber = scripture.split(' ')[2].split(':')[1];
		
	}
	
	
	// Format "N BOOK CHAPTER:VERSE-VERSE"
	
	if(scripture.match(n_book_chapter_colon_verse_hyphen_verse_pattern)) {
	
		//alert("Matched as N BOOK CHAPTER:VERSE-VERSE");
		
		bookName = scripture.split(' ')[0] + " " + scripture.split(' ')[1];
		chapterNumber = scripture.split(' ')[2].split(':')[0];
		verseNumber = scripture.split(' ')[2].split(':')[1];
		
	}
	
	
	// Format "N BOOK chapter CHAPTER"
	
	if(scripture.match(n_book_chap_chapter_pattern)) {
	
		//alert("Matched as N BOOK chapter CHAPTER");
		
		bookName = scripture.split(' ')[0] + " " + scripture.split(' ')[1];
		chapterNumber = scripture.split(' ')[3];
		verseNumber = "1";
		
	}
	
	
	// Format "PSALM NUMBER"
	
	if(scripture.match(psalm_chapter_pattern)) {
	
		//alert("Matched as PSALM NUMBER");
		
		bookName = "psalms";
		chapterNumber = scripture.split(' ')[1];
		verseNumber = "1";
		
	}
	
	// Format "NUMBERth PSALM"
	
	if(scripture.match(number_psalm_pattern)) {
	
		//alert("Matched as NUMBERth PSALM");
		
		bookName = "psalms";
		chapterNumber = scripture.split(' ')[0].replace('th','').replace('nd','').replace('st','');
		verseNumber = "1";
		
	}


	bookName = bookName.toLowerCase();

	if(bookName == "genesis") bookName = "ge";
	if(bookName == "exodus") bookName = "ex";
	if(bookName == "leviticus") bookName = "le";
	if(bookName == "numbers") bookName = "nu";
	
	if(bookName == "deuteronomy") bookName = "de";
	if(bookName == "joshua") bookName = "jos";
	if(bookName == "judges") bookName = "jg";
	if(bookName == "ruth") bookName = "ru";
	if(bookName == "1 samuel") bookName = "1sa";
	if(bookName == "2 samuel") bookName = "2sa";
	if(bookName == "1st samuel") bookName = "1sa";
	if(bookName == "2nd samuel") bookName = "2sa";
	if(bookName == "i samuel") bookName = "1sa";
	if(bookName == "i samuel") bookName = "2sa";

	if(bookName == "1 kings") bookName = "1ki";
	if(bookName == "2 kings") bookName = "2ki";
	if(bookName == "1 chronicles") bookName = "1ch";
	if(bookName == "2 chronicles") bookName = "2ch";
	if(bookName == "1st kings") bookName = "1ki";
	if(bookName == "2nd kings") bookName = "2ki";
	if(bookName == "1st chronicles") bookName = "1ch";
	if(bookName == "2nd chronicles") bookName = "2ch";
	if(bookName == "ezra") bookName = "ezr";
	if(bookName == "nehemiah") bookName = "ne";

	if(bookName == "esther") bookName = "es";
	if(bookName == "job") bookName = "job";
	if(bookName == "psalms") bookName = "ps";
	if(bookName == "psalm") bookName = "ps";
	if(bookName == "proverbs") bookName = "pr";
	if(bookName == "ecc") bookName = "ec";
	if(bookName == "ecclesiastes") bookName = "ec";
	if(bookName == "song of solomon") bookName = "ca";

	if(bookName == "isaiah") bookName = "isa";
	if(bookName == "jeremiah") bookName = "jer";
	if(bookName == "jer") bookName = "jer";
	if(bookName == "lamentations") bookName = "la";
	if(bookName == "ezekiel") bookName = "eze";
	if(bookName == "daniel") bookName = "da";
	if(bookName == "hosea") bookName = "ho";

	if(bookName == "joel") bookName = "joe";
	if(bookName == "amos") bookName = "am";
	if(bookName == "obadiah") bookName = "ob";
	if(bookName == "jonah") bookName = "jon";
	if(bookName == "micah") bookName = "mic";
	if(bookName == "mic") bookName = "mic";
	if(bookName == "nahum") bookName = "na";

	if(bookName == "habakkuk") bookName = "hab";
	if(bookName == "zephaniah") bookName = "zep";
	if(bookName == "haggai") bookName = "hag";
	if(bookName == "zechariah") bookName = "zec";
	if(bookName == "malachi") bookName = "mal";
	if(bookName == "matthew") bookName = "mt";
	if(bookName == "matt") bookName = "mt";
	if(bookName == "mat") bookName = "mt";

	if(bookName == "mark") bookName = "mr";
	if(bookName == "luke") bookName = "lu";
	if(bookName == "john") bookName = "joh";
	if(bookName == "acts") bookName = "ac";
	if(bookName == "romans") bookName = "ro";
	if(bookName == "1 corinthians") bookName = "1co";
	if(bookName == "rom") bookName = "ro";
	if(bookName == "1st corinthians") bookName = "1co";
	if(bookName == "i corinthians") bookName = "1co";

	if(bookName == "2 corinthians") bookName = "2co";
	if(bookName == "2nd corinthians") bookName = "2co";
	if(bookName == "ii corinthians") bookName = "2co";
	if(bookName == "galatians") bookName = "ga";
	if(bookName == "gal") bookName = "ga";
	if(bookName == "ephesians") bookName = "eph";
	if(bookName == "philippians") bookName = "php";
	if(bookName == "phil") bookName = "php";
	if(bookName == "colossians") bookName = "col";
	if(bookName == "col") bookName = "col";
	if(bookName == "1 thessalonians") bookName = "1th";
	if(bookName == "1st thessalonians") bookName = "1th";
	if(bookName == "i thessalonians") bookName = "1th";

	if(bookName == "2 thessalonians") bookName = "2th";
	if(bookName == "2nd thessalonians") bookName = "2th";
	if(bookName == "2nd thess") bookName = "2th";
	if(bookName == "ii thessalonians") bookName = "2th";
	if(bookName == "1 timothy") bookName = "1ti";
	if(bookName == "2 timothy") bookName = "2ti";
	if(bookName == "1st timothy") bookName = "1ti";
	if(bookName == "2nd timothy") bookName = "2ti";
	if(bookName == "i timothy") bookName = "1ti";
	if(bookName == "ii timothy") bookName = "2ti";
	if(bookName == "titus") bookName = "tit";
	if(bookName == "philemon") bookName = "phm";
	if(bookName == "hebrews") bookName = "heb";

	if(bookName == "james") bookName = "jas";
	if(bookName == "1 peter") bookName = "1pe";
	if(bookName == "2 peter") bookName = "2pe";
	if(bookName == "1 john") bookName = "1jo";
	if(bookName == "2 john") bookName = "2jo";
	if(bookName == "3 john") bookName = "3jo";
	if(bookName == "1st peter") bookName = "1pe";
	if(bookName == "2nd peter") bookName = "2pe";
	if(bookName == "1st john") bookName = "1jo";
	if(bookName == "2nd john") bookName = "2jo";
	if(bookName == "3rd john") bookName = "3jo";
	if(bookName == "i peter") bookName = "1pe";
	if(bookName == "ii peter") bookName = "2pe";
	if(bookName == "i john") bookName = "1jo";
	if(bookName == "ii john") bookName = "2jo";
	if(bookName == "iii john") bookName = "3jo";

	if(bookName == "jude") bookName = "jude";
	if(bookName == "revelation") bookName = "re";
	if(bookName == "rev") bookName = "re";

	if(chapterNumber < 10) chapterNumber = "0" + chapterNumber;
	if(chapterNumber < 100) chapterNumber = "0" + chapterNumber;
	
	// Replacing commas "," with hypern "-"
	var re = new RegExp (',') ;
	var verseNumber = verseNumber.replace(re, '-') ;
	
	// Replacing any VERSEa or VERSEb things
	var re = new RegExp ('a') ;
	var verseNumber = verseNumber.replace(re, '') ;
	var re = new RegExp ('b') ;
	var verseNumber = verseNumber.replace(re, '') ;
	var re = new RegExp ('c') ;
	var verseNumber = verseNumber.replace(re, '') ;
	
	// Getting first verse number when form VERSE-VERSE is used, for bookmark number
	bookMarkVerseNumber = verseNumber.split('-')[0];
	
	// Use for debugging
	//alert("Book name: " + bookName + "\nChapter number: " + chapterNumber + "\nVerse(s) number: " + verseNumber + " (first verse: " + bookMarkVerseNumber + ")");

	scriptureWindow = window.open("http://www.watchtower.org/bible/" + bookName + "/chapter_" + chapterNumber + ".htm?bk=" + bookName + ";chp=" + chapterNumber + ";vs=" + verseNumber + ";citation#bk" + bookMarkVerseNumber, "scriptureWindow", "top=120,left=180," + "height=450,width=640,status=no,toolbar=no,menubar=no,location=no," + "resizable=yes,scrollbars=yes");
	
	scriptureWindow.focus();

}


function displayCrossChapterVerses(verses, showBook) {
	
	var originalverse = verses;
	
	verses = verses.split('-')[0];
	
	if(showBook == true)
	document.write("<a title=\"" + titleText1 + " " + scriptures[0] + " " + titleText2 + "\" href=\"javascript:LookUp('" + verses + "');\">" + verses + "</a>");
	else if(showBook == false)
	document.write("<a title=\"" + titleText1 + " " + scriptures[0] + " " + titleText2 + "\" href=\"javascript:LookUp('" + verses + "');\">" + verses.split(' ')[1] + "</a>");
						
	document.write("-");
						
	document.write("<a title=\"" + titleText1 + " " + scriptures[0] + " " + titleText2 + "\" href=\"javascript:LookUp('" + originalverse.split(' ')[0] + " " + originalverse.split('-')[1].split(":")[0] + ":1-" + originalverse.split('-')[1].split(":")[1] + "');\">" + originalverse.split('-')[1] + "</a>");
				

}

