function mainPage_onLoad(page_name){
	
	if(page_name = 'detailed_charting'){
		chart_type_selected();
		timeframe_selected();
	}
	
}


function reload_graph(graph_form, ampSign, quoteSecurity, volSecurity, quoteDivBy, volDivBy, isMain, quoteTitle, volTitle){

	var selIndex = graph_form.graph_select.selectedIndex;
	var value = graph_form.graph_select[selIndex].value.split(",");
	var timeframe = value[0];
	var frequency = value[1];
	
	var graph_specs_url = "frequency=" + frequency + ampSign + "timeframe=" + timeframe + ampSign + "main=" + isMain;
	var quote_url = "chart.php?type=quote" + ampSign + "security=" + quoteSecurity + ampSign + graph_specs_url + ampSign + "divValBy=" + quoteDivBy + ampSign + "title=" + quoteTitle;
	var volume_url = "chart.php?type=volume" + ampSign + "security=" + volSecurity + ampSign + graph_specs_url + ampSign + "divValBy=" + volDivBy + ampSign + "title=" + volTitle;
	
	document.main_chart.SetVariable("update_url", quote_url);
	document.main_volume_chart.SetVariable("update_url", volume_url);
	
}

function open_top_link(linkName){

	var newWin = window.open(linkName,"infowindow","height=570,width=450,resizable=no,scrollbars=yes");
	
	newWin.focus();

}

function add_comment(code){

	open_top_link('add_comment.php?topic=' + code);
}


function reload_detailed_chart(security){

	var ampSign = '&';

	var timeframeIndex = detailed_chart_form.timeframe_select.selectedIndex;
	var timeframe = detailed_chart_form.timeframe_select[timeframeIndex].value;
	
	var frequencyIndex = detailed_chart_form.frequency_select.selectedIndex;
	var frequency = detailed_chart_form.frequency_select[frequencyIndex].value;
	
	var graph_specs_url = "frequency=" + frequency + ampSign + "timeframe=" + timeframe;
			
	var top_chartTypeIndex = detailed_chart_form.top_charttype_select.selectedIndex;
	var top_chartType = detailed_chart_form.top_charttype_select[top_chartTypeIndex].value;
	
	var bottom_chartTypeIndex = detailed_chart_form.bottom_charttype_select.selectedIndex;
	var bottom_chartType = detailed_chart_form.bottom_charttype_select[bottom_chartTypeIndex].value;
	
	var compareindexIndex = detailed_chart_form.compareindex_select.selectedIndex;
	var compareIndex = detailed_chart_form.compareindex_select[compareindexIndex].value;
	
	var comparesymbolIndex = detailed_chart_form.comparesymbol_select.selectedIndex;
	var compareSymbol = detailed_chart_form.comparesymbol_select[comparesymbolIndex].value;
	
	var d1Index = detailed_chart_form.start_day_select.selectedIndex;
	var d1 = detailed_chart_form.start_day_select[d1Index].value;
	
	var m1Index = detailed_chart_form.start_month_select.selectedIndex;
	var m1 = detailed_chart_form.start_month_select[m1Index].value;
	
	var y1Index = detailed_chart_form.start_year_select.selectedIndex;
	var y1 = detailed_chart_form.start_year_select[y1Index].value;
	
	var d2Index = detailed_chart_form.end_day_select.selectedIndex;
	var d2 = detailed_chart_form.end_day_select[d2Index].value;
	
	var m2Index = detailed_chart_form.end_month_select.selectedIndex;
	var m2 = detailed_chart_form.end_month_select[m2Index].value;
	
	var y2Index = detailed_chart_form.end_year_select.selectedIndex;
	var y2 = detailed_chart_form.end_year_select[y2Index].value;
	
	graph_specs_url = graph_specs_url + ampSign + "d1=" + d1 + ampSign + "m1=" + m1 + ampSign + "y1=" + y1 + ampSign + "d2=" + d2 + ampSign + "m2=" + m2 + ampSign + "y2=" + y2;
	
	var top_url = "detailed_chart.php?type=" + top_chartType + ampSign + "security=" + security + ampSign + "compareToSymbol=" + compareSymbol + ampSign + "compareToIndex=" + compareIndex + ampSign + graph_specs_url;
	var bottom_url = "detailed_chart.php?type=" + bottom_chartType + ampSign + "security=" + security + ampSign + graph_specs_url;
	
	var date1 = new Date();
	date1.setDate(d1);
	date1.setMonth(m1);
	date1.setYear(y1);
	
	var date2 = new Date();
	date2.setDate(d2);
	date2.setMonth(m2);
	date2.setYear(y2);
	
	
	// validate values
	var validate = true;
	timeArr = timeframe.split('-');
	if(timeArr[1] == 'y' && timeArr[0] > 1 && frequency == 'd'){
		alert("for timeframes greater than 1 year please select a wider frequency");
		validate = false;
	}
	if((date2 - date1)/(1000*60*60*24) > 365 && frequency == 'd'){
		alert("for timeframes greater than 1 year please select a wider frequency");
		validate = false;
	}
	if(date2 < date1){
		alert("the start date has to be prior to the end date");
		validate = false;
	}
	if(validate){
		update_detailed_chart(top_url, bottom_url);
	}
}

