/*
 * $Id: pools.js,v 1.17.124.1 2010-06-09 13:35:20 alongwil Exp $
 * Copyright (c) 2008 Orbis Technology Ltd. All rights reserved.
 *
 * This javascript file is sourced from the html/gameon/football/js/pools.html file and should not
 * be sourced from anywhere else unless the required constants and arrays are provided
 */

var weeks = 1;
var cols = 49;

function get_game_idx() {return document.getElementById('compdropdown').selectedIndex;}
function onpageClear() {clear(get_game_idx()); planDropDown(get_game_idx());}
function onpageLuckyDip() {select(get_game_idx());};
function onpageOk() {ok(get_game_idx());}

function onpageLastWeeks(v) {

	var d = v.toString();
	var picks = d.split("|");
	var game_id = get_game_idx();
	for (i = 0; i < picks.length; i++){
		buttonClick(picks[i]-1, game_id);
	}
	planSelect(picks.length,0);
};


function select(game_id)
{
	clear(game_id);
	planDropDown(game_id);
	
	var randomCounter = 0;
	
	while (trebleChance[game_id].object.planRequired > randomCounter)
	{
	
		var value = Math.round((cols-1)*Math.random())
		
		if (trebleChance[game_id].object.array[value] == 0)
		{
			randomCounter++;
			buttonClick(value, game_id);
		}
	}	
}

function buttonClickNoAlert(x, game_id)
{
	var value = trebleChance[game_id].object.array[x];
	var data = "_" + x.toString() + "_" + game_id;
	
	if (value == 1)
	{
		document.getElementById("box" + data).setAttribute("src",getParameter("GIF_URL") + "/Play/false.png");
		document.getElementById("pool" + data).className = "pools_team_box"+((x%25)%2==0?"":" bg_white");
		document.getElementById("sBall" + data).className = "ball";
		document.getElementById("largeBall" + data).className = "ball";
		value = 0;
	} 
	else if ( value == 0)
	{
		if (trebleChance[game_id].object.planRequired - trebleChance[game_id].object.numSelected <= 0)
		{
			//alert("Please unselect one before selecting this one");
		} else {
			document.getElementById("pool" + data).className = "pools_team_box_highlight";
			document.getElementById("box" + data).setAttribute("src",getParameter("GIF_URL") + "/Play/true.png");
			document.getElementById("sBall" + data).className = "ballSelected";
			document.getElementById("largeBall" + data).className = "ballSelected";
			value = 1;
		}
	}
	
	trebleChance[game_id].object.array[x] = value;
	
	numberRequired(game_id);
}

function buttonClick(x, game_id)
{
	var value = trebleChance[game_id].object.array[x];
	var data = "_" + x.toString() + "_" + game_id;
	
	if (value == 1)
	{
		document.getElementById("box" + data).setAttribute("src",getParameter("GIF_URL") + "/Play/false.png");
		document.getElementById("pool" + data).className = "pools_team_box"+((x%25)%2==0?"":" bg_white");
		document.getElementById("sBall" + data).className = "ball";
		document.getElementById("largeBall" + data).className = "ball";
		value = 0;
	} 
	else if ( value == 0)
	{
		if (trebleChance[game_id].object.planRequired - trebleChance[game_id].object.numSelected <= 0)
		{
			alert("Please unselect one before selecting this one");
		} else {
			document.getElementById("pool" + data).className = "pools_team_box_highlight";
			document.getElementById("box" + data).setAttribute("src",getParameter("GIF_URL") + "/Play/true.png");
			document.getElementById("sBall" + data).className = "ballSelected";
			document.getElementById("largeBall" + data).className = "ballSelected";
			value = 1;
		}
	}
	
	trebleChance[game_id].object.array[x] = value;
	
	numberRequired(game_id);
}

function checkCompleted(game_id)
{		
	if (game_id == -1)
	{	game_id = 0;
	}
	
	if (trebleChance[game_id].totalArray.length >= 1)
	{
		//document.getElementById("submitButton").className = "selectable";
		document.getElementById("submitButton").style.visibility = "visible";
	}
	else 
	{
		//document.getElementById("submitButton").className = "unselectable";
		document.getElementById("submitButton").style.visibility = "hidden";
	}
	
	// Calls priceFormula(game_id)
	weekDropDown(game_id);
}

