/********************************************************************
The following functions will only manipulate data passed by the 
PHP tournaments functions. Therefore all input data and js settings 
below must be formatted as specified by the PHP functions file.
********************************************************************/

var isSet = "N";
var sec = 00;   // set the seconds
var min = 120;   // set the minutes
var chgColor;

function leadingZero(nr)
{
	if (nr < 10) nr = "0" + nr;
	return nr;
}

function checkTimeZone() {
	// Function coutesy http://www.csgnetwork.com/timezoneproginfo.html
   	var rightNow = new Date();
   	var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
   	var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
   	var temp = date1.toGMTString();
   	var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
   	var temp = date2.toGMTString();
   	var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
   	var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
   	return hoursDiffStdTime;
}

function fSet(tourneyDate,tourneyTime,regDate,regTime) {

	//get current local time
	var today = new Date();
	var lYear = today.getYear();
	var lMonth = leadingZero(today.getMonth()+1);
	var lDay = leadingZero(today.getDate());
	var lHour = leadingZero(today.getHours());
	var lMin = leadingZero(today.getMinutes());
	var lSec = leadingZero(today.getSeconds());
	
	//Tourney times
	var tDay = tourneyDate.substr(0,2); // tournament start date
	var tMonth = tourneyDate.substr(3,2); // tournament start date
	var tYear = tourneyDate.substr(6,4); // tournament start date
	var tHour = tourneyTime.substr(0,2); // tournament start time
	var tMin = tourneyTime.substr(3,2); // tournament start time
	var tSec = tourneyTime.substr(6,2); // tournament start time
	
	//Reg times
	var rDay = regDate.substr(0,2); // registration start date
	var rMonth = regDate.substr(3,2); // registration start date
	var rYear = regDate.substr(6,4); // registration start date
	var rHour = regTime.substr(0,2); // registration start time
	var rMin = regTime.substr(3,2); // registration start time
	var rSec = regTime.substr(6,2); // registration start time
	
	//Get timezone of user
	var da = new Date();
	var diff = checkTimeZone();
	
	//Set new tourney start time (local)
	var newtHour = Number(diff)+Number(tHour);
	var tt = new Date ( tYear, tMonth-1, tDay, newtHour, tMin, tSec );
	
	//Set new registration start time (local)
	var newrHour = Number(diff)+Number(rHour);
	var rt = new Date ( rYear, rMonth-1, rDay, newrHour, rMin, rSec );
	
	//format tourney output
	var tyr = leadingZero(tt.getYear());
	var tmon = leadingZero(tt.getMonth()+1);
	var tday = leadingZero(tt.getDate());
	var thr = leadingZero(tt.getHours());
	var tmi = leadingZero(tt.getMinutes());
	
	//format registration output
	var ryr = leadingZero(rt.getYear());
	var rmon = leadingZero(rt.getMonth()+1);
	var rday = leadingZero(rt.getDate());
	var rhr = leadingZero(rt.getHours());
	var rmi = leadingZero(rt.getMinutes());
	
	//compare dates
	var l1970 = Date.UTC(lYear,lMonth,lDay,lHour,lMin,lSec); // current time
	var t1970 = Date.UTC(tyr,tmon,tday,thr,tmi,0); // tournament start time
	var r1970 = Date.UTC(ryr,rmon,rday,rhr,rmi,0); // registration start time
	if (l1970 < t1970 && l1970 > r1970) {
		document.write(tday+'/'+tmon+' '+thr+':'+tmi+'</td><td valign="center" align="center" nowrap bgcolor="#F0BE29"><font color="#000000"><strong>OPEN</strong></font>');
		}
	else if (l1970 < t1970 && l1970 < r1970) {
		document.write(tday+'/'+tmon+' '+thr+':'+tmi+'</td><td valign="top" nowrap>'+rday+'/'+rmon+' '+rhr+':'+rmi);
		}
	else if (l1970 > t1970) {
		document.write('<font color="#990000">CLOSED</font></td><td valign="top" nowrap><font color="#990000">CLOSED</font>');
		}
	}
	