function update_detailed_chart(top_url, bottom_url){
	
	document.top_chart.SetVariable("update_url", top_url);
	document.bottom_chart.SetVariable("update_url", bottom_url);
}


function chart_type_selected(){

	var top_chartTypeIndex = detailed_chart_form.top_charttype_select.selectedIndex;
	var top_chartType = detailed_chart_form.top_charttype_select[top_chartTypeIndex].value;
	
	if(top_chartType == 'compare'){
		detailed_chart_form.comparesymbol_select.disabled = false;
		detailed_chart_form.compareindex_select.disabled = false;
	} else{
		detailed_chart_form.comparesymbol_select.disabled = true;
		detailed_chart_form.compareindex_select.disabled = true;
	}

}

function detailed_chart_timeframe_selected(){

	var timeframeIndex = detailed_chart_form.timeframe_select.selectedIndex;
	var timeframe = detailed_chart_form.timeframe_select[timeframeIndex].value;
	
	if(timeframe == '0-s'){
		
		detailed_chart_form.start_day_select.disabled = false;
		detailed_chart_form.start_month_select.disabled = false;
		detailed_chart_form.start_year_select.disabled = false;
		detailed_chart_form.end_day_select.disabled = false;
		detailed_chart_form.end_month_select.disabled = false;
		detailed_chart_form.end_year_select.disabled = false;
	
	} else{
		detailed_chart_form.start_day_select.disabled = true;
		detailed_chart_form.start_month_select.disabled = true;
		detailed_chart_form.start_year_select.disabled = true;
		detailed_chart_form.end_day_select.disabled = true;
		detailed_chart_form.end_month_select.disabled = true;
		detailed_chart_form.end_year_select.disabled = true;
	}

}

function open_page_loader(linkName){

	var smallWin =	window.open(linkName,"loader_window","height=250,width=350,resizable=no,scrollbars=no");
	
	smallWin.focus();

}


function load_page(form) {
  
  // check stock selected is not blank
  if(form.stock_menu.options[form.stock_menu.selectedIndex].value == ""){
     alert("Please select a stock symbol");
   } else {
   
    window.opener.location= form.stock_menu[form.stock_menu.selectedIndex].value;
    window.close();
  }

}

function submit_historical_prices_form(){
	
	var d1Index = historical_prices_form.d1.selectedIndex;
	var d1 = historical_prices_form.d1[d1Index].value;
	
	var m1Index = historical_prices_form.m1.selectedIndex;
	var m1 = historical_prices_form.m1[m1Index].value;
	
	var y1Index = historical_prices_form.y1.selectedIndex;
	var y1 = historical_prices_form.y1[y1Index].value;
	
	var d2Index = historical_prices_form.d2.selectedIndex;
	var d2 = historical_prices_form.d2[d2Index].value;
	
	var m2Index = historical_prices_form.m2.selectedIndex;
	var m2 = historical_prices_form.m2[m2Index].value;
	
	var y2Index = historical_prices_form.y2.selectedIndex;
	var y2 = historical_prices_form.y2[y2Index].value;
	
	var date1 = new Date();
	date1.setDate(d1);
	date1.setMonth(m1);
	date1.setYear(y1);
	
	var date2 = new Date();
	date2.setDate(d2);
	date2.setMonth(m2);
	date2.setYear(y2);
	
	
	// validate values
	var validate = true;
	
	if((date2 - date1)/(1000*60*60*24) > 740){
		alert("please select a timeframe of 2 years or less");
		validate = false;
	}
	if(date2 < date1){
		alert("the start date has to be prior to the end date");
		validate = false;
	}
	if(validate){
		historical_prices_form.submit();
	}
}


