var blankImg = "/images/default/prodTabBg.gif";
var downImg = "/images/default/down.gif";
var upImg = "/images/default/up.gif";
var imgElsrc = new Array();

function setImg(obj, col) {
	for ( var i = 0; i < imgElsrc.length; i++) {
		var imgTemp = document.getElementById("img" + i);
		if (i != col) {
			imgTemp.src = blankImg;
		} else {
			if (obj.reverseSort[col])
				imgElsrc[col] = downImg;
			else
				imgElsrc[col] = upImg;

			imgTemp.src = imgElsrc[col];
		}
	}/* alert(imgElsrc.length) */
}

function sortTable2(sortValue) {
	if (sortValue.length > 10) {
		location.href = sortValue;
		return false;
	}
	col = sortValue.substr(0, 1);
	order = sortValue.substr(1, 1);
	// Get the table section to sort.
	var tblEl = document.getElementById("planetData2");
	// Set up an array of reverse sort flags, if not done already.
	if (tblEl.reverseSort == null)
		tblEl.reverseSort = new Array();

	// If this column was the last one sorted, reverse its sort direction.
	if (col == tblEl.lastColumn || order == 1)
		tblEl.reverseSort[col] = !tblEl.reverseSort[col];

	// Remember this column as the last one sorted.
	tblEl.lastColumn = col;

	// alert(tblE1);
	var colLength = tblEl.rows[0].cells.length;
	if (imgElsrc.length < colLength) {
		imgElsrc = new Array(colLength);
	}
	// Set Image
	setImg(tblEl, col);

	// Set the table display style to "none" - necessary for Netscape 6
	// browsers.
	var oldDsply = tblEl.style.display;
	tblEl.style.display = "none";

	// Sort the rows based on the content of the specified column using a
	// selection sort.

	var tmpEl;
	var i, j;
	var minVal, minIdx;
	var testVal;
	var cmp;

	for (i = 0; i < tblEl.rows.length - 1; i++) {
		// Assume the current row has the minimum value.
		minIdx = i;
		minVal = getTextValue(tblEl.rows[i].cells[col]);

		// Search the rows that follow the current one for a smaller value.
		for (j = i + 1; j < tblEl.rows.length; j++) {
			testVal = getTextValue(tblEl.rows[j].cells[col]);
			cmp = compareValues(minVal, testVal);
			// Reverse order?
			if (tblEl.reverseSort[col])
				cmp = -cmp;
			// If this row has a smaller value than the current minimum,
			// remember its
			// position and update the current minimum value.
			if (cmp > 0) {
				minIdx = j;
				minVal = testVal;
			}
		}

		// By now, we have the row with the smallest value. Remove it from the
		// table and insert it before the current row.
		if (minIdx > i) {
			tmpEl = tblEl.removeChild(tblEl.rows[minIdx]);
			tblEl.insertBefore(tmpEl, tblEl.rows[i]);
		}
	}

	// Restore the table's display style.
	tblEl.style.display = oldDsply;

	return false;
}

// -----------------------------------------------------------------------------
// Functions to get and compare values during a sort.
//-----------------------------------------------------------------------------

// This code is necessary for browsers that don't reflect the DOM constants
// (like IE).
if (document.ELEMENT_NODE == null) {
	document.ELEMENT_NODE = 1;
	document.TEXT_NODE = 3;
}

function getTextValue(el) {
	var i;
	var s;

	// Find and concatenate the values of all text nodes contained within the
	// element.
	s = "";
	for (i = 0; i < el.childNodes.length; i++)
		if (el.childNodes[i].nodeType == document.TEXT_NODE)
			s += el.childNodes[i].nodeValue;
		else if (el.childNodes[i].nodeType == document.ELEMENT_NODE
				&& el.childNodes[i].tagName == "BR")
			s += " ";
		else
			// Use recursion to get text within sub-elements.
			s += getTextValue(el.childNodes[i]);

	return normalizeString(s);
}

String.prototype.rmCurrency = function() {
	var price = this.replace(/,/, '');
	price = price.match(/\d+/);
	return price;
}

function compareValues(v1, v2) {
	var f1, f2;

	// If the values are numeric, convert them to floats.

	f1 = parseFloat(v1.rmCurrency());
	f2 = parseFloat(v2.rmCurrency());

	if (!isNaN(f1) && !isNaN(f2)) {
		v1 = f1;
		v2 = f2;
	}

	// Compare the two values.
	if (v1 == v2)
		return 0;
	if (v1 > v2)
		return 1
	return -1;
}

// Regular expressions for normalizing white space.
var whtSpEnds = new RegExp("^\\s*|\\s*$", "g");
var whtSpMult = new RegExp("\\s\\s+", "g");

function normalizeString(s) {

	s = s.replace(whtSpMult, " "); // Collapse any multiple whites space.
	s = s.replace(whtSpEnds, ""); // Remove leading or trailing white space.

	return s;
}
