//
// predictions is an array of participant ids
//
function motorracingleague_entry_save( race, player, email, comp_id, predictions )
{
	var mysack = new sack(MotorRacingLeagueAjax.blogUrl + "/wp-admin/admin-ajax.php" ); 

	  mysack.execute = 1;
	  mysack.method = 'POST';
	  mysack.setVar( "motorracingleague_action", "SaveEntry" );
	  mysack.setVar( "motorracingleague_race", race );
	  mysack.setVar( "motorracingleague_player", player );
	  mysack.setVar( "motorracingleague_email", email );
	  mysack.setVar( "motorracingleague_comp_id", comp_id );
	  for (i=0; i<predictions.length; i++) {
		  mysack.setVar( "motorracingleague_participant["+i+"]", predictions[i] );
	  }
	  mysack.encVar( "cookie", document.cookie, false );
	  mysack.onError = function() { alert('Error saving data.' )};
	  mysack.runAJAX();

	  return true;

} 

function motorracingleague_show_entry( champid, raceid )
{

	var mysack = new sack(MotorRacingLeagueAjax.blogUrl + "/wp-admin/admin-ajax.php" ); 

	  mysack.execute = 1;
	  mysack.method = 'POST';
	  mysack.setVar( "motorracingleague_action", "ShowEntries" );
	  mysack.setVar( "motorracingleague_race_id", raceid );
	  mysack.setVar( "motorracingleague_championship_id", champid );
	  mysack.encVar( "cookie", document.cookie, false );
	  mysack.onError = function() { alert('Error showing race.' )};
	  mysack.runAJAX();

	  return true;

}


//
// Replace div with contents
//
function motorracingleague_update( div, content )
{
	document.getElementById(div).innerHTML = content;
	
}

function motorracingleague_update_entry( noticediv, formdiv, resultsdiv, msg, content )
{
	motorracingleague_update( noticediv, msg )
	setTimeout(function(){motorracingleague_update(resultsdiv, content)},1000);
	document.getElementById(formdiv).style.display = 'none'; 	
	document.getElementById(resultsdiv).style.display = '';
	
//  Removed scriptaplous effects due to clashes with Javascript libraries prototype and JQuery
//	Effect.Fade(formdiv);
//	Effect.Appear(resultsdiv,{ duration: 3.0 });
	
}

function motorracingleague_toggle(div1, div2)
{
	document.getElementById(div1).style.display = 'none'; 	
	document.getElementById(div2).style.display = ''; 	
//	Effect.Fade(div1);
//	Effect.Appear(div2,{ duration: 1.0 });
}

/*
	Author:		Robert Hashemian (http://www.hashemian.com/)
	Modified by:	Munsifali Rashid (http://www.munit.co.uk/)
*/

/*
	Modified by: Ian Haycox
	Countdown timer to next prediction deadline
*/
function motorracingleague_countdown(obj)
{
	this.obj		= obj;
	this.Div		= "clock1";
	this.BackColor		= "white";
	this.ForeColor		= "black";
	this.TargetDate		= "12/31/2020 5:00 AM";
	this.ServerDate		= "12/31/2020 5:00 AM";
	this.DisplayFormat	= "%%D%%d, %%H%%h, %%M%%m, %%S%%s.";
	this.FinishStr      = "Too Late";
	this.CountActive	= true;
	
	this.DisplayStr;

	this.Calcage		= motorracingleague_cd_Calcage;
	this.CountBack		= motorracingleague_cd_CountBack;
	this.Setup		= motorracingleague_cd_Setup;
}

function motorracingleague_cd_Calcage(secs, num1, num2)
{
  s = ((Math.floor(secs/num1))%num2).toString();
  if (s.length < 2) s = "0" + s;
  return (s);
}
function motorracingleague_cd_CountBack(secs)
{
    if (secs < 0) {
	  document.getElementById(this.Div).innerHTML = this.FinishStr;
	  return;
  }

  this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g,	this.Calcage(secs,86400,100000));
  this.DisplayStr = this.DisplayStr.replace(/%%H%%/g,		this.Calcage(secs,3600,24));
  this.DisplayStr = this.DisplayStr.replace(/%%M%%/g,		this.Calcage(secs,60,60));
  this.DisplayStr = this.DisplayStr.replace(/%%S%%/g,		this.Calcage(secs,1,60));

  document.getElementById(this.Div).innerHTML = this.DisplayStr;
  if (this.CountActive) setTimeout(this.obj +".CountBack(" + (secs-1) + ")", 990);
}
function motorracingleague_cd_Setup()
{
	var dthen	= new Date(this.TargetDate);
  	var dnow	= new Date(this.ServerDate);
	ddiff		= new Date(dthen-dnow);
	gsecs		= Math.floor(ddiff.valueOf()/1000);
	this.CountBack(gsecs);
}
