/*
######################################################################
#                                                                    #
#   Sortable Tables                                                  #
#   ---------------                                                  #
#                                                                    #
#   This file will automatically generate a sortable/searchable      #
#   table from the variables listed in the main HTML file that       #
#   calls this file.                                                 #
#                                                                    #
#   P.Whitrow                                                        #
#   03/05/2002                                                       #
#                                                                    #
######################################################################
*/

function addStyleSheets(name,style){
 if(!document.styleSheets.length){document.createStyleSheet()}
 document.styleSheets[document.styleSheets.length-1].addRule(name,style);
}

// style sheet definitions
addStyleSheets('.STarrow', 'font-family:webdings;font-size:10;text-decoration:none;cursor:pointer;')
addStyleSheets('.STtab',   'font-family:verdana;font-size:11;')
addStyleSheets('.STbutt',  'font-family:verdana;font-size:11;border:1 solid;')
addStyleSheets('.STip',    'font-family:verdana;font-size:11;border:1 solid;')
addStyleSheets('.STtxt',   'font-family:verdana;font-size:10;')

// column assosiations (default 26 columns, but can add more)
function  dataArray(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) {
 this.col1=a;this.col2=b;this.col3=c;this.col4=d;this.col5=e;this.col6=f;this.col7=g;this.col8=h;this.col9=i;this.col10=j;this.col11=k;this.col12=l;this.col13=m;this.col14=n;this.col15=o;this.col16=p;this.col17=q;this.col18=r;this.col19=s;this.col20=t;this.col21=u;this.col22=v;this.col23=w;this.col24=x;this.col25=y;this.col26=z
}

var ignoreRowsQty=0

function createTable(tableName,tableHolderName){
 thn=document.getElementById(tableHolderName)
 str='<br><table width="'+tableWidth+'" class="STtab" border=0 id="'+tableName+'" style="background:'+tableBgColor+';" onclick="hilightRow();blurMe()"><tr><td type="header" colspan="'+tableHeadings.length+'" align="center" style="background:'+headerBgColor+';color:'+headerTxtColor+';">'+tableTitle+'</td></tr><tr><td type="header" colspan="'+tableHeadings.length+'" align=left><input type="STtxt" id="findText" class="STip">&nbsp;<button class="STbutt" onclick="this.blur();findData()">Find</button>&nbsp;<button class="STbutt" onclick="this.blur();findData()">Clear</button><div id="findResult" class="STtxt">(Search Results)</div></td></tr><tr><td type="header" colspan="'+tableHeadings.length+'" align=left>'+extraTableData+'</td></tr><tr><td style="background:'+headerBgColor+';" colspan="'+tableHeadings.length+'">&nbsp;</td></tr><tr>'
 for(a=0;a<tableHeadings.length;a++){
  if(a==0){
   if(rowNumbersEnabled){
    str+='<td type="header"></td>'
   }
  }else{
   str+='<td type="header"><b>'+tableHeadings[a]+'</b>&nbsp;<font onclick="sortBy(\'col'+a+'\',\''+tableName+'\',this)" class="STarrow" style="color:'+headerTxtColor+';" title="Ascending Order">5</font></td>'
  }
 }
 str+='</tr></table>'
 thn.innerHTML=str
 tmp=document.getElementsByTagName('TR')
 for(a=0;a<tmp.length;a++){
  if(tmp[a].parentElement.parentElement.id==tableName){
   ignoreRowsQty+=1
  }
 }
 makeTable(tableName)
}


