
function refineSearch()
{
	document.evaluatorForm.action = searchURL;
	update("ResultsPageNumber","1");
	return submitForm();
}


function newSearch()
{
	update("ResultsPageNumber","1");
	changePage(searchURL);
}


function changeView(theSelectBox)
{
	var newView = theSelectBox.options[theSelectBox.selectedIndex].value;
	update("ResultsView",newView);
	return submitForm();
}


function changeResultsPerPage(theSelectBox)
{
	var newResultsPerPage = theSelectBox.options[theSelectBox.selectedIndex].value;
	update("ResultsPerPage",newResultsPerPage);
	// Reset to first page after any results per page change, otherwise the pagination code wouldn't know which page to display
	update("ResultsPageNumber","1");
	return submitForm();
}


function changePageNumber(newPageNumber)
{
	update("ResultsPageNumber",newPageNumber);
	return submitForm();
}


function changeSort(newSortOrder)
{
	// This function can accept a second parameter - a string, that can hold the value 'default to asc', or not. See below.
	var newSortDirection = "dsc";
	if (document.evaluatorForm.ResultsSortDirection)
	{
		if (  (document.evaluatorForm.ResultsSortDirection.value == "dsc")  &&  (document.evaluatorForm.ResultsSortOrder.value == newSortOrder)  )
			newSortDirection = "asc";
	}
	
	// Fields that are better sorted in ascending order by default may pass in 'default to asc' as the second parameter
	if ( (changeSort.arguments.length > 1) && (changeSort.arguments[1] == 'default to asc') )
	{
		if (document.evaluatorForm.ResultsSortOrder.value != newSortOrder)
			newSortDirection = "asc";
	}

	update("ResultsSortOrder",newSortOrder);
	update("ResultsSortDirection",newSortDirection);
	// Default to first page after any sort parameter change
	update("ResultsPageNumber","1");
	return submitForm();
}


function changeDisplay(newDisplayState)
{
	// Switch the screen to and from a split screen view
	update("ResultsDisplay",newDisplayState);
	// Default to first page
	update("ResultsPageNumber","1");
	return submitForm();
}


function changePage(destinationURL)
{
	var finalDestinationURL = prepareForNetBenefits(destinationURL);
	location.href = finalDestinationURL;
	return false;
}


function prepareForNetBenefits(theURL)
{
	// Pass required name-value pairs in NetBenefits
	var updatedURL = theURL;
	
	// In NetBenefits, add their state parameters to the URL	
	if (window.application_context) if (application_context == 'netbenefits')
	{
		updatedURL = checkFieldPresent("fePlanId", updatedURL);
		updatedURL = checkFieldPresent("feHostRealm", updatedURL);					
	}
	return updatedURL;
}


function checkFieldPresent(theField, theURL)
{
	// Find the corresponding form field
	var theFormField = eval("document.evaluatorForm." + theField);
	var returnURL = theURL;
	
	// If the form field is present but not yet in the URL
	if ( (theFormField != undefined) && (theURL.indexOf(theField) == -1) )
	{
		// First choose the delimiter to precede it
		var theDelimiter = (returnURL.indexOf("?") == -1) ? "?" : "&";

		// Then append it to the URL
		returnURL = theURL + theDelimiter + theField + "=" + escape(theFormField.value);
	}
	return returnURL;
}


function displayInLargePopup(theURL)
{
	var theParameters = "";
	window.open(theURL, 'FidelityLargePopup', theParameters);
}


function update(theParam, newValue)
{
	// Update a hidden form field with a changed search parameter
	var theField = eval("document.evaluatorForm."+theParam);
	if (theField) theField.value = newValue;
	
}


function submitForm()
{
	updateResultsRequestedFundIndex();
	document.evaluatorForm.submit();
	return false;
}


function compareFunds()
{
	var compareSymbols = "";
	var numChecked = 0;

	with (document.evaluatorForm)
	{
		for (a=0; a<elements.length; a++)
		{
			if (  (elements[a].name=="compare")  &&  ((elements[a].checked)  || (elements[a].type=='hidden'))  )
			{
				numChecked++;
				compareSymbols += escape(elements[a].value) + compareURL_SymbolDelimiter;
			}
		}
	}
	
	if (numChecked > 5)
	{
		var error_text = "Only five funds may be compared at one time. Currently there are " + numChecked + " selected for comparison.";
		alert (error_text);
		return false;
	}

	if (numChecked == 0)
	{
		changePage(compareURL_LandingPage);
		return false;
	}

	changePage(compareURL_Servername + compareURL_StartPath + compareSymbols + compareURL_EndPath);
	return false;
}


function compareFundsForNetBenefits()
{
	var finalURL = compareURL_NetBenefits;
	var numChecked = 0;

	with (document.evaluatorForm)
	{
		for (a=0; a<elements.length; a++)
		{
			if (  (elements[a].name=="compare")  &&  ((elements[a].checked)  || (elements[a].type=='hidden'))  )
			{
				numChecked++;
				var theDelimiter = (finalURL.indexOf("?") == -1) ? "?" : "&";
				finalURL += theDelimiter + "fund" + "=" + escape(elements[a].value);
			}
		}
	}
	
	if (numChecked > 5)
	{
		alert("The Compare feature can only compare up to 5 funds. Please uncheck " +(numChecked-5)+ " funds and then try again.");
		return false;
	}

	if (numChecked == 0)
	{
		alert("Please check at least one box to compare investments.");
		return false;
	}

	changePage(finalURL);
	return false;
}