function priceFormula(game_id)
{
	var subTotalPrice = 0;

	for (i = 0; i < trebleChance[game_id].totalArray.length; i++)
	{
		subTotalPrice += parseFloat(trebleChance[game_id].totalArray[i].planPrice);
		
	}
	
	subTotalPrice = subTotalPrice * weeks;


	
	var discount = get_total_discount();

	//remove any discounts
	subTotalPrice = subTotalPrice - parseFloat(discount).toFixed(2);

	document.getElementById('price').innerHTML = subTotalPrice.toFixed(2);
}

/*Purpose of this function is, once a token has been freed,
to check the TCH betslip and see if the freed token can be applied to 
any existing offers
*/
function retrospecApplyOffers(game_id) {

	for (i = 0; i < trebleChance[game_id].totalArray.length; i++)
	{
		var offer = check_offer(trebleChance[game_id].totalArray[i].planId,"");
		if (offer != -1) {
			trebleChance[game_id].totalArray[i].token_id = specialOffers[offer].ct_id;
			specialOffers[offer].in_use = "Y";
		}
	}

}



function planSelect(x, game_id)
{
	var plandropdown = document.getElementById('plandropdown_' + game_id);
	var i;
	var _ind;
	for(i = 0; i < plandropdown.options.length; i++) {
		if(plandropdown.options[i].value.split('|')[1] == x) {
			var _ind = i;
			break;
		}
	}
	plandropdown.selectedIndex = _ind;
	
	planDropDown(game_id)
}

function planDropDown(game_id)
{
	if (game_id == -1)
	{	game_id = 0;
	}
	var plandropdown = document.getElementById('plandropdown_' + game_id);

	var plans = plandropdown.options[plandropdown.selectedIndex].value;
	
	var plan = plans.split("|");
	
	var _has_gif;

	if (plan.length == 4){
		trebleChance[game_id].object.planId = plan[0];
		trebleChance[game_id].object.planRequired = plan[1];
		trebleChance[game_id].object.planPrice = plan[2];
		_has_gif = plan[3];
	}
	
	document.getElementById("stake").value = trebleChance[game_id].object.planPrice;
	document.getElementById("numberof").value = trebleChance[game_id].object.planRequired;
	
	var planImage10 = document.getElementById('planImage_10_' + game_id); 
	var planImage11 = document.getElementById('planImage_11_' + game_id); 
	var planImage12 = document.getElementById('planImage_12_' + game_id); 
	//var planImage13 = document.getElementById('planImage_13_' + game_id); 
	//var planImage14 = document.getElementById('planImage_14_' + game_id); 
	
	// disable all
	planImage10.src = getParameter("GIF_URL") + "/Play/pools_perm10_unselected.jpg";
	planImage11.src = getParameter("GIF_URL") + "/Play/pools_perm11_unselected.jpg";
	planImage12.src = getParameter("GIF_URL") + "/Play/pools_perm12_unselected.jpg";
	//planImage13.src = getParameter("GIF_URL") + "/football/footballpools_plan13_svet.gif";
	//planImage14.src = getParameter("GIF_URL") + "/football/footballpools_plan14_svet.gif";

	if (_has_gif == 'Y') {

		if (trebleChance[game_id].object.planRequired == 10)
		{
			// enabled
			planImage10.src = getParameter("GIF_URL") + "/Play/pools_perm10_selected.jpg";
		}
		else if (trebleChance[game_id].object.planRequired == 11)
		{
			// enabled
			planImage11.src = getParameter("GIF_URL") + "/Play/pools_perm11_selected.jpg";
		}
		else if (trebleChance[game_id].object.planRequired == 12)
		{
			// enabled
			planImage12.src = getParameter("GIF_URL") + "/Play/pools_perm12_selected.jpg";
		}
		
	}
	
	numberRequired(game_id);
	check_offer("","");
}

