
function VarietyPerformance_ShowTrialReport(trialID)
{
	// document.all.SelectedTrialID.value = trialID;
	// __doPostBack('TrialSearchUC$TrialMapUC$HiddenShowReportLink','');	
	
	// set the value for NewZoomValue...
	$('#SelectedTrialID')[0].value = trialID;

	// call the hidden trial link click event.
	$('#trialLink')[0].click();	
	
}

// Used to selectively stop an AJAX request from occurring
// from the export trials button when we need to push
// to a save dialog - RadAjaxManager does not work properly when
// an AJAX request is made for a file 
function managerRequestStarted(ajaxManager, eventArgs)
{
	var i = $('#NumberOfCheckedTrials').text();
	var j = $('#UserChoosesMeasurements').text();
	
	if((eventArgs.EventTarget == "btnExportTrials") && parseInt(i) > 0 && parseInt(j) > 0)
	{
		eventArgs.EnableAjax = false;
	}
	
	// disable ajax if exporting measurements from "select measurements" control
	// (where validation has passed i.e. at least one measurement check box is checked)
	// (this relies on the "select measurements" control to have id=SelectMeasurements1)
	if (eventArgs.EventTarget == "SelectMeasurements1:btnGo" && $("#reportBody :checked").length > 0)
	{
		eventArgs.EnableAjax = false;
	}
	
}
		


function InitialiseHeaderEvents()
{
	try
	{
		$("h2:not(#trialSearchResultsHeader)").click(function()
		{
			expandoCollapsoSection(this);
								
			// Hide the error labels
			/* $("#noTrialReportLabel").hide();
			$("#noTrialSearchResultsLabel").hide();*/
									
		});
						
		// collapse reports on expanding any other section
		// Also, hide the trial search instructive text and
		// "No performance report" label
		// as soon as the trial search header is clicked
		
		$("#trialSearchHeader").click(function()
		{
			$("#trialSearchLabel").hide();
			$("#noPerformanceReportLabel").hide();
			CollapseOtherSection('#trialSearchResultsHeader','#trialSearchResultsContainer', '#trialSearchHeader');
			CollapseOtherSection('#performanceReportHeader','#PerformanceReport', '#trialSearchHeader');
			CollapseOtherSection('#trialReportHeader', '#TrialReport');
		});
						
		/*$("#trialSearchResultsHeader").click(function()
		{
			CollapseOtherSection('#trialReportHeader', '#TrialReport', '#trialSearchResultsHeader');
						
		});*/
						
		$("#availableYieldReportsHeader").click(function()
		{
			CollapseOtherSection('#performanceReportHeader', '#PerformanceReport', '#availableYieldReportsHeader');
		});
						
		$("#advancedTrialSearchHeader").click(function()
		{
			$(this).toggleClass("contracted");
			$(this).next().slideToggle("normal");		
		});	
						
		// Disable appropriate year dropdown when
		// radio buttons are clicked
		$("#radioSingleYear").click(function()
		{
			SetDisabledStateOfItems('ddlYearStart', 'ddlYearEnd', 'ddlYear', true);
												
		});
						
		$("#radioYearRange").click(function()
		{
			SetDisabledStateOfItems('ddlYearStart', 'ddlYearEnd', 'ddlYear', false);
		});
							
							
		/*$("#mapViewHeader").click(function()
		{
			setUpSliderZoom();
		});*/
						
						
	}
					
	catch (e) 
	{
	}
					
}
				
function expandoCollapsoSection(obj)
{
	$(obj).toggleClass("contracted");
	$(obj).next().slideToggle("normal");
							
	// Hide the error labels
	$("#noTrialReportLabel").hide();
	$("#noTrialSearchResultsLabel").hide();
}
			
				
function setUpSliderZoom()
{
	//alert('slider zoom starting');
	var zoomValue = $('#NewZoomValue')[0].value;
							
	// set the default to 24, if there isn't a value.							
	if (zoomValue == null || zoomValue == '')
	{
		zoomValue = 24;
	}
							
	setUpJQuerySlider(zoomValue);
	//alert('slider zoom finishing');
				
}

