/*Script copyrighted 2002-2003, Andrew McCafferty, All rights reserved.*/
	var bArray=new Array('Mat','Mark','Luke','John','Acts','Rom','1st Cor','2nd Cor','Gal','Eph','Phil','Col','1st Thes','2nd Thes','1st Tim','2nd Tim','Titus','Phile','Heb','James','1st Pet','2nd Pet',"1 John","2nd John","3rd John","Jude","Rev");
	var book = 4;
	var chapter = 6;
	var verse = 23;
	var xmlVerseCount;
	var xmlGreek;
	var xmlConcordance;
	var greekFonts = "Palatino Linotype,Gentium,Galatia SIL";
	var zIndex = 1;

function showGreek(){	
    xmlVerseCount = createDom();
	var html = '';
//	html += '<form name="Form">';
    html += '<div id="verseDiv">';
	html +=  '<table width="100%"><tbody><tr>';
	html +=  '<td class ="small">';
	html +=  ' <a href="javascript:{selectBook()}"><span id="book"></span></a>&nbsp;';
	html += ' <a href="javascript:{selectChapter(book)}"><span id="chapter"></span></a>';
	html += ' <a href="javascript:{selectVerse(book,chapter)}"><span id="verse"></span></a>';
	html += '</td><td>';
	html += ' <font face="' + greekFonts + '" size="3"><span id="greekSpan">&nbsp;</span></font>';
	html += '</td></tr></tbody></table>';
	html += '</div>';		
	html += '<div id= "parseDiv"  class="parseDiv"></div>';
	html += '<div id= "messageDiv"  class="parseDiv"></div>';
	html += '<div id= "optionsDiv"  class="parseDiv">';
    html += closeDivHTML('optionsDiv');
    html += '<p class="small">Non-Greek version:</p>';
    html += '<select id="selectBible" language="javascript" name="ConcordanceLangauge">';
    html += '<option selected="selected" value="none">None</option>';    
    html += '<option value="asv">American Standard Version</option>';
    html += '<option value="cuv">Chinese Union Version</option>';
    html += '</select>';
    html += '<p class="small" align="center"><a href="javascript:closeDiv(\'optionsDiv\')">[O.K.]</a></p>';        
	html += '</div>';		
	html += '<div id= "concordanceDiv"  class="concordanceDiv"></div>';		
//    html += '</form>';
	var d = document.getElementById('greekDiv');
	d.innerHTML = html;
	// Write greek span
	var d = document.getElementById('greekSpan');
	if ( xmlVerseCount != undefined){
		if ( greekFont() ){	
		    xmlVerseCount.load('/Greek/greekVerses.xml');
		    selectBook();
			d.style.visibility='visible';
			d.style.display='block';
		}
		else {
			d.style.visibility='visible';
			d.style.display='block';				
			d.innerHTML = '<p class="small">You need a Greek Unicode font.  A free font: <a href="http://www.sil.org">Gentium font</a> from SLI.</p>';
		}
	}	
	else{
		d.innerHTML = '<p class="small">Unable to create XML.</p>';			
	}

}

function createDom(){	
    if (window.ActiveXObject){	
        var dom = new ActiveXObject("Microsoft.xmlDom");
		dom.async=false;
		return dom;
	}
	else if (document.implementation && document.implementation.createDocument){	
	    dom = document.implementation.createDocument("","",null);
		return dom;
	}
	else{	
	    return undefined;
	}	
}

function greekVersePost(b,c,v){	
    closeDiv('parseDiv');
	verse = v;
	document.getElementById('verse').innerHTML = v;
    var body = 'greekVerse=true&book=' + b + '&chapter=' + c + '&verse=' + v;
	var box = document.Form.selectBible
	body = body + '&nonGreekBible=' + box.options[box.selectedIndex].value
	var url = "/Greek/greekVersePost.aspx";
  	var httpRequest = postXML(url,body,rf) 
}
	
function concordancePost(type,ntt_id,page){
    var body = 'concordance=true&' + type + '=true&ntt_id=' + ntt_id + '&page=' + page;
	var box = document.Form.selectBible
	body = body + '&nonGreekBible=' + box.options[box.selectedIndex].value
	var url = "/Greek/greekVersePost.aspx";
	var httpRequest = postXML(url,body,rf) 
    }

