/*
 * This file is part of Axapta Doc Generator.
 *
 * Copyright (C) 2005-2006  Lars K. Schunk.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without
 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
 * conditions:

 * The above copyright notice and this permission notice shall be included in all copies or substantial
 * portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
 * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
 * OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

function expandCollapse(linkId, blockId, desc) {
	var linkText = document.getElementById(linkId);
	var dynamicBlock = document.getElementById(blockId);

	if (dynamicBlock.style.display == "none") {
		dynamicBlock.style.display = "block";
		linkText.firstChild.nodeValue = "Hide " + desc;
	} else {
		dynamicBlock.style.display = "none";
		linkText.firstChild.nodeValue = "Show " + desc;
	}
}

function highlightFieldGroup() {
	var selectedFieldGroup = document.fieldGroup.fieldGroupSelection.selectedIndex;
	var hiddens = document.getElementsByTagName("input");
	var fields;

	// Remove all highlights
	fields = document.getElementById("tableFields").getElementsByTagName("tr");
	for (var i=0; i<fields.length; i++) {
		fields[i].style.backgroundColor = "transparent";
	}

	selectedFieldGroup--; // None = -1, AutoReport = 0, AutoLookup = 1, etc.
	if (selectedFieldGroup == -1)
		return;

	selectedFields = hiddens[selectedFieldGroup].value.split(";");
	if (!selectedFields[0])
		return;

	// Highlight fields in selected field group
	for (var i=0; i<selectedFields.length; i++) {
		for (var j=0; j<fields.length; j++) {
			if (fields[j].id == selectedFields[i])
				fields[j].style.backgroundColor = "yellow";
		}
	}
}

function getNestedNodeValue(node) {
	if (node.nodeValue || !node.firstChild)
		return node.nodeValue;
	else
		return getNestedNodeValue(node.firstChild);
}

// Row class
function Row(rowNode, sortColumnNo) {
	this.pos = getNestedNodeValue(rowNode.cells[0]);
	this.sortValue = getNestedNodeValue(rowNode.cells[sortColumnNo]);
	this.row = rowNode;
	this.propRow = rowNode.nextSibling;
}
// End of Row class

function sortTable(node) {
	function sortFunc_alpha(rowA, rowB) {
		a = rowA.sortValue.toLowerCase();
		b = rowB.sortValue.toLowerCase();
		
		if (a < b) return -1;
		if (a > b) return 1;
		return 0;
	}
	
	function sortFunc_num(rowA, rowB) {
		a = parseInt(rowA.sortValue);
		b = parseInt(rowB.sortValue);
		
		if (a < b) return -1;
		if (a > b) return 1;
		return 0;
	}

	var table = node.parentNode.parentNode.parentNode.parentNode;
	var tableRows = table.rows;
	var newRows = new Array();
	var sortColumnName = node.innerHTML;
	var sortColumnNo;
	var i;
	var j = 0;

	// Update the sort direction and the arrow indication
	if (node.parentNode.getAttribute("sortDir")=="asc") {
		node.parentNode.setAttribute("sortDir", "desc");
		sortColumnName = sortColumnName.substring(0, sortColumnName.length-1) + "\u25bc";
	}	else if (node.parentNode.getAttribute("sortDir")=="desc") {
		node.parentNode.setAttribute("sortDir", "asc");
		sortColumnName = sortColumnName.substring(0, sortColumnName.length-1) + "\u25b2";
	} else {
		node.parentNode.setAttribute("sortDir", "asc");
		sortColumnName += "&nbsp;\u25b2";
	}
	node.innerHTML = sortColumnName;
	
	// Find out which column to sort by (this depends on how
	// many "younger" siblings 'node' has)
	var tmpNode = node.parentNode;
	var counter = 0;
	while (tmpNode.previousSibling) {
		tmpNode = tmpNode.previousSibling;
		counter++;
	}
	sortColumnNo = counter;

	// Remove any sort order information from other columns (first younger siblings...)
	tmpNode = node.parentNode;
	while(tmpNode.previousSibling) {
		tmpNode = tmpNode.previousSibling;
		if (tmpNode && (tmpNode.getAttribute("sortDir")=="asc" || tmpNode.getAttribute("sortDir")=="desc")) {
			var tmpStr;
			tmpNode.setAttribute("sortDir", "");
			tmpStr = tmpNode.firstChild.innerHTML;
			tmpStr = tmpStr.substring(0, tmpStr.length-3);
			tmpNode.firstChild.innerHTML = tmpStr;
		}
	}

	// (... then older siblings)
	tmpNode = node.parentNode;
	while(tmpNode.nextSibling) {
		tmpNode = tmpNode.nextSibling;
		if (tmpNode && (tmpNode.getAttribute("sortDir")=="asc" || tmpNode.getAttribute("sortDir")=="desc")) {
			var tmpStr;
			tmpNode.setAttribute("sortDir", "");
			tmpStr = tmpNode.firstChild.innerHTML;
			tmpStr = tmpStr.substring(0, tmpStr.length-3);
			tmpNode.firstChild.innerHTML = tmpStr;
		}
	}
	
	// Copy rows to a new array of Row objects
	for (i=1; i<tableRows.length; i+=2) { // Note that we skip each property row
		newRows[j++] = new Row(tableRows[i], sortColumnNo);
	}

	// Pick the appropriate sort function based on the sort value in the first row
	var sortFunc;

	if (isNaN(parseInt(newRows[0].sortValue)))
		sortFunc = sortFunc_alpha;
	else
		sortFunc = sortFunc_num;
	
	// Sort the new array of rows
	newRows.sort(sortFunc);
	if (node.parentNode.getAttribute("sortDir")=="desc")
		newRows.reverse();

	// Rearrange the rows
	for (i=0; i<newRows.length; i++) {
		table.tBodies[0].appendChild(newRows[i].row);
		table.tBodies[0].appendChild(newRows[i].propRow);
	}
}









