function handle_refresh_change( menu_item ) {
	//change to a new interval to refresh
	global.refresh_timeout = menu_item.initialConfig.sec;
	set_refresh();
}

function handle_auto_refresh( button ) {
	//enable - disable auto refreshing.
	global.auto_refresh = !global.auto_refresh;
	set_refresh();
}

function set_refresh() {
	//clear the old timer
	clearInterval(global.refresh_tag);
	// if auto refresh is on, start it.
	if (global.auto_refresh == true) {	
		//setup a new timeout
		global.refresh_tag = setInterval( refresh, global.refresh_timeout );
	}
	//refresh now
	write_cookie();
	refresh('user');
}

function handle_gallops(button) {
	global.show_gallops = !global.show_gallops;
	write_cookie();
	refresh('user');
}

function handle_greyhounds(button) {
	global.show_greyhounds = !global.show_greyhounds;
	write_cookie();
	refresh('user');
}

function handle_harness(button) {
	global.show_harness = !global.show_harness;
	write_cookie();
	refresh('user');
}

function handle_scratchings(item, checked) {
	global.show_scratchings = checked;
	// They have selected a new setting, we now dont need to halt, regardless.
	global.refresh_halt = false;
	write_cookie();
	refresh('user');
}

function handle_showplace(item, checked) {
	global.show_place = checked;
	// They have selected a new setting, we now dont need to halt, regardless.
	global.refresh_halt = false;
	write_cookie();
	refresh('user');
}

function handle_showwin(item, checked) {
	global.show_win = checked;
	// They have selected a new setting, we now dont need to halt, regardless.
	global.refresh_halt = false;
	write_cookie();
	refresh('user');
}

function handle_showfgraph(item, checked) {
	global.show_fgraph = checked;
	// They have selected a new setting, we now dont need to halt, regardless.
	global.refresh_halt = false;
	write_cookie();
	refresh('user');
}

function handle_autonextrace(item, checked) {
	global.Race_Autoload = checked;
	// They have selected a new setting, we now dont need to halt, regardless.
	global.refresh_halt = false;
	write_cookie();
	refresh('auto');
}

function handle_fullstats(item, checked) {
	global.show_fullstats = checked;
	//hide or show the full stats.
	runner_cols.setHidden(runner_cols.getIndexById( "StatsOverallRank"),!global.show_fullstats);
	runner_cols.setHidden(runner_cols.getIndexById( "StatsHorseRank"),!global.show_fullstats);
	runner_cols.setHidden(runner_cols.getIndexById( "StatsTrainerRank"),!global.show_fullstats);
	runner_cols.setHidden(runner_cols.getIndexById( "StatsJockeyRank"),!global.show_fullstats);
	runner_cols.setHidden(runner_cols.getIndexById( "StatsStallionRank"),!global.show_fullstats);
	write_cookie();
	if (global.show_fullstats) {
		global.refresh_halt = false;
		refresh('user');
	}
}

function handle_reduction(item, checked) {
	global.show_reductionf = checked;
	runner_cols.setHidden(runner_cols.getIndexById( "BF_WIN_REDUCTION"),!global.show_reductionf); 
	write_cookie();
}

function handle_show_bfwap(item, checked) {
	global.show_bfwap = checked;
	runner_cols.setHidden(runner_cols.getIndexById( "BF_WIN_WAP"),!global.show_bfwap); 
	write_cookie();
}

function handle_show_barrier(item, checked) {
	global.show_barrier = checked;
	runner_cols.setHidden(runner_cols.getIndexById( "Barrier"),!global.show_barrier); 
	write_cookie();
}

function handle_show_WGT(item, checked) {
	global.show_WGT = checked;
	runner_cols.setHidden(runner_cols.getIndexById( "Handicap"),!global.show_WGT); 
	write_cookie();
}

function handle_show_jockey(item, checked) {
	global.show_jockey = checked;
	runner_cols.setHidden(runner_cols.getIndexById( "Jockey"),!global.show_jockey); 
	write_cookie();
}

function handle_show_bflay(item, checked) {
	global.show_bflay = checked;
	runner_cols.setHidden(runner_cols.getIndexById( "BF_WIN_LAY"),!global.show_bflay); 
	write_cookie();
}

function handle_show_stab(item, checked) {
	global.show_stab = checked;
	runner_cols.setHidden(runner_cols.getIndexById( "STAB_WIN_BACK"),!global.show_stab); 
	write_cookie();
}

function handle_show_nsw(item, checked) {
	global.show_nsw = checked;
	runner_cols.setHidden(runner_cols.getIndexById( "NSW_WIN_BACK"),!global.show_nsw); 
	write_cookie();
}

