var originalText = "";
var indexWin='';
var browseWin='';
var mainWin='';

function synch(val){
      document.location.hash=val;
}
var loop,looph,loopv,loopt=0;
var imgWin=String;
var servletPath="http://digitaldonne.tamu.edu/images/";
var rootPath="http://digitaldonne.tamu.edu/";
function viewImage(page)
{
       loop=loop+1;
       if (loop>1){
          imgWin.close();
       }
       imgWin = window.open('', 'imgWin','resizable=yes, scrollbars=yes, height=750, width=600, top=50, left=450');          
       imgWin.document.writeln('<html><head>');
       imgWin.document.writeln('<meta content=text/html;charset=utf-8/>');      
       imgWin.document.writeln('<title>Image Viewer</title>');
       imgWin.document.writeln('</head><body>');
       imgWin.document.writeln('<center>');
       imgWin.document.writeln('<table border="0" cellspacing="5" cellborder="10">');
       imgWin.document.writeln('<tr><td><img src="'+servletPath+page+'"></td></tr>');
       imgWin.document.writeln('</table></center></body></html>');       
       imgWin.document.close();              
       imgWin.focus();       
}
function loadNext(mid,pageval,poem,copy,myterm,groupby,orderby)
{
   document.form1.currmid.value=mid;
   document.form1.poem.value=poem;
   document.form1.image.value=poem;   
   document.form1.copyname.value=copy;
   document.form1.pageno.value=pageval;
   document.form1.toline.value=poem;
   document.form1.term.value=myterm;
   document.form1.group.value=groupby;   
   document.form1.orderby.value=orderby;      
   document.form1.submit();
}
function openVariant(variant,e)
{
   var posx = 0;
   var posy = 0;
   var nvariant;
   var nnvariant;
   nvariant = new String(variant);
   if (!e) var e = window.event;
   if (e.pageX || e.pageY){
      posx=e.screenX+10;
      posy=e.screenY+10;
   }else if (e.clientX || e.clientY){
      posx=e.screenX+10;
      posy=e.screenY+10;      
   }
   loopv=loopv+1;
   if (loopv>1){
      newWin.close();
   }   
   newWin = window.open('','newWin','scrollbars=no,menubar=no,height=25,width=325,left='+posx+',top='+posy+',resizable=yes,toolbar=no,location=no,status=no'); 
       newWin.document.writeln('<html><head>');
       newWin.document.writeln('<meta content=text/html;charset=utf-8></meta>');      
       newWin.document.writeln('<title>Variant Viewer</title>');
       newWin.document.writeln('</head><body>');
       newWin.document.writeln('<center>');
       nvariant = variant.replace(/\}/g,"");
       nnvariant = new String("");
       nnvariant = nvariant.replace(/\{/g,"");       
       newWin.document.writeln('<font face="Verdana"><table border="0" cellspacing="5" cellborder="10" bgcolor="#99CCCC">');
       newWin.document.writeln('<tr><td align="right"></td><td>'+nnvariant+'</td></tr>');
       newWin.document.writeln('</table></font></center></body></html>');          
   newWin.document.close();  
   newWin.focus();   
}
function gotoPage(topage,siglum,groupby,orderby){
   valtopage=parseInt(topage);
   document.form1.pageno.value=valtopage;
   document.form1.copyname.value=siglum;   
   document.form1.group.value=groupby;   
   document.form1.orderby.value=orderby;
   document.form1.submit();
}

function openFirstIndex(whatoption,whatpage,whatsiglum,whatgroup){
    indexWin = window.open(rootPath+'ShowIndex?siglum='+whatsiglum+'&page='+whatpage+'&orderby='+whatoption+'&group='+whatgroup,'indexWin');    
    indexWin.document.close();      
    indexWin.focus();    
}

function gotoIndex(whatoption,whatpage,whatsiglum,whatgroup){
  if (window.indexWin.open && !window.indexWin.closed){
  //if (window.opener && !window.opener.closed){  
    indexWin.focus();    
  }else{
    indexWin = window.open(rootPath+'ShowIndex?siglum='+whatsiglum+'&page='+whatpage+'&orderby='+whatoption+'&group='+whatgroup,'indexWin');    
    indexWin.document.close();          
    indexWin.focus();    
  }
}


function gotoMain(){
	if (!mainWin.closed && mainWin.location) {
		mainWin.location.href = 'index.html';
	}
	else {
		mainWin=window.open('index.html','mainWin');
		if (!mainWin.opener) mainWin.opener = self;
	}
	if (window.focus) {mainWin.focus()}
	return false;
}

