var oldSelection = null;
var oldSelector = null;
var oldPicture = null;

function selectElementByIdWorker(rootNode,theId) {
	if (rootNode.id == theId) {
		return rootNode;
	} else {
		var newNode;
		var i;
		
		for (i = 0; i < rootNode.length; i++) {
			newNode = selectElementByIdWorker(rootNode[i], theId);
			
			if (newNode != null) {
				return newNode;
			}
		}
		
		return null;
	}
}

function selectElementById(theId) {
	return selectElementByIdWorker(document.all, theId);
}

function selectElement(theElement) {
	return document.getElementById(theElement);
//	var result = selectElementByIdWorker(document.all, theElement);

//	return result;

//	var elem = null;
//	var elems = document.getElementsByName(theElement);
//	alert("Elements: " + elems.length + "  Name: " + theElement);
	
//	if (elems.length > 0) {
//		elem = elems[0];
//	}

//	return elem;
}

function showBoat(node,selection) {
	if (oldSelection != null) {
		oldSelection.style.display = "none";
		oldSelection = null;
	}
	
	if (oldSelector != null) {
		oldSelector.style.border = "solid 1px white";
		oldSelector.style.backgroundColor = "white";
		oldSelector = null;
	}
	
	if (node != null) {
		node.style.border = "solid 1px blue";
		node.style.backgroundColor = "#ddddff";
		oldSelector = node;
	}
	
	if (selection != null) {
		var selNode = selectElement(selection);
		
		if (selNode != null) {
			selNode.style.display = "";
			oldSelection = selNode;
		}
	}
}

function showPicture(selection,showMe) {
	var selNode = selectElement(selection);
	var mode;
	
	if (oldPicture != null) {
		oldPicture.style.display = "none";
		oldPicture = null;
	}
	
	if (selNode != null) {
		if (showMe)
			mode = "";
		else
			mode = "none";
			
		selNode.style.display = mode;
	}
}

function showInitial() {
	var node = selectElement("list_1");
	node = node.childNodes(0);
	showBoat(node,'boat1');
}

function parsePrice(name) {
	var price = 0;
	
	if (name != null) {
		price = name.substr(2);
		price = price.replace(/(,|_.*)/g,"");
		price = price * 1;
	}
	
	return price;
}

function showValues(value) {
	var min = value.substr(0,2) * 1000;
	var max = value.substr(3,2) * 1000;
	var elements = selectElement("boats").childNodes;
	var shown = 0;
	
	for (var i = 0; i < elements.length; i++) {
		var price = parsePrice(elements[i].name);
		
		if (price >= min && (price <= max || max == 0)) {
			elements[i].className = "shown";
			shown++;
		}
		else {
			elements[i].className = "hidden";
		}
	}
	
	if (shown == 0) {
	}
}

function showSelection() {
	var boat = location.search;
	var elem = null;
	boat = boat.replace('?boat=', '');
	boat = boat.replace(/&amp;.*/,'');
	
	if (boat.length > 1) {
		elem = selectElement(boat);
				
		if (elem != null) {
			elem.style.display = "";
		}	
		//document.getElementById(boat).style.display = "";
	}
}
