var maxPos = 14;
var totalCombinations = 0;
var arrCombination = new Array();


function $(id)
{
	return document.getElementById(id);
} // $


function emptyInputsTable()
{
	objTable = $('horsePosition');
	for(i = maxPos ; i >= 0; i--)
	{
		if( objTable.rows[i] != null )
		{
			objTable.deleteRow(i);
		}
	}
} // emptyInputsTable


function populateForm(wager)
{
	arrWager = wager.split('-');
	objTable = $('horsePosition');
	emptyInputsTable();
	
	if( arrWager[0] == 1 )
	{
		var texts = new Array();
		texts[0] = 'No';
		texts[1] = '1st';
		texts[2] = '2nd';
		texts[3] = '3rd';
		texts[4] = '4th';
		
		var objRow = objTable.insertRow(0);
		for(i = 0; i <= arrWager[1]; i++)
		{
			var objCell = objRow.insertCell(i);
			objCell.style.color = '#FFD800';
			objCell.style.padding = '5px 3px 15px 3px';
			objCell.innerHTML = texts[i];
		}
		
		for(i = 1; i <= maxPos; i++)
		{
			var objRow = objTable.insertRow(i);
			var objCell = objRow.insertCell(0);
			objCell.innerHTML = i;
			
			for(j = 1; j <= arrWager[1]; j++)
			{
				var objCell = objRow.insertCell(j);
				objCell.innerHTML = '<input type="checkbox" name="pos'+j+'[]" />';
			}
		}
	}
	else
	{
		var objRow = objTable.insertRow(0);
		var objCell = objRow.insertCell(0);
		objCell.style.color = '#FFD800';
		objCell.style.padding = '5px 3px 15px 3px';
		objCell.innerHTML = 'Picks';
		
		for(i = 1; i <= arrWager[1]; i++)
		{
			var objRow = objTable.insertRow(i);
			var objCell = objRow.insertCell(0);
			objCell.innerHTML = '<select name="picks['+i+']"><\/select>';
			var objSelect = $('calculatorTable').elements[ 'picks['+i+']' ];
			
			for(j = 1; j <= maxPos; j++)
			{
				var newOpt = document.createElement('option');
				newOpt.value = j;
				newOpt.text = j+' pick(s)';
				
				try
				{
					objSelect.add(newOpt, null);
				}
				catch(ex)
				{
					objSelect.add(newOpt);
				}
			}
		}
	}
} // populateForm


function countCombinations(col, max)
{
	var arrChecks = $('calculatorTable').elements['pos'+col+'[]'];
	for(var i = 0; i < maxPos; i++)
	{
		if( arrChecks[i].checked )
		{
			
			var matched = false;
			
			for(j = 0; j < arrCombination.length; j++)
			{
				if( parseInt(arrCombination[j]) == parseInt(i + 1) )
				{
					matched = true;
					break;
				}
			}
			
			if( !matched )
			{
				if( col == max )
				{
					totalCombinations++;
				}
				else
				{
					arrCombination[ arrCombination.length ] = (i + 1);
					countCombinations(col + 1, max);
				}
			}
		}
	}
	arrCombination.pop();
} // countCombinations


function calculateTicketCost()
{
	var arrAmountTypes = $('calculatorTable').elements['amountType[]'];
	var arrAmounts = $('calculatorTable').elements['amount[]'];
	var arrWagerTypes = $('calculatorTable').elements['wager[]'];
	
	for(i = 0; i < arrAmountTypes.length; i++)
	{
		if( arrAmountTypes[i].checked )
		{
			var amount = arrAmounts[i].value;
			break;
		}
	}
	
	for(i = 0; i < arrWagerTypes.length; i++)
	{
		if( arrWagerTypes[i].checked )
		{
			var arrWager = arrWagerTypes[i].value.split('-');
			var wagerType = arrWagerTypes[i].alt;
			break;
		}
	}
	
	if( arrWager[0] == 1 )
	{
		
		totalCombinations = 0;
		countCombinations(1, arrWager[1]);
		var ticketCost = totalCombinations * amount ;
	}
	else
	{
		var ticketCost = amount;
		for(i = 1; i <= arrWager[1]; i++)
		{
			objSelect = $('calculatorTable').elements[ 'picks['+i+']' ];
			var ticketCost = ticketCost * objSelect.options[ objSelect.selectedIndex ].value;
		}
	}
	
	$('wagerType').innerHTML = '$'+amount+' '+ wagerType;
	$('ticketCost').innerHTML = ticketCost;
} // calculateTicketCost