function openPage(pid,pline,pdisplay,ppageno,pterm,pcopyname,ppoem,pgroup){
  if (window.indexWin && window.indexWin.open && !window.indexWin.closed){
    indexWin.focus();    
  }else{
    indexWin = window.open(rootPath+'DisplayText?id='+pid+'toline='+pline+'&display='+pdisplay+'&pageno='+ppageno+'&term='+pterm+'&copyname='+pcopyname+'&poem='+ppoem+'&group='+pgroup,'indexWin');    
    indexWin.document.close();          
    indexWin.focus();    
  }
}

function gotoConcordanceIndex(){
   history.go(-1);
}

function gotoHome(){
   //document.write("index.html");
   //document.close();
   //window.location.target="_top";      
   window.open('index.html','_top');   
}

function gotoPoemIndex(whatURL){
   //document.write("index.html");
   //document.close();
   //window.location.target="_top";      
   window.open(whatURL,'_top');   
}


function help(){
   looph=looph+1;
   if (looph>1){
      helpWin.close();
   }   
   helpWin = window.open('help.html','helpWin','scrollbars=yes,menubar=no,height=650,width=625,left=50,top=50,resizable=yes,toolbar=no,location=no,status=no'); 
   helpWin.document.close();  
   helpWin.focus();   
}

function openDonne(tziviFile){
   var loc = "http://csdl.tamu.edu:8080/tzivi/displayPage.html";
   loopt=loopt+1;
   if (loopt>1){
      tileWin.close();
   }   		  
   tileWin = window.open(loc + '?image='+tziviFile,'tileWin','scrollbars=yes,menubar=no,height=830,width=540,left=50,top=50,resizable=yes,toolbar=no,location=no,status=no');
   tileWin.document.close();
   tileWin.focus();
}

//function openTilepic(tileLocation){
//   loopt=loopt+1;
//   if (loopt>1){
//      tileWin.close();
//   }   
//   tileWin = window.open(tileLocation,'tileWin','scrollbars=yes,menubar=no,height=800,width=800,left=50,top=50,resizable=yes,toolbar=no,location=no,status=no'); 
//   tileWin.document.close();  
//   tileWin.focus();   
//}

function openTechnical(){
   loopt=loopt+1;
   if (loopt>1){
      techWin.close();
   }   
   techWin = window.open('technical.html','techWin','scrollbars=yes,menubar=no,height=650,width=850,left=50,top=50,resizable=yes,toolbar=no,location=no,status=no'); 
   techWin.document.close();  
   techWin.focus();   
}

function help_2(){
   loop=loop+1;
   if (loop>1){
      helpWin.close();
   }   
   helpWin = window.open('','helpWin','scrollbars=no,menubar=no,height=25,width=325,left=50,top=50,resizable=yes,toolbar=no,location=no,status=no'); 
       helpWin.document.writeln('<html><head>');
       helpWin.document.writeln('<meta content=text/html;charset=utf-8></meta>');      
       helpWin.document.writeln('<title>Help</title>');
       helpWin.document.writeln('</head><body>');
       helpWin.document.writeln('<center>');
       helpWin.document.writeln('<font face="Verdana"><table border="0" cellspacing="5" cellborder="10" bgcolor="#99CCCC">');
       helpWin.document.writeln('<tr><td align="right"></td><td>'+'<i>corr</i>corrected</td></tr>');
       helpWin.document.writeln('</table></font></center></body></html>');          
   helpWin.document.close();  
   helpWin.focus();   
}

function reSort(option){
   document.form1.orderby.value=option;
   document.form1.submit();
}

function selectPoem(mid,pbeg,pend,sig,navpage){
   for (i=document.form1.theorder.length-1; i > -1; i--) {
      if (document.form1.theorder[i].checked) {
      document.form1.orderby.value=document.form1.theorder[i].value;
      }
   }
   var elems = document.getElementsByTagName("td");
   var elemsLen = elems.length;
   for (i = 0; i < elemsLen; i++) {
      elems[i].className="whiteTD";;
   }
   document.getElementById(mid).className="yellowTD";   
   document.form1.page.value=navpage;      
   document.form1.poem.value=mid;
   document.form1.siglum.value=sig;         
   document.form1.pagebegin.value=pbeg;   
   document.form1.poempage.value=1;      
   document.form1.pageend.value=pend;   
   document.form1.textcontent.value="";         
   document.form1.action='./SelectPoem'; 
   document.form1.submit();
}