function addToWatchList()
{
	// This function is almost identical to the Compare function above, but for Watch List instead
	var WatchListURL_Symbols = "";
	var numChecked = 0;

	with (document.evaluatorForm)
	{
		for (a=0; a<elements.length; a++)
		{
			if (  (elements[a].name=="compare")  &&  (elements[a].checked)  )
			{
				numChecked++;
				WatchListURL_Symbols += escape(elements[a].value) + WatchListURL_Delimiter;
			}
		}
	}
	
	if (numChecked > 30)
	{
		alert("The Add To Watch List feature can only compare up to 30 funds. Please uncheck " +(numChecked-30)+ " funds and then try again.");
		return false;
	}

	if (numChecked < 1)
	{
		alert("Please Select One or More Funds to Add to Watch List.");
		return false;
	}

	openPopup (WatchListURL_ServerNameAndPath + WatchListURL_Symbols);
	return false;
}


function openPopup (theURL)
{
	// Used mainly by Retail. Popup width and height are optional second and third parameters to this function
	var theHeight = 455;
	var theWidth = 250;
	var theName = 'quoteWindow';
	if (openPopup.arguments.length == 3)
	{
		theWidth = openPopup.arguments[1];
		theHeight = openPopup.arguments[2];
		theName = 'glossaryWindow';
	}
	var finalURL = prepareForNetBenefits(theURL);
	window.open (finalURL, theName,'width='+theWidth+',height='+theHeight+',left=480,top=190,location=no,resizable=yes,menubar=no,status=no,toolbar=no,scrollbars=yes');
	return false;
}


function openLargePopup (theURL)
{
	// Used mainly by NetBenefits
	var finalURL = prepareForNetBenefits(theURL);
	window.open (finalURL, 'FidelityLargePopup','width=600,height=520,left=100,top=100,location=no,resizable=yes,menubar=no,status=no,toolbar=no,scrollbars=yes');
	return false;
}


function updateResultsRequestedFundIndex()
{
	// This is just for Retail - the NB version never has a split screen and thus doesn't need this information
	if (window.application_context) if (application_context == 'netbenefits') return;

	// The fidelity_funds_found and all_funds_found	are defined in the main XSL file
	var results_per_page = parseInt(document.evaluatorForm.ResultsPerPage.value);
	var page_being_requested = parseInt(document.evaluatorForm.ResultsPageNumber.value);
	update("ResultsRequestedFundIndex", getIndexOfFundToDisplay(fidelity_funds_found, all_funds_found, results_per_page, page_being_requested));
}


function getIndexOfFundToDisplay (fidelity_funds_found, all_funds_found, results_per_page, page_being_requested)
{
	var number_of_fidelity_funds_on_final_fidelity_page = ((fidelity_funds_found-1) % (results_per_page / 2))+1;
	var fidelity_end_page = Math.ceil((fidelity_funds_found / (results_per_page / 2)));
	var pages_to_count = page_being_requested - 1;
	var number_of_funds = 0;
	var number_of_50_50_split_screens = (pages_to_count >= fidelity_end_page) ? (fidelity_end_page - 1) : pages_to_count;
	if (number_of_50_50_split_screens < 0) number_of_50_50_split_screens = 0;

	// Do more complex math for searches that results in a split screen
	if (fidelity_end_page > 0)
	{
		// Count funds on all the split screen pages
		if (pages_to_count > 0)
			number_of_funds += (results_per_page / 2) * number_of_50_50_split_screens;

		// Add funds on the last split page, where the Fidelity funds end
		if (pages_to_count >= fidelity_end_page)
			number_of_funds += (results_per_page - number_of_fidelity_funds_on_final_fidelity_page);
	}
	
	// Add funds on remaining pages
	if (pages_to_count > fidelity_end_page)
		number_of_funds += (results_per_page * (pages_to_count - fidelity_end_page));

	// Add one to the count of funds on previous pages, to be the index to the first fund on the requested page
	return (number_of_funds+1);
}

function checkBasicSearchForm(theForm)
{
	// Check the percent drop-down was not chosen on its own
	if (theForm.BasicFundPerformance_Percent.selectedIndex != 0)  if  (theForm.BasicFundPerformance_Period.selectedIndex == 0)
	{
		alert("Please select a performance period.");
		theForm.BasicFundPerformance_Period.focus();
		return false;
	}

	if (theForm.BasicFundPerformance_Percent.selectedIndex == 0)  if  (theForm.BasicFundPerformance_Period.selectedIndex != 0)
	{
		alert("Please select a performance percentage.");
		theForm.BasicFundPerformance_Period.focus();
		return false;
	}
}

function checkFlash()
{
	if (parseInt(flash.version) < 9)
	{
		document.getElementById('flash').style.display = 'none';
		document.getElementById('nonflash').style.display = 'block';
	}
}

function leaveFidelity(url)
{
	var message ="You are about to leave Fidelity.com for a site that is unaffiliated with Fidelity. Fidelity has not been involved in the preparation of the content supplied at the unaffiliated site and does not guarantee or assume any responsibility for its content.";
	if (confirm(message))
	{
		top.location.href = url;
	}
}

function findFunds()
{
	if (document.quick_search_form.keyword.value == "")
	{
		alert("Please Enter Search Terms");
		document.quick_search_form.keyword.focus();
		return false;
	}else
	{
		document.quick_search_form.MFff.value= document.quick_search_form.keyword.value;
		return true;
	}
}

function clearDefault(el) 
{
	if (el.defaultValue==el.value) el.value = ""
}