//---------------------------------------------------------------- Cell and Table Renderers -------------------------------------------------------------

// Date Details Renderers.

function render_code(text, cell, record, row_index, col_index, store ) {
	return "<img src='images/" + text + "_S.png'>";
}

function render_status(text, cell, record, row_index, col_index, store ) {
	return "<img src='images/sta_icon_" + text + ".png'>";
}

// Race details rederers.

function render_name(text, cell, record, row_index, col_index, store ) {
	return text;
}

function render_name_center(text, cell, record, row_index, col_index, store ) {
	return "<center>" + text + "</center>";
}

function render_lrg(text, cell, record, row_index, col_index, store ) {
	var sc = store.getAt(row_index).get('Scratched');
	if (sc == '1') return '<strike><font style="font: normal 14px Tahoma;" >' + text + '</font></strike>'; 
	else return '<font style="font: normal 14px Tahoma;" >' + text + '</font>';
}

function render_center_bold(text, cell, record, row_index, col_index, store ) {
	return "<b>" + text + "</b>";
}

function render_runner(text, cell, record, row_index, col_index, store ) {
	if ( store.getAt(row_index).get('Scratched')) return '<span class="scratched"><font style="font: normal 14px Tahoma;" >' + text + '</font></span>';
	else if ( store.getAt(row_index).get('Favourite')) return '<span class="favourite"><font style="font: normal 14px Tahoma;" >' + text + '</font></span>';
	return '<font style="font: normal 14px Tahoma;" >' + text + '</font>';
}

function render_runner_number(text, cell, record, row_index, col_index, store ) {
	if (store.getAt(row_index).get('Scratched')) return '<strike><font style="font: normal 14px Tahoma;" ><b>' + text + '</b></font></strike>'; 
	else return '<font style="font: normal 14px Tahoma;" ><b>' + text + '</b></font>';
}

function render_silk(silk, cell, record, row_index, col_index, store ) {	
	if (global.MarketType == 'G') {
		num = store.getAt(row_index).get('Number');
		return "<img src='/images/greyhounds/" + num + ".png'>";
	} else {
		if (silk) return "<img src='http://batdata.betfair.com.au/silks/" + silk + "'>"; 
		else return "<img src='/images/blank_silk.png'>";
	}
}

function render_place(text, cell, record, row_index, col_index, store ) {
	if (text == '') return '';
	else if (text == "1") return "<img src='images/first.png'></a>";
	else if (text == "2") return "<img src='images/second.png'></a>";
	else if (text == "3") return "<img src='images/third.png'></a>";	
	else if (text == "4") return '<font style="font: normal 14px Tahoma;" >4</font>';
}

function render_miniform(text, cell, record, row_index, col_index, store ) {
	if (!global.show_fgraph) {
		MoverWIN = store.getAt(row_index).get('MoverWIN');
		Runner = store.getAt(row_index).get('Name');
		link = 'handle_miniform("' + Runner + '")';
		if ((MoverWIN) || (text)) return "<a onClick='" + link + "' alt='Click to see complete form.' title='Click to see complete form.'><img align='absmiddle' src='images/more.png'><b>&nbsp;" + text + "</a><br>" + MoverWIN + "</b>";
		else return '-'; 
	} else {
		MoverWIN = store.getAt(row_index).get('MoverWIN');
		Runner = store.getAt(row_index).get('Name');
		link = 'handle_miniform("' + Runner + '")';
		table = '';
		MoverWIN = MoverWIN.split("");
		if (MoverWIN) {
			num = 10;
			table = '<table width="50px" height="10px" border="0" cellpadding="0" cellspacing="0"><tbody><tr>';
			for (loop = 0; loop < 10; loop++) {
				if (MoverWIN[loop] == "D") num--;
				else if (MoverWIN[loop] == "F") num++;
				if (MoverWIN[loop]) table = table + '<td width="5px"><table width="5px" height="' + num + 'px" border="0" cellpadding="0" cellspacing="0"><tr><td width="5px" height="' + num + 'px"></td></tr><tr><td bgcolor="#111111" width="5px" height="2px"></td></tr></table></td>';
				else table = table +                '<td width="5px"><table width="5px" height="' + num + 'px" border="0" cellpadding="0" cellspacing="0"><tr><td width="5px"></td></tr></table></td>';
			}
			table = table + "</tr></tbody></table>";
		}
		if ((MoverWIN) || (text)) return "<a onClick='" + link + "' alt='Click to see complete form.' title='Click to see complete form.'><img align='absmiddle' src='images/more.png'><b>&nbsp;" + text + "</a><a title='Runner Movement: " + MoverWIN + "'>" + table + "</a></b>";
		else return '-';
	}
}