//Similar to the above but for Toolbar version: more parameters, different output
function tbfSet(tourneyDate,tourneyTime,regDate,regTime,tName,tID,tFee) {

	//get current local time
	var today = new Date();
	var lYear = today.getYear();
	var lMonth = leadingZero(today.getMonth()+1);
	var lDay = leadingZero(today.getDate());
	var lHour = leadingZero(today.getHours());
	var lMin = leadingZero(today.getMinutes());
	var lSec = leadingZero(today.getSeconds());
	
	//Tourney times
	var tDay = tourneyDate.substr(0,2); // tournament start date
	var tMonth = tourneyDate.substr(3,2); // tournament start date
	var tYear = tourneyDate.substr(6,4); // tournament start date
	var tHour = tourneyTime.substr(0,2); // tournament start time
	var tMin = tourneyTime.substr(3,2); // tournament start time
	var tSec = tourneyTime.substr(6,2); // tournament start time
	
	//Reg times
	var rDay = regDate.substr(0,2); // registration start date
	var rMonth = regDate.substr(3,2); // registration start date
	var rYear = regDate.substr(6,4); // registration start date
	var rHour = regTime.substr(0,2); // registration start time
	var rMin = regTime.substr(3,2); // registration start time
	var rSec = regTime.substr(6,2); // registration start time
	
	//Get timezone of user
	var da = new Date();
	var diff = checkTimeZone();
	
	//Set new tourney start time (local)
	var newtHour = Number(diff)+Number(tHour);
	var tt = new Date ( tYear, tMonth-1, tDay, newtHour, tMin, tSec );
	
	//Set new registration start time (local)
	var newrHour = Number(diff)+Number(rHour);
	var rt = new Date ( rYear, rMonth-1, rDay, newrHour, rMin, rSec );
	
	//format tourney output
	var tyr = leadingZero(tt.getYear());
	var tmon = leadingZero(tt.getMonth()+1);
	var tday = leadingZero(tt.getDate());
	var thr = leadingZero(tt.getHours());
	var tmi = leadingZero(tt.getMinutes());
	
	//format registration output
	var ryr = leadingZero(rt.getYear());
	var rmon = leadingZero(rt.getMonth()+1);
	var rday = leadingZero(rt.getDate());
	var rhr = leadingZero(rt.getHours());
	var rmi = leadingZero(rt.getMinutes());
	
	//compare dates
	var l1970 = Date.UTC(lYear,lMonth,lDay,lHour,lMin,lSec); // current time
	var t1970 = Date.UTC(tyr,tmon,tday,thr,tmi,0); // tournament start time
	var r1970 = Date.UTC(ryr,rmon,rday,rhr,rmi,0); // registration start time
	
	//Only write output if the tourney is open (toolbar one-liner)
	if (l1970 < t1970 && l1970 > r1970 && isSet == "N") { // iSSet = Y ensures only the next tourney is displayed
		document.write('<tr><td valign="top" nowrap><span id="theTime" class="tbcountdown"></span><font color="#000099">'+thr+':'+tmi+'</font> &nbsp; &nbsp; <a href="http://www.slottournaments.net/feeds/" target="_blank" title="Click here for entry details and full schedule">'+tName+' ['+tFee+'] &raquo;</a></td></tr>');
		isSet = "Y";
		self.setTimeout('refresh()', 1800000);  // wait 30 minutes, then refresh
		//Set countdown min,sec
		var cmillisecs = (t1970 - l1970) / 1000 ; // seconds
		min = Math.floor(cmillisecs/60);  // The minutes
		sec = cmillisecs % 60;              // The balance of seconds
		countDown();
		}
	}
	
function setDefaultMessage() {
	if (isSet == "N") {
		document.write('<tr><td valign="top">&nbsp;<a href="http://www.slottournaments.net/feeds/" target="_blank" title="Click here for entry details and full schedule">Microgaming feed currently unavailable &raquo;</a></td></tr>');
		self.setTimeout('refresh()', 600000);  // wait 10 minutes, then refresh
		}
	}
	
function countDown() {
	sec--;
	if (sec == -01) {
    	sec = 59;
    	min = min - 1;
  		}
	else {
   		min = min;
  		}
	if (sec<=9) {
		sec = "0" + sec;
		}
  	
	time = (min <= 9 ? "0" + min : min) + ":" + sec; // format output
	
	if (document.getElementById && min < 20) { // show for last 20 minutes only
		theTime.innerHTML = time;
		}
  	
	SD=window.setTimeout("countDown();", 1000);
	if ((min == '10' && sec == '00') || (min < 10 && !chgColor)) {
		document.getElementById('theTime').style.backgroundColor = "#ee0000";
		chgColor = "Y";
		}
	if (min == '00' && sec == '00') {
		sec = "00";
		window.clearTimeout(SD);
		document.getElementById('theTime').style.backgroundColor = "#000099";
		theTime.innerHTML = "In Play";
		self.setTimeout('refresh()', 600000);  // wait 10 minutes, then refresh
		}
	}

function refresh() {
	self.location.href = "/feeds/tb_feed.php";
	}

function silentErrorHandler() {return true;}

window.onerror=silentErrorHandler;

