
var LOCAL_DEV = false;
var exportWin = null;
var testWin = null;

function AddCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1))
	{
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function AdminLogin()
{
	self.location.href = "login.php?caller=" + escape(self.location.href);
}

function CheckFloat(str, gtzero)
{
	if (str.length == 0) return false;
	else if (isNaN(str)) return false;
	else if (parseFloat(str) < 0) return false;

	if (gtzero && parseFloat(str) == 0) return false;

	return true;
}

function CheckInteger(str, gtzero)
{
	if (str.length == 0) return false;
	else if (isNaN(str)) return false;
	else if (! IsInteger(str)) return false;
	else if (parseInt(str) < 0) return false;

	if (gtzero && parseInt(str) == 0) return false;

	return true;
}

function CheckSmallInt(txt)
{
	if (! CheckInteger(txt.value))
	{
		alert("Enter an integer value");
		txt.value = "";
		txt.focus();
		return false;
	}

	if (parseInt(txt.value) > 65535)
	{
		alert("Maximum point value is 65,535");
		txt.value = "0";
		txt.focus();
		return false;
	}

	return true;
}

function ChooseLeague()
{
	self.location.href = "league_chooser.php";
}

function ChooseSeason(sid)
{
	self.location.href = "series_seasons.php?series_id=" + escape(sid);
}

function ChooseSeries(lid)
{
	self.location.href = "league_series.php?league_id=" + escape(lid);
}

function ClearDate(txt)
{
	if (txt.value.toLowerCase().indexOf("mm/dd") >= 0) txt.value = "";
	txt.select();
}

function ExportOpen(filename)
{
//	exportWin = open(filename, "exportWindow", "height=100,width=100");
	self.location.href = filename;
}

function IsInteger(str)
{
	return (str.toString().search(/^-?[0-9]+$/) == 0);
}

function LeagueCookieDelete()
{
	deleteCookie("league_id");
	deleteCookie("league_id", "", "danlisa.com");
	deleteCookie("league_id", "", "www.danlisa.com");

	SeriesCookieDelete();
}

function LeagueCookieSet(league_id)
{
	if (LOCAL_DEV) setCookie("league_id", league_id, expDate);

	else
	{
		setCookie("league_id", league_id, expDate, '', 'danlisa.com');
		setCookie("league_id", league_id, expDate, '', 'www.danlisa.com');
	}
}

function LoginCookieDelete(url_root)
{
	deleteCookie("driver_name");
	deleteCookie("driver_passwd");
	deleteCookie("driver_name", "");
	deleteCookie("driver_name", "", "danlisa.com");
	deleteCookie("driver_name", "", ".danlisa.com");
	deleteCookie("driver_name", "", "www..danlisa.com");
	deleteCookie("driver_passwd", "");
	deleteCookie("driver_passwd", "", "danlisa.com");
	deleteCookie("driver_passwd", "", ".danlisa.com");
	deleteCookie("driver_passwd", "", "www.danlisa.com");
	if (url_root != null)
	{
		deleteCookie("driver_name", url_root);
		deleteCookie("driver_name", url_root, "danlisa.com");
		deleteCookie("driver_name", url_root, ".danlisa.com");
		deleteCookie("driver_name", url_root, "www..danlisa.com");
		deleteCookie("driver_passwd", url_root);
		deleteCookie("driver_passwd", url_root, "danlisa.com");
		deleteCookie("driver_passwd", url_root, ".danlisa.com");
		deleteCookie("driver_passwd", url_root, "www.danlisa.com");
	}
}

function LoginCookieSet(driver_name, driver_passwd)
{
	if (LOCAL_DEV)
	{
		setCookie('driver_name', driver_name, expDate);
		setCookie('driver_passwd', driver_passwd, expDate);
	}

	else
	{
		setCookie('driver_name', driver_name, expDate, '', 'danlisa.com');
		setCookie('driver_name', driver_name, expDate, '', 'www.danlisa.com');
		setCookie('driver_passwd', driver_passwd, expDate, '', 'danlisa.com');
		setCookie('driver_passwd', driver_passwd, expDate, '', 'www.danlisa.com');
	}
}

function LogOff()
{
	if (confirm("Log off?"))
	{
		LoginCookieDelete();
		self.location.replace("index.php", 0);
	}
}

// Generic function called when page loads.  Various pages/javascripts will overwrite this function.
function PageLoad()
{
}

function SeasonCookieDelete()
{
	deleteCookie("season_id");
	deleteCookie("season_id", "", "danlisa.com");
	deleteCookie("season_id", "", "www.danlisa.com");
}

function SeasonCookieSet(season_id)
{
	if (LOCAL_DEV) setCookie("season_id", season_id, expDate);

	else
	{
		setCookie("season_id", season_id, expDate, '', 'danlisa.com');
		setCookie("season_id", season_id, expDate, '', 'www.danlisa.com');
	}
}

function SeriesCookieDelete()
{
	deleteCookie("series_id");
	deleteCookie("series_id", "", "danlisa.com");
	deleteCookie("series_id", "", "www.danlisa.com");

	SeasonCookieDelete();
}

function SeriesCookieSet(series_id)
{
	if (LOCAL_DEV) setCookie("series_id", series_id, expDate);

	else
	{
		setCookie("series_id", series_id, expDate, '', 'danlisa.com');
		setCookie("series_id", series_id, expDate, '', 'www.danlisa.com');
	}
}

function SetDate(txt)
{
	if (txt.value.length == 0) txt.value = "MM/DD/YY";
}

function TestUrl(txtField)
{
	if (txtField.value.length == 0)
	{
		alert("Enter a URL to test!");
		txtField.focus();
		return;
	}

	URLFix(txtField);
	if (! testWin || testWin.closed) testWin = open(txtField.value, "testWindow", "");
	else testWin.location.href = txtField.value;
	testWin.focus();
}

function URLFix(txtField, btn)
{
	if (txtField.value.length > 0 && txtField.value.indexOf("http://") < 0)
	{
		txtField.value = "http://" + txtField.value;
	}

	if (btn) btn.disabled = (txtField.value.length == 0);
}

// Cookie functions.
function setCookie(name, value, expires, path, domain, secure)
{
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");

	document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}

	else begin += 2;

	var end = document.cookie.indexOf(";", begin);
	if (end == -1) end = dc.length;

	return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain)
{
	if (getCookie(name))
	{
		document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date)
{
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0) date.setTime(date.getTime() - skew);
}

// Cookie expiration date: +6 months.
var expDate = new Date();
expDate.setTime(expDate.getTime() + (180 * 24 * 60 * 60 * 1000));