function SetPageDisplay()
{
	try
	{
		$("#navbar").show();
		$("#intro").show();
		$("#introText").show();
		$("#introHeading").show();
		//$("#searchHeader").show();
		//$("#searchContainer").show();
					
		setStateOfAddAndRemoveButtons();
	}

	catch (e) {}
}
				
function setStateOfAddAndRemoveButtons()
{
	var listRemaining = $('#listRemainingVarieties');
	var listFiltered = $('#listFilteredVarieties');
					
	if(listRemaining.attr("selectedIndex") == -1)
	{
		$('#btnAddVarietiesToFilter').attr("disabled", true);
	}
					
	else
	{
		$('#btnAddVarietiesToFilter').attr("disabled", false);
	}
					
	if(listFiltered.attr("selectedIndex") == -1)
	{
		$('#btnRemoveVarietiesFromFilter').attr("disabled", true);
	}
					
	else
	{
		$('#btnRemoveVarietiesFromFilter').attr("disabled", false);
	}
					
}
					
				
function SetDisabledStateOfItems(rangeItem1, rangeItem2, singleItem, wantRangeItemsDisabled)
{
	try 
	{
						
		RangeItem1 = document.getElementById(rangeItem1);
		RangeItem2 = document.getElementById(rangeItem2);
		SingleItem = document.getElementById(singleItem); 
						
		if(wantRangeItemsDisabled)
		{
			RangeItem1.selectedIndex = 0;
			RangeItem2.selectedIndex = 0;
		}
						
		else
		{
			SingleItem.selectedIndex = 0;
		}
						
		RangeItem1.disabled = wantRangeItemsDisabled;
		RangeItem2.disabled = wantRangeItemsDisabled;
		SingleItem.disabled = !wantRangeItemsDisabled;
	}
					
	catch(e)
	{
	}
} 
				
// only collapses a section if it is currently 
// visible on the page and expanded, and the section
// of the header intiating the function call has just been
// expanded
function CollapseOtherSection(otherHeader, otherContainer, initiatingHeader)
{
	if(!$(otherHeader).is('.contracted') && $(otherHeader).css("display") == 'block' && !$(initiatingHeader).is('.contracted'))
	{
		collapseSection(otherHeader, otherContainer);
	}
}
				
function DisplaySectionsForPerformanceReport(doesReportExist)
{
	// If report exists, show the performance report header,
	// hide the "No report available" label, and show the
	// Trial Search header/label.					
							
	collapseSection('#trialSearchHeader', '#trialSearchContainer', false);	
	collapseSection('#advancedTrialSearchHeader', '#advancedTrialSearchContainer', false);	
									
	if(doesReportExist >= 1)
	{
		if(doesReportExist == 1)
		{
			hideSection('#availableYieldReportsHeader', '#availableYieldReportsContainer');
		}
					
		// "view" button clicked
		else
		{
			collapseSection('#availableYieldReportsHeader', '#availableYieldReportsContainer');
		}
						
		expandSection('#performanceReportHeader', '#PerformanceReport');
		$("#noPerformanceReportLabel").hide();
		$("#trialSearchLabel").show();
	}
					
	// otherwise, show the "No report available" label;
	// hide the trial search. 
	else 
	{	
		if(doesReportExist == -1)
		{
			$("#noPerformanceReportLabel").show();
			// hide available yeild reports section
			hideSection('#availableYieldReportsHeader', '#availableYieldReportsContainer', false);
		}
					
		else
		{
			// show available yield reports section, hide the 'no long term yield report' 
			// section if it is currently visible
			expandSection('#availableYieldReportsHeader', '#availableYieldReportsContainer');
			hideSection('#trialSearchHeader', '#trialSearchContainer', false);
			$("#noPerformanceReportLabel").hide();
		}
						
		hideSection('#performanceReportHeader','#PerformanceReport', true);
		$("#trialSearchLabel").hide();
	}
								
					
	// hide Trial Search Results/Trial Report
	hideSection('#trialReportHeader','#TrialReport', true);
	hideSection('#trialSearchResultsHeader','#trialSearchResultsContainer', false);
					
	// hide leftover labels from a Trial (Report) search if still visible
	$("#noTrialSearchResultsLabel").hide();
	$("#noTrialReportLabel").hide();
				
	//collapseInnerHeaders();
}
				