function clear(game_id)
{
	for (i = 0; i < cols; i++)
	{

		document.getElementById("box_" + i + "_" + game_id).setAttribute("src",getParameter("GIF_URL") + "/Play/false.png");
		document.getElementById("sBall_" + i + "_" + game_id).className = "ball";
		document.getElementById("pool_" + i + "_" + game_id).className = "pools_team_box"+((i%25)%2==0?"":" bg_white");
		document.getElementById("largeBall_" + i + "_" + game_id).className = "ball";
	}
	trebleChance[game_id].object = new Object();
	trebleChance[game_id].object.planId = 0;
	trebleChance[game_id].object.planRequired = 0;
	trebleChance[game_id].object.planPrice = 0;
	trebleChance[game_id].object.numbSelected = 0;
	trebleChance[game_id].object.array = new Array();
	for (i = 0; i < cols; i++)
	{
		trebleChance[game_id].object.array[i] = 0;
	}
	
	
}


function writeBox(game_id)
{
	var scrolldiv = document.getElementById('scrollDiv_' + game_id);

	var html = "<table class=\"table-selectionlist\" cellpadding=\"0\" cellspacing=\"0\" >";

	var _tmpSelections = new Array();

    var _selsDelimiter = 'c';

	for (i = 0; i < trebleChance[game_id].totalArray.length; i++)
	{
		_tmpSelections.length = 0;
		
		for (j = 0; j < trebleChance[game_id].totalArray[i].array.length; j++)
		{
			if (trebleChance[game_id].totalArray[i].array[j] == 1 )
			{
				_tmpSelections[_tmpSelections.length] = document.getElementById('RID_' + j + '_' + game_id).value;
			}
		}

		var additional_js = "";
		if (getParameter("channel") == "8") {
			additional_js = "multiCcyOn();"
		}
		var stake = trebleChance[game_id].totalArray[i].planPrice
;
//		html += "<tr>";
//		html += "<td class='title'>Entry " + parseInt(i+1) + "</td>";
//		html += "<td class='numbers'><a href=\"#\" onclick=\"MM_openBrWindow('" + getParameter("CGI_URL") + "?action=GoFixturesSmall&selections=" + _tmpSelections.join(_selsDelimiter) + "','','width=267,height=500')\"><img src='" + getParameter("GIF_URL") + "/football/btn_veiw.gif' alt='view' /></a></td>";
//		html += "<td><a href='javascript:remove(" + i + "," + game_id + ");'><img src='" + getParameter("GIF_URL") + "/football/btn_x.gif' alt='delete' /></a></td>";
//		html += "</tr>";
		html += "<tr>";
		html += "<td class='title'>Entry " + parseInt(i+1) + "</td>";
		html += "<td class='numbers'><a href=\"#\" onclick=\"MM_openBrWindow('" + getParameter("CGI_URL") + "?action=GoFixturesSmall&selections=" + _tmpSelections.join(_selsDelimiter) + "&stake=" + stake + "','','width=336,height=500')\">view</a><span>|</span>";
		html += "<a href='javascript:remove(" + i + "," + game_id + ");"+ additional_js +"'>delete</a></td>";
		html += "</tr>";

	}

	html += "</table>";

	scrolldiv.innerHTML = html;

}

function remove(id, game_id)
{
	//remove the offer first
	remove_offer(trebleChance[game_id].totalArray[id].token_id);

	//splice command removes id from the totalArray
	trebleChance[game_id].totalArray.splice(id, 1);
	planDropDown(game_id);
	checkCompleted(game_id);
	retrospecApplyOffers(game_id);
	priceFormula(game_id);
	writeBox(game_id);

}

function ok(game_id)
{

	if (trebleChance[game_id].object.planRequired - trebleChance[game_id].object.numSelected == 0)
	{
		//find and set offers
		var offer = check_offer("","");
		if (offer != -1) {
			trebleChance[game_id].object.token_id = specialOffers[offer].ct_id;
			specialOffers[offer].in_use = "Y";
		}

		trebleChance[game_id].totalArray[trebleChance[game_id].totalArray.length] = trebleChance[game_id].object;
		clear(game_id);
		planDropDown(game_id);
		checkCompleted(game_id);
		priceFormula(game_id);
		writeBox(game_id);
	} else {
		alert("Incorrect number of selections made");
	}
}