function postXML(url,body,receivingFunction){
    if (window.XMLHttpRequest){
        httpRequest=new XMLHttpRequest()
  		httpRequest.onreadystatechange=receivingFunction
  		httpRequest.open("POST",url,true)
  		httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		httpRequest.send(body)
	}
	else if (window.ActiveXObject){
	    httpRequest=new ActiveXObject("Microsoft.XMLHTTP")
   	 	httpRequest.onreadystatechange=receivingFunction
    	httpRequest.open("POST",url,true)
  		httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		httpRequest.send(body)
	}
	return httpRequest
}

var rf = function receivingFunction(){	
    if (httpRequest.readyState==4){
 		if (httpRequest.status==200){
 		    closeAllDiv();
 		    var xmlTemp = createDom();
		  	if (window.ActiveXObject){
				xmlTemp.loadXML(httpRequest.responseText);
			}
			else if (window.XMLHttpRequest){    
			    xmlTemp = httpRequest.responseXML;
		    }
            var e = xmlTemp.getElementsByTagName('replyType');
            if(e[0].firstChild.nodeValue=='greekVerse'){
                xmlGreek = xmlTemp;
                var d = document.getElementById('concordanceDiv');
               
                if( d.innerHTML==''){
                    d.innerHTML=closeDivHTML('concordanceDiv') + '<p class="medium">Click on verse to change verses.<br>Click on Greek word to see parse.<br>Click on numbers in the parse box to see concordance.<br>Use "Options" to set English/Chinese version.</p>';
                	d.style.left = '10';
	                d.style.visibility='visible';
	                d.style.display='block';
                };
                //Add greek verse 
    			var nodeList = xmlGreek.getElementsByTagName('word');
	    		var greekHTML = '';
				for(i=0; i<nodeList.length; i++){
					greekHTML +='<span class="hand" onClick="{showParse('  + i + ')}">';
			        e = nodeList[i].getElementsByTagName('text');
			        greekHTML +=e[0].firstChild.nodeValue + ' ' + '</span>';						
			    } 
			    //Add non-greek verse
			    nodeList=xmlGreek.getElementsByTagName('nonGreekVerse');
			    if (nodeList[0].firstChild != undefined){
                    greekHTML +='&nbsp;'+nodeList[0].firstChild.nodeValue
                }
                //Add options link                
                greekHTML +='<span class="small">&nbsp;(<a href=\"javascript:showOptions()\">Options</a>)</span>';
			    document.getElementById('greekSpan').innerHTML = greekHTML;
			    //Update the reference spans
	            e = xmlGreek.getElementsByTagName('book');
	            book = e[0].firstChild.nodeValue; 
                document.getElementById('book').innerHTML = bArray[book-1];
			    e = xmlGreek.getElementsByTagName('chapter');
			    chapter = e[0].firstChild.nodeValue;
                document.getElementById('chapter').innerHTML = chapter+ ':';                    
			    e = xmlGreek.getElementsByTagName('verse');
			    verse = e[0].firstChild.nodeValue; 
    		    document.getElementById('verse').innerHTML = verse;
            }
            else{
                // Write concordance
                xmlConcordance = xmlTemp;
                var nodeList = xmlConcordance.getElementsByTagName('ntt_id');
                var ntt_id = nodeList[0].firstChild.nodeValue;
                nodeList = xmlConcordance.getElementsByTagName('concordanceType');
                var conType = nodeList[0].firstChild.nodeValue;
                var conHTML = closeDivHTML('concordanceDiv');
	    		conHTML +='<table>';
                //before
                conHTML +=getPageListRow('beforePage',conType,ntt_id);
                //verses
    			nodeList = xmlConcordance.getElementsByTagName('bibleVerse');
	    		var sg = '';
				for(i=0; i<nodeList.length; i++){
					conHTML += '<tr>';
			        e = nodeList[i].getElementsByTagName('book');
			        b = e[0].firstChild.nodeValue;
			        e = nodeList[i].getElementsByTagName('chapter');
                    c = e[0].firstChild.nodeValue;
			        e = nodeList[i].getElementsByTagName('verse');
    		        v = e[0].firstChild.nodeValue;
			        conHTML += '<td class = "small">' + bArray[b-1] + ' ' + c + ':' + v + '</td>';
			        e = nodeList[i].getElementsByTagName('text');
			        conHTML += '<td class = "small"><font face="' + greekFonts + '">' + e[0].firstChild.nodeValue + '</font></td>';
			        e = nodeList[i].getElementsByTagName('parse');
			        conHTML += '<td class = "small">' + e[0].firstChild.nodeValue + '</td>';
			        e = nodeList[i].getElementsByTagName('nonGreekVerse');
			        sg = '&nbsp;<a href="javascript:greekVersePost(' + b + ',' + c + ',' + v + ')">Greek</a>' 			        
                    conHTML += '<td class = "medium">' + e[0].firstChild.nodeValue + sg + '</td></tr>';
			    }
			    //after 
			    conHTML += getPageListRow('afterPage',conType,ntt_id);
	    		conHTML += '</table>';
                var d = document.getElementById('concordanceDiv');
			    d.innerHTML = conHTML;                    
            	d.style.left = '10';
	            d.style.visibility='visible';
	            d.style.display='block';
            	zIndex = zIndex + 1;
	            d.style.zIndex = zIndex;	
            }				    
        }
	    else{   
		    showMessage('Ajax Error ' + httpRequest.status);
        }	
    }
	else{   
		showMessage('Waiting for reply(' + httpRequest.readyState + ')');
	}	
}

