var FORM_STATE;		//global reference to event lookup state form
var ChartHoverObjs = [];
var ChartIds = [];
var ChartImageCache = [];
var ChartMapCache = [];
var CURRENT_TRADING_HORIZON;

function initialize() {
	FORM_STATE = window.document.event_lookup_state;	
	var FORM_LOOKUP = window.document.event_lookup;	
	
	onMouseOver = function() {}
	// symbol entry form
	if (FORM_LOOKUP) {	
		var instrumentType;
		if (FORM_LOOKUP.instrument_type.options) {
			instrumentType = FORM_LOOKUP.instrument_type.options[FORM_LOOKUP.instrument_type.options.selectedIndex].value;
		} else {
			instrumentType = FORM_LOOKUP.instrument_type.value;
		}
	
		var selectedExchange = FORM_LOOKUP.exchange.options.selectedIndex;
		ResetExchangeList(instrumentType, FORM_LOOKUP.exchange, 'symbol');
		FORM_LOOKUP.exchange.options[FORM_LOOKUP.exchange.options.selectedIndex].selected = false;
		FORM_LOOKUP.exchange.options[selectedExchange].selected = true;
		if (GetProgrammingFieldValue('CurrentPage') != 'index') {
			FORM_LOOKUP.symbol.focus();
		}
	}
	
	// load results and chart
        if (GetProgrammingFieldValue('OutputFormat') != 'normal') {
	  setPrintChart(FORM_STATE.trading_horizon.value);
	  ChangeResultList(FORM_STATE.result_list.value, 'results_' + FORM_STATE.trading_horizon.value);
	} else {
	  if (FORM_STATE && document.getElementById('chart')) {
	    if (typeof(ChartHover) != 'undefined') ChartHover.ChangeHoversState(FORM_STATE.hover_state.value);
	    changeTradingHorizon(FORM_STATE.trading_horizon.value);
	    updateChartStyle(FORM_STATE.chart_style.value);
	    CURRENT_TRADING_HORIZON = FORM_STATE.trading_horizon.value;
	    ChangeResultList(FORM_STATE.result_list.value, 'results_' + FORM_STATE.trading_horizon.value); 
	  } 
	}
}

function setPrintChart(TRADING_HORIZON) {
  document.getElementById('chart').src = document.getElementById('chart_PRINT').value + '&' + document.getElementById('chart_'+TRADING_HORIZON).value;
}

function setChart(TRADING_HORIZON) {
  var ajax = new AjaxRequest();
  var current_chart = TRADING_HORIZON+FORM_STATE.chart_style.value;
  var current_chart_style = FORM_STATE.chart_style.value; 

  if (typeof(ChartHover) != 'undefined') {
    ChartHover.DisableAllHovers();
  }

  var chart_id;
  if (ChartIds[current_chart]) {
    AjaxRequest.AbortAllCallsWithCommand('instrumentsummarychartid');
    chart_id = ChartIds[current_chart];
    document.getElementById('chart').src = ChartImageCache[chart_id].src;
    if (typeof(ChartHover) != 'undefined') {
      if (!ChartHover.EnableCachedHover(chart_id)) {
	new ChartHover(chart_id, document.getElementById("chart"), document.getElementById("chartmap"));
      };
    }
  } else {	
    new AjaxRequest({global_behavior:'force'}).api('instrumentsummarychartid',{xml_tag: 'chart_id', url_params: document.getElementById('chart_' + TRADING_HORIZON).value,
			success: function(response) {
			chart_id = response;
			ChartIds[current_chart] =  chart_id;
			ChartImageCache[chart_id] = new Image();
			ChartImageCache[chart_id].src = document.getElementById('chart_URL').value + '&chi='+ChartIds[current_chart];
			document.getElementById('chart').src = ChartImageCache[chart_id].src;
			if ((typeof(ChartHover) != 'undefined') && (chart_id != '')) {
			   new ChartHover(chart_id, document.getElementById("chart"), document.getElementById("chartmap"));
			}
		      }
	
		    });	
  }
}
 