function numberRequired(game_id)
{
	if (game_id == -1)
	{	game_id = 0;
	}
	trebleChance[game_id].object.numSelected = 0;

	for (j = 0; j < trebleChance[game_id].object.array.length; j++)
	{
		if (trebleChance[game_id].object.array[j] == 1)
		{
			trebleChance[game_id].object.numSelected++;
		}
	}

	document.getElementById('numberOfSelections').innerHTML = trebleChance[game_id].object.planRequired - trebleChance[game_id].object.numSelected;
}

function weekDropDown(game_id)
{
	var weekdropdown = document.getElementById('weekdropdown_' + game_id);
	weeks = weekdropdown.options[weekdropdown.selectedIndex].value;
	

	//change weeks is a gamer changer.  Reset offers and try and reapply.
	reset_offers(game_id);

	retrospecApplyOffers(game_id);
	priceFormula(game_id);
}


function doDropDownSuper6Wrapper(theDD) {

	doDropDownSuper6("MAIN");
	//doDropDownSuper6("");

}

function doDropDownSuper6(prefix,theDD) {


	var _tmp_index = theDD.options[theDD.selectedIndex].value;
	var _tmp_current = document.getElementById('FPBG_GAME_CURRENT').value;

	// Switch the off the display of the previously selected game
	document.getElementById(prefix +'_FPBG_GAME_' + _tmp_current).style.display = 'none';
	document.getElementById(prefix +'_FPBG_SEL_' + _tmp_current).style.display = 'none';
	document.getElementById(prefix +'_FPBG_VIEW_' + _tmp_current).style.display = 'none';
	document.getElementById(prefix +'_FPBG_WEEK_' + _tmp_current).style.display = 'none';
	document.getElementById(prefix +'_FPBG_PLAN_' + _tmp_current).style.display = 'none';
	document.getElementById(prefix +'_FPBG_PLANDROP_' + _tmp_current).style.display = 'none';
	document.getElementById(prefix +'_FPBG_DEADLINE_' + _tmp_current).style.display = 'none';

	// Switch on the display of the newlyy selected game
	document.getElementById(prefix +'_FPBG_GAME_' + _tmp_index).style.display = 'block';
	document.getElementById(prefix +'_FPBG_SEL_' + _tmp_index).style.display = 'block';
	document.getElementById(prefix +'_FPBG_VIEW_' + _tmp_index).style.display = 'block';
	document.getElementById(prefix +'_FPBG_WEEK_' + _tmp_index).style.display = 'block';
	document.getElementById(prefix +'_FPBG_PLAN_' + _tmp_index).style.display = 'block';
	document.getElementById(prefix +'_FPBG_PLANDROP_' + _tmp_index).style.display = 'block';
	document.getElementById(prefix +'_FPBG_DEADLINE_' + _tmp_index).style.display = 'block';
	document.getElementById(prefix +'_FPBG_GAME_CURRENT').value = _tmp_index;

	// Do the computation to refresh the dynamic fields
	planDropDown(theDD.selectedIndex);
	numberRequired(theDD.selectedIndex);
	checkCompleted(theDD.selectedIndex);

	// Modifies the close date to be the one of the newly selected game in order to
	// Display the correct countdown
	var _tmp_date = document.getElementById(prefix +'_FPBG_GAME_' + theDD.selectedIndex + '_CLOSEDATE').value;
	//document.getElementById('FPBG_GAME_closedate').value = _tmp_date;
	document.getElementById(prefix +'_FPBG_GAME_CURRENT_closedate').value = _tmp_date;
	document.getElementById(prefix +'_FPBG_GAME_SHOW_closedate').innerHTML = _tmp_date;


}