function getPageListRow(beforeAfter,conType,ntt_id){
	var nodeList = xmlConcordance.getElementsByTagName(beforeAfter);
	var conHTML = '';
    var e, page,b,c,v;
	for(i=0; i<nodeList.length; i++){
        e = nodeList[i].getElementsByTagName("page");
        page = e[0].firstChild.nodeValue
		conHTML += '<tr><td colSpan=4 class = "small">';
        conHTML += '<a href=\"javascript:concordancePost(\''+ conType + '\',' + ntt_id + ','+ page +')\">';
        e = nodeList[i].getElementsByTagName('firstBook');
        b = e[0].firstChild.nodeValue;
        e = nodeList[i].getElementsByTagName('firstChapter');
        c = e[0].firstChild.nodeValue;
        e = nodeList[i].getElementsByTagName('firstVerse');
        v = e[0].firstChild.nodeValue;
        conHTML += bArray[b-1] + ' ' + c + ':' + v + ' - ';
        e = nodeList[i].getElementsByTagName('lastBook');
        b = e[0].firstChild.nodeValue;
        e = nodeList[i].getElementsByTagName('lastChapter');
        c = e[0].firstChild.nodeValue;
        e = nodeList[i].getElementsByTagName('lastVerse');
        v = e[0].firstChild.nodeValue;
        conHTML += bArray[b-1] + ' ' + c + ':' + v + '</a></td></tr>';                        			
	}
	return conHTML;
}

function selectBook(){	
    closeAllDiv();
    var d = document.getElementById('parseDiv');
	var html = closeDivHTML('parseDiv') + '<p class="small">Book:</p>';
	html = html + '<table><tr>'
	for(i=1;i<=27;i++){	
	    html += '<td><p class = "small"><a href="javascript:selectChapter(';
		html += i + ')">' ;
		html += bArray[i-1] + '</a></p></td>';
		if(i%4 == 0){
			html += '</tr><tr>'
	    }
	}
	html += '</tr></table>'
	d.innerHTML = html;
	d.style.left = '20';
	d.style.visibility='visible';
	d.style.display='block';
	zIndex = zIndex + 1;
	d.style.zIndex = zIndex;	
}

function selectChapter(b){	
    closeAllDiv();
    book = b;
	document.getElementById('book').innerHTML = bArray[b-1];
	var d = document.getElementById('parseDiv');
	var html = closeDivHTML('parseDiv') + '<p class="small">Chapter:</p>';
	var count;
	//Get number of chapters
	for(i=0; i < xmlVerseCount.documentElement.childNodes.length; i++){	
	    if( xmlVerseCount.documentElement.childNodes[i].attributes[0].value == b + ''){	
	        count = xmlVerseCount.documentElement.childNodes[i].childNodes.length;
			i = xmlVerseCount.documentElement.childNodes.length;
		}
	}
	html = html + '<table><tr>'
	for(i=1;i<=count;i++){	
	    html += '<td><p align="center" class = "small"><a href="javascript:selectVerse('
		html += b + ','
		html += i + ')">' 
		html += i + '</a></p></td>'
		if(i%5 == 0 && i<count){
			html += '</tr><tr>'
		}
	}
	html += '</tr></table>'
	d.innerHTML = html;
	d.style.left = '40';
	d.style.visibility='visible';
	d.style.display='block';
    zIndex = zIndex + 1;
	d.style.zIndex = zIndex;	
}