function render_jockey(text, cell, record, row_index, col_index, store ) {
	linkj = 'handle_jockey("' + text + '")';
	Owner = store.getAt(row_index).get('Owner').replace(/'/g,'');
	Trainer = store.getAt(row_index).get('Trainer').split(" (");
	linkt = 'handle_trainer("' + Trainer[0] + '","' + Owner + '")';
	if (text) out = "<b><a onClick='" + linkj + "' alt='Click for Jockey Statistics' title='Click for Jockey Statistics'><img align='absmiddle' src='images/more.png'>&nbsp;" + text + "</a></b>"; 
	else out = '-';
	if (Owner) out = out + "<br><a onClick='" + linkt + "' alt='Click for Trainer / Owner Statistics' title='Click for Trainer / Owner Statistics'><img align='absmiddle' src='images/more.png'><b>&nbsp;" + Trainer[0] + "</a></b>&nbsp(" + Trainer[1];
	else out = out + '<br>-';
	return out;	
}

function render_trainer(text, cell, record, row_index, col_index, store ) {
	Owner = store.getAt(row_index).get('Owner').replace(/'/g,'');
	Trainer = store.getAt(row_index).get('Trainer').split(" (");
	link = 'handle_trainer("' + Trainer[0] + '","' + Owner + '")';
	if (Owner) return "<a onClick='" + link + "' alt='Click Me!' title='Click Me!'><img align='absmiddle' src='images/more.png'></a><b>&nbsp;" + Trainer[0] + "</b>(" + Trainer[1];
	else return '-';
}

function render_bf_back(BFw, cell, record, row_index, col_index, store ) {
	if (!store.getAt(row_index).get('Scratched')) {
		//market Graph
		rID = store.getAt(row_index).get('ID');
		WinURL = 'http://au.site.sports.betfair.com/betting/LoadRunnerInfoAction.do?marketId=' + global.MarketID + '&selectionId=' + rID + '&timeZone=Australia/Sydney&region=GBR&locale=en&brand=betfair&currency=AUD';
		PlcURL = 'http://au.site.sports.betfair.com/betting/LoadRunnerInfoAction.do?marketId=' + global.PlcMarketID + '&selectionId=' + rID + '&timeZone=Australia/Sydney&region=GBR&locale=en&brand=betfair&currency=AUD';
		linkw = 'handle_graph("' + WinURL + '")';
		linkp = 'handle_graph("' + PlcURL + '")';
		text2 = '';
		text = '';
		//odds
		if (global.show_win) {		
			BFw = get_price_less_commission(BFw);
			BFwm = store.getAt(row_index).get('BF_WIN_MATCHED');
			//if it's closed etc, dont show avalible, or link
			if ((global.MarketStatus == 'CLOSED') || (global.MarketStatus == 'INTERIM')) {
				if (BFw) text = "<a alt='Matched: " + format_number(BFwm,2) + "' title='Matched: " + format_number(BFwm,2) + "'>" + format_number(BFw,2); else text = "-";
			} else {
				BFwa = store.getAt(row_index).get('BF_WIN_BACK_VOL');
				if (BFw) text = "<a href='http://www.betfair.com/?mi=" + global.MarketID + "&ex=2' target='_blank' alt='Matched: " + format_number(BFwm,2) + " - Avaliable @ Current Price: " + format_number(BFwa,2) + "' title='Matched: " + format_number(BFwm,2) + " - Avaliable @ Current Price: " + format_number(BFwa,2) + "'>" + format_number(BFw,2) + "</a>"; else text = "-";
				text = text + "&nbsp;<a alt='Click Me!' title='Open Betfair Win Market Graph' onClick='" + linkw + "' ><img align='absmiddle' src='/images/chart.png'></a>";
			}
		}
		if (global.show_place) {
			BFp = get_price_less_commission(store.getAt(row_index).get('BF_PLACE_BACK'));
			BFpm = store.getAt(row_index).get('BF_PLACE_MATCHED');
			//if it's closed etc, dont show avalible, or link
			if ((global.MarketStatus == 'CLOSED') || (global.MarketStatus == 'INTERIM')) {
				if (BFp) text2 = "<a alt='Matched: " + format_number(BFpm,2) + "' title='Matched: " + format_number(BFpm,2) + "'>" + format_number(BFp,2) + "</a>"; else text2 + "-";
			} else {
				BFpa = store.getAt(row_index).get('BF_PLACE_BACK_VOL');
				if (BFp) text2 = "<a href='http://www.betfair.com/?mi=" + global.PlcMarketID + "&ex=2' target='_blank' alt='Matched: " + format_number(BFpm,2) + " - Avaliable @ Current Price: " + format_number(BFpa,2) + "' title='Matched: " + format_number(BFpm,2) + " - Avaliable @ Current Price: " + format_number(BFpa,2) + "'>" + format_number(BFp,2) + "</a>"; else text2 + "-";
				if (global.MarketStatus != "CLOSED") text2 = text2 + "&nbsp;<a alt='Click Me!' title='Open Betfair Place Market Graph' onClick='" + linkp + "' ><img align='absmiddle' src='/images/chart.png'></a><center>";
			}
		}
		
		if (!global.show_win) return "<font style='font: normal 14px Tahoma;' >" + text2 + "</font>";
		else if (!global.show_place) return "<font style='font: normal 14px Tahoma;' >" + text + "</font>";
		else return text + "<br />" + text2;
		
	} else return '';
}

function render_bf_lay(BFw, cell, record, row_index, col_index, store ) {
	if (!store.getAt(row_index).get('Scratched')) {
		if (global.show_win) {
			BFwm = store.getAt(row_index).get('BF_WIN_MATCHED');
			if ((global.MarketStatus == 'CLOSED') || (global.MarketStatus == 'INTERIM')) {
				if (BFw) text = "<a alt='Matched: " + format_number(BFwm,2) + "' title='Matched: " + format_number(BFwm,2) + "'>" + format_number(BFw,2) + "</a>"; else text = "-";
			} else {
				BFwa = store.getAt(row_index).get('BF_WIN_LAY_VOL');
				if (BFw) text = "<a href='http://www.betfair.com/?mi=" + global.MarketID + "&ex=2' target='_blank' alt='Matched: " + format_number(BFwm,2) + " - Avaliable @ Current Price: " + format_number(BFwa,2) + "' title='Matched: " + format_number(BFwm,2) + " - Avaliable @ Current Price: " + format_number(BFwa,2) + "'>" + format_number(BFw,2) + "</a>"; else text = "-";
			}
		} else text = "";
		if (global.show_place) {
			BFp = store.getAt(row_index).get('BF_PLACE_LAY');
			BFpm = get_price_less_commission(store.getAt(row_index).get('BF_PLACE_MATCHED'));
			if ((global.MarketStatus == 'CLOSED') || (global.MarketStatus == 'INTERIM')) {
				if (BFp) text2 = "<a alt='Matched: " + format_number(BFpm,2) + "' title='Matched: " + format_number(BFpm,2) + "'>" + format_number(BFp,2) + "</a>"; else text + "-";
			} else {
				BFpa = store.getAt(row_index).get('BF_PLACE_LAY_VOL');
				if (BFp) text2 = "<a href='http://www.betfair.com/?mi=" + global.PlcMarketID + "&ex=2' target='_blank' alt='Matched: " + format_number(BFpm,2) + " - Avaliable @ Current Price: " + format_number(BFpa,2) + "' title='Matched: " + format_number(BFpm,2) + " - Avaliable @ Current Price: " + format_number(BFpa,2) + "'>" + format_number(BFp,2) + "</a>"; else text2 = "-";
			}
		} else text2 = "";
		
		if (!global.show_win) return "<font style='font: normal 14px Tahoma;' >" + text2 + "</font>";
		else if (!global.show_place) return "<font style='font: normal 14px Tahoma;' >" + text + "</font>";
		else return text + "<br />" + text2;
	} else return '';
}

function render_advantage(dud, cell, record, row_index, col_index, store ) {
	if (!store.getAt(row_index).get('Scratched') && global.show_advantage) {
		//Get all the odds, take one away to get profit only.
		BFw = store.getAt(row_index).get('BF_WIN_BACK'); if (BFw) BFw = get_price_less_commission(BFw) - 1;
		BFp = store.getAt(row_index).get('BF_PLACE_BACK'); if (BFp) BFp = get_price_less_commission(BFp) - 1;
		BFWw = store.getAt(row_index).get('BF_WIN_WAP'); if (BFWw) BFWw = get_price_less_commission(BFWw) - 1;
		BFWp = store.getAt(row_index).get('BF_PLACE_WAP'); if (BFWp) BFWp = get_price_less_commission(BFWp) - 1;
		NSWw = store.getAt(row_index).get('NSW_WIN_BACK'); if (NSWw) NSWw = NSWw - 1;
		NSWp = store.getAt(row_index).get('NSW_PLACE_BACK'); if (NSWp) NSWp = NSWp - 1;
		STABw = store.getAt(row_index).get('STAB_WIN_BACK'); if (STABw) STABw = STABw - 1;
		STABp = store.getAt(row_index).get('STAB_PLACE_BACK'); if (STABp) STABp = STABp - 1;
		UNIw = store.getAt(row_index).get('UNI_WIN_BACK'); if (UNIw) UNIw = UNIw - 1;
		UNIp = store.getAt(row_index).get('UNI_PLACE_BACK'); if (UNIp) UNIp = UNIp - 1;

		//check what to compare against etc...
		switch (global.advantage_type)
		{
		//BF Back Price Comparisions
		case 'NSW':
			if (BFw && NSWw) ADVw = ((BFw - NSWw) / NSWw) * 100; else ADVw = 0;
			if (BFp && NSWp) ADVp = ((BFp - NSWp) / NSWp) * 100; else ADVp = 0;
			break;
		case 'VIC':
			if (BFw && STABw) ADVw = ((BFw - STABw) / STABw) * 100; else ADVw = 0;
			if (BFp && STABp) ADVp = ((BFp - STABp) / STABp) * 100; else ADVp = 0;
			break;
		case 'QLD':
			if (BFw && UNIw) ADVw = ((BFw - UNIw) / UNIw) * 100; else ADVw = 0;
			if (BFp && UNIp) ADVp = ((BFp - UNIp) / UNIp) * 100; else ADVp = 0;
			break;
		case 'BT':
			//work out the bext tote prices.
			BestTotew = NSWw; 
			if (BestTotew < STABw) BestTotew = STABw; 
			if (BestTotew < UNIw) BestTotew = UNIw;
			BestTotep = NSWp; 
			if (BestTotep < STABp) BestTotep = STABp; 
			if (BestTotep < UNIp) BestTotep = UNIp;
			//compare to BF.
			if (BFw && BestTotew) ADVw = ((BFw - BestTotew) / BestTotew) * 100; else ADVw = 0;
			if (BFp && BestTotep) ADVp = ((BFp - BestTotep) / BestTotep) * 100; else ADVp = 0;
			break;
		case 'LTP':
			//work out the worst tote prices.
			Totew = NSWw; 
			if (Totew > STABw) Totew = STABw; 
			if (Totew > UNIw) Totew = UNIw;
			Totep = NSWp; 
			if (Totep > STABp) Totep = STABp; 
			if (Totep > UNIp) Totep = UNIp;
			//compare to BF.
			if (BFw && Totew) ADVw = ((BFw - Totew) / Totew) * 100; else ADVw = 0;
			if (BFp && Totep) ADVp = ((BFp - Totep) / Totep) * 100; else ADVp = 0;
			break;
		case 'ATP':
			//work out the average price
			Totew = (NSWw + STABw + UNIw) / 3; 
			Totep = (NSWp + STABp + UNIp) / 3; 
			//compare to BF.
			if (BFw && Totew) ADVw = ((BFw - Totew) / Totew) * 100; else ADVw = 0;
			if (BFp && Totep) ADVp = ((BFp - Totep) / Totep) * 100; else ADVp = 0;
			break;	
		//BF WAP Comparisions
		case 'W-NSW':
			if (BFWw && NSWw) ADVw = ((BFWw - NSWw) / NSWw) * 100; else ADVw = 0;
			if (BFWp && NSWp) ADVp = ((BFWp - NSWp) / NSWp) * 100; else ADVp = 0;
			break;
		case 'W-VIC':
			if (BFWw && STABw) ADVw = ((BFWw - STABw) / NSWw) * 100; else ADVw = 0;
			if (BFWp && STABp) ADVp = ((BFWp - STABp) / NSWp) * 100; else ADVp = 0;
			break;
		case 'W-QLD':
			if (BFWw && UNIw) ADVw = ((BFWw - UNIw) / UNIw) * 100; else ADVw = 0;
			if (BFWp && UNIp) ADVp = ((BFWp - UNIp) / UNIp) * 100; else ADVp = 0;
			break;	
		case 'W-BT':
			//work out the bext tote prices.
			BestTotew = NSWw; 
			if (BestTotew < STABw) BestTotew = STABw; 
			if (BestTotew < UNIw) BestTotew = UNIw;
			BestTotep = NSWp; 
			if (BestTotep < STABp) BestTotep = STABp; 
			if (BestTotep < UNIp) BestTotep = UNIp;
			//compare to BF.
			if (BFWw && BestTotew) ADVw = ((BFWw - BestTotew) / BestTotew) * 100; else ADVw = 0;
			if (BFWp && BestTotep) ADVp = ((BFWp - BestTotep) / BestTotep) * 100; else ADVp = 0;
			break;
		case 'W-LTP':
			//work out the worst tote prices.
			Totew = NSWw; 
			if (Totew > STABw) Totew = STABw; 
			if (Totew > UNIw) Totew = UNIw;
			Totep = NSWp; 
			if (Totep > STABp) Totep = STABp; 
			if (Totep > UNIp) Totep = UNIp;
			//compare to BF.
			if (BFWw && Totew) ADVw = ((BFWw - Totew) / Totew) * 100; else ADVw = 0;
			if (BFWp && Totep) ADVp = ((BFWp - Totep) / Totep) * 100; else ADVp = 0;
			break;
		case 'W-ATP':
			//work out the average price
			count = 0;
			if (NSWw) { Totew = NSWw; count++; }
			if (STABw) { Totew = Totew + STABw; count++; } 
			if (UNIw) { Totew = Totew + UNIw; count++ }
			Totew = Totew / count;
			count = 0;
			if (NSWp) { Totep = NSWp; count++; }
			if (STABp) { Totep = Totep + STABp; count++; } 
			if (UNIp) { Totep = Totep + UNIp; count++ }
			Totep = Totep / count;
			//compare to BF.
			if (BFWw && Totew) ADVw = ((BFWw - Totew) / Totew) * 100; else ADVw = 0;
			if (BFWp && Totep) ADVp = ((BFWp - Totep) / Totep) * 100; else ADVp = 0;
			break;
		}
		
		if (global.show_win) {
			if (ADVw > 0) text = "<span class='favourite'>" + format_number(ADVw,2) + "</span>"; else text = format_number(ADVw,2);
		}
		
		if (global.show_place) {
			if (ADVp > 0) text2 = "<span class='favourite'>" + format_number(ADVp,2) + "</span>"; else text2 = format_number(ADVp,2);
		}
	
		if (!global.show_win) return "<font style='font: normal 14px Tahoma;' >" + text2 + "</font>";
		else if (!global.show_place) return "<font style='font: normal 14px Tahoma;' >" + text + "</font>";
		else return text + "<br>" + text2;
	} else return '';
}

function render_nsw_back(text, cell, record, row_index, col_index, store ) {
	if (!store.getAt(row_index).get('Scratched')) return render_odds(text , store.getAt(row_index).get('NSW_PLACE_BACK')); else return '';
}

function render_stab_back(text, cell, record, row_index, col_index, store ) {
	if (!store.getAt(row_index).get('Scratched')) return render_odds(text , store.getAt(row_index).get('STAB_PLACE_BACK')); else return '';
} 

function render_uni_back(uni, cell, record, row_index, col_index, store ) {
	if (!store.getAt(row_index).get('Scratched')) return render_odds(uni , store.getAt(row_index).get('UNI_PLACE_BACK')); else return '';
}

function render_bookie_sp(text, cell, record, row_index, col_index, store ) {
	if (!store.getAt(row_index).get('Scratched')) if (!text) return text = "-"; else return text = format_number(parseFloat(text),2);
}

function render_bf_wap(BFw, cell, record, row_index, col_index, store ) {
	if (!store.getAt(row_index).get('Scratched')) 
		return render_odds(get_price_less_commission(BFw), get_price_less_commission(store.getAt(row_index).get('BF_PLACE_WAP'))); else return '';
}

function render_bf_reduction(red, cell, record, row_index, col_index, store ) {
	if (!store.getAt(row_index).get('Scratched')) return render_odds(red , store.getAt(row_index).get('BF_PLACE_REDUCTION')); else return '';
}

function render_winplace_text(text, cell, record, row_index, col_index, store ) {
	if (!store.getAt(row_index).get('Scratched'))  {
		out = "Win:<br>";
		if (global.show_place) out = out + "Place:";
		return out;
	} else return '';
}

function render_odds(text,text2) {
	
	if (global.show_win) {
		if (!text) text = "-"; 
		else text = format_number(parseFloat(text),2);
	} else text = '';
	if (global.show_place) {
		if (!text2) text2 = "-"; 
		else text2 = format_number(parseFloat(text2),2);
	} else text2 = '';
	
	if (!global.show_win) return "<font style='font: normal 14px Tahoma;' >" + text2 + "</font>";
	else if (!global.show_place) return "<font style='font: normal 14px Tahoma;' >" + text + "</font>";
	else return text + "<br>" + text2;
	
}


function get_price_less_commission(BFw) {
	// recalculate the price less their commission.
	if ((global.my_commission > 0) && (BFw)) {
		BFw = (BFw - 1) * ((100 - global.my_commission) / 100) + 1;
		BFw = BFw * 100;
		BFw = Math.round(BFw);
		BFw = BFw / 100;
	}
	return BFw;
}


function render_stats_strike(strike, cell, record, row_index, col_index, store ) {
	if ((global.show_fullstats) && (!store.getAt(row_index).get('Scratched')))  {
		strikep = store.getAt(row_index).get('StatsOverallWinPercentage');
		if (strike) return "<b>" + strike + "</b><br>(" + strikep + "%)"; 
		else return '-';
	} else return '';
}

function render_stats_strikeHorse(strike, cell, record, row_index, col_index, store ) {
	if ((global.show_fullstats) && (!store.getAt(row_index).get('Scratched')))  {
		strikep = store.getAt(row_index).get('StatsHorseWinPercentage');
		if (strike) return "<b>" + strike + "</b><br>(" + strikep + "%)"; 
		else return '-';
	} else return '';
}

function render_stats_strikeTrainer(strike, cell, record, row_index, col_index, store ) {
	if ((global.show_fullstats) && (!store.getAt(row_index).get('Scratched')))  {
		strikep = store.getAt(row_index).get('StatsTrainerWinPercentage');
		if (strike) return "<b>" + strike + "</b><br>(" + strikep + "%)"; 
		else return '-';
	} else return '';
}

function render_stats_strikeJockey(strike, cell, record, row_index, col_index, store ) {
	if ((global.show_fullstats) && (!store.getAt(row_index).get('Scratched')))  {
		strikep = store.getAt(row_index).get('StatsJockeyWinPercentage');
		if (strike) return "<b>" + strike + "</b><br>(" + strikep + "%)"; 
		else return '-';
	} else return '';
}

function render_stats_strikeStallion(strike, cell, record, row_index, col_index, store ) {
	if ((global.show_fullstats) && (!store.getAt(row_index).get('Scratched')))  {
		strikep = store.getAt(row_index).get('StatsStallionWinPercentage');
		if (strike) return "<b>" + strike + "</b><br>(" + strikep + "%)"; 
		else return '-';
	} else return '';
}