// JavaScript Document
DataGrid = function() {
	
	this.instanceName = null;
	
	this.headerTableId = null;
	this.dataTableId = null;
	this.columnCount = null;
	
	this.thAscendingClassName = null;
	this.thDescendingClassName = null;
	
	this.rowNormalClassName = null;
	this.rowAlternateClassName = null;
	this.rowSelectedClassName = null;
	this.rowHeaderClassName = null;
	
	this.selectedRowIndex = null;
	this.sortable = true;
	this.selectable = true;
	this.sortedColumnIndex = null;
	this.sortedColumnType = "string";
	this.sortDirection = "desc";
	this.selectedDataID = null;
	
	/*
	*
	*/
	this.initialize = function() {
		this.redraw();
	}
	
	/*
	*
	*/
	this.selectRowDataID = function(dataID) {
		if(this.selectable == false) {
			return;
		}
		dataTable = document.getElementById(this.dataTableId);
		
		for(i=0; i<dataTable.rows.length; i++) {
			try {
				currentDataID = dataTable.rows.item(i).attributes.getNamedItem("sv:dataID").value;
				if(dataID == currentDataID) {
					this.selectRow(i);
					break;
				}
			}
			catch (e) {
			}
		}
	}
	
	/*
	*
	*/
	this.selectRow = function(rowIndex) {
		if(this.selectable == false) {
			return;
		}
		if(this.selectedRowIndex!=null) {
			if((this.selectedRowIndex%2)==0) {
				document.getElementById(this.dataTableId).rows.item(this.selectedRowIndex).className = this.rowNormalClassName;
			}
			else {
				document.getElementById(this.dataTableId).rows.item(this.selectedRowIndex).className = this.rowAlternateClassName;
			}
		}
		
		document.getElementById(this.dataTableId).rows.item(rowIndex).className = this.rowSelectedClassName;
		
		this.selectedRowIndex = rowIndex;
		
		try {
			dataID = document.getElementById(this.dataTableId).rows.item(rowIndex).attributes.getNamedItem("sv:dataID").value;
			if(dataID) {
				this.selectedDataID = dataID;
			}
			else {
				this.selectedDataID = null;
			}
		}
		catch (e) {
			this.selectedDataID = null;
		}
	}
	
	/*
	*
	*/
	this.sortColumn = function(columnIndex,columnType) {
		if(this.sortable == false) {
			return;
		}
		if(this.sortedColumnIndex!=null) {
			document.getElementById(this.headerTableId).rows.item(0).cells.item(this.sortedColumnIndex).className = null;
		}
		if(this.sortedColumnIndex==columnIndex) {
			if(this.sortDirection=="desc") {
				document.getElementById(this.headerTableId).rows.item(0).cells.item(columnIndex).className = this.thDescendingClassName;
				this.sortDirection = "asc";
				this.sortedColumnType = columnType;
				this.sortedColumnIndex = columnIndex;
				this.doSort();
			}
			else if(this.sortDirection=="asc") {
				document.getElementById(this.headerTableId).rows.item(0).cells.item(columnIndex).className = this.thAscendingClassName;
				this.sortDirection = "desc";
				this.sortedColumnType = columnType;
				this.sortedColumnIndex = columnIndex;
				this.doSort();
			}
		}
		else {
			document.getElementById(this.headerTableId).rows.item(0).cells.item(columnIndex).className = this.thAscendingClassName;
			this.sortDirection = "desc";
			this.sortedColumnType = columnType;
			this.sortedColumnIndex = columnIndex;
			this.doSort();
		}
	}
	
	/*
	*
	*/
	this.doSort = function() {
		dataTable = document.getElementById(this.dataTableId);
		rowArray = new Array();
		for(i=0; i<dataTable.rows.length; i++) {
			rowArray[i] = "";
			for(j=0; j<this.columnCount; j++) {
				rowArray[i] += dataTable.rows.item(i).cells.item(j).innerHTML + "|";
			}
			rowArray[i] += this.sortedColumnIndex + "|" + this.sortedColumnType + "|" + this.sortDirection;
		}
		
		rowArray.sort(this.rowCompare);
		
		for(p=0; p<rowArray.length; p++) {
			rowData = new Array();
			rowData = rowArray[p].split("|");
			for(q=0; q<this.columnCount; q++) {
				dataTable.rows.item(p).cells.item(q).innerHTML = rowData[q];
			}
		}
		
		if(this.selectedRowIndex!=null) {
			if((this.selectedRowIndex%2)==0) {
				document.getElementById(this.dataTableId).rows.item(this.selectedRowIndex).className = this.rowNormalClassName;
			}
			else {
				document.getElementById(this.dataTableId).rows.item(this.selectedRowIndex).className = this.rowAlternateClassName;
			}
			this.selectedRowIndex = null;
		}
	}
	
	/*
	*
	*/
	this.rowCompare = function(aRow,bRow) {
		aRowData = aRow.split("|");
		bRowData = bRow.split("|");
		columnIndex = aRowData[aRowData.length-3];
		columnType = aRowData[aRowData.length-2];
		sortDirection = aRowData[aRowData.length-1];
		
		aStr =aRowData[columnIndex];
		bStr =bRowData[columnIndex];
		
		aStr = aStr.replace(/&nbsp;/g , "" );
		bStr = bStr.replace(/&nbsp;/g , "" );
		
		switch(columnType) {
			case "string":
				aValue = aStr;
				bValue = bStr;
				if(aValue < bValue) {
					if(sortDirection == "desc") {
						return -1;
					}
					else {
						return 1;
					}
				}
				if(aValue > bValue) {
					if(sortDirection == "desc") {
						return 1;
					}
					else {
						return -1;
					}
				}
				return 0;
				break;
			case "int":
				aValue = parseInt(aStr,10);
				bValue = parseInt(bStr,10);
				if(aValue < bValue) {
					if(sortDirection == "desc") {
						return -1;
					}
					else {
						return 1;
					}
				}
				if(aValue > bValue) {
					if(sortDirection == "desc") {
						return 1;
					}
					else {
						return -1;
					}
				}
				return 0;
				break;
			case "float":
				aStr = aStr.replace(/,/g , "." );
				bStr = bStr.replace(/,/g , "." );
				aValue = parseFloat(aStr);
				bValue = parseFloat(bStr);
				if(aValue < bValue) {
					if(sortDirection == "desc") {
						return -1;
					}
					else {
						return 1;
					}
				}
				if(aValue > bValue) {
					if(sortDirection == "desc") {
						return 1;
					}
					else {
						return -1;
					}
				}
				return 0;
				break;
			case "date":
				aStr = aStr.split(".");
				bStr = bStr.split(".");
				aValue = Date.UTC(parseInt(aStr[2],10), parseInt(aStr[1],10)-1, parseInt(aStr[0],10));
				bValue = Date.UTC(parseInt(bStr[2],10), parseInt(bStr[1],10)-1, parseInt(bStr[0],10));
				if(aValue < bValue) {
					if(sortDirection == "desc") {
						return -1;
					}
					else {
						return 1;
					}
				}
				if(aValue > bValue) {
					if(sortDirection == "desc") {
						return 1;
					}
					else {
						return -1;
					}
				}
				return 0;
				break;
		}
	}
	
	/*
	*
	*/
	this.redraw = function() {
		if(this.sortedColumnIndex!=null) {
			document.getElementById(this.headerTableId).rows.item(0).cells.item(this.sortedColumnIndex).className = null;
			this.sortedColumnIndex = null;
		}
		
		dataTable = document.getElementById(this.dataTableId);
		for(i=0; i<dataTable.rows.length; i++) {
			if(dataTable.rows.item(i).childNodes.item(1).nodeName == "TH") {
				dataTable.rows.item(i).className = this.rowHeaderClassName;
				continue;
			}
			if((i%2)==0) {
				dataTable.rows.item(i).className = this.rowNormalClassName;
			}
			else {
				dataTable.rows.item(i).className = this.rowAlternateClassName;
			}
			
			try {
				currentOnClick = dataTable.rows.item(i).attributes.getNamedItem("onclick").nodeValue+"; ";
			}
			catch(ex) {
				currentOnClick = "";
			}
			dataTable.rows.item(i).setAttribute("onclick",currentOnClick+this.instanceName+".selectRow("+i+")");
		}
	}
}