function selectVerse(b,c){	
    closeAllDiv();
    chapter = c;
	document.getElementById('chapter').innerHTML = c + ':';
	var d = document.getElementById('parseDiv');
	var html = closeDivHTML('parseDiv') + '<p class="small">Verse:</p>';
	var count = 2;
	//Get number of chapters
	for(i=0; i < xmlVerseCount.documentElement.childNodes.length; i++){	
	    if(xmlVerseCount.documentElement.childNodes[i].attributes[0].value == b + ''){	
	        for(j=0; j < xmlVerseCount.documentElement.childNodes[i].childNodes.length; j++){	
	            if(xmlVerseCount.documentElement.childNodes[i].childNodes[j].attributes[0].value == c + ''){	
	                count = xmlVerseCount.documentElement.childNodes[i].childNodes[j].childNodes[0].nodeValue;
				}
			}
		}
	}
	html = html + '<table><tr>'
	for(i=1;i<=count;i++){	
	    html += '<td><p align="center" class = "small"><a href="javascript:greekVersePost('
		html += b + ','
		html += c + ','
		html += i + ')">' 
		html += i + '</a></p></td>'
		if(i%5 == 0 && i<count){
			html += '</tr><tr>'
		}
	}
	html += '</tr></table>'
	d.innerHTML = html;
	d.style.left = '60';
	d.style.visibility='visible';
	d.style.display='block';
    zIndex = zIndex + 1;
	d.style.zIndex = zIndex;	
}	

function closeDivHTML(id){	
    var x = '<p width="110" class="small" style="{text-align:right}" ><img src="/Greek/plus.gif" onClick="closeDiv(\''+id+'\')"></p>';
	return x	
}
	
function closeAllDiv(){	
    closeDiv('parseDiv');
    closeDiv('messageDiv');
    closeDiv('optionsDiv');    
}

function closeDiv(id){	
    var d = document.getElementById(id);
	d.style.visibility='hidden';
	d.style.display='none';
}

function greekFont(){	
    var d = document.getElementById('parseDiv');
	d.style.display='block';
	d.style.visibility='hidden';
	var greekHTML = '<font face="' + greekFonts + '" size="3">';
	for(i=1;i<=10;i++){ 
	    greekHTML += String.fromCharCode(8055); 	
	}
	greekHTML += '</font>';
	d.innerHTML = greekHTML;
	if(d.offsetWidth < 110){	
	    d.innerHTML = '';
		return true;
	}
	else{	
	    d.innerHTML = '';
		return false;
	}
}

function showMessage(msg){	
    closeAllDiv();
    var html = closeDivHTML('messageDiv');   
	var d = document.getElementById('messageDiv');
	html += '<p class="small">' + msg + '</p>';
	d.innerHTML = html;
	d.style.left = '10';
	d.style.top = document.getElementById('verseDiv').offsetHeight;
	d.style.visibility='visible';
	d.style.display='block';
    zIndex = zIndex + 1;
	d.style.zIndex = zIndex;	
}

function showOptions(){	
    closeAllDiv();
    var html = closeDivHTML('optionsDiv');   
	var d = document.getElementById('optionsDiv');
	d.style.left = '10';
	d.style.top = document.getElementById('verseDiv').offsetHeight;
	d.style.visibility='visible';
	d.style.display='block';
    zIndex = zIndex + 1;
	d.style.zIndex = zIndex;	
}


function showParse(i){
    closeAllDiv();
    var html = closeDivHTML('parseDiv'); 
	var nodeList = xmlGreek.getElementsByTagName('word');
	var e; 
	e = nodeList[i].getElementsByTagName('lemma');
	html += '<p class="small"><font face="' + greekFonts + '" size="3">' + e[0].firstChild.nodeValue +  '</font> ';
    var lc,tc,pc,ntt;
	lc = nodeList[i].getElementsByTagName('lemmaCount');
	pc = nodeList[i].getElementsByTagName('parseCount');		
	ntt = nodeList[i].getElementsByTagName('ntt_id');
	html += '(<a href=\"javascript:concordancePost(\'lemma\',' + ntt[0].firstChild.nodeValue + ',0)\">' + lc[0].firstChild.nodeValue + '</a>,';
	html += '<a href=\"javascript:concordancePost(\'parse\',' + ntt[0].firstChild.nodeValue + ',0)\">' + pc[0].firstChild.nodeValue + '</a>)</p>';
	e = nodeList[i].getElementsByTagName('parse');
	html += '<p class="small">' + e[0].firstChild.nodeValue +  '</p>';
	e = nodeList[i].getElementsByTagName('english');
	html += '<p class="small">' + e[0].firstChild.nodeValue +  '</p>';
	var d = document.getElementById('parseDiv');
	d.innerHTML = html;
	d.style.left = '10';
	d.style.top = document.getElementById('verseDiv').offsetHeight;
	d.style.visibility='visible';
	d.style.display='block';
    zIndex = zIndex + 1;
	d.style.zIndex = zIndex;	
}

