var rightclickmessage = "Copyright \u00a9 Daily Racing Form";

function rightClick(e) {
	if (document.all) {
		if (event.button == 2) {
			alert(rightclickmessage);
			return false;
		}
	}
	else if (document.layers) {
		if (e.which == 3) {
			alert(rightclickmessage);
			return false;
		}
	}
	else {
		if (e.which == 3) {
			alert(rightclickmessage);
			return false;
		}
	}
}

if (document.layers) {
	document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown = rightClick;

function getCalendarDate() {
	var months = new Array(13);
	months[0] = "January";
	months[1] = "February";
	months[2] = "March";
	months[3] = "April";
	months[4] = "May";
	months[5] = "June";
	months[6] = "July";
	months[7] = "August";
	months[8] = "September";
	months[9] = "October";
	months[10] = "November";
	months[11] = "December";
	var now = new Date();
	var year = now.getYear()
	if (year < 2000) {
		year = year + 1900;
	}
	var dateString = months[now.getMonth()] + ' ' + now.getDate() + ', ' + year;
	return dateString;
}

function getClockTime() {
	var now = new Date();
	// alert("now "+now);
	var hour = now.getHours();
	var minute = now.getMinutes();
	var ap = "AM";
	if (hour > 11) {
		ap = "PM";
	}
	if (hour > 12) {
		hour = hour - 12;
	}
	if (hour == 0) {
		hour = 12;
	}
	if (minute < 10) {
		minute = "0" + minute;
	}
	if (hour < 10) {
		hour = "0" + hour;
	}
	var timeString = hour + ':' + minute + ' ' + ap;
	return timeString;

}

function getServerTime() {

	var now = new Date();
	var hour = now.getHours();
	var minute = now.getMinutes();
	var ap = "AM";
	if (hour > 11) {
		ap = "PM";
	}
	if (hour > 12) {
		hour = hour - 12;
	}
	if (hour == 0) {
		hour = 12;
	}
	if (minute < 10) {
		minute = "0" + minute;
	}
	if (hour < 10) {
		hour = "0" + hour;
	}
	var timeString = hour + ':' + minute + ' ' + ap;
	startRequest();
}

function startRequest()

{

	xmlhttpObj1 = GetXMLHTTPConnection();

	xmlhttpObj1.open("GET", baseURL + "/ajaxResults", true)

	xmlhttpObj1.onreadystatechange = handleStateChange;

	xmlhttpObj1.send(null);
}

function handleStateChange() {
	var date;
	if (xmlhttpObj1.readyState == 4) {
		if (xmlhttpObj1.status == 200) {
			date = xmlhttpObj1.responseXML.getElementsByTagName("date")[0].childNodes[0].nodeValue;
			document.getElementById("currenttime").innerHTML = date;

		}
		else {
			alert("Error loading pagen" + xmlhttpObj1.status + ":" + xmlhttpObj1.statusText);
		}

	}

}

function getServerTime1() {

	var now = new Date();
	var hour = now.getHours();
	var minute = now.getMinutes();
	var ap = "AM";
	if (hour > 11) {
		ap = "PM";
	}
	if (hour > 12) {
		hour = hour - 12;
	}
	if (hour == 0) {
		hour = 12;
	}
	if (minute < 10) {
		minute = "0" + minute;
	}
	if (hour < 10) {
		hour = "0" + hour;
	}
	var timeString = hour + ':' + minute + ' ' + ap;
	startRequest1();
}

function startRequest1()

{

	xmlhttpObj1 = GetXMLHTTPConnection();

	xmlhttpObj1.open("GET", baseURL + "/ajaxResults", true)

	xmlhttpObj1.onreadystatechange = handleStateChange1;

	xmlhttpObj1.send(null);
}

function handleStateChange1() {
	var date;
	if (xmlhttpObj1.readyState == 4) {
		if (xmlhttpObj1.status == 200) {
			date = xmlhttpObj1.responseXML.getElementsByTagName("datetime")[0].childNodes[0].nodeValue;

			document.getElementById("TimeInfo").innerHTML = date;
		}
		else {
			alert("Error loading pagen" + xmlhttpObj1.status + ":" + xmlhttpObj1.statusText);
		}

	}

}

function getUpdateTime(now) {
	var strtime = now.substring(0, now.indexOf(':'));
	if (strtime < 10) {
		if (strtime.length == 1) {
			strtime = '0' + strtime;
		}
	}
	return strtime + now.substring(now.indexOf(':'), now.length);
}

function removeSecond(timeVal) {
	if (timeVal == null || timeVal == '' || timeVal.length < 11)
		return removeSecondForChange(timeVal);
	var val = timeVal.substring(0, 5) + timeVal.substring(8);
	if (val.substring(0, 1) == '0')
		val = val.substring(1);
	return val;
}

function removeSecondForChange(timeVal) {
	var timeIndicator = ' AM'
	if (timeVal == null || timeVal == '')
		return timeVal;
	var val = timeVal.substring(0, 2);
	if (val > 11) {
		timeIndicator = ' PM'
	}
	if (val > 12) {
		val = val - 12;
	}

	var valtemp = timeVal.substring(2, 5);
	// alert(val+valtemp);
	return val + valtemp + timeIndicator;
}

function removeDate(dateVal) {
	// alert("dateVal "+dateVal);
	if (dateVal == null || dateVal == '' || dateVal.length < 23)
		return dateVal;
	// var time=getUpdateTime(dateVal.substring(11));
	// alert(dateVal.substring(11));
	return removeSecondForChange(dateVal.substring(11))
}

function isOdd(number) {
	var numberB = (number / 2);
	var numberC = (Math.floor(numberB));
	if (numberB == numberC) {
		return false;
	}
	else {
		return true;
	}
}
function removeCents(val) {
	if (val == null || val == "")
		return val;
	if (val.indexOf('.') > -1) {
		return val.substring(0, val.indexOf('.'))
	}
	else {
		return val;
	}
}
function convertTo2Dollar(minimum, priceVal) {
	if (minimum == null || minimum == "" || minimum == 0)
		return removeCents(priceVal);
	if (priceVal == 'SCR')
		return priceVal;
	/*
	 * if (minimum == '1.00') { return
	 * removeCents(FormatNumber((2.00*(priceVal.replace(',',''))).toFixed(2)))
	 * //return
	 * removeCents(FormatNumber((2.00*(parseInt(priceVal.replace(',','')))).toFixed(2))) }
	 */
	// return removeCents(priceVal);
	return priceVal;
}

function replaceSlashToDash(val) {
	var regEx = new RegExp('/', 'gi');
	return val.replace(regEx, '-')
}

function getRaceString(val) {
	if (val == null)
		return "";
	if (val == '1')
		return '1st';
	else if (val == '2')
		return '2nd';
	else if (val == '3')
		return '3rd';
	else
		return val + 'th';
}

function getScratchedHorses(scratchList) {
	return getDemilitedArray(scratchList, ",", "-");
}
function IsHorseScratched(scratchArr, currentPgmNo) {
	if (scratchArr == null)
		return false;
	if (currentPgmNo == null || currentPgmNo == "")
		return false;
	for ( var i = 0; i < scratchArr.length; i++) {
		if (scratchArr[i] == currentPgmNo) {
			return true;
		}
	}
}

function isWinnerScratched(scratchArr, winnerList) {
	if (scratchArr == null)
		return false;
	if (winnerList == null || winnerList == "")
		return false;
	var winnerArr = getDemilitedArray(winnerList, "/");
	for ( var wI = 0; wI < scratchArr.length; wI++) {
		for ( var wJ = 0; wJ < winnerArr.length; wJ++) {
			if (scratchArr[wI] == winnerArr[wJ]) {
				return true;
			}
		}
	}
}

function getScriptFileName(poolId) {

	if (poolId == null || typeof poolId == 'undefined' || "WPS" == poolId) {
		return '/scripts/toteboard/raceview.js'
	}
	else if ("EX" == poolId) {
		return '/scripts/toteboard/exacta.js'
	}
	else if ("QU" == poolId) {
		return '/scripts/toteboard/quinella.js'
	}
	else if ("DBL" == poolId) {
		return '/scripts/toteboard/dailydoubleview.js'
	}
	else if ("PK3" == poolId) {
		return '/scripts/toteboard/pick3.js'
	}
	else if ("PK4" == poolId) {
		return '/scripts/toteboard/pick4.js'
	}
	else if ("PK6" == poolId) {
		return '/scripts/toteboard/pick6.js'
	}
	else if ("DBLAF" == poolId) {
		return '/scripts/toteboard/dailydoubleviewafter.js'
	}
	else if ("GSL" == poolId) {
		return '/scripts/toteboard/grandslam.js'
	}
	else {
		return '/scripts/toteboard/raceview.js'
	}

}

var MONTH_NAMES = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
		'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
var DAY_NAMES = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
function LZ(x) {
	return (x < 0 || x > 9 ? "" : "0") + x
}

// ------------------------------------------------------------------
// Utility functions for parsing in getDateFromFormat()
// ------------------------------------------------------------------
function _isInteger(val) {
	var digits = "1234567890";
	for ( var i = 0; i < val.length; i++) {
		if (digits.indexOf(val.charAt(i)) == -1) {
			return false;
		}
	}
	return true;
}
function _getInt(str, i, minlength, maxlength) {
	for ( var x = maxlength; x >= minlength; x--) {
		var token = str.substring(i, i + x);
		if (token.length < minlength) {
			return null;
		}
		if (_isInteger(token)) {
			return token;
		}
	}
	return null;
}

// ------------------------------------------------------------------
// getDateFromFormat( date_string , format_string )
//
// This function takes a date string and a format string. It matches
// If the date string matches the format string, it returns the
// getTime() of the date. If it does not match, it returns 0.
// ------------------------------------------------------------------
function getDateFromFormat(val, format) {

	val = val + "";
	format = format + "";
	var i_val = 0;
	var i_format = 0;
	var c = "";
	var token = "";
	var token2 = "";
	var x, y;
	var now = new Date();
	var year = now.getYear();
	var month = now.getMonth() + 1;
	var date = 1;
	var hh = now.getHours();
	var mm = now.getMinutes();
	var ss = now.getSeconds();
	var ampm = "";

	while (i_format < format.length) {
		// Get next token from format string
		c = format.charAt(i_format);
		token = "";
		while ((format.charAt(i_format) == c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
		}
		// Extract contents of value based on format token
		if (token == "yyyy" || token == "yy" || token == "y") {
			if (token == "yyyy") {
				x = 4;
				y = 4;
			}
			if (token == "yy") {
				x = 2;
				y = 2;
			}
			if (token == "y") {
				x = 2;
				y = 4;
			}
			year = _getInt(val, i_val, x, y);
			if (year == null) {
				return 0;
			}
			i_val += year.length;
			if (year.length == 2) {
				if (year > 70) {
					year = 1900 + (year - 0);
				}
				else {
					year = 2000 + (year - 0);
				}
			}
		}
		else if (token == "MMM" || token == "NNN") {
			month = 0;
			for ( var i = 0; i < MONTH_NAMES.length; i++) {
				var month_name = MONTH_NAMES[i];
				if (val.substring(i_val, i_val + month_name.length).toLowerCase() == month_name.toLowerCase()) {
					if (token == "MMM" || (token == "NNN" && i > 11)) {
						month = i + 1;
						if (month > 12) {
							month -= 12;
						}
						i_val += month_name.length;
						break;
					}
				}
			}
			if ((month < 1) || (month > 12)) {
				return 0;
			}
		}
		else if (token == "EE" || token == "E") {
			for ( var i = 0; i < DAY_NAMES.length; i++) {
				var day_name = DAY_NAMES[i];
				if (val.substring(i_val, i_val + day_name.length).toLowerCase() == day_name.toLowerCase()) {
					i_val += day_name.length;
					break;
				}
			}
		}
		else if (token == "MM" || token == "M") {
			month = _getInt(val, i_val, token.length, 2);
			if (month == null || (month < 1) || (month > 12)) {
				return 0;
			}
			i_val += month.length;
		}
		else if (token == "dd" || token == "d") {
			date = _getInt(val, i_val, token.length, 2);
			if (date == null || (date < 1) || (date > 31)) {
				return 0;
			}
			i_val += date.length;
		}
		else if (token == "hh" || token == "h") {
			hh = _getInt(val, i_val, token.length, 2);
			if (hh == null || (hh < 1) || (hh > 12)) {
				return 0;
			}
			i_val += hh.length;
		}
		else if (token == "HH" || token == "H") {
			hh = _getInt(val, i_val, token.length, 2);
			if (hh == null || (hh < 0) || (hh > 23)) {
				return 0;
			}
			i_val += hh.length;
		}
		else if (token == "KK" || token == "K") {
			hh = _getInt(val, i_val, token.length, 2);
			if (hh == null || (hh < 0) || (hh > 11)) {
				return 0;
			}
			i_val += hh.length;
		}
		else if (token == "kk" || token == "k") {
			hh = _getInt(val, i_val, token.length, 2);
			if (hh == null || (hh < 1) || (hh > 24)) {
				return 0;
			}
			i_val += hh.length;
			hh--;
		}
		else if (token == "mm" || token == "m") {
			mm = _getInt(val, i_val, token.length, 2);
			if (mm == null || (mm < 0) || (mm > 59)) {
				return 0;
			}
			i_val += mm.length;
		}
		else if (token == "ss" || token == "s") {
			ss = _getInt(val, i_val, token.length, 2);
			if (ss == null || (ss < 0) || (ss > 59)) {
				return 0;
			}
			i_val += ss.length;
		}
		else if (token == "a") {
			if (val.substring(i_val, i_val + 2).toLowerCase() == "am") {
				ampm = "AM";
			}
			else if (val.substring(i_val, i_val + 2).toLowerCase() == "pm") {
				ampm = "PM";
			}
			else {
				return 0;
			}
			i_val += 2;
		}
		else {
			if (val.substring(i_val, i_val + token.length) != token) {
				return 0;
			}
			else {
				i_val += token.length;
			}
		}
	}
	// If there are any trailing characters left in the value, it doesn't match
	if (i_val != val.length) {
		return 0;
	}
	// Is date valid for month?
	if (month == 2) {
		// Check for leap year
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { // leap
																																				// year
			if (date > 29) {
				return 0;
			}
		}
		else {
			if (date > 28) {
				return 0;
			}
		}
	}
	if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
		if (date > 30) {
			return 0;
		}
	}
	// Correct hours value
	if (hh < 12 && ampm == "PM") {
		hh = hh - 0 + 12;
	}
	else if (hh > 11 && ampm == "AM") {
		hh -= 12;
	}
	var newdate = new Date(year, month - 1, date, hh, mm, ss);
	return newdate.getTime();
}

// ------------------------------------------------------------------
// formatDate (date_object, format)
// Returns a date in the output format specified.
// The format string uses the same abbreviations as in getDateFromFormat()
// ------------------------------------------------------------------

/**
 * Formates a given date object and returns the value as a string.
 * @param date The date to format.
 * @param format A string of the format. Uses the same abbreviations as the getDateFromFormat function.
 * @param useUTC Optional. Boolean, true to use the utc values, false to use the regular values. False if not provided.
 * @return A string of the formated date.
 */
function formatDate(date, format, useUTC) {
	
	// If useUTC is not defined then set it to false.
	if(typeof useUTC == "undefined")
		useUTC = false;
	
	format = format + "";
	var result = "";
	var i_format = 0;
	var c = "";
	var token = "";
	
	// Variables to hold the pieces of the date.
	var y, M, d, E, H, m, s;
	
	// If we are using the utc values, then retrieve those from the date object.
	if(useUTC) {
		y = date.getUTCFullYear() + "";
		M = date.getUTCMonth() + 1;
		d = date.getUTCDate();
		E = date.getUTCDay();
		H = date.getUTCHours();
		m = date.getUTCMinutes();
		s = date.getUTCSeconds();
	}
	else { // Get the regular date values.
		y = date.getFullYear() + "";
		M = date.getMonth() + 1;
		d = date.getDate();
		E = date.getDay();
		H = date.getHours();
		m = date.getMinutes();
		s = date.getSeconds();
	}
	
	var yyyy, yy, MMM, MM, dd, hh, h, mm, ss, ampm, HH, H, KK, K, kk, k;
	// Convert real date parts into formatted versions
	var value = new Object();
	if (y.length < 4) {
		y = "" + (y - 0 + 1900);
	}
	value["y"] = "" + y;
	value["yyyy"] = y;
	value["yy"] = y.substring(2, 4);
	value["M"] = M;
	value["MM"] = LZ(M);
	value["MMM"] = MONTH_NAMES[M - 1];
	value["NNN"] = MONTH_NAMES[M + 11];
	value["d"] = d;
	value["dd"] = LZ(d);
	value["E"] = DAY_NAMES[E + 7];
	value["EE"] = DAY_NAMES[E];
	value["H"] = H;
	value["HH"] = LZ(H);
	if (H == 0) {
		value["h"] = 12;
	}
	else if (H > 12) {
		value["h"] = H - 12;
	}
	else {
		value["h"] = H;
	}
	value["hh"] = LZ(value["h"]);
	if (H > 11) {
		value["K"] = H - 12;
	}
	else {
		value["K"] = H;
	}
	value["k"] = H + 1;
	value["KK"] = LZ(value["K"]);
	value["kk"] = LZ(value["k"]);
	if (H > 11) {
		value["a"] = "PM";
	}
	else {
		value["a"] = "AM";
	}
	value["m"] = m;
	value["mm"] = LZ(m);
	value["s"] = s;
	value["ss"] = LZ(s);
	while (i_format < format.length) {
		c = format.charAt(i_format);
		token = "";
		while ((format.charAt(i_format) == c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
		}
		if (value[token] != null) {
			result = result + value[token];
		}
		else {
			result = result + token;
		}
	}
	return result;
}

/**
 * Returns the current date with a UTC offset of the latest possible racing that can be done on the tote board.
 * This is used so people in the east cost do not lose there tote feeds when midnight rolls over.
 * @return Date that has had it's utc hours offset.
 */
function getCurrentUTCDate() {
	var dte = new Date();
	var OFFSET = 8;

	dte.setUTCHours(dte.getUTCHours() - OFFSET);
	
	return dte;
}

/**
 * Returns the formated version of the race number. This function was created to make it easier to change the race number in
 * the file name. This is something that seems to change on a regular basisis with the tote.
 * @param raceNumber The race number to format.
 * @return Put a 0 on the front if raceNumber less than 10. 
 */
function formatRaceNumber(raceNumber) {
	if (raceNumber < 10)
		return "0" + raceNumber;
	
	return raceNumber;
}