function DisplaySectionsForYieldResults()
{
	// display the 'Available Long Term Yield Reports' section and
	// hide the performance report section if it's visible
					
	// hide Trial Search Results/Trial Report
	hideSection('#trialReportHeader','#TrialReport', true);
	hideSection('#trialSearchResultsHeader','#trialSearchResultsContainer', false);
	hideSection('#trialSearchHeader', '#trialSearchContainer', false);	
					
	// Hide Performance Report
	hideSection('#performanceReportHeader', '#PerformanceReport', true);
				
	// Display (and expand if needed) yield results resction
	expandSection('#availableYieldReportsHeader', '#availableYieldReportsContainer');
				
}
				
function hideSection(sectionHeader, sectionContainer, isAReportSection)
{
					
	$(sectionHeader).hide();
	$(sectionHeader).addClass('contracted');
	$(sectionContainer).hide();
					
	// if a report section, reset the URL of the iframe so no reloading takes place
	if(isAReportSection)
	{
		$(sectionContainer + "Viewer").attr("src","");
	}
				
}
				
function DisplaySectionsForTrialSearchResults(resultsFound,wantMap)
{	
	// remove noBorder class if the mapContainer has it - e.g. after
	// "show location" button is clicked
	if($("#mapContainer").is('.noBorder'))
	{
		$("#mapContainer").removeClass("noBorder");	
	}
					
	// hide the 'available long term yield reports' section if visible
	hideSection('#availableYieldReportsHeader', '#availableYieldReportsContainer');
										
	// remove the performance report from view.
	hideSection('#performanceReportHeader','#PerformanceReport', true);
					
	// remove the Trial report from view.
	hideSection('#trialReportHeader','#TrialReport', true);
						
	if(resultsFound >= 0)
	{
		// collapse the trial search panel/advanced panel
		collapseSection('#trialSearchHeader', '#trialSearchContainer', false);
		collapseSection('#advancedTrialSearchHeader', '#advancedTrialSearchContainer', false);

		if(resultsFound == 1)
		{
			//  expand the trial search results panel.
			expandSection('#trialSearchResultsHeader', '#trialSearchResultsContainer');
			$("#noTrialSearchResultsLabel").hide();
		}
						
		// hide trial search results panel & show msg if no results.
		else if(resultsFound == 0)
		{
			hideSection("#trialSearchResultsHeader","#trialSearchResultsContainer", false);
			$("#noTrialSearchResultsLabel").show();
		}
	}
					
	// hide label & show basic search if validation failed on basic search parameter.
	/*else
	{					
		hideSection("#trialSearchResultsHeader","#trialSearchResultsContainer", false);
		$("#noTrialSearchResultsLabel").hide();
						
		expandSection('#trialSearchHeader', '#trialSearchContainer', false);
						
		// show advanced search if validation failed on advanced search parameters.
		if(resultsFound == -2)
		{
			expandSection('#advancedTrialSearchHeader', '#advancedTrialSearchContainer', false);
		}
							
	}*/
										
	// make the map view visible, if
	// map view is selected.
	if(wantMap == 1)
	{
		expandSection("#mapViewHeader", "#mapContainer");
		setUpSliderZoom();													
	}
						
	else if (wantMap == 0)
	{
		hideSection('#mapViewHeader', '#mapContainer', false);
	}
					
	// when user has to choose a town location from trial map UC
	else if (wantMap == -1)
	{
		expandSection("#mapViewHeader", "#mapContainer");
		$("#mapContainer").addClass("noBorder");
		$("#mapViewHeader").hide();
	}
										
	// hack as when AJAX applied to produce trial search results onclick
	// event no longer fired
	$('#trialSearchResultsHeader').unbind('click');
	$('#trialSearchResultsHeader').click(function()
	{
		expandoCollapsoSection('#trialSearchResultsHeader');
		CollapseOtherSection('#trialReportHeader', '#TrialReport', '#trialSearchResultsHeader');
	});
					
	$("#mapViewHeader").unbind('click');
	$("#mapViewHeader").click(function()
	{
		expandoCollapsoSection('#mapViewHeader');
		setUpSliderZoom();	
	});	
					
	// update the hidden field containing the number of checked trials
	// whenever a checkbox is selected or deselected
	$("#trialSearchResults :checkbox").click(function()
	{
		updateNumberOfCheckedTrials(this);

	});
													
}
				