function doDropDown(theDD) {

	var _tmp_index = theDD.options[theDD.selectedIndex].value;
	var _tmp_current = document.getElementById('FPBG_GAME_CURRENT').value;

	// Switch the off the display of the previously selected game
	document.getElementById('FPBG_GAME_' + _tmp_current).style.display = 'none';
	document.getElementById('FPBG_SEL_' + _tmp_current).style.display = 'none';
	document.getElementById('FPBG_VIEW_' + _tmp_current).style.display = 'none';
	document.getElementById('FPBG_WEEK_' + _tmp_current).style.display = 'none';
	document.getElementById('FPBG_PLAN_' + _tmp_current).style.display = 'none';
	document.getElementById('FPBG_PLANDROP_' + _tmp_current).style.display = 'none';
	document.getElementById('FPBG_DEADLINE_' + _tmp_current).style.display = 'none';

	// Switch on the display of the newlyy selected game
	document.getElementById('FPBG_GAME_' + _tmp_index).style.display = 'block';
	document.getElementById('FPBG_SEL_' + _tmp_index).style.display = 'block';
	document.getElementById('FPBG_VIEW_' + _tmp_index).style.display = 'block';
	document.getElementById('FPBG_WEEK_' + _tmp_index).style.display = 'block';
	document.getElementById('FPBG_PLAN_' + _tmp_index).style.display = 'block';
	document.getElementById('FPBG_PLANDROP_' + _tmp_index).style.display = 'block';
	document.getElementById('FPBG_DEADLINE_' + _tmp_index).style.display = 'block';
	document.getElementById('FPBG_GAME_CURRENT').value = _tmp_index;

	// Do the computation to refresh the dynamic fields
	planDropDown(theDD.selectedIndex);
	numberRequired(theDD.selectedIndex);
	checkCompleted(theDD.selectedIndex);

	// Modifies the close date to be the one of the newly selected game in order to
	// Display the correct countdown
	var _tmp_date = document.getElementById('FPBG_GAME_' + theDD.selectedIndex + '_CLOSEDATE').value;
	//document.getElementById('FPBG_GAME_closedate').value = _tmp_date;
	document.getElementById('FPBG_GAME_CURRENT_closedate').value = _tmp_date;
	document.getElementById('FPBG_GAME_SHOW_closedate').innerHTML = _tmp_date;
}

function viewAllFixtures() {
	store_treble_chance_in_cookie();

	var _tmpSelections = new Array();

	var _selsDelimiter = 'c';

	var _game_idx = get_game_idx();
	var _planDD  = document.getElementById('plandropdown_' + _game_idx);
    var _num_req = _planDD.options[_planDD.selectedIndex].value.split('|')[1];
	var _game_id = document.getElementById('FPBG_GAME_CURRENT').value;

	var i;
	for(i = 0; i < trebleChance[_game_idx].object.array.length ;i++) {
		if(trebleChance[_game_idx].object.array[i] == 1) {
			_tmpSelections[_tmpSelections.length] = document.getElementById('RID_' + i + '_' + _game_idx).value;
		}
	}

	window.open(getParameter("CGI_URL") + '?action=GoFixturesAll&num_req=' + _num_req + '&saxgame_id=' + _game_id + '&selections=' + _tmpSelections.join(_selsDelimiter),'fixtures','width=770,height=556');
}

function place_treblechance_bet(game_id) {

    var _tmpSelections = new Array();
    var _tmpBet = new Array();

    var _selsDelimiter = 'c';
    var _betDelimiter = '|';

    // A group of selections contains all the selections for a given row
    // Building the bet string

    var i,j;
	var _ta;

	var _tmp = document.getElementById('FPBG_GAME_CURRENT').value;
	if (isNaN(_tmp)) {
		// In some cases FPBG_GAME_CURRENT has a value like FPBG_GAME_1001 
		// (if display_game has not been called to update it?)
		var _saxgame_id = _tmp.split('_')[2];
	} else {
		var _saxgame_id = _tmp;
	}

	for (i = 0; i < trebleChance[game_id].totalArray.length ; i++) {

		_tmpSelections.length = 0;
		_ta = trebleChance[game_id].totalArray[i].array;

		for (j = 0; j < _ta.length; j++) {
			if(_ta[j]) {
				 _tmpSelections[_tmpSelections.length] = document.getElementById('BALL_' + j + '_' + game_id).value;
			}
		}

		// Adding the game_id
		_tmpBet[i*5] = _saxgame_id;

		// Adding the stake
		_tmpBet[i*5 + 1] = trebleChance[game_id].totalArray[i].planPrice;

		// Adding the number of subscriptions
		_tmpBet[i*5 + 2] = weeks;

		// Adding the Openbet Plan Id here.
		_tmpBet[i*5 + 3] = trebleChance[game_id].totalArray[i].planId;

		// Adding the selections
		_tmpBet[i*5 + 4] = _tmpSelections.join(_selsDelimiter);

	}

    // Placing the bet using the generic call defined in bet.html
    place_bet('Football', _tmpBet.join(_betDelimiter));

}

