// Ajax to the querys fetching items for select boxes
function getOptions(parent,parentId,superParentId)
{
	if (parent == "Countries")
		DWREngine._execute(_cfscriptLocation, null, 'GetDenominations', superParentId, showDenominationOptions);
	
	DWREngine._execute(_cfscriptLocation, null, 'GetPublications', parentId, superParentId, showPublicationOptions);
}

// Displays the denomination items returned from the Ajax querys
function showDenominationOptions(resultQuery)
{
	//first, clear out the existing options
	var mySelect = document.getElementById('denomination');
	mySelect.options.length = 0;
	//now populate the new options
	var j = 0;
	mySelect.options[j] = new Option('All',0);
	j++;
	for (var i=0; i < resultQuery.length; i++ ) {
		mySelect.options[j] = new Option(resultQuery[i].DENOMINATIONDESCRIPTION,resultQuery[i].DENOMINATIONDESCRIPTION);
		j++;
	}
	
	mySelect.selectedIndex = 0;
}

// Displays the publication items returned from the Ajax querys
function showPublicationOptions(resultQuery)
{
	//first, clear out the existing options
	var mySelect = document.getElementById('publication');
	mySelect.options.length = 0;
	
	//now populate the new options
	var j = 0;
	mySelect.options[j] = new Option('All',0);
	j++;
	
	for (var i=0; i < resultQuery.length; i++ )
	{
		mySelect.options[j] = new Option(resultQuery[i].PUBLICATIONNAME,resultQuery[i].PUBLICATIONID);
		j++;
	}
	
	mySelect.selectedIndex = 0;
}

// Displays the correct text in the select menus
function GetOptionsText (select)
{
	var options = new Array();
	var j = 0;
	
	for (var i=0;i<select.length;i++)
	{
		if (select.options[i].selected == true)
		{
			options[j] = select.options[i].text;
			j++;
		}
	}			
		
	return options;
}

// Grabs the filter box on submit
function GetFilters (form)
{
	var filters = document.getElementById("filtersBox");
	var filterString = document.getElementById("filterString");
	
	filterString.value = filters.innerHTML;
	
	filterString.value;
	
	form.submit();
}

// Adds filter from the selected items
function AddFilter ()
{
	var output = "";
	var filters = document.getElementById("filtersBox");
	var filterCount = document.getElementById("filterCount").value;
	var filterCountInput = document.getElementById("filterCount");
	var filterText = "filter" + filterCount;
	var countrySelect = document.getElementById("country");
	var denominationSelect = document.getElementById("denomination");
	var publicationsSelect = document.getElementById("publication");		
	var countries = new Array();
	var denominations = new Array();
	var publications = new Array();
	
	countries = GetOptionsText(countrySelect);
	denominations = GetOptionsText(denominationSelect);
	publications = GetOptionsText(publicationsSelect);
	
	output = filters.innerHTML;
	
	if (output.match("None selected."))
		output = "";
	
	output += "<span id=" + filterText + ">";
	
	// Format country output
	if (countries.length == 1 || countries[1] == "All")
		output += "Country: ";
	else 
		output += "Countries: ";	
		
	if (countries[1] == "All")
		output += "All";
	else
		output += countries.toString();
		
	// Format denomination output
	if (denominations.length == 1 || denominations[1] == "All")
		output += "; Denomination: ";
	else 
		output += "; Denominations: ";	
		
	if (denominations[1] == "All")
		output += "All";
	else
		output += denominations.toString();
		
	// Format publications output
	if (publications.length == 1 || publications[1] == "All")
		output += "; Publication: ";
	else 
		output += "; Publications: ";	
		
	if (publications[1] == "All")
		output += "All";
	else
		output += publications.toString();
		
	output += ";  <a href='JavaScript:RemoveFilter(&quot;" + filterText + "&quot;);'><img class='noBorder' src='_images/x.jpg' alt='(x)' /></a><br/>";
	
	output += "<input type='hidden' name='Country" + filterCount + "' id='Country" + filterCount + "' value='" + countrySelect.value + "' >";
	
	output += "<input type='hidden' name='Denomination" + filterCount + "' id='Denomination" + filterCount + "' value='" + denominationSelect.value + "' >";
	
	output += "<input type='hidden' name='Publication" + filterCount + "' id='Publication" + filterCount + "' value='" + publicationsSelect.value + "' >";
	
	output += "</span>";
	
	filters.innerHTML = output;

	filterCount++;
	
	filterCountInput.value = filterCount;
	
}

// Removes filter
function RemoveFilter (filterId)
{
	var filterSpan = document.getElementById(filterId);
	var filters = document.getElementById("filtersBox");
	var output = "";
	
	filterSpan.parentNode.removeChild(filterSpan);
	
	output = filters.innerHTML;
	
	if (!output.match("Count"))
		filters.innerHTML = "None selected.";	
}

function Pagination (pageLink)
{
	var pageInput = document.getElementById("page");
	
	pageInput.value = pageLink;
	
	GetFilters(document.forms[0]);
	
	document.forms[0].submit();
}

function Pagination (pageLink, sort)
{
	var pageInput = document.getElementById("page");
	var sortInput = document.getElementById("SortNew");
	
	pageInput.value = pageLink;
	
	if (typeof(sort) !== 'undefined')
		sortInput.value = sort;
		
	GetFilters(document.forms[0]);
	
	document.forms[0].submit();
}
