function gridInit() {
    // Find all tables with class sortable and make them sortable
    if (!document.getElementsByTagName) return;
    tbls = document.getElementsByTagName("table");
    for (ti=0;ti<tbls.length;ti++) {
        thisTbl = tbls[ti];
        if (((' '+thisTbl.className.toLowerCase()+' ').indexOf("datagrid") != -1) && (thisTbl.id)) {
            gridMakeSortable(thisTbl);
            alternateRowColors(thisTbl);
            fillEmptyCells(thisTbl);
        }
        if (((' '+thisTbl.className.toLowerCase()+' ').indexOf("dataview") != -1)) {
            fillEmptyCells(thisTbl);
        }
    }
}
	


function fillEmptyCells(table) {
   if (document.getElementsByTagName) {
      var tableCells = table.getElementsByTagName("TD");
      var usesInnerText = false;
      for (var i=0; i<tableCells.length; i++) {
         if (tableCells[i].innerText) { usesInnerText = true; i = tableCells.length+1; }
      }
      if (usesInnerText) {
         for (var i=0; i<tableCells.length; i++) {
            if (!tableCells[i].innerText) {
               if (tableCells[i].innerHTML == "") { tableCells[i].innerHTML = "&nbsp;"; }
            } else if (tableCells[i].innerText == "") {
               tableCells[i].innerText = "&nbsp;";
            }
         }
      }
   }
}


function alternateRowColors(table){
 if(document.getElementsByTagName){  
   var rows = table.getElementsByTagName("tr");  
   for(i = 0; i < rows.length; i++){          
     //if(i % 2 != 0){
     if((i % 2 != 0) && (rows[i].className != 'inactive') && (rows[i].className != 'active')){
       rows[i].className = "odd";
     }      
   }
 }
}

	
function gridMakeSortable(table) {
    if (table.rows && table.rows.length > 0) {
        firstRow = table.rows[0];
    }
    if (!firstRow) return;
    
	var qsParm = new Array();
	var qs = window.location.search.substring(1);
	var parms = qs.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
	
    
    for (var i=0;i<firstRow.cells.length;i++) {
        var cell = firstRow.cells[i];
        if(firstRow.cells[i].id != ''){
        	id=firstRow.cells[i].id;
			theCell=document.getElementById(id);
      	  	theCell.className='sort'; 
			
		   //if NN6 then OK to use the standard setAttribute
		   if ((!document.all)&&(document.getElementById)){
		       theCell.setAttribute("onClick","gridSort(this)");
		   }    
		   //workaround for IE 5.x
		   if ((document.all)&&(document.getElementById)){
		       theCell["onclick"]=new Function("gridSort(this)");
		   }
      	  	
      	  	if (qsParm['sortASC']==id){
      	  		theCell.className='sortASC';
      	  	}
      	  	if (qsParm['sortDESC']==id){
      	  		theCell.className='sortDESC';
      	  	}
        }
    } 
}

function gridSort(t){
	var sortDir='sortASC'
	var column=t.id;
	if (t.className=='sortASC'){
		sortDir='sortDESC'
	}
	window.location='?' + persistantVars() + '&' + sortDir + '=' + column;
}