var dgDisplay = null;
var dgDisplay2 = null;

init = function() {
	// inicijalizacija tabele za prikaz osnovnih informacija
	dgDisplay = new DataGrid();
	dgDisplay.instanceName = 				"dgDisplay";
	dgDisplay.headerTableId = 				"display_caption";
	dgDisplay.dataTableId = 				"display_data";
	dgDisplay.columnCount = 				10;
	dgDisplay.thAscendingClassName = 		"display_caption_asc";
	dgDisplay.thDescendingClassName = 		"display_caption_desc";
	dgDisplay.rowNormalClassName = 			"display_data";
	dgDisplay.rowAlternateClassName = 		"display_data_alt";
	dgDisplay.rowSelectedClassName = 		"display_data_select";
	dgDisplay.initialize();
	
	// inicijalizacija tabele za prikaz osnovnih informacija
	dgDisplay2 = new DataGrid();
	dgDisplay2.instanceName = 				"dgDisplay2";
	dgDisplay2.headerTableId = 				"display_caption2";
	dgDisplay2.dataTableId = 				"display_data2";
	dgDisplay2.columnCount = 				10;
	dgDisplay2.thAscendingClassName = 		"display_caption_asc2";
	dgDisplay2.thDescendingClassName = 		"display_caption_desc2";
	dgDisplay2.rowNormalClassName = 		"display_data2";
	dgDisplay2.rowAlternateClassName = 		"display_data_alt2";
	dgDisplay2.rowSelectedClassName = 		"display_data_select2";
	dgDisplay2.initialize();
	
	
}