function updateNumberOfCheckedTrials(chb)
{
	if($(chb).attr("checked"))
	{
		//alert('adding');
		($('#NumberOfCheckedTrials')).text(parseInt($('#NumberOfCheckedTrials').text()) + 1);
	}
						
	else
	{
		//alert('subtracting');
		($('#NumberOfCheckedTrials')).text(parseInt($('#NumberOfCheckedTrials').text()) - 1);
	}
}
		
function slider_changed(e, ui)
{
	// debug display ui.value...
	 //alert( "new Value: " + ui.value ); 		
					
	// set the value for NewZoomValue...
	$('#NewZoomValue')[0].value = ui.value;
					
	// alert($('#NewZoomValue')[0].value);
	// call the hidden map link click event.
	$('#mapLink')[0].click();														
}
				
function setUpJQuerySlider(currentZoomValue)
{
	//alert('starting setUpJQuerySlider, currentZoomValue is: ' + currentZoomValue);
	try 
	{ 				
		// set the default to 24, if there isn't a value.							
		var zoomValue = 24;
		if (currentZoomValue != null)
		{
			// alert('setting zoomValue to currentZoomValue of: ' + currentZoomValue);
			zoomValue = currentZoomValue;
		}
											
		// alert("running setUpJQuerySlider()");
		if ($('#example') != null)
		{
			//alert("calling: $('#example').slider()");												
			$('#example').slider({
				startValue: zoomValue,
				minValue: "1",
				maxValue: "34",
				change: slider_changed								
			});	
		}
	}
	catch (e) {}
}			

function DisplaySectionsForTrialReport(doesReportExist)
{
									
	// collapse the trial search panel
	collapseSection('#trialSearchHeader', '#trialSearchContainer');
	collapseSection('#advancedTrialSearchHeader', '#advancedTrialSearchContainer', false);	

	// collapse the trial search results panel.
	collapseSection('#trialSearchResultsHeader', '#trialSearchResultsContainer');
				
	if(doesReportExist == 1)
	{
		// expand the trial report panel.
		expandSection('#trialReportHeader','#TrialReport');
		$("#noTrialReportLabel").hide();
	}
					
	// no results - show message
	else
	{
		$("#noTrialReportLabel").show();
	}
										
	collapseInnerHeaders();
		
}
				
function collapseSection(sectionHeading, sectionContainer)
{
	$(sectionHeading).show();
					
	if(!$(sectionHeading).is('.contracted'))
	{
		$(sectionHeading).addClass("contracted");
		$(sectionContainer).slideToggle("normal");
	}
}
				
function expandSection(sectionHeading, sectionContainer)
{
	$(sectionHeading).show();
				
	// expand the section if it's contracted to begin with
	if($(sectionHeading).is('.contracted'))
	{
		$(sectionHeading).removeClass("contracted");
		$(sectionContainer).hide();
		$(sectionContainer).slideDown("normal");
		$(sectionContainer).show();
	}
}
				
function collapseInnerHeaders()
{
	// make advanced trial search /map view collapsed - on
	// page postback/refresh the collapsible containers will be hidden
	$("#advancedTrialSearchHeader").addClass("contracted"); 
	$("#mapViewHeader").addClass("contracted");
}				
				
// Retrieves a certain query string parameter from the URL.
function getUrlParameter( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );

	if( results == null )
	{
		return "";
	}

	else
	{
		return results[1];
	}
}