var i=0;
// these indexes are just to make the code easier to read
var idx_name = 0;
var idx_num_selections = 1;
var idx_accumulator = 2;
var idx_singles = 3;
// setup bet types:
var bettypes = Array();
	//array format: Array(name,num_selections,do_accumulator,do_singles);
	// the basics
	bettypes[i] = Array("The Basics",0,false,false); i++;
	bettypes[i] = Array("Single",1,false,true); i++;
	bettypes[i] = Array("Double",2,false,false); i++;
	bettypes[i] = Array("Treble",3,false,false); i++;
	bettypes[i] = Array("Fourfold",4,false,false); i++;
	bettypes[i] = Array("Fivefold",5,false,false); i++;
	bettypes[i] = Array("Sixfold",6,false,false); i++;
	bettypes[i] = Array("Sevenfold",7,false,false); i++;
	bettypes[i] = Array("Eightfold",8,false,false); i++;
	// full covers w/o singles
	bettypes[i] = Array("Full Covers without singles",0,false,false); i++;
	bettypes[i] = Array("Trixie",3,true,false); i++;
	bettypes[i] = Array("Yankee",4,true,false); i++;
	bettypes[i] = Array("Super Yankee (Canadian)",5,true,false); i++;
	bettypes[i] = Array("Heinz",6,true,false); i++;
	bettypes[i] = Array("Super Heinz",7,true,false); i++;
	bettypes[i] = Array("Goliath",8,true,false); i++;
	// full covers w/ singles
	bettypes[i] = Array("Full Covers with singles",0,false,false); i++;
	bettypes[i] = Array("Patent",3,true,true); i++;
	bettypes[i] = Array("Lucky 15 (Yap)",4,true,true); i++;
	bettypes[i] = Array("Lucky 31",5,true,true); i++;
	bettypes[i] = Array("Lucky 63",6,true,true); i++;
	// these don't exist (but we can do them):
	bettypes[i] = Array("Super Heinz with singles",7,true,true); i++;
	bettypes[i] = Array("Goliath with singles",8,true,true); i++;

var currently_showing_selections = 1;
var betForm;
var type;
var eachway;
var stake;
var _winodds1;
var _winodds2;
var _eachodds1;
var _eachodds2;
var _finishpos;
var winodds1 = Array();
var winodds2 = Array();
var eachodds1 = Array();
var eachodds2 = Array();
var finishpos = Array();
var win_amt = 0;
var total_staked = 0;
var amounts = Array(); // we index this from 1 not zero so that the amount for a double is amounts[2], treble is amounts[3] just to make it easier to read.
var eachamounts = Array(); // the zeroth is set to the stake value in calcBet().

