function sortNumber(a, b) {
	return a - b;
}

function validateNotEmpty(fieldId, message){
	field = $("#" + fieldId);
	if(field.length == 0){
		return true;
	}
	if(field.val().length == 0){
		alert(message);
		return false;
	}
	return true;
}


function defaultRedirect() {
	categoryId = $("#selCategories option:selected").val();
	if (categoryId == undefined) {
		categoryId = 0;
	}
	agencyId = $("#selAgencies option:selected").val();
	if (agencyId == undefined) {
		agencyId = 0;
	}
	window.location = '/catalog/category/' + categoryId + '/agency/' + agencyId
			+ '/filter//type';
	return false;
}

function catalogSearchRedirect() {
	// Get the selected categories
	categories = $(".cb_category:checked");
	allCategories = $('#AllCategories');
	var categoryList = '';
	var agencyList = '';
	if (!allCategories.is(':checked')) {
		arrCategory = new Array(categories.length);
		for (i = 0; i < categories.length; i++) {
			arrCategory[i] = categories[i].value;
		}
		arrCategory.sort(sortNumber);
		for (i = 0; i < arrCategory.length; i++) {
			if (categoryList.length > 0) {
				categoryList += ",";
			}
			categoryList += arrCategory[i];
		}
	}
	if (categoryList.length == 0) {
		categoryList = 0;
	}

	// Get the selected agencies
	agencies = $(".cb_agency:checked");
	allAgencies = $("#AllAgencies");
	if (!allAgencies.is(':checked')) {
		arrAgency = new Array(agencies.length);
		for (i = 0; i < agencies.length; i++) {
			arrAgency[i] = agencies[i].value;
		}
		arrAgency.sort(sortNumber);
		for (i = 0; i < arrAgency.length; i++) {
			if (agencyList.length > 0) {
				agencyList += ",";
			}
			agencyList += arrAgency[i];
		}
	}
	if (agencyList.length == 0) {
		agencyList = 0;
	}

	searchFilter = $("#search2").val();
	//Replace non-alpha numeric characters
	searchFilter =  searchFilter.replace(/[^a-zA-Z\s0-9]+/g,'');
	typeFilter = '';
	if ($('#XML:checked').val() != null) {
		typeFilter += 'x';
	}
	if ($('#CSV-text:checked').val() != null) {
		typeFilter += 'c';
	}
	if ($('#KML-KMZ:checked').val() != null) {
		typeFilter += 'k';
	}
	if ($('#ESRI:checked').val() != null) {
		typeFilter += 'e';
	}
	if ($('#Other:checked').val() != null) {
		typeFilter += 'o';
	}

	window.location = '/catalog/category/' + categoryList + '/agency/'
			+ agencyList + '/filter/' + searchFilter + '/type/' + typeFilter;
	return false;
}

var rawDisplayed = true;
var toolsDisplayed = true;

function toggleToolsVisible(){
	var toolsTable = $("#tblTools");
	var toolsTBody = $("#hiddenRowsTools");
	//Remove one from the count for the header row
	var totalRows = $(toolsTable).find('tr').length - 1;
	var hiddenRows = 0;
	if(toolsDisplayed){
		toolsTBody.hide();
		hiddenRows = $(toolsTBody).find('tr').length;
	}
	else{
		toolsTBody.show();
	}
	$("#toolsCount").text("[ Showing " + (totalRows - hiddenRows) + " out of " + totalRows + " results ]");
	toolsDisplayed = !toolsDisplayed;
	
}

function toggleRawVisible() {
	var rawTable = $("#tblInstant");
	var rawTBody = $("#hiddenRowsInstant");
	//Remove one from the count for the header row
	var totalRows = $(rawTable).find('tr').length - 1;
	var hiddenRows = 0;
	if(rawDisplayed){
		rawTBody.hide();
		hiddenRows = $(rawTBody).find('tr').length;
	}
	else{
		rawTBody.show();
	}
	$("#instantCount").text("[ Showing " + (totalRows - hiddenRows) + " out of " + totalRows + " results ]");
	rawDisplayed = !rawDisplayed;
	
}

function toggleCollapsible(){
	$("#collapsible_text").toggle();
	if($("#collapsible_label").text() == '(less)'){
		$("#collapsible_label").text('(more)');
	}
	else{
		$("#collapsible_label").text('(less)');
	}
	return false;
}


$(document).ready(function() {
	toggleRawVisible();
	toggleToolsVisible();
	
	toggleCollapsible();
	
	$(".cb_agency").click(function(){
		$("#AllAgencies").removeAttr('checked');
	});
	
	$("#AllAgencies").click(function(){
		$(".cb_agency").removeAttr('checked');
	});
	
	$(".cb_category").click(function(){
		$("#AllCategories").removeAttr('checked');
	});
	
	$("#AllCategories").click(function(){
		$('.cb_category').removeAttr('checked');
	});
});