//Copyright(c)2005,2007 Bob Swift and other contributors. All rights reserved.
function getElementInnerText(element){return element.abbr;}
function trimSafe(v){return((v==undefined)||(v==null))?"":v.Trim();}String.prototype.Trim=new Function("return this.replace(/^\\s+|\\s+$/g,'')");
function compareNumber(v1,v2){if(isNaN(v2)){if(isNaN(v1))return 0;return-1;}if(v1<v2)return-1;if(v1==v2)return 0;return 1;}
function compareSeparatedNumber(v1,v2){for(i=0;(i<v1.length)&&(i<v2.length);i++){var result=compareNumber(parseInt(v1[i]),parseInt(v2[i]));if(result!=0)return result;}if(v1.length<v2.length)return-1;if(v2.length<v1.length)return 1;return 0;}
function sortByCell(sortCell,firstDataRowIndex,footingCount){var compareFunction;var valueParser;compareFunction=compareNumber;if(sortCell.columnType=="I"){valueParser=function(value){return parseInt(getElementInnerText(value));};}else if(sortCell.columnType=="F"){valueParser=function(value){return parseFloat(getElementInnerText(value));};}else if(sortCell.columnType=="C"){valueParser=function(value){return parseFloat(getElementInnerText(value).replace(/[^\d\.\,\-]*([\d\.\,\-*]*).*/,'$1'));};}else if(sortCell.columnType.charAt(0)=='D'){if(Date.parseString){valueParser=function(value){var date=Date.parseString(getElementInnerText(value),sortCell.columnType.substring(1));return((date==null)?NaN:date.getTime());};}else{valueParser=function(value){return Date.parse(getElementInnerText(value));};}}else if((sortCell.columnType=="/")||(sortCell.columnType==".")||(sortCell.columnType=="-")||(sortCell.columnType==":")){compareFunction=compareSeparatedNumber;valueParser=function(value){return getElementInnerText(value).split(sortCell.columnType);};}else if(sortCell.columnType=="A"){valueParser=function(value){return 0;};}else{compareFunction=function(v1,v2){if(v1<v2)return-1;if(v1==v2)return 0;return 1;};valueParser=function(value){return trimSafe(getElementInnerText(value));};}var table=sortCell.sortTable.tBodies[0];var rowCount=table.rows.length-firstDataRowIndex-footingCount;var map=Array(rowCount);var values=Array(rowCount);var compareTest=(sortCell.sortDescending?-1:1);sortCell.sortDescending=!sortCell.sortDescending;var firstCell=table.rows[0].cells[sortCell.columnIndex];if((firstCell!=null)&&(firstCell.sortTable.sortImage!=null)&&(firstCell.sortTable.sortImage!=undefined)){firstCell.sortTable.sortImage.setAttribute("src",firstCell.sortDescending?firstCell.sortTable.sortAttributeDescending:firstCell.sortTable.sortAttributeAscending);firstCell.appendChild(firstCell.sortTable.sortImage);}var i;for(i=0;i<rowCount;i++){map[i]=sortCell.sortFirstTime?i:(rowCount-1-i);var row=table.rows[i+firstDataRowIndex];var cellValue=row.cells[sortCell.columnIndex];values[i]=valueParser(cellValue);}sortCell.sortFirstTime=false;var didSwap;do{didSwap=false;for(i=0;i<rowCount-1;i++){if(compareFunction(values[map[i]],values[map[i+1]])==compareTest){saveIndex=map[i];map[i]=map[i+1];map[i+1]=saveIndex;didSwap=true;}}}while(didSwap);var tableRows=new Array();for(i=0;i<rowCount+footingCount;i++){tableRows.push(table.rows[i+firstDataRowIndex]);}for(i=0;i<rowCount+footingCount;i++){table.removeChild(tableRows[i]);}for(i=0;i<rowCount;i++){var row=tableRows[map[i]];table.appendChild(row);if(row.autoNumber){row.cells[0].innerHTML=i+1;}}for(i=0;i<footingCount;i++){table.appendChild(tableRows[i+rowCount]);}}
function enableSortOnCell(cell,i,table,columnTypes,firstDataRowIndex,sortTip,autoNumber,footingCount,rowIndex,iconLocation){cell.style.cursor="pointer";cell.sortTable=table;cell.sortFirstTime=true;cell.sortDescending=false;cell.columnIndex=(autoNumber?(i+1):i);cell.columnType=(columnTypes&&columnTypes[i])?columnTypes[i]:"S";if(columnTypes[i]!="X"){cell.onmouseover=function(){this.saveTitle=this.getAttribute('title');this.setAttribute('title',sortTip+" "+((this.saveTitle!=null)&&(this.saveTitle!=undefined)?this.saveTitle:''));};cell.onmouseout=function(){this.setAttribute('title',this.saveTitle);};cell.onclick=function(){sortByCell(this,firstDataRowIndex,footingCount);};}else{cell.columnType=columnTypes[i].substring(1);}}
function sumColumn(table,index,firstDataRowIndex,columnType){var total=0;for(var i=firstDataRowIndex;i<table.rows.length;i++){var cell=table.rows[i].cells[index];var value;if(cell){if(columnType=='C'){value=parseFloat(getElementInnerText(cell).replace(/[^\d\.\,\-]*([\d\.\,\-*]*).*/,'$1'))}else{value=parseFloat(getElementInnerText(cell));}if(!isNaN(value)){total=total+value;}}}return total;}
function appendTotalRow(table,columnTypes,firstDataRowIndex){var row=document.createElement('tr');var table=table.tBodies[0];var columnCount=(0<table.rows.length)?table.rows[table.rows.length-1].cells.length:0;for(var columnIndex=0;columnIndex<columnCount;columnIndex++){var column=document.createElement('th');column.className='confluenceTh';column.innerHTML=(columnIndex<columnTypes.length&&((columnTypes[columnIndex]=='I')||(columnTypes[columnIndex]=='F')||(columnTypes[columnIndex]=='C')))?sumColumn(table,columnIndex,firstDataRowIndex,columnTypes[columnIndex]):'';row.appendChild(column);}table.appendChild(row);}
function customizeMacroTable(tableId,columnTypes,firstDataRowIndex,highlightColor,enableSorting,sortTip,sortColumn,sortDescending,autoNumber,autoNumberSort,enableHeadingAttributes,footingCount,autoTotal,iconLocation){var table=(typeof(tableId)=="string")?document.getElementById(tableId):null;if(table){if(autoTotal){appendTotalRow(table,columnTypes,firstDataRowIndex);footingCount++;}if(iconLocation!=""){table.sortAttributeAscending=contextPath+iconLocation+"down.gif";table.sortAttributeDescending=contextPath+iconLocation+"up.gif";table.sortImage=document.createElement("IMG");}var lastClickableRow=firstDataRowIndex-1;if(lastClickableRow<0){lastClickableRow=0;}var sortCell=null;var dataRowCount=1;var colAttrs=columnAttributes.split(",");var attrList=Array(colAttrs.length);for(var i=0;i<colAttrs.length;i++){attrList[i]=colAttrs[i].Trim().split(";;");}for(var rowIndex=0;rowIndex<table.rows.length;rowIndex++){var row=table.rows[rowIndex];for(var i=0;i<row.cells.length;i++){if(enableSorting&&(rowIndex<=lastClickableRow)){enableSortOnCell(row.cells[i],i,table,columnTypes,firstDataRowIndex,sortTip,autoNumber,footingCount,rowIndex,iconLocation);}if((rowIndex==0)&&(sortColumn!='')&&(sortCell==null)&&(((i+1).toString()==sortColumn)||(trimSafe(getElementInnerText(row.cells[i]))==sortColumn)||(trimSafe(row.cells[i].getAttribute('title'))==sortColumn))){sortCell=row.cells[i];}if(firstDataRowIndex<=rowIndex){if((columnTypes[i]=="I")||(columnTypes[i]=="F")||(columnTypes[i]=="C")){row.cells[i].style.textAlign="right";}}if(columnTypes[i]=="H"){row.cells[i].style.display="none";}if(enableHeadingAttributes||(firstDataRowIndex<=rowIndex)){if(i<attrList.length){for(var j=0;j<attrList[i].length;j++){var attr=attrList[i][j].Trim().split("=");if(1<attr.length){var aName=attr[0].Trim();var aValue=attr[1].Trim();if((aName.toLowerCase()=="style")&&(2<aValue.length)){if((aValue.charAt(0)=='"')){aValue=aValue.substring(1,aValue.length-1);}row.cells[i].style.cssText=aValue;}else{row.cells[i].setAttribute(aName,aValue);}}}}}}if(autoNumber){var column=document.createElement(!autoNumberSort||(rowIndex<firstDataRowIndex)?'th':'td');column.className=(!autoNumberSort||(rowIndex<firstDataRowIndex)?'confluenceTh':'confluenceTd');column.innerHTML=((rowIndex<firstDataRowIndex)||(table.rows.length-firstDataRowIndex-footingCount<rowIndex)?'':dataRowCount++);column.setAttribute('align','right');row.autoNumber=!autoNumberSort;row.insertBefore(column,row.cells[0]);if(enableSorting&&autoNumberSort&&(rowIndex<=lastClickableRow)){enableSortOnCell(column,-1,table,"I",firstDataRowIndex,sortTip,autoNumber,footingCount);}}if((highlightColor!="")&&(firstDataRowIndex<=rowIndex)){row.onmouseover=function(){this.tableHighLightRowColor=this.bgColor;this.bgColor=highlightColor;};row.onmouseout=function(){this.bgColor=this.tableHighLightRowColor;this.tableHighLightRowColor=null;};}}if(sortCell!=null){sortCell.sortDescending=sortDescending;sortByCell(sortCell,firstDataRowIndex,footingCount);}}return table;}