function handle_show_uni(item, checked) {
	global.show_uni = checked;
	runner_cols.setHidden(runner_cols.getIndexById( "UNI_WIN_BACK"),!global.show_uni); 
	write_cookie();
}

function handle_show_booksp(item, checked) {
	global.show_booksp = checked;
	runner_cols.setHidden(runner_cols.getIndexById( "TABSP"),!global.show_booksp); 
	write_cookie();
}

function handle_show_silk(item, checked) {
	global.show_silk = checked;
	runner_cols.setHidden(runner_cols.getIndexById( "SilkURL"),!global.show_silk); 
	write_cookie();
}

function handle_results(item, checked) {
	global.show_results = checked;
	write_cookie();
	runner_cols.setHidden(runner_cols.getIndexById( "Placed"),!global.show_results); 
}

function handle_advantage(item, checked) {
	global.show_advantage = checked;
	runner_cols.setHidden(runner_cols.getIndexById( "Advantage" ),!global.show_advantage); 
	write_cookie();
}

function handle_advantage_change( menu_item ) {
	global.advantage_type = menu_item.initialConfig.adv;
	// They have selected a new setting, we now dont need to halt, regardless.
	global.refresh_halt = false;
	write_cookie();
	refresh('user');
}

function handle_advantage_pool_change( menu_item ) {
	global.advantage_percentage = menu_item.initialConfig.adv_pool;
	// They have selected a new setting, we now dont need to halt, regardless.
	global.refresh_halt = false;
	write_cookie();
	refresh('user');
}

function handle_bfcommission_change( menu_item ) {
	global.my_commission = menu_item.initialConfig.commission;
	// They have selected a new setting, we now dont need to halt, regardless.
	global.refresh_halt = false;
	write_cookie();
	refresh('user');
}

function handle_races_grid_rowselect( model, row_index ) {
	
	var row = race_date_grid.getStore().getAt( row_index );
	//store some global variables that are useful.
	global.MarketID = row.get( 'MarketID' );
	global.RaceNumber = row.get( 'RaceNumber' );
	global.TrackCode = row.get( 'TrackCode' );
	// They have selected a new race, we now dont need to halt, regardless.
	global.refresh_halt = false;
	// refresh (show the data)
	refresh('user');
}

function handle_help( button ) {
	text = '<iframe width="670px" height="600px" id="help_frame" style="border: medium none ; margin: 0px; padding: 0px; overflow: auto;" src="help/help.html"></iframe>';
	Ext.Msg.show({title: 'Help - Betfair Racing Zone', msg: text, buttons: '', minWidth: 700});
}

function handle_about( button ) {
	text = '<center><br>Racing Zone v2.0 <br><br>Copyright 2009 Betfair Pty Ltd';
	text = text + '<br><br>Contact Email: <a href="mailto:racingfeedback@betfair.com">racingfeedback@betfair.com</a></center>';
	Ext.Msg.show({title: 'About - Betfair Racing Zone', msg: text, buttons: '', minWidth: 400});
}

function handle_miniform(Runner) {
	
	Runner = Runner.replace(/%20/g,'+');
	Runner = Runner.replace(/ /g,'+');
	Runner = Runner.replace("++",'+');
	
	t = global.RaceDate.split("-");
	today = t[2] + "-" + t[1] + "-" + t[0];
	
	text = '<table><tr><td class="boxes" >';
	if ((global.MarketType == "R") && (global.MarketCountry == "AU")) { 
		text = text + '<center><b>Full Form:</b> <i>(if Avaliable)</i><br></center>';
		text = text + '<br><p><iframe id="form_frame" style="border: medium none ; margin: 0px; padding: 0px; overflow: auto;" width="100%" height="300px" src="/racing_scripts/getform.php?horse=' + Runner + '&date=' + today + '"></iframe>';
	}
	text = text + '<center><b>Mini Form</b><br><i>Example: 803x71, shown in miniForm column.</i></center>';
	text = text + '<br>This indicates how the runner has fared in recent runs from oldest left to most recent right, where:';
	text = text + '<br><br>1-9 indicates the position the runner ran.';
	text = text + '<br>0 indicates the runner ran greater than 9th place.';
	text = text + '<br>x indicates the runner has had a spell greater than 3 months.';
	text = text + '<br><br><center><b>Runner Movement</b><br><i>Example: FFFSSSDDF, shown in miniForm column.</i></center>';
	text = text + '<br>This information is also available as a graph, you can enable it in the settings menu.';
	text = text + '<br><br>The data indicates how the runner price is currently changing, where:';
	text = text + '<br><br>D is Drifting (the price is increasing or going out).';
	text = text + '<br>F is Firming (the price is decreasing or coming in).';
	text = text + '<br>S is Steady (the price is remaining the same).';
	text = text + '<br>This data is updated more frequently towards the start of an event, but will only update each time the racing zone refreshes in your browser.';
	text = text + 'The data is based on the BF Weighted Average Price (WAP).';
	text = text + "</td></tr></table>";
	Ext.Msg.show({title: 'Form Statistics:', msg: text, buttons: '', minWidth: 850});
}

