function ImageSwitch(image_id, src) {
	document.getElementById(image_id).src = src;
}
function ImageSwitchDirect(image, src) {
	image.src = src;
}
function BalanceHeight(ids, super_min) {
	
	// First pass: ensure that we're dealing with the “natural” height of the element. This really doesn't matter unless we're recalculating the height after the page has loaded.
	for (var i = 0; i < ids.length; i++)
	{
		if(document.getElementById(ids[i]))
		{
			document.getElementById(ids[i]).style.height = "auto";
		}
	}
	
	// Second pass: find the tallest box.
	var height = 0;
	for (var i = 0; i < ids.length; i++)
	{
		if(document.getElementById(ids[i]))
		{
			height = Math.max(document.getElementById(ids[i]).clientHeight, height);
		}
	}
	
	// Third pass: set all heights to that of the tallest box.
	for (var i = 0; i < ids.length; i++)
	{
		if(document.getElementById(ids[i]))
		{
			document.getElementById(ids[i]).style.height = height + "px";
		}
	}
}

function ChangeManager(handle) {
	
	// First, let's hide all of the managers that we don't want to see and show that manager that we do want to see.
	var children = document.getElementById("manager_content").childNodes;
	for (var i = 0; i < children.length; i++) {
		if (children[i].nodeType == 1) { // Only triggers if it's an element node.
			children[i].style.display = (children[i].id == "manager_" + handle) ? "block" : "none";
		}
	}
	
	// Before we finish, we need to rebalance the heights.
	BalanceHeight(["body1", "body2"]);
}

function ToggleDescription(image, row_id) {
	var row = document.getElementById(row_id);
	row.style.display = row.style.display == 'none' ? '' : 'none';
	image.src = row.style.display == 'none' ? '/images/common/tree_closed.png' : '/images/common/tree_open.png';
	
	// We need to rebalance the height of the sides since we're changing the height of one.
	BalanceHeight(['body1', 'body2']);
}
function ActivateSolutions(solution_id, question) {
	var solutions = document.getElementById("solutions_list").childNodes;
	for (var i = 0; i < solutions.length; i++) {
		if (solutions[i].nodeType == 1) {
			solutions[i].className = "inactive";
		}
	}
	document.getElementById(solution_id).className = "solutions";
	
	var active_question = document.getElementById("active_question");
	active_question.removeChild(active_question.firstChild);
	
	var title = question.innerHTML.truncate(48);
	active_question.appendChild(document.createTextNode(title));
}
function ActivateQuestions(question_id) {
	var questions = document.getElementById("questions_list").childNodes;
	for (var i = 0; i < questions.length; i++) {
		if (questions[i].nodeType == 1) {
			questions[i].className = "inactive";
		}
	}
	document.getElementById("questions_" + question_id).className = "questions";
	
	var categories = document.getElementById("categories_list").childNodes;
	var element_index = 2;
	for (var i = 0; i < categories.length; i++) {
		if (categories[i].nodeType == 1) {
			if (categories[i].id == question_id) {
				categories[i].className = "faq_category1";
				categories[i].setAttribute("src", "/images/contact/faq/" + categories[i].id + "_active.png");
			}
			else {
				categories[i].className = "faq_category" + element_index;
				categories[i].setAttribute("src", "/images/contact/faq/" + categories[i].id + "_inactive.png");
				element_index++;
			}
		}
	}
}

// Useful string functions
String.prototype.truncate = function (length) {
	return this.length > length + 1 ? this.substr(0, length).trim() + "…" : this;
}
String.prototype.trim = function () {
	return this.replace(/^\s*(\S*)\s*$/, "$1");
}