/*********************************************************************
setMaxWidth(e):
if the element e has the custom attribute MAXWIDTH set to "yes", this function will adjust the width of the element so that the right edge will meet the edge of the container. 
*********************************************************************/

function setMaxWidth(e) {
	if (e.getAttribute("maxwidth") == "yes") {
		p = e.parentNode;
		pw = p.scrollWidth;
		pl = 0;
		while (p != e.offsetParent) {
			pl += p.offsetLeft - 10;
			p = p.offsetParent;
		}
		l = pw - e.offsetLeft + pl - 15;
		e.style.width = l + "px";
	}
}

/*********************************************************************
showHideDiv(e):
toggles the display style attribute between none and block for the element e 
*********************************************************************/

function showHideDiv(e) {
	if (e.style.display == "none") {
		e.style.display = "block";
		var es = e.getElementsByTagName('*');
		for (var i=0; i<es.length; i++) {
			setMaxWidth(es[i]);
		}
	}
	else {
		e.style.display = "none";
	}
}

/*********************************************************************
maxContainingIFrameHeight():
if the document is contained in an iframe (not a window), this function expands the height of the iframe to display the whole document
*********************************************************************/

function maxContainingIFrameHeight() {
	if (window == top) {
		return;
	}
	var i = parent.document.getElementsByName(window.name)[0];
	if (i.tagName == "IFRAME") {
		setMaxWidth(i);
		i.height = 200;
		n=0;
		h = (document.documentElement.scrollHeight > document.body.scrollHeight) ? document.documentElement.scrollHeight : document.body.scrollHeight;		
		while (1) {
			h = (document.documentElement.scrollHeight > document.body.scrollHeight) ? document.documentElement.scrollHeight : document.body.scrollHeight;		
			if (n == 20 || h <= i.height) {
				break;
			}
			i.height = h + 10;
			n++;
		} 
	}
}

/*********************************************************************
getText(obj):
returns the text content of the object
*********************************************************************/

function getText(obj) {
	return (obj.innerText) ? obj.innerText : (obj.textContent) ? obj.textContent : "";
}


/*********************************************************************
sortTable(tableId, sortBy, ascending)
sorts the table by sortBy
*********************************************************************/

function sortTable(tableId, sortBy, ascending) {
	var t, i, j, c, m, tmp, k;

	t = document.getElementById(tableId);
	
	/* check each row in table has same number of cells */
	c = t.rows[0].cells.length;
	for (i=1; i<t.rows.length-1; i++) {
		if (c != t.rows[i].cells.length) return false;
	}

	/* search for sortBy in the first row of tableId */
	for (c=0; c<t.rows[0].cells.length; c++) {
		if (getText(t.rows[0].cells[c]).replace("\n", "") == sortBy.replace("\n", "")) {
			break;
		}
	}
	if (c == t.rows[0].cells.length) return false;

	/* sort table */
	for (i=1; i<t.rows.length-1; i++) { /* for each row in the table... */
		/* find the min or max (depending on ascending value) in subsequent rows */
		m = i;
		for (j=i+1; j<t.rows.length; j++) { 
			if (getText(t.rows[j].cells[c]) != "" && 
					((!ascending && getText(t.rows[m].cells[c]).toLowerCase() < getText(t.rows[j].cells[c]).toLowerCase()) ||
					 ascending && getText(t.rows[m].cells[c]).toLowerCase() > getText(t.rows[j].cells[c]).toLowerCase()) ) {
				m = j;
			}
		}
		/* swap i with the min/max row */
		if (m != i) {
			for (k=0; k<t.rows[i].cells.length; k++) {
				tmp = t.rows[i].cells[k].innerHTML + "";
				t.rows[i].cells[k].innerHTML = t.rows[m].cells[k].innerHTML + "";
				t.rows[m].cells[k].innerHTML = tmp;
			}
		}
	}
	return true;
}