function updateChartStyle(STYLE) {
	
	// Styles are in pulldown field
	if (document.getElementById('ChartStylePulldown')) {
		var ChartStyleField = document.getElementById('ChartStylePulldown');
		if (GetValue(ChartStyleField) != STYLE) {
			SelectOptionValue(STYLE, ChartStyleField)
		}
		
	// Styles are links on the page
	} else if (document.getElementById('ChartStyleB')) {
		document.getElementById('ChartStyleB').style.fontWeight = 'normal';
		document.getElementById('ChartStyleC').style.fontWeight = 'normal';
		document.getElementById('ChartStyleL').style.fontWeight = 'normal';
		document.getElementById('ChartStyleM').style.fontWeight = 'normal';
		
		document.getElementById('ChartStyleB').style.cursor = 'pointer';
		document.getElementById('ChartStyleC').style.cursor = 'pointer';
		document.getElementById('ChartStyleL').style.cursor = 'pointer';
		document.getElementById('ChartStyleM').style.cursor = 'pointer';
		
		document.getElementById('ChartStyle' + STYLE).style.fontWeight = 'bold';
		document.getElementById('ChartStyle' + STYLE).style.cursor = 'auto';
	}
	
	if (FORM_STATE.chart_style.value != STYLE) {
		
		FORM_STATE.chart_style.value = STYLE;
		if (typeof(ChartHover) != 'undefined') {
			document.getElementById("chart").onload = null;	
		}
	 
		document.getElementById('chart').src = document.getElementById('chart_loading').value;
		
		var chart_src = document.getElementById('chart_ALL').value;
		document.getElementById('chart_ALL').value = chart_src.replace(/cst=./, 'cst=' + STYLE);
		
		var chart_src = document.getElementById('chart_S').value;
		document.getElementById('chart_S').value = chart_src.replace(/cst=./, 'cst=' + STYLE);
		
		var chart_src = document.getElementById('chart_I').value;
		document.getElementById('chart_I').value = chart_src.replace(/cst=./, 'cst=' + STYLE);
		
		var chart_src = document.getElementById('chart_L').value;
		document.getElementById('chart_L').value = chart_src.replace(/cst=./, 'cst=' + STYLE);
		
	 	setChart(FORM_STATE.trading_horizon.value);
	}
}

function changeTradingHorizon(TRADING_HORIZON) {

	if (document.getElementById('thz_' + TRADING_HORIZON)) {
		if (typeof(ChartHover) != 'undefined') {
			document.getElementById("chart").onload = null;	
		}
		document.getElementById('chart').src = document.getElementById('chart_loading').value;
		CURRENT_TRADING_HORIZON = TRADING_HORIZON;
		setChart(TRADING_HORIZON);
		
		FORM_STATE.trading_horizon.value = TRADING_HORIZON;
		document.getElementById('thz_ALL').style.display = 'none';
		document.getElementById('thz_S').style.display = 'none';
		document.getElementById('thz_I').style.display = 'none';
		document.getElementById('thz_L').style.display = 'none';
		document.getElementById('thz_' + TRADING_HORIZON).style.display = 'inline';
		
		ChangeResultList(FORM_STATE.result_list.value, 'results_' + TRADING_HORIZON);
	
		if (document.getElementById('summary_' + TRADING_HORIZON)) {
			document.getElementById('summary_ALL').style.display = 'none';
			document.getElementById('summary_S').style.display = 'none';
			document.getElementById('summary_I').style.display = 'none';
			document.getElementById('summary_L').style.display = 'none';
			document.getElementById('summary_' + TRADING_HORIZON).style.display = 'inline';
		}
	}

	
}

function saveSettings(FORM) {
	var Preferences = new Array();
	
	Preferences['el_cst'] = FORM.chart_style.value;
	Preferences['el_thz'] = FORM.trading_horizon.value;
	Preferences['el_results'] = FORM.result_list.value;
	if (typeof(ChartHover) != 'undefined') Preferences['hover_state'] = ChartHover.GetHoversState();
	setPreferenceValues('Page', Preferences);
	
}

function useSettings(FORM, SETTING) {
	var url = './serve.shtml?page=event_lookup' + GetProgrammingFieldValue('QueryStringParameters');
	
	if (FORM.symbol.value && FORM.symbol.value != '') {
		
		url += '&instrument_type=' + FORM.instrument_type.value;
		url += '&symbol=' + FORM.symbol.value;
		url += '&exchange=' + FORM.exchange.value;
	}
	
	random_number = (Math.round((Math.random()*9999)+1))
	window.location.replace(url + '&use_setting=' + SETTING + '&refresh' + random_number + '=1');
}