function store_treble_chance_in_cookie() {

	var _tmpSelections = new Array();
	var _tmpBet = new Array();
	var _competitions = new Array();
	var _selsDelimiter = 'c';
	var _betDelimiter = '|';
	var _competitionDelimiter = "X";

	var i, j, g;
	var _ta;
	var _saxgame_id;
	var _planDD;
	var _weekDD;

	_competitions.length = 0;

	// Storing the game sort
	_competitions[0] = 'TCH';

	// Storing the current competition displayed
	_competitions[1] = document.getElementById('FPBG_GAME_CURRENT').value;

	// Storing each competition
	for (g = 0; g < trebleChance.length; g++) {

		_tmpBet.length = 0;

		// Adding the openbet saxgame_id
		_saxgame_id = document.getElementById('compdropdown').options[g].value;
		_tmpBet[0] = _saxgame_id;

		// Storing the plan_id
		_planDD = document.getElementById('plandropdown_' + g);
		_tmpBet[1] = _planDD.options[_planDD.selectedIndex].value.split('|')[0];

		// Storing the number of subscriptions
		_weekDD = document.getElementById('weekdropdown_' + g);
		_tmpBet[2] = _weekDD.options[_weekDD.selectedIndex].value;

		// Storing the current/temporary selections
		_ta = trebleChance[g].object.array;
		_tmpSelections.length = 0;
		for (j = 0; j < _ta.length; j++) {
			if(_ta[j]) _tmpSelections[_tmpSelections.length] = document.getElementById('BALL_' + j + '_' + g).value;
		}
		_tmpBet[3] = _tmpSelections.join(_selsDelimiter);

		// Storing each entry associated with the competition
		for (i = 0; i < trebleChance[g].totalArray.length; i++) {
			_tmpSelections.length = 0;

			// Storing the selections
			_ta = trebleChance[g].totalArray[i].array;
			for (j = 0; j < _ta.length; j++) {
				if(_ta[j]) _tmpSelections[_tmpSelections.length] = document.getElementById('BALL_' + j + '_' + g).value;
			}

			// If the entry is valid then we store it
			if(_tmpSelections.length > 0) {
				_tmpBet[_tmpBet.length] = trebleChance[g].totalArray[i].planId;
				_tmpBet[_tmpBet.length] = _tmpSelections.join(_selsDelimiter);
			}
		}
		_competitions[_competitions.length] = _tmpBet.join(_betDelimiter);
	}

	// Saving the game in a cookie
	clear_save_cookie();
	save_gameplay(_competitions.join(_competitionDelimiter));
}

function display_game(game_idx) {

	if(!document.getElementById('compdropdown')) return;
	var _compDD = document.getElementById('compdropdown');

	if (game_idx < 0 || game_idx >= _compDD.length) {
		alert ('game_idx ' + game_idx + ' is invalid')
		return;
	}
	_compDD.selectedIndex = game_idx;
	
	// First, undisplay the other games and display the right one
	var _tmp_id;
	var _new_id = -1;
	for(var i = 0 ; i < _compDD.options.length ; i++) {
		_tmp_id = _compDD.options[i].value;
		_tmp_style = (game_idx != i ? 'none' : 'block');
		document.getElementById('FPBG_GAME_' + _tmp_id).style.display = _tmp_style;
		document.getElementById('FPBG_SEL_' + _tmp_id).style.display = _tmp_style;
		document.getElementById('FPBG_VIEW_' + _tmp_id).style.display = _tmp_style;
		document.getElementById('FPBG_WEEK_' + _tmp_id).style.display = _tmp_style;
		document.getElementById('FPBG_PLAN_' + _tmp_id).style.display = _tmp_style;
		document.getElementById('FPBG_PLANDROP_' + _tmp_id).style.display = _tmp_style;
		document.getElementById('FPBG_DEADLINE_' + _tmp_id).style.display = _tmp_style;
		if(game_idx == i) _new_id = _tmp_id;
	}
	
	document.getElementById('FPBG_GAME_CURRENT').value = _new_id;
	
	// Do the computation to refresh the dynamic fields
	planDropDown(_compDD.selectedIndex);
	numberRequired(_compDD.selectedIndex);
	checkCompleted(_compDD.selectedIndex);
	
	// Modifies the close date to be the one of the newly selected game in order to
	// Display the correct countdown
	var _tmp_date = document.getElementById('FPBG_GAME_' + _compDD.selectedIndex + '_CLOSEDATE').value;
	//document.getElementById('FPBG_GAME_closedate').value = _tmp_date;
	document.getElementById('FPBG_GAME_CURRENT_closedate').value = _tmp_date;
	document.getElementById('FPBG_GAME_SHOW_closedate').innerHTML = _tmp_date;
}

