﻿/**
 * @author Michael Haufe (michael [at] lanex.com)
 * @param {string} The string being tested for being a bible reference or not
 * @return JSON string || false
 */
 
function checkSearchType(str) {
    var chkdVal = refTest(str);

    document.getElementById('bkval').value ="";
    
    if (chkdVal == false) {
        // the string entered isn't a bible reference. 
        document.getElementById('isVerseRef').value = "false";
    } else {
        // the string could be interpreted as a bible reference, now check if the override
        // checkbox is checked.
        if (document.getElementById('chkBoxBooksAsKeys').checked) {
            document.getElementById('isVerseRef').value = "false";
        } else {
            document.getElementById('isVerseRef').value = "true";
            document.getElementById('bkval').value = chkdVal;
        }
        //alert(document.getElementById('isVerseRef').value);
    }
    
}

function refTest(ref){
   
	if(!String.prototype.trim){
		String.prototype.trim = function(){
			return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
		};
	}
	ref = ref.trim();
	if(ref === ""){
		return false;
	}

	function RegList(arr){
		var len = arr.length;
		function _exec(str){
			var i=0,item;
			for(i;i<len;i++){ //loop through every entry in the array
				item = arr[i]
				switch(typeof item[0]){
					case "string": 
						str = str.replace(new RegExp(item[0],"g"),item[1]);
					break;
					default: //assume its a regular expression
						str = str.replace(item[0],item[1])
					break;
				}
			}
			return str;
		}
		return {exec : _exec}
	}
	var bookMark = {};
    var bookFix = new RegList([
        [/(?:\bgenesis\b|\bgen\b|\bgn\b)/gi,"{1}"],[/(?:\bexodus\b|\bexod\b|\bexo\b|\bex\b)/gi,"{2}"],[/(?:\bleviticus\b|\blev\b)/gi,"{3}"],
		[/(?:\bnumbers\b|\bnum\b)/gi,"{4}"],[/(?:\bdeuteronomy\b|\bdeut\b)/gi,"{5}"],[/(?:\bjoshua\b|\bjosh\b|\bjos\b)/gi,"{6}"],
		[/(?:\bjudges\b|\bjudg\b|\bjdg\b|\bjd\b)/gi,"{7}"],[/(?:\bruth\b)/gi,"{8}"],[/(?:\b1\s*samuel\b|\b1\s*sam\b|\b1\s*sa\b)/gi,"{9}"],
		[/(?:\b2\s*samuel\b|\b2\s*sam\b|\b2\s*sa\b)/gi,"{10}"],[/(?:\bezra\b)/gi,"{15}"],[/(?:\bjob\b)/gi,"{18}"],
		[/(?:\brevelation\b|\brev\b|\bre\b)/gi,"{66}"],[/(?:\b1\s*kings\b|\b1\s*kgs\b|\b1\s*ki\b)/gi,"{11}"],
		[/(?:\b2\s*kings\b|\b2\s*kgs\b|\b2\s*ki\b)/gi,"{12}"],[/(?:\b1\s*chronicles\b|\b1\s*chr\b|\b1\s*chro\b|\b1\s*ch\b)/gi,"{13}"],
		[/(?:\b2\s*chronicles\b|\b2\s*chr\b|\b2\s*chro\b|\b1\s*ch\b)/gi,"{14}"],[/(?:\bnehemiah\b|\bneh\b)/gi,"{16}"],
		[/(?:\besther\b|\besth\b|\best\b)/gi,"{17}"],[/(?:\bpsalms\b|\bpsalm\b|\bps\b|\bpsa\b)/gi,"{19}"],[/(?:\bproverbs\b|\bpr\b|pro\b)/gi,"{20}"],
		[/(?:\becclesiastes\b|\beccl\b|\becc\b)/gi,"{21}"],[/(?:\bsong\s*of\s*songs\b|\bsong\b|\bsng\b|\bsos\b|\bsong\s*of*solomon\b)/gi,"{22}"],
        [/(?:\bisaiah\b|\bisa\b|\bis\b)/gi,"{23}"],[/(?:\bjeremiah\b|\bjer\b)/gi,"{24}"],[/(?:\blamentations\b|\blam\b)/gi,"{25}"],
		[/(?:\bezekiel\b|\bezek\b|\beze\b|\bezk\b)/gi,"{26}"],[/(?:\bdaniel\b|\bdan\b)/gi,"{27}"],[/(?:\bhosea\b|\bhos\b)/gi,"{28}"],
		[/(?:\bjoel\b)/gi,"{29}"],[/(?:\bamos\b)/gi,"{30}"],[/(?:\bobadiah\b|\bobad\b|\boba\b)/gi,"{31}"],
		[/(?:\bjonah\b|\bjon\b|\bjnh\b)/gi,"{32}"],[/(?:\bmicah\b|\bmic\b)/gi,"{33}"],[/(?:\bnahum\b|\bnah\b)/gi,"{34}"],
		[/(?:\bhabakkuk\b|\bhab\b|\bhbk\b)/gi,"{35}"],[/(?:\bzephaniah\b|\bzeph\b|\bzep\b)/gi,"{36}"],
		[/(?:\bhaggai\b|\bhagg\b|\bhag\b|\bhg\b)/gi,"{37}"],[/(?:\bzechariah\b|\bzech\b|\bzec\b)/gi,"{38}"],[/(?:\bmalachi\b|\bmal\b)/gi,"{39}"],
		[/(?:\bmatthew\b|\bmatt\b|\bmt\b)/gi,"{40}"],[/(?:\bmark\b|\bmk\b|\bmar\b)/gi,"{41}"],[/(?:\bluke\b|\blk\b|\bluk\b)/gi,"{42}"],
        [/(?:\bacts\b|\bact\b|\bac\b)/gi,"{44}"],[/(?:\bromans\b|\brom\b)/gi,"{45}"],[/(?:\b1\s*corinthians\b|\b1\s*cor\b|\b1\s*co\b)/gi,"{46}"],
		[/(?:\b2\s*corinthians\b|\b2\s*cor\b|\b2\s*co\b)/gi,"{47}"],[/(?:\bgalatians\b|\bgal\b)/gi,"{48}"],[/(?:\bephesians\b|\beph\b)/gi,"{49}"],
		[/(?:\bphilippians\b|\bphil\b|\bphp\b)/gi,"{50}"],[/(?:\bcolossians\b|\bcol\b)/gi,"{51}"],[/(?:\b1\s*thessalonians\b|\b1\s*th\b|\b1\s*thes\b)/gi,"{52}"],
		[/(?:\b2\s*thessalonians\b|\b2\s*th\b|\b2\s*thes\b)/gi,"{53}"],[/(?:\b1\s*timothy\b|\b1\s*tim\b|\b1\s*ti\b)/gi,"{54}"],
		[/(?:\b2\s*timothy\b|\b2\s*tim\b|\b2\s*ti\b)/gi,"{55}"],[/(?:\btitus\b|\btit\b)/gi,"{56}"],	[/(?:\bphilemon\b|\bphlm\b|\bphile\b|\bphm\b|\bphi\b)/gi,"{57}"],
		[/(?:\bhebrews\b|\bheb\b|\bhe\b)/gi,"{58}"],[/(?:\bjames\b|\bjas\b|\bjam\b|\bja\b)/gi,"{59}"],[/(?:\b1\s*peter\b|\b1\s*pet\b|\b1\s*pe\b)/gi,"{60}"],
        [/(?:\b2\s*peter\b|\b2\s*pet\b|\b2\s*pe\b)/gi,"{61}"],[/(?:\b1\s*john\b|\b1\s*jn\b)/gi,"{62}"],[/(?:\b2\s*john\b|\b2\s*jn\b)/gi,"{63}"],
		[/(?:\b3\s*john\b|\b3\s*jn\b)/gi,"{64}"],[/(?:\bjohn\b|\bjn\b)/gi,"{43}"],[/(?:\bjude\b)/gi,"{65}"]
	])
	//fix user input
	var BookList = [,//index 0 empty on purpose
	    "Genesis","Exodus","Leviticus","Numbers","Deuteronomy","Joshua","Judges","Ruth","1 Samuel","2 Samuel","1 Kings","2 Kings","1 Chronicles",
	    "2 Chronicles","Ezra","Nehemiah","Esther","Job","Psalms","Proverbs","Ecclesiastes","Song of Solomon","Isaiah","Jeremiah","Lamentations",
	    "Ezekiel","Daniel","Hosea","Joel","Amos","Obadiah","Jonah","Micah","Nahum","Habakkuk","Zephaniah","Haggai","Zechariah","Malachi","Matthew",
        "Mark","Luke","John","Acts","Romans","1 Corinthians","2 Corinthians","Galatians","Ephesians","Philippians","Colossians","1 Thessalonians",
        "2 Thessalonians","1 Timothy","2 Timothy","Titus","Philemon","Hebrews","James","1 Peter","2 Peter","1 John","2 John","3 John","Jude","Revelation"
    ]
		//Replace "Ruth 1 1" with "Ruth 1:1",remove spaces,Replace "Ruth 1.1" with "Ruth 1:1",put a booknumber after every comma, remove any book numbers side by side
		//{B}C1:V1-C2:V2 --> ["B.C1.V1","B.C2.V2"]
		//{B}C1:V1-V2 --> ["B.C1.V1","B.C1.V2"]
		//{B}C1-C2:V1 --> ["B.C1.V1","B.C2.V1"]
		//{B}C1:V1 --> ["B.C1.V1","B.C1.V1"]
		//{B}C1-C2 ---> ["B.C1.1","B.C2.End"]
		//{B}C1 --> ["B.C1.1","B.C1.End"]
		//{B} --> ["B.1.1","B.End.End"]
		var refFix = new RegList([
			[";",","],[/\}(\d+)\s*(\d+)/g,"}$1:$2"],[/\s*/g,""],[/(\d+)\.(\d+)/g,"$1:$2"],
			[/(\{\d+\})([^{]+)/g,function(match,bookNum,contents){return (bookNum + contents.replace(/\,/g,","+bookNum))}],[/\d+\}\{/,""],
			[/\{(\d+)\}(\d+)\:(\d+)\-(\d+)\:(\d+)/g,function(match,$1,$2,$3,$4,$5){
			    return "[\""+BookList[$1]+"."+$2+"."+$3+"\",\""+BookList[$1]+"."+$4+"."+$5+"\"]";
			}],
			[/\{(\d+)\}(\d+)\:(\d+)\-(\d+)/g,function(match,$1,$2,$3,$4){
			    return "[\""+BookList[$1]+"."+$2+"."+$3+"\",\""+BookList[$1]+"."+$2+"."+$4+"\"]";
			}],
			[/\{(\d+)\}(\d+)\-(\d+)\:(\d+)/g,function(match,$1,$2,$3,$4){
			    return "[\""+BookList[$1]+"."+$2+"."+$4+"\",\""+BookList[$1]+"."+$3+"."+$4+"\"]";
			}],
			[/\{(\d+)\}(\d+)\:(\d+)/g,function(match,$1,$2,$3){
			    return "[\""+BookList[$1]+"."+$2+"."+$3+"\",\""+BookList[$1]+"."+$2+"."+$3+"\"]";
			}],
			[/\{(\d+)\}(\d+)\-(\d+)/g,function(match,$1,$2,$3){
			    return "[\""+BookList[$1]+"."+$2+".1\",\""+BookList[$1]+"."+$3+".End\"]";
			}], 
			[/\{(\d+)\}(\d+)/g,function(match,$1,$2){
			    return "[\""+BookList[$1]+"."+$2+".1\",\""+BookList[$1]+"."+$2+".End\"]";
			}],
			[/\{(\d+)\}/g,function(match,$1){
			    return "[\""+BookList[$1]+".1.1\",\""+BookList[$1]+".End.End\"]";
			}]
		])
		ref = bookFix.exec(ref) //normalize booknames
		ref = refFix.exec(ref) //normalize the references
		ref = "["+ref+"]";

        if(ref.split(".").length < 3) //then the string did not match up to the regular expressions
            return false;
    
        try{
            ref = eval("("+ref+")")
            if(!ref[0]){
                return false;
            }
        }catch(e){
            return false;
        }
		ref = ref[0]; //only the first bible reference
		ref[0] = ref[0].split(".");//convert the substrings into array entries
		ref[1] = ref[1].split(".");
		//turn it into a bookmark string
		ref = "{"+
				"'StartingBookmark':{"+
					"'EditionID':"+1+","+ //assuming first edition for now
					"'BookShortName':'"+ref[0][0]+"',"+
					"'ChapterNbr':"+ref[0][1]+","+
					"'VerseNbr':"+ref[0][2]+
				"},"+
				"'EndingBookmark':{"+
					"'EditionID':"+1+","+//assuming first edition for now
					"'BookShortName':'"+ref[1][0]+"',"+
					"'ChapterNbr':"+ref[1][1]+","+
					"'VerseNbr':"+ref[1][2]+
				"}}"
		return ref;
}
