/*
######################################################################
#                                                                    #
#   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
document.write(
'<style type="text/css">'+
'.STarrow{font-family:webdings;font-size:10;text-decoration:none;}'+
'.STtab{font-family:verdana;font-size:11;}'+
'.STbutt{font-family:verdana;font-size:11;border:1 solid;cursor:hand;}'+
'.STip{font-family:verdana;font-size:11;border:1 solid;}'+
'.STtxt{font-family:verdana;font-size:10;color:#000000;cursor:default;}'+
'.STfil{font-family:verdana;font-size:10;color:#000000;cursor:pointer;}'+
'<\/style>'
)

// column assosiations
function  dataArray() {
 for(a=0;a<arguments.length;a++){
  str='this.col'+(a+1)+'="'+arguments[a].replace(/\"/g,"")+'"'
  eval(str)
 }
}

var ignoreRowsQty=0
var bgCols=new Array()
var clearingTable=false

function createTable(tableName,tableHolderName,s){
 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;<input type="checkbox" id="matchCase"><font onclick="matchCaseCheck()" style="cursor:pointer;">Match Case<\/font>&nbsp;&nbsp;&nbsp;<button class="STbutt" onclick="this.blur();findData(\''+tableName+'\')">Find<\/button>&nbsp;<button class="STbutt" onclick="this.blur();clearTable(\''+tableName+'\')">Clear<\/button><div id="findResult" class="STtxt">(Search Results)<\/div><\/td><\/tr>'
 if(extraTableData!=''){
  str+='<tr><td type="header" colspan="'+tableHeadings.length+'" align=left>'+extraTableData+'<\/td><\/tr>'
 }
 str+='<tr><td 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" onclick="sortBy('+a+',\''+tableName+'\')" title="Click to sort.">&nbsp;<b>'+tableHeadings[a]+'<\/b>&nbsp;<font id="arr'+a+'" class="STarrow" style="color:'+headerTxtColor+';" title="Ascending Order">5<\/font></td>'
  }
 }
 str+='<\/tr><\/table>'
 thn.innerHTML=str
 tmp=document.getElementById(tableName).getElementsByTagName('TR')
 for(a=0;a<tmp.length;a++){
  if(tmp[a].parentElement.parentElement.id==tableName){
   ignoreRowsQty+=1
  }
 }
 makeTable(tableName,s)
}


function makeTable(tableName,s){
 tb=document.getElementById(tableName)
 ft=document.getElementById('findText')
 fr=document.getElementById('findResult')
 c=0
 for (a=0;a<tableData.length;a++){
  tb.insertRow() 
  if(c==0){
   r=tb.rows[tb.rows.length-1];r.type="tableDataRow";r.style.background=dataBgColor1;
   bgCols[a]=dataBgColor1
   c=1
  }else{
   r=tb.rows[tb.rows.length-1];r.type="tableDataRow";r.style.background=dataBgColor2;
   bgCols[a]=dataBgColor2
   c=0
  }
  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){
    // documents
    case (isDocument(tmp2)):
          com='c'+b+'=r.insertCell();c'+b+'.innerHTML="&nbsp;<a href=\''+docCheck(tmp2)+'\' target=\'new\' style=\'color:'+linkTxtColor+';\' onmouseover=this.style.color=\''+linkHoverColor+'\' onmouseout=this.style.color=\''+linkTxtColor+'\' type=\'linker\' title=\'Click here to open this file in a new window.\' onclick=\'blurMe()\'>'+docTxtCheck(tmp2)+'&nbsp;</a>"';
          break;
	// web address
	case ((tmp2.indexOf('http://')>=0||tmp2.indexOf('https://')>=0)&&tmp2.indexOf('img')<0):
		  com='c'+b+'=r.insertCell();c'+b+'.innerHTML="&nbsp;<a href=\''+linkCheck(tmp2)+'\' target=\'new\' style=\'color:'+linkTxtColor+';\' onmouseover=this.style.color=\''+linkHoverColor+'\' onmouseout=window.status=\'\';this.style.color=\''+linkTxtColor+'\' type=\'linker\' title=\'Click here open a window for this location.\' onclick=\'blurMe()\'>'+linkTxtCheck(tmp2)+'&nbsp;</a>"';
	      break;
    // email
	case (tmp2.indexOf('@')>=0&&tmp2.indexOf('.')>=0):
		  com='c'+b+'=r.insertCell();c'+b+'.innerHTML="&nbsp;<a style=\'color:'+linkTxtColor+';\' onmouseover=this.style.color=\''+linkHoverColor+'\' onmouseout=this.style.color=\''+linkTxtColor+'\' href=\'mailto:'+linkCheck(tmp2)+'\' type=\'linker\' title=\'Click here to send email.\' onclick=\'blurMe()\'>'+linkTxtCheck(tmp2)+'&nbsp;</a>"';
	      break;
    // number format
    case (tmp2.indexOf('[N]')>=0):
          com='c'+b+'=r.insertCell();c'+b+'.innerHTML="&nbsp;'+numberFormat(tmp2)+'&nbsp;"';
          break;
	// standard text
    default:
		  com='c'+b+'=r.insertCell();c'+b+'.className=\'STtxt\';c'+b+'.innerHTML="&nbsp;'+tmp2+'&nbsp;";';
	      break;
   }
   eval(com)
  }
 }
  tb.insertRow() 
  r=tb.rows[tb.rows.length-1];r.type="author";r.style.background=dataBgColor1;
  c0=r.insertCell();c0.colSpan=tableHeadings.length;c0.className='STtxt';c0.innerHTML='Edited by <a style=\'color:'+linkTxtColor+';\' onmouseover=this.style.color=\''+linkHoverColor+'\' onmouseout=this.style.color=\''+linkTxtColor+'\' href=\'http://www.briles.net\' target=\'new\' type=\'linker\' title=\'Click here to visit authors homepage.\'>R. Trent Briles</a>'

 i=document.getElementById(tableName).getElementsByTagName('TD')
 for(a=0;a<i.length;a++){
  if(i[a].type=='header'){
   i[a].style.background=headerBgColor
   i[a].style.color=headerTxtColor
   if(i[a].title=='Click to sort.'){
    i[a].style.cursor='hand'
   }else{
    i[a].style.cursor='default'
   }
  }
 }
 fr.innerText='(Search Results)'
 if(s)sortBy(s,tableName)
}

function numberFormat(num){
  var n=num.split('[N]')[0]
  var arr=new Array('0'), i=0; 
  while (n>0) 
    {arr[i]=''+n%1000; n=Math.floor(n/1000); i++;}
  arr=arr.reverse();
  for (var i in arr) if (i>0) //padding zeros
    while (arr[i].length<3) arr[i]='0'+arr[i];
  return arr.join();
}

function isDocument(docStr){
 extFound=false
 for(x=0;x<fileExts.length;x++){
  if(docStr.indexOf(fileExts[x])>=0){
   extFound=true
  }
 }
 return extFound
}

function docCheck(str){
 if(str.indexOf('[')>=0){
  return str.split('[')[0]
 }else{ 
  return str
 }
}

function docTxtCheck(str){
 if(str.indexOf('[')>=0){
  return str.split('[')[1].split(']')[0]
 }else{ 
  return str
 }
}

function linkCheck(str){
 if(str.indexOf('[')>=0){
  return str.split('[')[0]
 }else{ 
  return str
 }
}

function linkTxtCheck(str){
 if(str.indexOf('[')>=0){
  return str.split('[')[1].split(']')[0]
 }else{ 
  return str
 }
}

function resetTable(tableName){
 tb=document.getElementById(tableName)
 i=tb.rows.length 
 for(a=ignoreRowsQty;a<i;a++){
  tb.deleteRow()
 }
}

var retVal=0

function sortBy(n,tableName){
 prop='col'+n
 dir=document.getElementById('arr'+n)
 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)
 }
 resetTable(tableName)
 makeTable(tableName)
}

function sortFuncUp(a,b) {
 c=a[sortProp].toLowerCase()
 d=b[sortProp].toLowerCase()
 f1 = parseFloat(c);
 f2 = parseFloat(d);
 if (!isNaN(f1) && !isNaN(f2)) {
  c = f1;
  d = f2;
 }
 if (c > d) return retVal=1;  
 if (c < d) return retVal=-1;		
 return 0;    
}

function sortFuncDn(a,b) {
 c=a[sortProp].toLowerCase()
 d=b[sortProp].toLowerCase()
 f1 = parseFloat(c);
 f2 = parseFloat(d);
 if (!isNaN(f1) && !isNaN(f2)) {
  c = f1;
  d = f2;
 }
 if (c < d) return retVal=1;  
 if (c > d) return retVal=-1;		
 return 0;    
}

function findData(tableName){ 
 //alert(getCellContent(tableName,5,1))
 var seperator='~'
 ft=document.getElementById('findText')
 fr=document.getElementById('findResult')
 if(ft.value!=''){
  clearTable(tableName)
  var dataQty=0
  i=document.getElementById(tableName).getElementsByTagName('TR')
  for(a=ignoreRowsQty;a<i.length-1;a++){
    if(i[a].type!='header'){
	 var startNum
	 if(rowNumbersEnabled){
	  startNum=1
	 }else{
	  startNum=0
	 }
	 tmp=''
	 for(x=startNum;x<i[a].childNodes.length;x++){
	  tmp+=i[a].childNodes[x].innerText+seperator
	 }
	 if(document.getElementById('matchCase').checked){
      if(tmp.indexOf(ft.value)>=0){
       i[a].style.background=hilightBgColor
       //i(a).scrollIntoView(true); 
	   dataQty++
      }
	 }else{
      if(tmp.toLowerCase().indexOf(ft.value.toLowerCase())>=0){
       i(a).style.background=hilightBgColor
       //i(a).scrollIntoView(true); 
	   dataQty++
      }
	 }
    }
  }
  if(dataQty==1){
   fr.innerText=dataQty+" occurence of '"+ft.value+"' was found."
  }else{
   fr.innerText=dataQty+" occurences of '"+ft.value+"' were found."
  }
  if(clearingTable){
   ft.value=''
   fr.innerText='(Search Results)'
   clearingTable=false
  }
 }
}

function clearTable(tableName){
 i=document.getElementById(tableName).getElementsByTagName('TR')
 for(a=ignoreRowsQty;a<i.length-1;a++){
  if(i[a].type!='header'){
   i[a].style.background=bgCols[a-ignoreRowsQty]
  }
 }
 document.getElementById('findResult').innerText='(Search Results)'
}

var tableRowData=new Array()
function hilightRow(){
 bgc=event.srcElement.parentElement
 if(bgc.tagName=='TR'&&bgc.type=='tableDataRow'){
   if(bgc.style.background==hilightRowColor){
    bgc.style.background=tableRowData[bgc.rowIndex-ignoreRowsQty]
   }else{
    tableRowData[bgc.rowIndex-ignoreRowsQty]=bgc.style.background
    bgc.style.background=hilightRowColor
   }
 }
}

function getCellContent(tableName,r,c){
 return document.getElementById(tableName).rows(r).cells(c).innerText
}


function blurMe(){
 if(event.srcElement.tagName=='A'){
   event.srcElement.blur()
 }
}

function matchCaseCheck(){
 c=document.getElementById('matchCase')
 if(c.checked){
  c.checked=false
 }else{
  c.checked=true
 }
}