function load_game_entry(game_idx, plan_id, selections) {
	
	// Check validity of game_idx
	if(game_idx == -1) return;

	// Determine plan_id, price and num_picks from the plan_id
	var planDD_opt = document.getElementById('plandropdown_' + game_idx).options;
    var plan_idx;
    for(plan_idx = 0; plan_idx < planDD_opt.length; plan_idx++) {
        if(planDD_opt[plan_idx].value.split('|')[0] == plan_id) break;
    }
    if(plan_idx == planDD_opt.length) return;

	var param = planDD_opt[plan_idx].value.split('|');

	// building the entry
	var _tmp_entry = new Object();
	_tmp_entry.planId = plan_id;

	// Start filling the object
	_tmp_entry.planId =       plan_id;
	_tmp_entry.planRequired = param[1];
	_tmp_entry.planPrice =    param[2];

	// Completing with selections
	_tmp_entry.array = new Array(cols);
	var i;
	for(i = 0; i < _tmp_entry.array.length; i++) _tmp_entry.array[i] = 0;

	if(selections != '') {
		var _selections = selections.split('c');
		
		for(i = 0; i < _selections.length; i++) 
			_tmp_entry.array[_selections[i]-1] = 1;			
	}

	trebleChance[game_idx].totalArray[trebleChance[game_idx].totalArray.length]	= _tmp_entry;
}

function load_game(game_idx, plan_idx, subscriptions, selections) {

	// Check validity of both game_idx and plan_idx
	if(game_idx == -1 || plan_idx == -1) return;

	// Game and plan are valid so lets go and update them
	document.getElementById('plandropdown_' + game_idx).selectedIndex = plan_idx;
	planDropDown(game_idx);

	var i;
	if(selections != '') {
		var _selections = selections.split('c');
		for(i = 0; i < _selections.length; i++) {
			buttonClick((parseInt(_selections[i]) - 1), game_idx);
		}
	}

	// Handling the number of weeks
	var _week_idx;
	var _weekDD = document.getElementById('weekdropdown_' + game_idx);
	for(_week_idx = 0; _week_idx < _weekDD.options.length ; _week_idx++) {
		if(_weekDD.options[_week_idx].value == subscriptions) break;
	}
	_weekDD.selectedIndex = _week_idx;
}

function get_game_index(game_id) {
	// from the id, determine the index
	var compDD_opt = document.getElementById('compdropdown').options;
	var _game_idx;
	for(_game_idx = 0; _game_idx < compDD_opt.length; _game_idx++) {
		if(compDD_opt[_game_idx].value == game_id) break;
	}

	return (_game_idx >= compDD_opt.length ? -1 : _game_idx);
}

function get_plan_index(game_idx, plan_id) {
	// The game_is is valid lets find the plan index in the drop down
	var planDD_opt = document.getElementById('plandropdown_' + game_idx).options;
	var _plan_idx;
	for(_plan_idx = 0; _plan_idx < planDD_opt.length; _plan_idx++) {
		if(planDD_opt[_plan_idx].value.split('|')[0] == plan_id) break;
	}
	return (_plan_idx == planDD_opt.length ? -1 : _plan_idx);
}