function selectPage(mid,pbeg,pend,sig,whatpage,navpage){
   for (i=document.form1.theorder.length-1; i > -1; i--) {
      if (document.form1.theorder[i].checked) {
      document.form1.orderby.value=document.form1.theorder[i].value;
      }
   }
   document.form1.page.value=navpage;   
   document.form1.poem.value=mid;
   document.form1.siglum.value=sig;         
   document.form1.pagebegin.value=pbeg;   
   document.form1.poempage.value=whatpage;      
   document.form1.pageend.value=pend;   
   document.form1.textcontent.value="";         
   document.form1.action='./SelectPoem'; 
   document.form1.submit();
}

function selectOrder(mid,pbeg,pend,sig,whatOrder,whatpage){
   if (whatOrder='code') document.form1.orderby.value='code';
   else document.form1.orderby.value='orderby';
   document.form1.poem.value=mid;
   document.form1.siglum.value=sig;         
   document.form1.pagebegin.value=pbeg;   
   document.form1.textcontent.value="";         
   document.form1.poempage.value=whatpage;      
   document.form1.pageend.value=pend;  
   document.form1.action='./SelectPoem'; 
   document.form1.submit();
}

function updateText(mid,sig,whatPage,whatFile){
   confirm('Are you sure to update transcription '+whatFile+'\n with:\n'+document.form1.textvalue.value)
   document.form1.poem.value=mid;
   document.form1.poempage.value=whatPage;         
   document.form1.textstring.value=whatFile;   
   document.form1.siglum.value=sig;            
   document.form1.textcontent.value=document.form1.textvalue.value; 
   document.form1.oldtext.value=originalText;   
   document.form1.action='./UpdateText'; 
   document.form1.submit();
}

function getOriginalText(){
   originalText = document.form1.textvalue.value;
}

yellowCellId=0;
function changeColor(whatCase,whatCell) {
   if (whatCase==2){
       document.getElementById(yellowCellId).className="whiteTD";   
       yellowCellId=whatCell;
       document.getElementById(whatCell).className="yellowTD";
       return;
   }
   if (whatCase==1){
       if (document.getElementById(whatCell).className!="yellowTD") document.getElementById(whatCell).className="blueTD";
   }else{
      if (document.getElementById(whatCell).className!="yellowTD") document.getElementById(whatCell).className="whiteTD"
   }   
}

function changeColorList(whatCase,whatCell,myvalue) {
   if (whatCase==1){
       if (document.getElementById(whatCell).className!="yellowTD") document.getElementById(whatCell).className="blueTD";
       if (document.getElementById((whatCell+myvalue)).className!="yellowTD") document.getElementById((whatCell+myvalue)).className="blueTD";       
   }else{
      if (document.getElementById(whatCell).className!="yellowTD") document.getElementById(whatCell).className="whiteTD";
      if (document.getElementById((whatCell+myvalue)).className!="yellowTD") document.getElementById((whatCell+myvalue)).className="whiteTD";      
   }   
}

function mysubmit(siglumVal,lett,ind){
   if (document.form1.stopval[0].checked) document.form1.stop.value='yes'
   else document.form1.stop.value='no';
   document.form1.letter.value=lett;
   document.form1.termind.value=ind;
   document.form1.cond.value='next';
   document.form1.siglum.value=siglumVal;   
   document.form1.termid.value='0';
   document.form1.target='indexFrame';
   document.form1.action='./GenIndex';
   document.form1.submit();
   document.form1.target='mainFrame';
   document.form1.action='./SearchTerm';
   document.form1.submit();
}

function concordanceFirstPage(siglumVal,lett){
   document.form1.siglum.value=siglumVal;   
   document.form1.letter.value=lett;
   document.form1.target='mainFrame';
   document.form1.action='./SearchTerm'
   document.form1.submit();
}

function tosearch(mytotal,lett,term,option,page,id,ind){
   if(mytotal>0){
      document.form1.letter.value=lett
      document.form1.term.value=term
      document.form1.termid.value=id
      document.form1.termind.value=ind
      document.form1.target='mainFrame';
      document.form1.action='./SearchTerm'
      document.form1.submit();
   }
}

function showSW(){
   document.getElementById("stop-words").className="stop-words-on";
}

function hideSW(){
   document.getElementById("stop-words").className="stop-words";
}

function selectCopy(source){
   newPoem = source.options[source.selectedIndex].value;
   window.parent.location.href = newPoem+'-biblio.html';
}
