BWPricesHomepage = {
	
	bwiIndex: function(child, width, height) {
		PM.global.newGraph(child, '/prices/index/prices-graph', {createGraphDIV: true, 
			width: width + 'px', height: height + 'px'});
	},
	
	bwiIndexWithTimeSpan: function(child, width, height, span) {
		PM.global.newGraph($(child).update(''), '/prices/index/prices-graph?span=' + span, {createGraphDIV: true, 
			width: width + 'px', height: height + 'px'});
	},
	
	changeIndex: function(index) {
		
		// Hide summaries
		$('bwi_summary').hide();
		$('bw380_summary').hide();
		$('bw180_summary').hide();
		$('bwdi_summary').hide();
		$('wti_summary').hide();
		
		// Set all tabs to not selected.
		$('bwi_tab').className = "tabs2010";
		$('bw380_tab').className = "tabs2010";
		$('bw180_tab').className = "tabs2010";
		$('bwdi_tab').className = "tabs2010";
		$('wti_tab').className = "tabs2010";
		
		// Check which index we are on and set it as selected.
		$(index+'_tab').className = "tabs2010_selected";
		
		// Bring current index graph to the front.
		$('homepage_graph_index_bwi').style.zIndex = -1;
		$('homepage_graph_index_bw380').style.zIndex = -1;
		$('homepage_graph_index_bw180').style.zIndex = -1;
		$('homepage_graph_index_bwdi').style.zIndex = -1;
		$('homepage_graph_index_wti').style.zIndex = -1;
		$('homepage_graph_index_'+index).style.zIndex = 0;
					
		// Change the summary at the top of the graph.
		$(index+'_summary').show();
		
	},
	
	// Clicking a radio button to select which grade we are showing in the biggest price movements table.
	changeLargestGrade: function(grade) {
		
		// Set all tabs to not selected.
		$('heatmap_ifo380_tab').className = "tabs2010";
		$('heatmap_ifo180_tab').className = "tabs2010";
		$('heatmap_mdo_tab').className = "tabs2010";
		$('heatmap_mgo_tab').className = "tabs2010";
		
		// Check which index we are on and set it as selected.
		$('heatmap_'+grade+'_tab').className = "tabs2010_selected";
		
		// Hide all tables and heatmaps, then show the one for what grade we've selected.
		$('prices_2010_home_largestmovements_ifo380').hide();
		$('prices_2010_home_largestmovements_ifo180').hide();
		$('prices_2010_home_largestmovements_mdo').hide();
		$('prices_2010_home_largestmovements_mgo').hide();			
		
		$('prices_2010_home_largestmovements_'+grade).show();
		
	},
	
	changeMarketTab: function(market_tab) {
		
		// Hide all the market areas.
		$('prices_2010_markets_news').hide();
		$('prices_2010_markets_external').hide();
		$('prices_2010_markets_commentary').hide();		
				
		// Set all tabs to not selected.
		$('market_news_tab').className = "tabs2010";
		$('market_external_tab').className = "tabs2010";
		$('market_commentary_tab').className = "tabs2010";
		
		// Check which index we are on and set it as selected.
		$('market_'+market_tab+'_tab').className = "tabs2010_selected";		
		
		// Show the market tab that's selected.
		$('prices_2010_markets_'+market_tab).show();		
		
	},
	
	changeShowPorts: function(ports_tab) {
		
		// Hide old tab, show new tab.
		if(ports_tab == 'featured_ports') {
			
			$('prices_2010_homepage_featured_ports').show();
			$('featured_ports_tab').className = 'tabs2010_selected';
			$('prices_2010_homepage_port_selections').hide();
			$('port_selections_tab').className = 'tabs2010';
			
		} else {
			
			$('prices_2010_homepage_featured_ports').hide();
			$('featured_ports_tab').className = 'tabs2010';
			$('prices_2010_homepage_port_selections').show();
			$('port_selections_tab').className = 'tabs2010_selected';
			
		}
		
	},
	
	clickGraph: function() {
		
		// Which graph are we on?
		if($('wti_summary').style.display != 'none') {
			
			window.location = '/markets/wti';
			
		} else if($('bwi_summary').style.display != 'none') {
			
			window.location = '/prices/index/bwi';
			
		} else if($('bw380_summary').style.display != 'none') {
			
			window.location = '/prices/index/bw380';
			
		} else if($('bw180_summary').style.display != 'none') {
			
			window.location = '/prices/index/bw180';
			
		} else if($('bwdi_summary').style.display != 'none') {
			
			window.location = '/prices/index/bwdi';
			
		}
		
	},
	
	graph: function(child, grade, width, height) {
		PM.global.newGraph(child, "/markets/prices/homepagegraph/" + grade + ".json", {createGraphDIV: true, 
			width: width + 'px', height: height + 'px' });
	},
	
	redrawWTI: function (type) {
		PM.global.newGraph($('bw-wti-chart'), "/markets/makeWTIChart?"+(type != "" ? "term="+type : ""), {
				createGraphDIV: false, width: '165px', height: '100px'});
	},
	
	setTickerFeeds: function(title, link, source) {
		bwticker.aFeeds = [[['latest-news'],[title],['<a href=\"'+link+'\"><\/a>'],[source]]];
	}		
	
};

BWPricesIndex = {
	
	giantGraph: function(child, width, height, index) {
		PM.global.newGraph(child, '/prices/index/giant-graph?index=' + index, {createGraphDIV: true, 
			width: width + 'px', height: height + 'px'});		
	}
	
};

BWPriceMatch = {
		
	// Deletes a saved port selection.
	deleteSearch: function(port_selection_id) {
		if(confirm('Really delete this saved port selection?'))
			document.location='/markets/newprices/deletesearch?n='+port_selection_id;
		return false;
	},	
	
	// Updates what is shown in the fancy drop-down, blats the contents of the pricematch screen,
	// and jumps to the selection region/pricematch.
	goToDropDownSelect: function(fancy_drop_text, jump_to) {
		
		// Change the text of the fancy drop-down.
		$('sdh_show_name').update(fancy_drop_text);
		
		// Blat the list of ports in the pricematch (actually replace it with a swirly working icon).
		$('pricematch_pricetable').update('<img style="background-color: #f2f2f2; padding: 10px;" src="'+ window.ASSET_BASE + '/global/aqua2spinner12.v09.01.12.gif" alt="Loading..." height="12" width="12"/>');
		
		// OK, now reload.
		window.location = jump_to;
		
	},
	
	// Hides all popovers and their dropshadows (this is to catch ones which don't get caught onmouseout).
	hidePopovers: function() {
		
		// Get all the popovers in the table.
		var popovers = $$('#pricematch_pricetable td').pluck('id');
		
		for (i = 0; i < popovers.length; i++) {	
			
			// Check to make sure that this is a popover-td.
			if(popovers[i].substring(0, 22) == 'pricematch_table_cell_') {
				
				// Close its popover and dropshadow.
				$(popovers[i].substring(22)).hide();
				$(popovers[i].substring(22)+'_dropshadow').hide();
				
			}
			
		}
		
	},		
	
	mouseOutFancyDD: function(dd_id) {
		
		// Mouse out the originally selected one in case it is still selected.
		$('selected_dd_id').className = 'fancy_dropdown_select_item';
		
		// Mouse out the newly selected on.
		$(dd_id).className = 'fancy_dropdown_select_item';
		
	},
	
	mouseOverFancyDD: function(dd_id) {
		
		// Mouse out the originally selected one in case it is still selected.
		$('selected_dd_id').className = 'fancy_dropdown_select_item';
		
		// Mouse in the newly selected on.
		$(dd_id).className = 'fancy_dropdown_select_item_selected';
		
	},		
		
	// Shows/hides the popover price details for a port/grade in the pricematch page.
	pricematchPopover: function(div, portname, gradename, latest, previous, change) {
		
		// Hide all popovers so far.
		BWPriceMatch.hidePopovers();
	
		// Are we changing to showing the popover?  If so, we need to populate it with the
		// right data!  Make sure that we got some sensible data to show.
		if(latest != '' && previous != '') {
			
			var text = '<strong>'+portname+' '+gradename+'</strong>';
			text += '<div style="overflow: hidden;">';
			text += '<div style="display: inline; float: left; padding-right: 10px; text-align: right; width: 70px;">Last Price: </div>';
			text += '<div style="display: inline;"><strong>'+latest+'</strong> USD/mt</div></div>';
			text += '<div style="overflow: hidden;">';
			text += '<div style="display: inline; float: left; padding-right: 10px; text-align: right; width: 70px;">Previous Price: </div>';
			text += '<div style="display: inline;"><strong>'+previous+'</strong> USD/mt</div></div>';
			text += '<div style="overflow: hidden;">';
			text += '<div style="display: inline; float: left; padding-right: 10px; text-align: right; width: 70px;">Change</div>';
			text += '<div style="display: inline;"><strong>'+change+'</strong> USD/mt</div></div>';				
			
			$(div).update(text)
			
			$(div).show();
			$(div+'_dropshadow').show();
			
		}
		
	},

	saveSearch: function(){
		var months=new Array(13);
		months[1]="January";
		months[2]="February";
		months[3]="March";
		months[4]="April";
		months[5]="May";
		months[6]="June";
		months[7]="July";
		months[8]="August";
		months[9]="September";
		months[10]="October";
		months[11]="November";
		months[12]="December";
		var time=new Date();
		var lmonth=months[time.getMonth() + 1];
		var date=time.getDate();
		var year=time.getYear();
		if (year < 2000) year = year + 1900; 
		suggestion='Ports chosen on ' + lmonth + " " + date + ", " + year;
		name=prompt("What do you want to remember this port selection as?",suggestion);
		if(name == 'null' || name == '' || name == false || name == null){
			return false;
		} else {
			document.location='/markets/newprices/savesearch?n='+name;
		}
	},
	
	toggleDropDown: function() {
		
		// Is the drop-down open already?  If so, close it.
		if($('selection_dropdown').style.display != 'none') {
			$('selection_dropdown').hide();
		} else {
			// Set the width of the fancy drop-down to be the same width as the toggle box.
			$('selection_dropdown').style.width = ($('sdh_show_name').offsetWidth-3+90)+'px';
			$('selection_dropdown').show();
		}
		
	},
	
	windowClick: function(e) {
				
		// Somewhere on the window was clicked.  Was it something other than
		// the drop-down area?
		if(e.target.id.substring(0,4) != 'sdh_') {
			if($('selection_dropdown').style.display != 'none') $('selection_dropdown').hide();
		}
		
	}
		
};