function copyOddsForm(type) {
	if(bettypes[type][idx_num_selections]!=0) {
		$("#betcalc_innards").slideDown();
		// copy master node
		var oddsTable = document.getElementById("oddstable");

		if(currently_showing_selections<bettypes[type][idx_num_selections]) {
			// need to add more rows
			for(var i=currently_showing_selections+1 ; i<=bettypes[type][idx_num_selections] ; i++) {
				var newOddsTable = oddsTable.cloneNode(true);
				newOddsTable.setAttribute("id","oddsTable_"+i);
				oddsTable.parentNode.insertBefore(newOddsTable, oddsTable);
			}
		} else {
			// need to remove some rows
			for(var i=currently_showing_selections ; i>bettypes[type][idx_num_selections] ; i--) {
				removeElementById("oddsTable_"+i);
			}
		}
		// remember how many are showing for next time.
		currently_showing_selections = bettypes[type][idx_num_selections];
	} else {
		$("#betcalc_innards").slideUp();
	}
	// remove ugly dotted line around select
	betForm.elements['bettypeselect'].blur();
}
function populateBetTypes() {
	// called once from onload
	betForm = document.forms['betform'];
	betForm.elements['bettypeselect'].options.length = 0;
	for(i=0 ; i<bettypes.length ; i++) {
		betForm.elements['bettypeselect'].options[i] = new Option();
		betForm.elements['bettypeselect'].options[i].value = i;
		betForm.elements['bettypeselect'].options[i].innerHTML = bettypes[i][idx_name];
		if(bettypes[i][idx_num_selections]==0) {
			betForm.elements['bettypeselect'].options[i].className = "bc_header";
		}
	}
}
function doEachWay(stake,odds1,odds2,eachodds1,eachodds2,finishpos) {
	if(finishpos=="w" || finishpos=="p") {
		// work out decimal win odds
		winodds = odds1 / odds2;
		// and decimal each way multiplier
		eachodds = eachodds1 / eachodds2;
		// times the win odds by the each way odds (eg quarter or fifth it)
		eachodds = winodds * eachodds;
		// calc return.
		return stake+(eachodds*stake);
	} else if(finishpos=="nr") {
		return stake;
	} else {
		return 0;
	}
}
function doSingle(stake,odds1,odds2,finishpos) {
	if(finishpos=="w") {
		return stake+stake/odds2*odds1;
	} else if(finishpos=="nr") {
		return stake;
	} else {
		return 0;
	}
}
function shouldDoAccumulator(type,num_selections) {
	return !(num_selections==1 && !bettypes[type][idx_singles]) && (bettypes[type][idx_accumulator] || bettypes[type][idx_num_selections]==num_selections);
}
function doRound(i,num) {
	amounts[num] = doSingle(amounts[num-1],winodds1[i],winodds2[i],finishpos[i]);
	eachamounts[num] = doEachWay(eachamounts[num-1],winodds1[i],winodds2[i],eachodds1[i],eachodds2[i],finishpos[i]);
	if(shouldDoAccumulator(type,num)) {
		total_staked += stake;
		win_amt += amounts[num];
		if(eachway) {
			total_staked += stake;
			win_amt += eachamounts[num];
		}
	}
}
function calcBet() {
	// some speed optimisation tips implemented from the fascinating: http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html
	// (and some of my own cunning devising)
	//var startTime=new Date();
	betForm = document.forms['betform'];
	type = betForm.elements['bettypeselect'].value;
	eachway = betForm.elements['eachway'].checked;
	stake = parseFloat(betForm.stake.value);
	_winodds1 = document.getElementsByName("winodds1[]");
	_winodds2 = document.getElementsByName("winodds2[]");
	_eachodds1 = document.getElementsByName("eachodds1[]");
	_eachodds2 = document.getElementsByName("eachodds2[]");
	_finishpos = document.getElementsByName("finishpos[]");

	// blank previous values
	total_staked = 0;
	win_amt = 0;
	amounts = Array();
	eachamounts = Array();
	finishpos = Array();
	amounts[0] = stake;
	eachamounts[0] = stake;

	for(var i=0 ; i<bettypes[type][idx_num_selections] ; i++) {
		winodds1[i] = _winodds1[i].value;
		winodds2[i] = _winodds2[i].value;
		eachodds1[i] = _eachodds1[i].value;
		eachodds2[i] = _eachodds2[i].value;
		finishpos[i] = _finishpos[i].value;
		// accessing .value inside the loops below takes longer than creating an array of values
		// eg, using a goliath bet           (ff)     (ie)
		// accessing .value inside the loop: 640ms    210ms
		// using this array of values:        32ms     15ms
	}	// unsetting (or setting to "false") the _ arrays here makes no difference to speed.

// these loops go through all possible combinations with these selections without any duplicates
	for(i=0 ; i<bettypes[type][idx_num_selections] ; i++) {
		doRound(i,1); // single
		for(var j=i+1 ; j<bettypes[type][idx_num_selections] ; j++) {
			doRound(j,2); // double
			// (and so on if required for trebles, up to eightfolds)
			// - it only enters these loops if the accumulator requires that number (eg it'll never enter the fivefold+ loops for a yankee)
			for(var k=j+1 ; k<bettypes[type][idx_num_selections] ; k++) {
				doRound(k,3); // treble
				for(var l=k+1 ; l<bettypes[type][idx_num_selections] ; l++) {
					doRound(l,4); // fourfold
					for(var m=l+1 ; m<bettypes[type][idx_num_selections] ; m++) {
						doRound(m,5); // fivefold
						for(var n=m+1 ; n<bettypes[type][idx_num_selections] ; n++) {
							doRound(n,6); // sixfold
							for(var o=n+1 ; o<bettypes[type][idx_num_selections] ; o++) {
								doRound(o,7); // sevenfold
								for(var p=o+1 ; p<bettypes[type][idx_num_selections] ; p++) {
									doRound(p,8); // eightfold
								}
							}
						}
					}
				}
			}
		}
	}
	document.getElementById("bettype").innerHTML = bettypes[type][idx_name];
	document.getElementById("total_stake").innerHTML = number_format(round(total_staked,2), 2, '.', ',');
	document.getElementById("returnamt").innerHTML = number_format(round(win_amt,2), 2, '.', ',');
	document.getElementById("profit").innerHTML = number_format(round(win_amt-total_staked,2), 2, '.', ',');
	//var endTime=new Date();
	//alert(endTime-startTime);
}
// old betcalc had 385 lines.