var pageURLbox = false;
var dirtyFlag = false;

function onPageLoad(current_db) {
	jQuery("#navbar_" + current_db).css("color", "#006fc4");
	jQuery('#datacontent input').change(function() { dirtyFlag = true; });
	jQuery('#datacontent select').change(function() { dirtyFlag = true; });
}

function showURLbox() {
	if (!pageURLbox) {
		jQuery("#pageURLbox").fadeIn();
		updateURLbox();
		pageURLbox = true;
	} else {
		pageURLbox = false;
		hideURLbox();
	}
}

function hideURLbox() {
	pageURLbox = false;
	jQuery("#pageURLbox").fadeOut()
}

function updateURLbox() {
	document.getElementById("pageURL").value = getURL();
}

function getURL() {
	var lochref = location.href;
	lochref = lochref.replace("#mapbox", "");
	if ((lochref.indexOf("?") == -1) || (dirtyFlag)) {
		if (lochref.indexOf("?") > 0) {
			return lochref.substr(0, lochref.indexOf("?")) + "?" + getInputs("_");	
		} else {
			return lochref + "?" + getInputs("_");	
		}
	} else {
		return lochref;
	}
}

function loadData() {
	document.getElementById("loadingMsg").innerHTML = "<center><table><tr><td><img src='img/loading_circle.gif'></td><td>Please wait while we search our database.</td></tr></table></center>";
	jQuery("#loadingMsg").slideDown();
	document.getElementById("_search").value = "Searching...";
	document.getElementById("_search").disabled = true;
	var input = getInputs("_");
	jQuery("#datacontent").slideUp("fast");
	jQuery("#results").load("controller/servlet.php?current_db=" + current_db + "&action=results&key=" + session_key + "&" + input, function() {
		jQuery("#loadingMsg").slideUp("fast", function() {
			document.getElementById("loadingMsg").innerHTML = "";
			updateURLbox();
  	    });
		document.getElementById("_search").value = "Search";
		document.getElementById("_search").disabled = false;
		jQuery("#tools").fadeIn("fast");
	});	
}

function loadQuery(input) {
	var items = input.split("&");
	for (var i=0;i<items.length;i++) {
		var values = items[i].split("=");
		document.getElementById(values[0].substr(1)).value = values[1].replace(/\+/g, " ");
	}
	jQuery("#loadingMsg").slideDown();
	document.getElementById("loadingMsg").innerHTML = "<center><table><tr><td><img src='img/loading_circle.gif'></td><td>Please wait while we search our database.</td></tr></table></center>";
	document.getElementById("_search").value = "Searching...";
	document.getElementById("_search").disabled = true;
	jQuery("#datacontent").slideUp("fast");
	jQuery("#results").load("controller/servlet.php?current_db=" + current_db + "&action=results&key=" + session_key + "&" + input, function() {
		jQuery("#loadingMsg").slideUp("fast", function() {
			document.getElementById("loadingMsg").innerHTML = "";
			updateURLbox();
  	    });
		document.getElementById("_search").value = "Search";
		document.getElementById("_search").disabled = false;
		jQuery("#tools").fadeIn("fast");
	});	
}

function searchAgain() {
	document.getElementById("_pos").value = 0;
	jQuery("#datacontent").slideDown("fast");
	jQuery("#tools").fadeOut("fast");
}

function getInputs(delim) {
	var inputs = new Array();
	jQuery('#datacontent input').each(function(el) {
		inputs.push(delim + this.id + "=" + escape(this.value));
	})
	jQuery('#datacontent select').each(function(el) {
		inputs.push(delim + this.id + "=" + escape(this.value));
	})
	jQuery('.datacontentModule input').each(function(el) {
		inputs.push(delim + this.id + "=" + escape(this.value));
	})
	jQuery('.datacontentModule select').each(function(el) {
		inputs.push(delim + this.id + "=" + escape(this.value));
	})
	return inputs.join("&");
}

function addThis() {
	addthis_url = getURL();
	addthis_title = document.title;
	return addthis_click(this);
}

function nextList() {
	dirtyFlag = true;
	document.getElementById("_pos").value = document.getElementById("_pos").value * 1 + 20;
	loadData();
}

function prevList() {
	dirtyFlag = true;
	document.getElementById("_pos").value = document.getElementById("_pos").value * 1 - 20;
	loadData();
}