BWPriceMatchPortSelector = {
		
	// A port check box was clicked, so check to see whether or not we should have "Select All" checked.
	checkWhichSelected: function(region, port_force_check) {	

		var ports = $$('#toggleregion_contents_'+region+' input[type=checkbox]');
	
		// Check to see if we are forcing a port to be checked (as happens when we click on
		// a port NAME rather than the checkbox)
		if(port_force_check != '') {			
			
			// Find the port we're clicking on.
			for (i = 0; i < ports.length; i++) {
				
				if($(ports[i]).value == port_force_check) {
					
					// We found it.  Now swap its checkedness.
					if($(ports[i]).checked ) $(ports[i]).checked = false;
					else $(ports[i]).checked = true;
					
				}
				
			}
			
		}

		var all_selected = true;
		
		// Get all ports in the region and see if all are selected.
		for (i = 0; i < ports.length; i++) {
			if(!(ports[i].checked)) {
				all_selected = false;
				continue;
			}
		}		
		
		if(all_selected) $('region_select_all_'+region).checked = true;
		else $('region_select_all_'+region).checked = false;
		
	},			
	
	selectAll: function(region, region_force_check) {
		
		// Check to see if we are forcing a region select all box to be checked (as happens when we click on
		// the select all TEXT rather than the checkbox)
		if(region_force_check) {
			if ($('region_select_all_'+region).checked == true)
				$('region_select_all_'+region).checked = false;
			else
				$('region_select_all_'+region).checked = true;
		}
		
		// Get all ports in the region and set them all to selected/notselected.
		var ports = $$('#toggleregion_contents_'+region+' input[type=checkbox]');
		
		// Get all ports in the region and see if all are selected.
		for (i = 0; i < ports.length; i++) {
			// Checking or unchecking?
			$(ports[i]).checked = $('region_select_all_'+region).checked; 
		}
				
	},		

	// Toggles regions open/closed in port selector
	toggleRegion: function(region) {
	
		// If classname is togglebox_closed we are toggling to open, otherwise we are toggling to closed.
		if($('toggleregion_'+region).className == 'togglebox_closed') {
			
			$('toggleregion_'+region).className = 'togglebox_open';
			$('toggleregion_expand_'+region).update('[-] hide ports');
			
			// Open up the contents.
			$('toggleregion_contents_'+region).show();
			
		} else {
			
			$('toggleregion_'+region).className = 'togglebox_closed';
			$('toggleregion_expand_'+region).update('[+] show ports');
			
			// Open up the contents.
			$('toggleregion_contents_'+region).hide();
			
		}
		
	}
	
};