function handle_trainer(Trainer,Owner) {
	Trainer = Trainer.replace(/%20/g,'+');
	Trainer = Trainer.replace(/ /g,'+');
	Trainer = Trainer.replace("++",'+');
	text = '<table><tr><td height="400px" width="700px" class="boxes" ><b>Owner: </b>' + Owner + '<br><iframe id="trainer_frame" style="border: medium none ; margin: 0px; padding: 0px; overflow: auto; width: 100%; height: 100%;" src="/racing_scripts/getstats.php?trainer=' + Trainer + '"></td></tr></table>';
	Ext.Msg.show({title: 'Trainer & Owner Information:', msg: text, buttons: '', minWidth: 600});
}

function handle_jockey(Jockey) {
	Jockey = Jockey.replace(/%20/g,'+');
	Jockey = Jockey.replace(/ /g,'+');
	Jockey = Jockey.replace("++",'+');
	text = '<table><tr><td height="400px" width="700px" class="boxes" ><iframe id="jockey_frame" style="border: medium none ; margin: 0px; padding: 0px; overflow: auto; width: 100%; height: 100%;" src="/racing_scripts/getstats.php?jockey=' + Jockey + '"></td></tr></table>';
	Ext.Msg.show({title: 'Jockey Statistics:', msg: text, buttons: '', minWidth: 600});
}

function handle_allaz() {
	t = global.RaceDate.split("-");
	today = t[2] + "-" + t[1] + "-" + t[0];
	
	text = '<br><p><iframe id="all_scratchings" style="border: medium none ; margin: 0px; padding: 0px; overflow: auto;" width="100%" height="300px" src="/racing_scripts/getaz.php?date=' + today + '&type=' + global.MarketType + '"></iframe>';
	Ext.Msg.show({title: 'A to Z Lists for ' + global.RaceDate, msg: text, buttons: '', minWidth: 600});
}

function handle_allresults() {
	t = global.RaceDate.split("-");
	today = t[2] + "-" + t[1] + "-" + t[0];
	
	text = '<br><p><iframe id="all_scratchings" style="border: medium none ; margin: 0px; padding: 0px; overflow: auto;" width="100%" height="300px" src="/racing_scripts/getresults.php?date=' + today + '&type=' + global.MarketType + '"></iframe>';
	Ext.Msg.show({title: 'Results for ' + global.RaceDate, msg: text, buttons: '', minWidth: 650});
}

function handle_allscratchings() {
	t = global.RaceDate.split("-");
	today = t[2] + "-" + t[1] + "-" + t[0];
	
	text = '<br><p><iframe id="all_scratchings" style="border: medium none ; margin: 0px; padding: 0px; overflow: auto;" width="100%" height="300px" src="/racing_scripts/getscratchings.php?date=' + today + '&type=' + global.MarketType + '"></iframe>';
	Ext.Msg.show({title: 'Scratchings for ' + global.RaceDate, msg: text, buttons: '', minWidth: 650});
}

function handle_allform() {
	t = global.RaceDate.split("-");
	today = t[2] + "-" + t[1] + "-" + t[0];
	text = '<br><p><iframe id="all_form" style="border: medium none ; margin: 0px; padding: 0px; overflow: auto;" width="100%" height="500px" src="/racing_scripts/getcompleteform.php?date=' + today + '&track=' + global.TrackCode + '&race=' + global.RaceNumber + '"></iframe>';
	Ext.Msg.show({title: 'Complete form for ' + race_details_store.getAt(0).get('VenueName') + ' race ' + global.RaceNumber, msg: text, buttons: '', minWidth: 800});
}

function handle_graph(URL) {
	text = '<table><tr><td height="500px" width="800px" class="boxes" ><iframe id="graph_frame" style="border: medium none ; margin: 0px; padding: 0px; overflow: auto; width: 100%; height: 100%;" src="' + URL + '"></td></tr></table>';
	Ext.Msg.show({title: 'Market Details:', msg: text, buttons: '', minWidth: 850});
}

function handle_svcerror() {
	errormsg = race_details_store.getAt(0).get('ServiceStatus');
	Ext.Msg.show({title: 'Degraded Service Notice.', width: 400, buttons: '', msg: "<center><br>The following services within the racing zone<br />may be currently degraded:<br /><br />" + errormsg + "<br /></center>"});
}