function write_select_subscriptions(minSubs, maxSubs) {
	if (minSubs > maxSubs) {
		write_select(minSubs, minSubs, minSubs);
		return;
	}

	var write_sel_args = "";
	var count = 0;
	var wrote_last_value = false;
	for (i = minSubs; i <= maxSubs; i++) {
		if (i == minSubs) {
			write_sel_args = write_sel_args + i + ", " + i + ", " + i;
			wrote_last_value = true;
		} else {
			if (count < 20) {
				write_sel_args = write_sel_args + ", " + i + ", " + i;
				wrote_last_value = true;
			} else {
				if (i % 5 == 0) {
					write_sel_args = write_sel_args + ", " + i + ", " + i;
					wrote_last_value = true;
				} else {
					wrote_last_value = false;
				}
			}
		}
		count++;
	}
	if (!wrote_last_value) {
		write_sel_args = write_sel_args + ", " + maxSubs + ", " + maxSubs;
	}

	eval("write_select(" + write_sel_args + ");");
}

/* Copied from write_select() 
 * Returns HTML <option>s for a <select> drop down.
 */
function get_select_options() {

	var sel = arguments[0];
	var i;
	var n;
	var v;
	var sel_tag;

	var result = '';

	for(i = 1; i < arguments.length; i += 2) {
		v = arguments[i];
		n = arguments[i + 1];
		sel_tag = (sel == v) ? " selected" : "";
		result = result + '<option value="' + v + '"'
					+ sel_tag + '>' + n + '</option>';
	}
	return result;
}

/* Copied from write_select_subscriptions 
 * Returns HTML <option>s for a <select> drop down.
 */	
function get_select_subscriptions(minSubs, maxSubs) {
	if (minSubs > maxSubs) {
		write_select(minSubs, minSubs, minSubs);
		return;
	}

	var write_sel_args = "";
	var count = 0;
	var wrote_last_value = false;
	for (i = minSubs; i <= maxSubs; i++) {
		if (i == minSubs) {
			write_sel_args = write_sel_args + i + ", " + i + ", " + i;
			wrote_last_value = true;
		} else {
			if (count < 20) {
				write_sel_args = write_sel_args + ", " + i + ", " + i;
				wrote_last_value = true;
			} else {
				if (i % 5 == 0) {
					write_sel_args = write_sel_args + ", " + i + ", " + i;
					wrote_last_value = true;
				} else {
					wrote_last_value = false;
				}
			}
		}
		count++;
	}
	if (!wrote_last_value) {
		write_sel_args = write_sel_args + ", " + maxSubs + ", " + maxSubs;
	}

	return eval("get_select_options(" + write_sel_args + ");");

}

/* Adds the <option>s to a <select> drop down
function write_sub_options (id,min,max) {

	var handle = document.getElementById(id);

	handle.innerHTML = get_select_subscriptions(min, max);
}
*/

function write_sub_options(selID,minSubs,maxSubs){
	//alert("ID: "+selID+", min: "+minSubs+", max: "+maxSubs);
	var theInd = 0;
	//var theOpt;
	var theOpt = document.getElementById(selID).options;
	
	if (minSubs>maxSubs){
		theOpt[theInd] = new Option(minSubs,minSubs,false,true);
		return;
	}
	
	for (var i=minSubs;i<=maxSubs;i++){
		if (i==minSubs){
			theOpt[theInd] = new Option(i,i,false,true);
		} else {
			if(theInd<20){
				theOpt[theInd] = new Option(i,i,false,false);
			} else {
				if (i%5==0){
					theOpt[theInd] = new Option(i,i,false,false);
				} else {
					theInd--
				}
			}
		}
		theInd++;
	}
}


function reset_offers (game_id) {

	for (var i=0; i < specialOffers.length;i++) {
		if (specialOffers[i].in_use == "Y") {
			
			//find which offer has this token in use and remove
			for (var j=0; j < trebleChance[game_id].totalArray.length;j++) {
				if (trebleChance[game_id].totalArray[j].token_id == specialOffers[i].ct_id) {
					trebleChance[game_id].totalArray[j].token_id = "";
					specialOffers[i].in_use = "N";
				}
			}
		}
	}
}