BWPortPrices = {
		
	applyCustomDates: function() {
	
		// Check to see if the start_date falls after the end_date, if so, swap them.
		var start_year = $('select_start_year').value;
		var end_year = $('select_end_year').value;
		
		var start_month = $('select_start_month_'+start_year).value;
		var end_month = $('select_end_month_'+end_year).value;
		
		if(parseInt(start_year) > parseInt(end_year)) {
			
			// Swap years.
			$('select_start_year').value = end_year;
			$('select_end_year').value = start_year;

			start_year = $('select_start_year').value;
			end_year = $('select_end_year').value;
			
			// Update the years.
			BWPortPrices.changeCustomStartEndYear();
			
			// Swap months.
			$('select_start_month_'+start_year).value = end_month;
			$('select_end_month_'+end_year).value = start_month;

			start_month = $('select_start_month_'+start_year).value;
			end_month = $('select_end_month_'+end_year).value;
						
		}
		
		else if (parseInt(start_year) == parseInt(end_year) && parseInt(start_month) > parseInt(end_month)) {
			
			// Swap months
			$('select_start_month_'+start_year).value = end_month;
			$('select_end_month_'+end_year).value = start_month;
			
			start_month = $('select_start_month_'+start_year).value;
			end_month = $('select_end_month_'+end_year).value;
			
		}
		
		// Save-the-date...
		if(start_month.length == 1) start_month = '0'+start_month;
		if(end_month.length == 1) end_month = '0'+end_month;
				
		// Build dates from the start/end year/month.
		var end_day = $('ldom_'+end_year+'_'+end_month).value;
		
		if(end_day.length == 1) end_day = '0'+end_day;		
		
		var start_date = start_year+'-'+start_month+'-01';
		var end_date = end_year+'-'+end_month+'-'+end_day;
		$('date_start').value = start_date;
		$('date_end').value = end_date;		
		
		
		// We know that any dates selected here are valid, so it's safe to store them for
		// this view.		
		var view = $('view').value;
		$(view+'_start_year').value = start_year;
		$(view+'_start_month').value = start_month;
		$(view+'_end_year').value = end_year;
		$(view+'_end_month').value = end_month;		
		
		BWPortPrices.changeTimePeriod(3, true);	
					
	},
		
	changeCustomStartEndYear: function() {
		
		// Get the currently selected start and end years.
		var start_year = $('select_start_year').value;
		var end_year = $('select_end_year').value;
	
		// Get the ids of all the start month drop-downs.
		var start_month_dd_ids = $$("select[id^='select_start_month_']");
	
		// Hide all the start month drop downs except for the one which corresponds to the currently 
		// selected value in the start year.
		for (i = 0; i < start_month_dd_ids.length; i++) {
		
			if(start_month_dd_ids[i].id == 'select_start_month_'+start_year) $(start_month_dd_ids[i].id).show();
			else $(start_month_dd_ids[i].id).hide();
		
		}
		
		// Get the ids of all the end month drop-downs.
		var end_month_dd_ids = $$("select[id^='select_end_month_']");
	
		// Hide all the end month drop downs except for the one which corresponds to the currently 
		// selected value in the end year.
		for (i = 0; i < end_month_dd_ids.length; i++) {
		
			if(end_month_dd_ids[i].id == 'select_end_month_'+end_year) $(end_month_dd_ids[i].id).show();
			else $(end_month_dd_ids[i].id).hide();
		
		}
		
	},	
	
	changeGrade: function(grade) {
		
		// Check to see if we are redrawing...
		if($('topgraph_drawing').value == 0 && $('botgraph_drawing').value == 0) {
		
			// Unselect the currently selected grade selector.		
			if($('IFO380_tab') != null) $('IFO380_tab').className = "tab ";
			if($('IFO180_tab') != null) $('IFO180_tab').className = "tab ";
			if($('MDO_tab') != null) $('MDO_tab').className = "tab ";
			if($('MGO_tab') != null) $('MGO_tab').className = "tab ";
			if($('LS380_tab') != null) $('LS380_tab').className = "tab ";
			if($('LS180_tab') != null) $('LS180_tab').className = "tab ";
			$('data_tab').className = "tab ";
			if($('commentary_tab') != null) $('commentary_tab').className = "tab ";
			
			// Select the tab for this 'grade'
			$(grade+'_tab').className = "tab sel";
			
			// Is this the data tab?
			if(grade == 'data') {
	
				// Hide the view & time period controls.
				$('view_type_controls').hide();
				$('time_period_controls').hide();
				$('date_controls').hide();
				
				$('overview_panel').hide();
				$('trend_panel').hide();
				$('compare_panel').hide();
				$('maverage_panel').hide();
				if($('commentary_panel') != null) $('commentary_panel').hide();
				
				$('data_panel').show();	
				
			} else if(grade == 'commentary') {
	
				// Hide the view & time period controls.
				$('view_type_controls').hide();
				$('time_period_controls').hide();
				$('date_controls').hide();
				
				$('overview_panel').hide();
				$('trend_panel').hide();
				$('compare_panel').hide();
				$('maverage_panel').hide();
				$('data_panel').hide();	
				
				$('commentary_panel').show();	
				
			} else {
				
				$('data_panel').hide();
				if($('commentary_panel') != null) $('commentary_panel').hide();
				
				// Show the view & time period controls.
				$('view_type_controls').show();
				$('time_period_controls').show();
				
				// Are we on custom dates?  If so, we need to show the controls for them!
				if($('time_period').value == 3) $('date_controls').show();
									
				// Update the selected grade.
				$('grade').value = grade;
				
				// Update the grade-dependent overlay for the Overview controls.
				var select_overlay = $('select_overlay').value;
				var overlay_option = '';
				
				if(grade == 'IFO380' || grade == 'LS380') { 
					overlay_option = 'bw380'; 
					overlay_upper = 'BW380'; 
				}
				else if(grade == 'IFO180' || grade == 'LS180') { 
					overlay_option = 'bw180'; 
					overlay_upper = 'BW180'; 
				}
				else { 
					overlay_option = 'bwdi'; 
					overlay_upper = 'BWDI'; 
				}
				
				/*var gdo = '<input type="radio" name="overview_select_overlay" class="prices_panel_radio_button" ';
				gdo += 'value="'+overlay_option+'" ';
				
				if(select_overlay == 'bw380' || select_overlay == 'bw180' || select_overlay == 'bwdi') gdo += ' checked ';
				
				gdo += ' onclick="BWPortPrices.updateOverviewGraphs(1,1); BWPortPrices.updateOverviewSummary();"/>'+overlay_upper+'</div>';
				onclick="if(BWPortPrices.pricesCheck('.$has_prices.', \''.urlencode($_SERVER['REQUEST_URI']).'\')) {BWPortPrices.changeView(0);}"/>'.strtoupper($indexoption).'
							
				$('grade_dependent_overlay').update(gdo);*/
				
				$('gdo_radio_button').value=overlay_option;
				$('gdo_text').update(overlay_option.toUpperCase());
				if(select_overlay == 'bw380' || select_overlay == 'bw180' || select_overlay == 'bwdi') $('gdo_radio_button').checked = true;
								
				// Update whatever view we are on.
				var view = $('view').value;			
	
				var port_code = $('port_code').value.replace(' ', '_');
				
				BWPortPrices.changeView(view);
				
			}
			
			// Log the change.
			BWPortPrices.logClick(grade);
			
		}
		
	},
	
	changeSelectedRegion: function() {
			
		// Figure out which region is currently selected in the region drop down.
		var selected_region = $('region_code').value;
		
		// Save the selected region in the hidden variable.
		$('select_region').value = selected_region;
		
		// Get the ids of all the port drop downs.
		var port_dd_ids = $$("select[id^='port_select_']");
		
		// Hide all the port drop downs except for the one which corresponds to the currently 
		// selected value in the region drop down.
		for (i = 0; i < port_dd_ids.length; i++) {
			
			if(port_dd_ids[i].id == 'port_select_'+selected_region) $(port_dd_ids[i].id).show();
			else $(port_dd_ids[i].id).hide();
			
		}
		
		BWPortPrices.checkComparePort();
			
	},
	
	changeTimePeriod: function(time_period, force_refresh) {
		
		// Check to see if we are redrawing...
		if($('topgraph_drawing').value == 0 && $('botgraph_drawing').value == 0) {
			
			// Swap the styles.
			var old_period = $('time_period').value;
			$('time_period_selector_'+old_period).className = 'segments2010';
			$('time_period_selector_'+time_period).className = 'segments2010_selected';
			
			// Store the new hidden time_period value.
			$('time_period').value = time_period;
			
			// Update the date_start and date_end.
			if(time_period == 0) $('date_start').value = $('date_30days').value;
			else if(time_period == 1) $('date_start').value = $('date_3months').value;
			else if(time_period == 2) $('date_start').value = $('date_1year').value;
			
			if(time_period < 3) $('date_end').value = $('date_now').value;
			
			// If we are using a custom time period, show and get the custom date_start and date_end values.
			if(time_period == 3) {
				
				// Set the 'using custom dates' flag to true.
				$('custom_dates').value = 1;
				
				$('date_controls').show();
				
				// Refresh our values for start/end year/month.
				var start_year = $('select_start_year').value;
				var end_year = $('select_end_year').value;
				
				var start_month = $('select_start_month_'+start_year).value;
				var end_month = $('select_end_month_'+end_year).value;
				
				// Start date is easy, it's the first of the month indicated in our start
				// year/month drop-down.
				if(start_month.length == 1) start_month = '0'+start_month;
				if(end_month.length == 1) end_month = '0'+end_month;
				
				// End date needs to be the LAST date of the month showing, so retrieve
				// whatever the last date of that month is.
				var last_day = $('ldom_'+end_year+'_'+end_month).value;
							
				$('date_start').value = start_year+'-'+start_month+'-01';
				$('date_end').value = end_year+'-'+end_month+'-'+last_day;
				
			} else {
				
				// Set the 'using custom dates' flag to false.
				$('custom_dates').value = 0;
				
				$('date_controls').hide();
				
			}		
					
			// Set all graphs to blank and update the currently showing graphs (only if we changed time period!)
			if(time_period != old_period || force_refresh) {
				$('cmgraphtop').update('');
				$('cmgraphbot').update('');
				$('ovgraphtop').update('');
				$('ovgraphbot').update('');
				$('mavgraphtop').update('');
				$('mavgraphbot').update('');
				$('trendgraph').update('');
				
				if($('view').value == 0) BWPortPrices.updateOverviewGraphs(1,1);
				else if($('view').value == 1) BWPortPrices.updateTrendGraph();
				else if($('view').value == 2) BWPortPrices.updateMAverageGraphs();
				else if($('view').value == 3) BWPortPrices.updateCompareGraphs(1,1);
				
				// Update the content of the overview and maverage tables.
				BWPortPrices.updateCompareSummary();
				BWPortPrices.updateOverviewSummary();
				BWPortPrices.updateMAverageSummary();
				BWPortPrices.updateTrendSummary();
			}
			
			// Log the change.
			BWPortPrices.logClick($('grade').value);
			
		}
	
	},
	
	changeView: function(view) {
		
		// Check to see if we are redrawing...
		if($('topgraph_drawing').value == 0 && $('botgraph_drawing').value == 0) {
		
			// The first thing we need to do is to update the custom date selector.
			// Nothing else should be updated until AFTER we get that sorted out, or else
			// the graphs will try to use the wrong dates!
			var port_code = $('port_code').value;
			var grade = $('grade').value;
			
			var vs = 'none';
			
			// If this is the overview, get the index that we are comparing against.
			if(view == '0') {
				
				var index = 'bwi';
				for (var i=0; i < document.overview_form.overview_select_overlay.length; i++) {
					if(document.overview_form.overview_select_overlay[i].checked) 
						index = document.overview_form.overview_select_overlay[i].value;
				}
				
				vs = index;
				
			} else if(view == '3') {
				
				var region_code = $('region_code').value; // Which drop down?
				var compare = $('port_select_'+region_code).value;
				
				vs = compare;
				
			}
			
			// If we are comparing against BWI, we will always be rebasing, so set rebase to true.
			if(vs == 'bwi') {
				
				document.overview_form.overview_select_rebase[0].checked = false;
				document.overview_form.overview_select_rebase[1].checked = true;
				
			// All others are actual value.
			} else {
	
				document.overview_form.overview_select_rebase[0].checked = true;
				document.overview_form.overview_select_rebase[1].checked = false;
			
			}			
			
			// If we are actually changing view from what we had before, use the previously stored
			// dates instead of what's currently in the drop-downs as our default start/end values.  
			// This is so that you can tab back and forth between views and it will 'remember' what
			// you had in the drop down for each.  Otherwise, just use what's currently in the drop-downs.
			var start_year = $('select_start_year').value;
			var start_month = $('select_start_month_'+start_year).value;
			var end_year = $('select_end_year').value;
			var end_month = $('select_end_month_'+end_year).value;
			
			if(view != $('view').value) {
				
				start_year = $(view+'_start_year').value;
				start_month = $(view+'_start_month').value;
				end_year = $(view+'_end_year').value;
				end_month = $(view+'_end_month').value;
				
			}		
	
			if(start_month.length == 1) start_month = '0'+start_month;
			if(end_month.length == 1) end_month = '0'+end_month;
					
			// Build dates from the start/end year/month.
			var end_day = $('ldom_'+end_year+'_'+end_month).value;
			
			if(end_day.length == 1) end_day = '0'+end_day;		
			
			var start_date = start_year+'-'+start_month+'-01';
			var end_date = end_year+'-'+end_month+'-'+end_day;
			
			// To be safe, set all view selector styles to unselected...
			$('view_selector_0').className = 'segments2010';
			$('view_selector_1').className = 'segments2010';
			$('view_selector_2').className = 'segments2010';
			$('view_selector_3').className = 'segments2010';
			
			var old_view = $('view').value;
			
			// Now we can set the right one to selected.
			$('view_selector_'+view).className = 'segments2010_selected';
			
			// Are we on custom dates?  If so, we need to update those FIRST...
			if($('time_period').value == 3) {
				
				$('ovgraphtop').update('<img style="background-color: #f2f2f2; padding: 10px;" src="' + window.ASSET_BASE + '/global/aqua2spinner12.v09.01.12.gif" alt="Loading..." height="12" width="12"/>');
				$('ovgraphbot').update('<img style="background-color: #f2f2f2; padding: 10px;" src="' + window.ASSET_BASE + '/global/aqua2spinner12.v09.01.12.gif" alt="Loading..." height="12" width="12"/>');
				$('trendgraph').update('<img style="background-color: #f2f2f2; padding: 10px;" src="' + window.ASSET_BASE + '/global/aqua2spinner12.v09.01.12.gif" alt="Loading..." height="12" width="12"/>');
				$('mavgraphtop').update('<img style="background-color: #f2f2f2; padding: 10px;" src="' + window.ASSET_BASE + '/global/aqua2spinner12.v09.01.12.gif" alt="Loading..." height="12" width="12"/>');
				$('mavgraphbot').update('<img style="background-color: #f2f2f2; padding: 10px;" src="' + window.ASSET_BASE + '/global/aqua2spinner12.v09.01.12.gif" alt="Loading..." height="12" width="12"/>');
				$('cmgraphtop').update('<img style="background-color: #f2f2f2; padding: 10px;" src="' + window.ASSET_BASE + '/global/aqua2spinner12.v09.01.12.gif" alt="Loading..." height="12" width="12"/>');
				$('cmgraphbot').update('<img style="background-color: #f2f2f2; padding: 10px;" src="' + window.ASSET_BASE + '/global/aqua2spinner12.v09.01.12.gif" alt="Loading..." height="12" width="12"/>');
									
				new Ajax.Updater($('date_controls'), '/prices/_prices_dates_dropdowns.php?port_code='+port_code+'&grade='+grade+'&view='+view+'&vs='+vs+'&start_date='+start_date+'&end_date='+end_date, 
					{method:'get', 
					onComplete:function() {
					
						// Set the values of the hidden dates (only if we are in custom date view).
						if($('time_period').value == 3) {
							
							var start_year = $('select_start_year').value;
							var start_month = $('select_start_month_'+start_year).value;
							var end_year = $('select_end_year').value;
							var end_month = $('select_end_month_'+end_year).value;
							
							if(start_month.length == 1) start_month = '0'+start_month;
							if(end_month.length == 1) end_month = '0'+end_month;
									
							// Build dates from the start/end year/month.
							var end_day = $('ldom_'+end_year+'_'+end_month).value;
							
							if(end_day.length == 1) end_day = '0'+end_day;		
							
							var start_date = start_year+'-'+start_month+'-01';
							var end_date = end_year+'-'+end_month+'-'+end_day;
							
							$('date_start').value = start_date;
							$('date_end').value = end_date;
							
							// Set the ones for this view.
							$(view+'_start_year').value = start_year;
							$(view+'_start_month').value = start_month;
							$(view+'_end_year').value = end_year;
							$(view+'_end_month').value = end_month;
							
						} else { 
							
							var start_date = $('date_start');
							var end_date = $('end_date');
							
						}
												
						// Store the new hidden time_period value.
						$('view').value = view;
										
						$('overview_panel').hide();
						$('trend_panel').hide();
						$('compare_panel').hide();
						$('maverage_panel').hide();
						$('data_panel').hide();
										
						if(view == 0) {
							
							if(old_view != view) {
							
								// Get hidden values that we need to know what's checked in the overview control panel.
								for (var i=0; i < document.overview_form.overview_select_data.length; i++) {
									if(document.overview_form.overview_select_data[i].value == $('select_data').value) 
										document.overview_form.overview_select_data[i].checked = true;
								}
								
								for (var i=0; i < document.overview_form.overview_select_overlay.length; i++) {
									if(document.overview_form.overview_select_overlay[i].value == $('select_overlay').value) 
										document.overview_form.overview_select_overlay[i].checked = true;
								}
								
								for (var i=0; i < document.overview_form.overview_select_rebase.length; i++) {
									if(document.overview_form.overview_select_rebase[i].value == $('select_rebase').value) 
										document.overview_form.overview_select_rebase[i].checked = true;
								}
								
								for (var i=0; i < document.overview_form.overview_select_axis.length; i++) {
									if(document.overview_form.overview_select_axis[i].value == $('select_axis').value) 
										document.overview_form.overview_select_axis[i].checked = true;
								}
								
							}
							
							$('overview_panel').show();
							BWPortPrices.updateOverviewGraphs(1,1);
							BWPortPrices.updateOverviewSummary();
						}
						
						if(view == 1) {
							
							if(old_view != view) {
							
								// Get hidden values that we need to know what's checked in the trend control panel.
								for (var i=0; i < document.trend_form.trend_select_period.length; i++) {
									if(document.trend_form.trend_select_period[i].value == $('select_period').value) 
										document.trend_form.trend_select_period[i].checked = true;
								}
								
							} 
							
							$('trend_panel').show();
							BWPortPrices.updateTrendGraph();
							BWPortPrices.updateTrendSummary();
							
						}				
						
						if(view == 2) {
							
							if(old_view != view) {
							
								// Get hidden values that we need to know what's checked in the moving average control panel.
								for (var i=0; i < document.maverage_form.maverage_select_period.length; i++) {
									if(document.maverage_form.maverage_select_period[i].value == $('select_period').value) 
										document.maverage_form.maverage_select_period[i].checked = true;
								}
								
							}
		
							$('maverage_panel').show();
							BWPortPrices.updateMAverageGraphs();
							BWPortPrices.updateMAverageSummary();
						}
						
						if(view == 3) {
							
							if(old_view != view) {
							
								// Get hidden values that we need to know what's checked in the compare control panel.
								for (var i=0; i < document.compare_form.compare_select_data.length; i++) {
									if(document.compare_form.compare_select_data[i].value == $('select_data').value) 
										document.compare_form.compare_select_data[i].checked = true;
								}
								
								for (var i=0; i < document.compare_form.compare_select_axis.length; i++) {
									if(document.compare_form.compare_select_axis[i].value == $('select_axis').value) 
										document.compare_form.compare_select_axis[i].checked = true;
								}
								
							}
							
							// Region can only change in one place...in the compare view, so we don't need to update it here.
		
							$('compare_panel').show();
							BWPortPrices.updateCompareGraphs(1,1);
							BWPortPrices.updateCompareSummary();		
							
						}		
		
						// Log the change.
						BWPortPrices.logClick($('grade').value);
						
				}});
				
			} else {
				
				var start_date = $('date_start');
				var end_date = $('end_date');
									
				// Store the new hidden time_period value.
				$('view').value = view;
								
				$('overview_panel').hide();
				$('trend_panel').hide();
				$('compare_panel').hide();
				$('maverage_panel').hide();
				$('data_panel').hide();
								
				if(view == 0) {
					
					if(old_view != view) {
					
						// Get hidden values that we need to know what's checked in the overview control panel.
						for (var i=0; i < document.overview_form.overview_select_data.length; i++) {
							if(document.overview_form.overview_select_data[i].value == $('select_data').value) 
								document.overview_form.overview_select_data[i].checked = true;
						}
						
						for (var i=0; i < document.overview_form.overview_select_overlay.length; i++) {
							if(document.overview_form.overview_select_overlay[i].value == $('select_overlay').value) 
								document.overview_form.overview_select_overlay[i].checked = true;
						}
						
						for (var i=0; i < document.overview_form.overview_select_rebase.length; i++) {
							if(document.overview_form.overview_select_rebase[i].value == $('select_rebase').value) 
								document.overview_form.overview_select_rebase[i].checked = true;
						}
						
						for (var i=0; i < document.overview_form.overview_select_axis.length; i++) {
							if(document.overview_form.overview_select_axis[i].value == $('select_axis').value) 
								document.overview_form.overview_select_axis[i].checked = true;
						}
						
					}
					
					$('overview_panel').show();
					BWPortPrices.updateOverviewGraphs(1,1);
					BWPortPrices.updateOverviewSummary();
				}
				
				if(view == 1) {
					
					if(old_view != view) {
					
						// Get hidden values that we need to know what's checked in the trend control panel.
						for (var i=0; i < document.trend_form.trend_select_period.length; i++) {
							if(document.trend_form.trend_select_period[i].value == $('select_period').value) 
								document.trend_form.trend_select_period[i].checked = true;
						}
						
					} 
					
					$('trend_panel').show();
					BWPortPrices.updateTrendGraph();
					BWPortPrices.updateTrendSummary();
					
				}				
				
				if(view == 2) {
					
					if(old_view != view) {
					
						// Get hidden values that we need to know what's checked in the moving average control panel.
						for (var i=0; i < document.maverage_form.maverage_select_period.length; i++) {
							if(document.maverage_form.maverage_select_period[i].value == $('select_period').value) 
								document.maverage_form.maverage_select_period[i].checked = true;
						}
						
					}
		
					$('maverage_panel').show();
					BWPortPrices.updateMAverageGraphs();
					BWPortPrices.updateMAverageSummary();
				}
				
				if(view == 3) {
					
					if(old_view != view) {
					
						// Get hidden values that we need to know what's checked in the compare control panel.
						for (var i=0; i < document.compare_form.compare_select_data.length; i++) {
							if(document.compare_form.compare_select_data[i].value == $('select_data').value) 
								document.compare_form.compare_select_data[i].checked = true;
						}
						
						for (var i=0; i < document.compare_form.compare_select_axis.length; i++) {
							if(document.compare_form.compare_select_axis[i].value == $('select_axis').value) 
								document.compare_form.compare_select_axis[i].checked = true;
						}
						
					}
					
					// Region can only change in one place...in the compare view, so we don't need to update it here.
		
					$('compare_panel').show();
					BWPortPrices.updateCompareGraphs(1,1);
					BWPortPrices.updateCompareSummary();		
					
				}		
		
				// Log the change.
				BWPortPrices.logClick($('grade').value);
				
			}			
			
		} 
		
	},
	
	checkComparePort: function() {
		
		// Check to see if we have a selected port in the compare drop down.  If we do, enable the
		// compare button, otherwise disable it.
		
		var region_code = $('region_code').value; // Which drop down?
		var compare = $('port_select_'+region_code).value;
		
		if(compare == '') $('compare_ports_button').disabled = true;
		else $('compare_ports_button').disabled = false;
		
	},		
	
	compareGraphTop: function(child, width, height, date_start, date_end, port_code, grade, compare, show_spread, custom_dates) {
		$(child).update(''); // clear old graph
		var qstring = 'date_start='+date_start+'&date_end='+date_end+'&port_code='+port_code;
		qstring += '&grade='+grade+'&compare='+compare+'&show_spread='+show_spread+'&custom_dates='+custom_dates;
		PM.global.newExtendedGraphWithWait(child, '/prices/graphs/comparegraphtop?'+qstring, {createGraphDIV: true, 
			width: width + 'px', height: height + 'px'}, 'topgraph_drawing');
	},
	
	compareGraphBot: function(child, width, height, date_start, date_end, port_code, grade, compare, about, custom_dates) {
		$(child).update(''); // clear old graph
		var qstring = 'date_start='+date_start+'&date_end='+date_end+'&port_code='+port_code;
		qstring += '&grade='+grade+'&compare='+compare+'&about='+about+'&custom_dates='+custom_dates;
		PM.global.newExtendedGraphWithWait(child, '/prices/graphs/comparegraphbot?'+qstring, {createGraphDIV: true, 
			width: width + 'px', height: height + 'px'}, 'botgraph_drawing');
	},
	
	downloadData: function(data_type, date_range) {
		
		// Get all common arguments.
		var port_code = $('port_code').value.replace(' ', '_');
		var grade = $('grade').value;
				
		// Get additional arguments depending and pass in what's required for the requested data_type.
		switch(data_type) {
		
		case 'compare':
			
			// Get the value of the port we are comparing against.
			var region_code = $('region_code').value; // Which drop down?
			var compare = $('port_select_'+region_code).value.replace(' ', '_');
			
			// Are we showing the spread?
			var show_spread = 1;
			for (var i=0; i < document.compare_form.compare_select_data.length; i++) {
				if(document.compare_form.compare_select_data[i].checked) 
					show_spread = document.compare_form.compare_select_data[i].value;
			}
			
			var url = "/download/port_prices/csv";
			url += "?port_code="+port_code+"&grade="+grade+"&date_range="+date_range;
			url += "&data_type="+data_type+"&compare="+compare+"&show_spread="+show_spread;
			window.open(url);
			
			break;
			
		case 'maverage':
			
			// Get the value of the period.
			var period = 10;
			for (var i=0; i < document.maverage_form.maverage_select_period.length; i++) {
				if(document.maverage_form.maverage_select_period[i].checked) 
					period = document.maverage_form.maverage_select_period[i].value;
			}
			
			var url = "/download/port_prices/csv";
			url += "?port_code="+port_code+"&grade="+grade+"&date_range="+date_range;
			url += "&data_type="+data_type+"&period="+period;
			window.open(url);
			
			break;
		
		case 'overview':
			
			var index = 'bwi';
			for (var i=0; i < document.overview_form.overview_select_overlay.length; i++) {
				if(document.overview_form.overview_select_overlay[i].checked) 
					index = document.overview_form.overview_select_overlay[i].value;
			}
			
			// If 'none' was selected, hide the other controls, and the bottom graph.
			if(index == 'none') {
				$('prices_overview_overlay_controls').hide();
				$('ovgraphbot').hide();
			} else {
				$('prices_overview_overlay_controls').show();
				$('ovgraphbot').show();
			}
			
			// Are we rebasing?
			var rebase = 1;
			for (var i=0; i < document.overview_form.overview_select_rebase.length; i++) {
				if(document.overview_form.overview_select_rebase[i].checked) 
					rebase = document.overview_form.overview_select_rebase[i].value;
			}
			
			// Are we showing the spread?
			var show_spread = 1;
			for (var i=0; i < document.overview_form.overview_select_data.length; i++) {
				if(document.overview_form.overview_select_data[i].checked) 
					show_spread = document.overview_form.overview_select_data[i].value;
			}
			
			var url = "/download/port_prices/csv";
			url += "?port_code="+port_code+"&grade="+grade+"&date_range="+date_range;
			url += "&data_type="+data_type+"&show_spread="+show_spread+"&index="+index+"&rebase="+rebase;
			window.open(url);
			
			break;
			
		case 'trend':
			
			// Get the value of the period.
			var period = 10;
			for (var i=0; i < document.trend_form.trend_select_period.length; i++) {
				if(document.trend_form.trend_select_period[i].checked) 
					period = document.trend_form.trend_select_period[i].value;
			}
			
			var url = "/download/port_prices/csv";
			url += "?port_code="+port_code+"&grade="+grade+"&date_range="+date_range;
			url += "&data_type="+data_type+"&period="+period;
			window.open(url);
			
			break;
			
		}
		
	},	
	
	logClick: function(grade) {
		
		// Creates a fake page log entry to track user clicks on AJAX tabs.
				
		// Get port ref, underscored.
		var getportref = $('port_code').value.replace(' ', '_').toLowerCase();
		
		// Create the 'name' of the page (fake link to sv_script_name).
		var pagename = '/markets/prices/'+$('port_code').value.replace(' ', '/').toLowerCase()+'/';
		
		// Create the 'title' of the page (fake Page Subcategory)
		var page_sub = 'Latest Prices - '+$('port_name').value;
						
		if(grade == 'data') page_sub += ' Data';
		else if (grade == 'commentary') page_sub += ' Commentary';
		else page_sub += ' '+grade;
		
		var analytics = pagename+grade+'/';
		
		if(grade != 'data' && grade != 'commentary') {
			
			var time_period = $('time_period').value;
			var view = $('view').value;
		
			switch(time_period) {
			
				case '0':
					
					page_sub += ' 30 Day';
					analytics += '30day/';
					break;
					
				case '1':
					
					page_sub += ' 3 Month';
					analytics += '3month/';
					break;
				
				case '2':
					
					page_sub += ' 1 Year';
					analytics += 'year/';
					break;
					
				case '3':
					
					page_sub += ' Custom Dates';
					analytics += 'custom/';
					break;
				
			}
			
			switch(view) {
			
				case '0':
					
					page_sub += ' Overview';
					analytics += 'overview/';
					break;
					
				case '1':
					
					page_sub += ' Trends';
					analytics += 'trends/';
					break;
				
				case '2':
					
					page_sub += ' Moving Average';
					analytics += 'maverage/';
					break;
					
				case '3':
					
					page_sub += ' Compare';
					analytics += 'compare/';
					break;
				
			}
			
		} 
		
		new Ajax.Request('/misc/_faux_page_view.php?p='+getportref,
				  {
				    method:'get',
				    parameters: {faux_page_main: 'Prices', faux_page_sub: page_sub, faux_pagename: pagename}
				  });
		
		// Change the page title.
		document.title = 'Bunkerworld Prices - '+page_sub;
		
		var pageTracker = _gat._getTracker($('analytics_id').value+'');
		pageTracker._trackPageview(analytics);
		
	},
	
	logMoreInfoClick: function() {
		
		// Get port ref, underscored.
		var getportref = $('port_code').value.replace(' ', '_').toLowerCase();
		
		var view = $('view').value;
		
		var grade = $('grade').value;
		
		// Create the 'name' of the page (fake link to sv_script_name).
		var pagename = '/markets/prices/'+$('port_code').value.replace(' ', '/').toLowerCase()+'/'+grade+'/'+view+'/';
		
		// Create the 'title' of the page (fake Page Subcategory)
		var page_sub = 'Latest Prices - '+view+' - More Info';
		
		var analytics = pagename + 'moreinfo';
		
		new Ajax.Request('/misc/_faux_page_view.php?p='+getportref,
				  {
				    method:'get',
				    parameters: {faux_page_main: 'Prices', faux_page_sub: page_sub, faux_pagename: pagename}
				  });
		
		var pageTracker = _gat._getTracker($('analytics_id').value+'');
		pageTracker._trackPageview(analytics);
		
	},		
	
	maverageGraphTop: function(child, width, height, date_start, date_end, port_code, grade, period, custom_dates) {
		$(child).update(''); // clear old graph
		var qstring = 'date_start='+date_start+'&date_end='+date_end+'&port_code='+port_code;
		qstring += '&grade='+grade+'&period='+period+'&custom_dates='+custom_dates;
		PM.global.newExtendedGraphWithWait(child, '/prices/graphs/maveragegraphtop?'+qstring, {createGraphDIV: true, 
			width: width + 'px', height: height + 'px'}, 'topgraph_drawing');		
	},
	
	maverageGraphBot: function(child, width, height, date_start, date_end, port_code, grade, period, custom_dates) {
		$(child).update(''); // clear old graph
		var qstring = 'date_start='+date_start+'&date_end='+date_end+'&port_code='+port_code;
		qstring += '&grade='+grade+'&period='+period+'&custom_dates='+custom_dates;
		PM.global.newExtendedGraphWithWait(child, '/prices/graphs/maveragegraphbot?'+qstring, {createGraphDIV: true, 
			width: width + 'px', height: height + 'px'}, 'botgraph_drawing');		
	},
	
	memberCheck: function(is_member, redirect) {
				
		if(is_member == 1) return true;
		
		else {
			
			// Show the pageveil.
			$('pageveil').show();
			
			// Show the popover member div.
			$('member_check').show();
					
			return false;
			
		}
		
	},
	
	memberCheckClose: function() {
		
		// Hide the pageveil.
		$('pageveil').hide();
		
		// Hide the popover subscriber div.
		$('member_check').hide();
		
	},	
	
	overviewGraphTop: function(child, width, height, date_start, date_end, port_code, grade, index, rebase, show_spread, custom_dates) {
		$(child).update(''); // clear old graph
		var qstring = 'date_start='+date_start+'&date_end='+date_end+'&port_code='+port_code;
		qstring += '&grade='+grade+'&index='+index+'&rebase='+rebase+'&show_spread='+show_spread+'&custom_dates='+custom_dates;
		PM.global.newExtendedGraphWithWait(child, '/prices/graphs/overviewgraphtop?'+qstring, {createGraphDIV: true, 
			width: width + 'px', height: height + 'px'}, 'topgraph_drawing');		
	},
	
	overviewGraphBot: function(child, width, height, date_start, date_end, port_code, grade, index, rebase, about, custom_dates) {
		$(child).update(''); // clear old graph
		var qstring = 'date_start='+date_start+'&date_end='+date_end+'&port_code='+port_code;
		qstring += '&grade='+grade+'&index='+index+'&about='+about+'&rebase='+rebase+'&custom_dates='+custom_dates;
		PM.global.newExtendedGraphWithWait(child, '/prices/graphs/overviewgraphbot?'+qstring, {createGraphDIV: true, 
			width: width + 'px', height: height + 'px'}, 'botgraph_drawing');		
	},
	
	pricesCheck: function(has_prices, redirect) {
		
		if(has_prices == 1) return true;
		
		else {
			
			// Show the pageveil.
			$('pageveil').show();
			
			// Show the popover subscriber div.
			$('prices_check').show();
			
			// Put everything back to defaults (we only need to do this on the overview panel since we
			// have no way of getting to the other panels if this popup is active).
			for (var i=0; i < document.overview_form.overview_select_data.length; i++) {
				if(document.overview_form.overview_select_data[i].value == 0) 
					document.overview_form.overview_select_data[i].checked = true;
				else
					document.overview_form.overview_select_data[i].checked = false;
			}
			
			for (var i=0; i < document.overview_form.overview_select_overlay.length; i++) {
				if(document.overview_form.overview_select_overlay[i].value == 'bwi') 
					document.overview_form.overview_select_overlay[i].checked = true;
				else
					document.overview_form.overview_select_overlay[i].checked = false;
			}
			
			for (var i=0; i < document.overview_form.overview_select_rebase.length; i++) {
				if(document.overview_form.overview_select_rebase[i].value == 0) 
					document.overview_form.overview_select_rebase[i].checked = true;
				else
					document.overview_form.overview_select_rebase[i].checked = false;
			}
			
			for (var i=0; i < document.overview_form.overview_select_axis.length; i++) {
				if(document.overview_form.overview_select_axis[i].value == 'zero') 
					document.overview_form.overview_select_axis[i].checked = true;
				else
					document.overview_form.overview_select_axis[i].checked = false;
			}
			
			
			return false;
			
		}
		
	},
	
	pricesCheckClose: function() {
		
		// Hide the pageveil.
		$('pageveil').hide();
		
		// Hide the popover subscriber div.
		$('prices_check').hide();
		
	},
	
	trendGraph: function(child, width, height, date_start, date_end, port_code, grade, period, custom_dates) {
	
		$(child).update(''); // clear old graph
		var qstring = 'date_start='+date_start+'&date_end='+date_end+'&port_code='+port_code;
		qstring += '&grade='+grade+'&period='+period+'&custom_dates='+custom_dates;
		PM.global.newExtendedGraphWithWait(child, '/prices/graphs/trendgraph?'+qstring, {createGraphDIV: true, 
			width: width + 'px', height: height + 'px'}, 'topgraph_drawing');		
				
	},
	
	updateCompareGraphs: function(updatetop, updatebot) {
		
		// Get the start and end dates, port_code, and grade.
		var date_start = $('date_start').value;
		var date_end = $('date_end').value;
		var port_code = $('port_code').value;
		var grade = $('grade').value;
		
		// Get the value of which axis we are showing the differential about.
		var about = 'avg';
		for (var i=0; i < document.compare_form.compare_select_axis.length; i++) {
			if(document.compare_form.compare_select_axis[i].checked) 
				about = document.compare_form.compare_select_axis[i].value;
		}
		
		// Get the value of the port we are comparing against.
		var region_code = $('region_code').value; // Which drop down?
		var compare = $('port_select_'+region_code).value;
		
		// Are we showing the spread?
		var show_spread = 1;
		for (var i=0; i < document.compare_form.compare_select_data.length; i++) {
			if(document.compare_form.compare_select_data[i].checked) 
				show_spread = document.compare_form.compare_select_data[i].value;
		}
		
		// Custom dates?
		var custom_dates = $('custom_dates').value;
		
		// Update saved hidden values.
		$('select_region').value = region_code;
		$('select_port').value = compare;
		$('select_axis').value = about;
		$('select_data').value = show_spread;
				
		if(updatetop) BWPortPrices.compareGraphTop($('cmgraphtop'), 470, 300, date_start, date_end, port_code, grade, compare, show_spread, custom_dates);
		if(updatebot) BWPortPrices.compareGraphBot($('cmgraphbot'), 470, 200, date_start, date_end, port_code, grade, compare, about, custom_dates);
		
	},
	
	updateCompareSummary: function() {
		
		// Get the start and end dates, port_code, and grade.
		var date_start = $('date_start').value;
		var date_end = $('date_end').value;
		var port_code = $('port_code').value;
		var grade = $('grade').value;
		
		// Get the value of the port we are comparing against.
		var region_code = $('region_code').value; // Which drop down?
		var compare = $('port_select_'+region_code).value;
		
		// Update saved hidden values.
		$('select_region').value = region_code;
		$('select_port').value = compare;

		$('compare_summary').update('<img src="' + window.ASSET_BASE+ '/global/aqua2spinner12.v09.01.12.gif"/>');
		new Ajax.Updater($('compare_summary'), '/prices/_port_prices_compare.php?date_start='+date_start+'&date_end='+date_end+'&port_code='+port_code+'&grade='+grade+'&compare='+compare, {'evalScripts': true}); 
		
	},
	
	updateCustomDates: function() {
		
		// Get the currently selected dates.
		var start_year = $('select_start_year').value;
		var end_year = $('select_end_year').value;
		var start_month = $('select_start_month_'+start_year).value;
		var end_month = $('select_end_month_'+end_year).value;
		var end_day = $('ldom_'+end_year+'_'+end_month).value;
				
		if(start_month.length == 1) start_month = '0'+start_month;
		if(end_month.length == 1) end_month = '0'+end_month;
		if(end_day.length == 1) end_day = '0'+end_day;		
		
		var start_date = start_year+'-'+start_month+'-01';
		var end_date = end_year+'-'+end_month+'-'+end_day;
		
		// Get port, grade, view.
		var port_code = $('port_code').value.replace(' ', '_');
		var grade = $('grade').value;
		var view = $('view').value;
		
		var vs = 'none';
		
		// If this is the overview, get the index that we are comparing against.
		if(view == '0') {
			
			var index = 'bwi';
			for (var i=0; i < document.overview_form.overview_select_overlay.length; i++) {
				if(document.overview_form.overview_select_overlay[i].checked) 
					index = document.overview_form.overview_select_overlay[i].value;
			}
			
			vs = index;
			
		} else if(view == '3') {
			
			var region_code = $('region_code').value; // Which drop down?
			var compare = $('port_select_'+region_code).value;
			
			vs = compare;
			
		} 
		
		new Ajax.Updater($('date_controls'), '/prices/_prices_dates_dropdowns.php?port_code='+port_code+'&grade='+grade+'&view='+view+'&vs='+vs+'&start_date='+start_date+'&end_date='+end_date); 
		
	},
	
	updateDataPanel: function(panel) {
		
		// Swap the styles.
		var old_panel = $('data_panel_select').value;
		$('data_time_selector_'+old_panel).className = 'segments2010';
		$('data_time_selector_'+panel).className = 'segments2010_selected';
		$('data_panel_select').value = panel;
		
		// Hide the three data panels, then show the right one.
		$('data_daily_panel').hide();
		$('data_monthly_panel').hide();
		$('data_thirtyday_panel').hide();
		
		$('data_'+panel+'_panel').show();
		
		// Hide the three bylines, then show the right one.
		$('prices_byline_daily').hide();
		$('prices_byline_monthly').hide();
		$('prices_byline_thirtyday').hide();
		
		$('prices_byline_'+panel).show();
		
	},		
	
	updateMAverageGraphs: function() {
		
		// Get the start and end dates, port_code, and grade.
		var date_start = $('date_start').value;
		var date_end = $('date_end').value;
		var port_code = $('port_code').value;
		var grade = $('grade').value;
		
		// Get the value of the period.
		var period = 10;
		for (var i=0; i < document.maverage_form.maverage_select_period.length; i++) {
			if(document.maverage_form.maverage_select_period[i].checked) 
				period = document.maverage_form.maverage_select_period[i].value;
		}
		
		// Custom dates?
		var custom_dates = $('custom_dates').value;
		
		// Update saved hidden values.
		$('select_period').value = period;
		
		BWPortPrices.maverageGraphTop($('mavgraphtop'), 530, 300, date_start, date_end, port_code, grade, period, custom_dates);
		BWPortPrices.maverageGraphBot($('mavgraphbot'), 530, 200, date_start, date_end, port_code, grade, period, custom_dates);
		
	},
	
	updateMAverageSummary: function() {
		
		// Get the start and end dates, port_code, and grade.
		var date_start = $('date_start').value;
		var date_end = $('date_end').value;
		var port_code = $('port_code').value;
		var grade = $('grade').value;
		
		// Get the value of the period.
		var period = 10;
		for (var i=0; i < document.maverage_form.maverage_select_period.length; i++) {
			if(document.maverage_form.maverage_select_period[i].checked) 
				period = document.maverage_form.maverage_select_period[i].value;
		}
		
		// Update saved hidden values.
		$('select_period').value = period;

		$('maverage_summary').update('<img src="' + window.ASSET_BASE+ '/global/aqua2spinner12.v09.01.12.gif"/>');
		new Ajax.Updater($('maverage_summary'), '/prices/_port_prices_maverage.php?date_start='+date_start+'&date_end='+date_end+'&port_code='+port_code+'&grade='+grade+'&period='+period, {'evalScripts': true}); 
		
	},
	
	updateOverviewGraphs: function(updatetop, updatebot) {
		
		// Get the start and end dates, port_code, and grade.
		var date_start = $('date_start').value;
		var date_end = $('date_end').value;
		var port_code = $('port_code').value;
		var grade = $('grade').value;
		
		// Get the value of the index.
		var index = 'bwi';
		for (var i=0; i < document.overview_form.overview_select_overlay.length; i++) {
			if(document.overview_form.overview_select_overlay[i].checked) 
				index = document.overview_form.overview_select_overlay[i].value;
		}
		
		// If 'none' was selected, hide the other controls, and the bottom graph.
		if(index == 'none') {
			$('prices_overview_overlay_controls').hide();
			$('ovgraphbot').hide();
		} else {
			$('prices_overview_overlay_controls').show();
			$('ovgraphbot').show();
		}
		
		// Get the value of which axis we are showing the differential about.
		var about = 'avg';
		for (var i=0; i < document.overview_form.overview_select_axis.length; i++) {
			if(document.overview_form.overview_select_axis[i].checked) 
				about = document.overview_form.overview_select_axis[i].value;
		}
		
		// Are we rebasing?
		var rebase = 1;
		for (var i=0; i < document.overview_form.overview_select_rebase.length; i++) {
			if(document.overview_form.overview_select_rebase[i].checked) 
				rebase = document.overview_form.overview_select_rebase[i].value;
		}
		
		// Are we showing the spread?
		var show_spread = 1;
		for (var i=0; i < document.overview_form.overview_select_data.length; i++) {
			if(document.overview_form.overview_select_data[i].checked) 
				show_spread = document.overview_form.overview_select_data[i].value;
		}
		
		// Custom dates?
		var custom_dates = $('custom_dates').value;
		
		// Update saved hidden values.
		$('select_overlay').value = index;
		$('select_axis').value = about;
		$('select_rebase').value = rebase;
		$('select_data').value = show_spread;
		
		if(updatetop) BWPortPrices.overviewGraphTop($('ovgraphtop'), 530, 300, date_start, date_end, port_code, grade, index, rebase, show_spread, custom_dates);
		if(updatebot) BWPortPrices.overviewGraphBot($('ovgraphbot'), 530, 200, date_start, date_end, port_code, grade, index, rebase, about, custom_dates);
		
	},
	
	updateOverviewSummary: function() {
		
		// Get the start and end dates, port_code, and grade.
		var date_start = $('date_start').value;
		var date_end = $('date_end').value;
		var port_code = $('port_code').value;
		var grade = $('grade').value;
		
		// Get the value of the index.
		var index = 'bwi';
		for (var i=0; i < document.overview_form.overview_select_overlay.length; i++) {
			if(document.overview_form.overview_select_overlay[i].checked) 
				index = document.overview_form.overview_select_overlay[i].value;
		}
		
		// Are we rebasing?
		var rebase = 1;
		for (var i=0; i < document.overview_form.overview_select_rebase.length; i++) {
			if(document.overview_form.overview_select_rebase[i].checked) 
				rebase = document.overview_form.overview_select_rebase[i].value;
		}
		
		$('overview_explain_bw380').hide();
		$('overview_explain_bw180').hide();
		$('overview_explain_bwdi').hide();
		
		if(grade == 'IFO380') $('overview_explain_bw380').show();
		if(grade == 'IFO180') $('overview_explain_bw180').show();
		if(grade == 'MDO' || grade == 'MGO') $('overview_explain_bwdi').show();	
		
		// Update saved hidden values.
		$('select_overlay').value = index;
		$('select_rebase').value = rebase;
		
		$('overview_summary').update('<img src="' + window.ASSET_BASE+ '/global/aqua2spinner12.v09.01.12.gif"/>');
		new Ajax.Updater($('overview_summary'), '/prices/_port_prices_overview.php?date_start='+date_start+'&date_end='+date_end+'&port_code='+port_code+'&grade='+grade+'&index='+index+'&rebase='+rebase, {'evalScripts': true}); 
		
	},
	
	updateTrendGraph: function() {
		
		// Get the start and end dates, port_code, and grade.
		var date_start = $('date_start').value;
		var date_end = $('date_end').value;
		var port_code = $('port_code').value;
		var grade = $('grade').value;
		
		// Get the value of the period.
		var period = 10;
		for (var i=0; i < document.trend_form.trend_select_period.length; i++) {
			if(document.trend_form.trend_select_period[i].checked) 
				period = document.trend_form.trend_select_period[i].value;
		}
		
		// Custom dates?
		var custom_dates = $('custom_dates').value;
		
		// Update saved hidden values.
		$('select_period').value = period;
		
		BWPortPrices.trendGraph($('trendgraph'), 530, 300, date_start, date_end, port_code, grade, period, custom_dates);
		
	},
	
	updateTrendSummary: function() {
		
		// Get the value of the period.
		var period = 10;
		for (var i=0; i < document.trend_form.trend_select_period.length; i++) {
			if(document.trend_form.trend_select_period[i].checked) 
				period = document.trend_form.trend_select_period[i].value;
		}
				
		var trend_text = '<p>This graph shows the net direction trend of a '+period+' day period for a given day.  The magnitude';
		trend_text += 'of that change is ignored.  For example, if '+(4*(period/5))+' of the '+period+' prices over the last ';
		if(period == 5) trend_text += 'week ';
		else if(period == 10) trend_text += '2 weeks ';
		else trend_text += '4 weeks ';		
		trend_text += 'were up, and '+(period/5)+' ';
		if(period == 5) trend_text += 'was ';
		else trend_text += 'were ';
		trend_text += 'down, the net direction would be ';
		trend_text += '+'+(3*(period/5))+', i.e. there are '+(3*(period/5))+' days of prices rising that are not ';
		trend_text += 'countered by a day where prices fell.</p>';
		
		// Update saved hidden values.
		$('select_period').value = period;
		
		$('trend_text').update(trend_text);		
		
	},
	
	zoomGraph: function(which_graph) {
		
		// Get all our common arguments.
		var date_start = $('date_start').value;
		var date_end = $('date_end').value;
		var port_code = $('port_code').value;
		var grade = $('grade').value;
		
		// Custom dates?
		var custom_dates = $('custom_dates').value;
		
		// Which graph are we showing?
		switch(which_graph) {
		
			case 'compare_top':
				
				// Get the required arguments for the top compare graph.
				
				// Get the value of the port we are comparing against.
				var region_code = $('region_code').value; // Which drop down?
				var compare = $('port_select_'+region_code).value;
				
				// Are we showing the spread?
				var show_spread = 1;
				for (var i=0; i < document.compare_form.compare_select_data.length; i++) {
					if(document.compare_form.compare_select_data[i].checked) 
						show_spread = document.compare_form.compare_select_data[i].value;
				}
				
				// Open the new window with the zoomed-in graph.
				var urlstring = '/prices/prices_graph.php?graph=compare_top&port_code='+port_code.replace(' ', '_');
				urlstring += '&grade='+grade+'&date_start='+date_start+'&date_end='+date_end;
				urlstring += '&show_spread='+show_spread+'&compare='+compare.replace(' ','_');
				urlstring += '&custom_dates='+custom_dates;
				window.open(urlstring, '', 'width=850,height=600,scrollbars=yes,location=no,status=no');
								
				break;
				
			case 'compare_bot':
				
				// Get the required arguments for the bottom compare graph.
				
				// Get the value of which axis we are showing the differential about.
				var about = 'avg';
				for (var i=0; i < document.compare_form.compare_select_axis.length; i++) {
					if(document.compare_form.compare_select_axis[i].checked) 
						about = document.compare_form.compare_select_axis[i].value;
				}
				
				// Get the value of the port we are comparing against.
				var region_code = $('region_code').value; // Which drop down?
				var compare = $('port_select_'+region_code).value;
				
				// Open the new window with the zoomed-in graph.
				var urlstring = '/prices/prices_graph.php?graph=compare_bot&port_code='+port_code.replace(' ', '_');
				urlstring += '&grade='+grade+'&date_start='+date_start+'&date_end='+date_end;
				urlstring += '&about='+about+'&compare='+compare.replace(' ','_');
				urlstring += '&custom_dates='+custom_dates;
				window.open(urlstring, '', 'width=850,height=600,scrollbars=yes,location=no,status=no');
				
				break;
		
			case 'maverage_top':
				
				// Get the required arguments for the top moving average graph.
				
				// Get the value of the period.
				var period = 10;
				for (var i=0; i < document.maverage_form.maverage_select_period.length; i++) {
					if(document.maverage_form.maverage_select_period[i].checked) 
						period = document.maverage_form.maverage_select_period[i].value;
				}
				
				// Open the new window with the zoomed-in graph.
				var urlstring = '/prices/prices_graph.php?graph=maverage_top&port_code='+port_code.replace(' ', '_');
				urlstring += '&grade='+grade+'&date_start='+date_start+'&date_end='+date_end;
				urlstring += '&period='+period;
				urlstring += '&custom_dates='+custom_dates;
				window.open(urlstring, '', 'width=850,height=600,scrollbars=yes,location=no,status=no');
				
				break;
				
			case 'maverage_bot':
				
				// Get the required arguments for the bottom moving average graph.
				
				// Get the value of the period.
				var period = 10;
				for (var i=0; i < document.maverage_form.maverage_select_period.length; i++) {
					if(document.maverage_form.maverage_select_period[i].checked) 
						period = document.maverage_form.maverage_select_period[i].value;
				}
				
				// Open the new window with the zoomed-in graph.
				var urlstring = '/prices/prices_graph.php?graph=maverage_bot&port_code='+port_code.replace(' ', '_');
				urlstring += '&grade='+grade+'&date_start='+date_start+'&date_end='+date_end;
				urlstring += '&period='+period;
				urlstring += '&custom_dates='+custom_dates;
				window.open(urlstring, '', 'width=850,height=600,scrollbars=yes,location=no,status=no');
				
				break;
		
			case 'overview_top':
			
				// Get the required arguments for the top overview graph.
				
				// Get the value of the index.
				var index = 'bwi';
				for (var i=0; i < document.overview_form.overview_select_overlay.length; i++) {
					if(document.overview_form.overview_select_overlay[i].checked) 
						index = document.overview_form.overview_select_overlay[i].value;
				}
				
				// Are we rebasing?
				var rebase = 1;
				for (var i=0; i < document.overview_form.overview_select_rebase.length; i++) {
					if(document.overview_form.overview_select_rebase[i].checked) 
						rebase = document.overview_form.overview_select_rebase[i].value;
				}
				
				// Are we showing the spread?
				var show_spread = 1;
				for (var i=0; i < document.overview_form.overview_select_data.length; i++) {
					if(document.overview_form.overview_select_data[i].checked) 
						show_spread = document.overview_form.overview_select_data[i].value;
				}
				
				// Open the new window with the zoomed-in graph.
				var urlstring = '/prices/prices_graph.php?graph=overview_top&port_code='+port_code.replace(' ', '_');
				urlstring += '&grade='+grade+'&date_start='+date_start+'&date_end='+date_end;
				urlstring += '&show_spread='+show_spread+'&rebase='+rebase+'&index='+index;
				urlstring += '&custom_dates='+custom_dates;
				window.open(urlstring, '', 'width=850,height=600,scrollbars=yes,location=no,status=no');
				
				break;
				
			case 'overview_bot':
				
				// Get the required arguments for the bottom overview graph.
				
				// Get the value of the index.
				var index = 'bwi';
				for (var i=0; i < document.overview_form.overview_select_overlay.length; i++) {
					if(document.overview_form.overview_select_overlay[i].checked) 
						index = document.overview_form.overview_select_overlay[i].value;
				}
				
				// Are we rebasing?
				var rebase = 1;
				for (var i=0; i < document.overview_form.overview_select_rebase.length; i++) {
					if(document.overview_form.overview_select_rebase[i].checked) 
						rebase = document.overview_form.overview_select_rebase[i].value;
				}
								
				// Get the value of which axis we are showing the differential about.
				var about = 'avg';
				for (var i=0; i < document.overview_form.overview_select_axis.length; i++) {
					if(document.overview_form.overview_select_axis[i].checked) 
						about = document.overview_form.overview_select_axis[i].value;
				}
				
				// Open the new window with the zoomed-in graph.
				var urlstring = '/prices/prices_graph.php?graph=overview_bot&port_code='+port_code.replace(' ', '_');
				urlstring += '&grade='+grade+'&date_start='+date_start+'&date_end='+date_end;
				urlstring += '&about='+about+'&rebase='+rebase+'&index='+index;
				urlstring += '&custom_dates='+custom_dates;
				window.open(urlstring, '', 'width=850,height=600,scrollbars=yes,location=no,status=no');
				
				break;
										
			case 'trend':
				
				// Get the required arguments for the trend graph.
				
				// Get the value of the period.
				var period = 10;
				for (var i=0; i < document.trend_form.trend_select_period.length; i++) {
					if(document.trend_form.trend_select_period[i].checked) 
						period = document.trend_form.trend_select_period[i].value;
				}
				
				// Open the new window with the zoomed-in graph.
				var urlstring = '/prices/prices_graph.php?graph=trend&port_code='+port_code.replace(' ', '_');
				urlstring += '&grade='+grade+'&date_start='+date_start+'&date_end='+date_end;
				urlstring += '&period='+period;
				urlstring += '&custom_dates='+custom_dates;
				window.open(urlstring, '', 'width=850,height=600,scrollbars=yes,location=no,status=no');
				
				break;
			
		}
			
	}
	
};