function makeTable(tableName){
 tb=document.getElementById(tableName)
 ft=document.getElementById('findText')
 fr=document.getElementById('findResult')
 for (a=0;a<tableData.length;a++){
  tb.insertRow() 
  r=tb.rows[tb.rows.length-1];r.type="tableDataRow";r.style.background=dataBgColor;
  if(rowNumbersEnabled){
   c0 = r.insertCell();c0.innerText=a+1;c0.type='header'
  }
  for(b=1;b<tableHeadings.length;b++){
   tmp='tableData['+a+'].col'+b
   tmp2=eval(tmp)
   switch (true){
	// web address
	case (tmp2.indexOf('http://')>=0||tmp2.indexOf('https://')>=0):
		  com='c'+b+'=r.insertCell();c'+b+'.innerHTML="<a href=\'#\' value=\''+tmp2+'\' style=\'color:'+linkTxtColor+';\' onmouseover=this.style.color=\''+linkHoverColor+'\' onmouseout=this.style.color=\''+linkTxtColor+'\' onclick=\'gotoAddress(this.value)\' type=\'linker\' title=\'Click here open a window for this location.\'>'+tmp2+'</a>"';
	      break;
    // email
	case (tmp2.indexOf('@')>=0&&tmp2.indexOf('.')>=0):
		  com='c'+b+'=r.insertCell();c'+b+'.innerHTML="<a style=\'color:'+linkTxtColor+';\' onmouseover=this.style.color=\''+linkHoverColor+'\' onmouseout=this.style.color=\''+linkTxtColor+'\' href=\'mailto:'+tmp2+'\' type=\'linker\' title=\'Click here to send email.\'>'+tmp2+'</a>"';
	      break;
	// standard text
    default:
		  com='c'+b+'=r.insertCell();c'+b+'.innerHTML="'+tmp2+'";';
	      break;
   }
   eval(com)
  }
 }
 i=document.getElementsByTagName('TD')
 for(a=0;a<i.length;a++){
  if(i[a].type=='header'){
   i[a].style.background=headerBgColor
   i[a].style.color=headerTxtColor
   i[a].style.cursor='default'
  }
 }
 fr.innerText='(Search Results)'
}

function clearTable(tableName){
 tb=document.getElementById(tableName)
 i=tb.rows.length 
 for(a=ignoreRowsQty;a<i;a++){
  tb.deleteRow()
 }
}

var retVal=0

function sortBy(prop,tableName,dir) {
 retVal=0
 sortProp=prop;
 if(dir.innerText=='5'){
  dir.innerText='6'
  dir.title="Descending Order"
  tableData=tableData.sort(sortFuncUp)
 }else{
  dir.innerText='5'
  dir.title="Ascending Order"
  tableData=tableData.sort(sortFuncDn)
 }
 clearTable(tableName)
 makeTable(tableName)
}

function sortFuncUp(dataArray1,dataArray2) {
 if (dataArray1[sortProp]>dataArray2[sortProp]) retVal=1;
 else if (dataArray1[sortProp]<dataArray2[sortProp]) retVal=-1;
 else retVal=1;
 return retVal;
}

function sortFuncDn(dataArray1,dataArray2) {
 if (dataArray1[sortProp] < dataArray2[sortProp]) return (retVal ? -1: 1);  
 else if (dataArray1[sortProp] > dataArray2[sortProp]) return (retVal ? 1: -1);		
 else return 0;    
}

var clearingTable=false

function findData(){ 
 var seperator='~'
 ft=document.getElementById('findText')
 fr=document.getElementById('findResult')
 if(event.srcElement.innerText=='Clear'){
  clearingTable=true
  ft.value=clearingTable
 }
 if(ft.value!=''){
  var dataQty=0
  i=document.getElementsByTagName('TR')
  for(a=ignoreRowsQty;a<i.length;a++){
    if(i[a].type!='header'){
	 var startNum
	 if(rowNumbersEnabled=='yes'){
	  startNum=1
	 }else{
	  startNum=0
	 }
	 tmp=''
	 for(x=startNum;x<i[a].childNodes.length;x++){
	  tmp+=i[a].childNodes[x].innerText+seperator
	 }
     if(tmp.indexOf(ft.value)>=0){
      i(a).style.background=hilightBgColor
      //i(a).scrollIntoView(true); 
	  dataQty++
     }else{
      i(a).style.background=dataBgColor
     }
    }
  }
 fr.innerText=dataQty+" occurences of '"+ft.value+"' were found."
  if(clearingTable){
   ft.value=''
   fr.innerText='(Search Results)'
   clearingTable=false
  }
 }
}

var lastColor

function hilightRow(){
 if(event.srcElement.parentElement.tagName=='TR'&&event.srcElement.parentElement.type=='tableDataRow'){
  bgc=event.srcElement.parentElement
  if(bgc.style.background==hilightRowColor){
   bgc.style.background=lastColor
  }else{
   lastColor=bgc.style.background
   bgc.style.background=hilightRowColor
  }
 }
}

function blurMe(){
 if(event.srcElement.tagName=='A'){
   event.srcElement.blur()
 }
}

function gotoAddress(val){
 blurMe()
 window.open(val,'','')
}
