﻿var _TotalStates = 0;
var _StatesSelected = 0;
var _DaysToPlay = '';
var _WayToPlay = '';
var _BetToPlay = '';
var _DrawDisplayed = false;
var _DrawExpired = false;
var _UpgradeMessage = 'You are using a web browser that is not compatible with our order system.  Please upgrade to the latest version of Microsoft Internet Explorer, Firefox, Netscape, Opera, or Safari before placing your order.';

pageInit();

function expAction(id, game, time, draw) {
	var ref;
	
	_DrawExpired = true;
	verifyUpdateText();
	
	if (document.getElementById) {
		ref = document.getElementById(id);
	}
	
	if (ref) {
		ref.value = '1';
		alert('It is now passed the ' + game + ' order cutoff time (' + time + ' Eastern Time).\nIt is too late to play in the ' + draw.replace(/&nbsp;/gi, ' ') + ' drawing.\n\nWhen you click the OK button on this message, the page will refresh,\nbut any entries you made on this page will be saved on the new page.');
		ref.form.submit();
	}
	else {
		alert('It is now passed the ' + game + ' order cutoff time (' + time + ' Eastern Time).\nIt is too late to play in the ' + draw.replace(/&nbsp;/gi, ' ') + ' drawing.\n\nAfter clicking OK to close this message, please click the Refresh or\nReload button on your Internet browser.\n\nIf you do not refresh the page you will get an error when you try to\nadd the order to your Cart.');
	}
};

function pageInit() {
	var objDays;
	var objWay;
	var objBet;
	
	if (document.getElementsByTagName) {
		var list = document.getElementsByTagName('input');
		var num = list.length;

		for (var i=0; i<num; i++) {
			if (list[i].name.indexOf('StatesList') >= 0) {
				_TotalStates++;
				
				if (list[i].checked) {
					_StatesSelected++;
				}
			}
			else {
				if (list[i].name.indexOf('OptionDays') >= 0) {
					if (list[i].value == '1') {
						objDays = list[i];
					}
					if (list[i].checked) {
						_DaysToPlay = list[i].value;
					}
				}
				else {
					if (list[i].name.indexOf('OptionPlay') >= 0) {
						if (list[i].value == 'sb') {
							objWay = list[i];
						}
						if (list[i].checked) {
							_WayToPlay = list[i].value;
						}
					}
					else {
						if (list[i].name.indexOf('OptionBet') >= 0) {
							if (list[i].value == '25') {
								objBet = list[i];
							}
							if (list[i].checked) {
								_BetToPlay = list[i].value;
							}
						}
					}
				}
			}
		}

		if (_DaysToPlay == '') {
			objDays.checked = true;
			_DaysToPlay = '1';
		}

		if (_WayToPlay == '') {
			objWay.checked = true;
			_WayToPlay = 'sb';
		}

		if (_BetToPlay == '') {
			objBet.checked = true;
			_BetToPlay = '25';
		}

		verifyUpdateText();
	}
	else {
		alert(_UpgradeMessage);
	}
};

function statesAll() {
	if (document.getElementsByTagName) {
		var list = document.getElementsByTagName('input');
		var num = list.length;
		var count = 0;

		for (var i=0; i<num; i++) {
			if (list[i].name.indexOf('StatesList') >= 0) {
				list[i].checked = true;
				count++;
			}
		}

		_StatesSelected = count;
		verifyUpdateText();
	}
	else {
		alert(_UpgradeMessage);
	}
};

function statesNone() {
	if (document.getElementsByTagName) {
		var list = document.getElementsByTagName('input');
		var num = list.length;

		for (var i=0; i<num; i++) {
			if (list[i].name.indexOf('StatesList') >= 0) {
				list[i].checked = false;
			}
		}

		_StatesSelected = 0;
		verifyUpdateText();
	}
	else {
		alert(_UpgradeMessage);
	}
};

function statesToggle() {
	if (document.getElementsByTagName) {
		var list = document.getElementsByTagName('input');
		var num = list.length;
		var count = 0;
		var chk;

		for (var i=0; i<num; i++) {
			if (list[i].name.indexOf('StatesList') >= 0) {
				chk = !list[i].checked;
				list[i].checked = chk;
				
				if (chk) {
					count++;
				}
			}
		}

		_StatesSelected = count;
		verifyUpdateText();
	}
	else {
		alert(_UpgradeMessage);
	}
};

function middayAll() {
	if (document.getElementsByTagName) {
		var list = document.getElementsByTagName('input');
		var num = list.length;
		var count = 0;
		var chk;

		for (var i=0; i<num; i++) {
			if (list[i].name.indexOf('StatesList') >= 0) {
				chk = (list[i].title.indexOf('Midday')>=0);
				list[i].checked = chk;
				
				if (chk) {
					count++;
				}
			}
		}

		_StatesSelected = count;
		verifyUpdateText();
	}
	else {
		alert(_UpgradeMessage);
	}
};

function eveningAll() {
	if (document.getElementsByTagName) {
		var list = document.getElementsByTagName('input');
		var num = list.length;
		var count = 0;
		var chk;

		for (var i=0; i<num; i++) {
			if (list[i].name.indexOf('StatesList') >= 0) {
				chk = (list[i].title.indexOf('Evening')>=0);
				list[i].checked = chk;
				
				if (chk) {
					count++;
				}
			}
		}

		_StatesSelected = count;
		verifyUpdateText();
	}
	else {
		alert(_UpgradeMessage);
	}
};

function numsType(te) {
	if ((_Game == 3) || (_Game == 4)) {
		if (document.getElementById) {
			var oldnums = _Numbers;
			var pat = (_Game == 3) ? /((\b|^)((\d)\D?(\d)\D?(\d))(\b|$))/g : /((\b|^)((\d)\D?(\d)\D?(\d)\D?(\d))(\b|$))/g;
			var ary = te.value.match(pat);
			
			if (ary) {
				_Numbers = ary.length;
			}
			else {
				_Numbers = 0;
			}
			
			if (oldnums != _Numbers) {
				verifyUpdateText();
			}
		}
		else {
			alert(_UpgradeMessage);
		}
	}
	else {
		alert('This page is corrupted.  When you click OK it will automatically reload.');
		window.location.reload();
	}
};

function recalcEntry() {
	if (document.getElementById) {
		var obj = document.getElementById(_EntryID);
		
		if (obj) {
			numsType(obj);
		}
	}
	else {
		alert(_UpgradeMessage);
	}
	
	return false;
};

function verifyUpdateText() {
	if (document.getElementById) {
		var template = 'You will be playing <strong>=1=</strong> =2= for <strong>=3=</strong>.';
		var obj;
		var playmultiple = 0;
		var wager = 0;
		
		if (!_DrawDisplayed) {
			document.getElementById('sub_draw_mid').innerHTML = "<strong>" + _Drawing_mid + "</strong>";
			document.getElementById('sub_draw_eve').innerHTML = "<strong>" + _Drawing_eve + "</strong>";
			document.getElementById('sub_mid_draw').innerHTML = '<strong>Midday</strong> games you order will be played on <strong>' + _Drawing_mid + '</strong>';
			document.getElementById('sub_eve_draw').innerHTML = '<strong>Evening</strong> games you order will be played on <strong>' + _Drawing_eve + '</strong>';
			_DrawDisplayed = true;
		}
		
		obj = document.getElementById('sub_states');
		
		if (obj) {
			
			if (_StatesSelected == '') {
				template = '<strong class="errortext">Please select one or more state games to play.</strong>';
			}
			else {
				if (_DrawExpired) {
					template = '<strong class="errortext">Order cutoff time expired.&nbsp; Please Refresh the page.</strong>';
				}
				else {
					template = template.replace(/=1=/, _StatesSelected.toString());
					
					if (_StatesSelected == 1) {
						template = template.replace(/=2=/, 'state game');
					}
					else {
						template = template.replace(/=2=/, 'different state games');
					}
				}
			}
			
			if (_DaysToPlay == '1') {
				template = template.replace(/=3=/, _DaysToPlay + ' day only');
			}
			else {
				template = template.replace(/=3=/, _DaysToPlay + ' consecutive days');
			}
			
			obj.innerHTML = template;
		}

		obj = document.getElementById('sub_tickets');
		
		if (obj) {
			if (_Numbers < 1) {
				obj.innerHTML = 'You have not entered any valid ticket numbers to play.'
			}
			else {
				if (_Numbers == 1) {
					obj.innerHTML = 'You will be playing <strong>1</strong> ticket' + ((_StatesSelected > 1) ? ' in each state game.' : '.');
				}
				else {
					obj.innerHTML = 'You will be playing <strong>' + _Numbers + '</strong> tickets' + ((_StatesSelected > 1) ? ' in each state game.' : '.');
				}
			}
		}
		
		template = 'Each ticket will be played <strong>=1=</strong>.';
		obj = document.getElementById('sub_play');
		
		if (obj) {
			switch (_WayToPlay) {
				case 'sb':
					template = template.replace(/=1=/, 'Straight + Box');
					playmultiple = 2;
					break;
			
				case 's':
					template = template.replace(/=1=/, 'Straight');
					playmultiple = 1;
					break;
			
				case 'b':
					template = template.replace(/=1=/, 'Box');
					playmultiple = 1;
					break;
			
				default:
					template = 'Please select how to play each ticket.';
			}
				
			obj.innerHTML = template;
		}

		template = 'You are wagering <strong>=1=</strong> for every ticket played.';
		obj = document.getElementById('sub_bet');
		
		if (obj) {
			switch (_BetToPlay) {
				case '25':
					template = template.replace(/=1=/, '$0.25');
					wager = .25;
					break;
			
				case '50':
					template = template.replace(/=1=/, '$0.50');
					wager = .5;
					break;
			
				case '100':
					template = template.replace(/=1=/, '$1.00');
					wager = 1;
					break;
			
				default:
					template = 'Please select the wager for each ticket.';
			}
				
			obj.innerHTML = template;
		}
		
		var obj = document.getElementById('sub_nums');
		
		if (obj) {
			var append = '&nbsp; ( <a href="#" onclick="return recalcEntry();">Recalculate</a> )';
			
			if (_Numbers < 1) {
				obj.innerHTML = 'You have not entered any valid ticket numbers to play.' + append;
			}
			else {
				if (_Numbers == 1) {
					obj.innerHTML = 'You entered <strong>1</strong> valid ticket number to play.' + append;
				}
				else {
					obj.innerHTML = 'You entered <strong>' + _Numbers + '</strong> valid ticket numbers to play.' + append;
				}
			}
		}
		
		if ((_StatesSelected >= 1) && (_StatesSelected <= _TotalStates) && (_Numbers >= 1) && (_DaysToPlay >= 1) && (_DaysToPlay <= 7) && (playmultiple >= 1) && (playmultiple <= 2) && (wager >= .25) && (wager <= 1)) {
			var ttl = (Math.round(_StatesSelected * _Numbers * _DaysToPlay * playmultiple * wager * 100) / 100).toString();
			var ttlstr = '$';
			var pt = ttl.indexOf('.');
			
			if (pt == -1) {
				ttlstr += ttl + '.00';
			}
			else {
				if (pt == 0) {
					ttlstr += '0' + ((ttl + '00').substr(0, 3));
				}
				else {
					ttlstr += ((ttl + '00').substr(0, pt + 3));
				}
			}
			
			template = 'The total charge for this order is <strong>' + ttlstr + '</strong>';
			obj = document.getElementById('sub_total');
			
			if (obj) {
				obj.innerHTML = template;
				obj.style.visibility = 'visible';
			}

			obj = document.getElementById(_SubmitID);
			
			if (obj) {
				obj.disabled = false;
			}
		}
		else {
			obj = document.getElementById('sub_total');
			
			if (obj) {
				obj.style.visibility = 'hidden';
			}

			obj = document.getElementById(_SubmitID);
			
			if (obj) {
				obj.disabled = true;
			}
		}
	}
	else {
		alert(_UpgradeMessage);
	}
};

function stateClick(obj) {
	if (obj.checked) {
		_StatesSelected++;
	}
	else {
		_StatesSelected--;
	}

	verifyUpdateText();
};

function daysClick(obj) {
	_DaysToPlay = obj.value;
	verifyUpdateText();
};

function wayClick(obj) {
	_WayToPlay = obj.value;
	verifyUpdateText();
};

function betClick(obj) {
	_BetToPlay = obj.value;
	verifyUpdateText();
};

function mouseWait() {
	try {
		document.body.style.cursor = 'wait';
	}
	catch(e) {
	}
};

function mouseNormal() {
	try {
		document.body.style.cursor = '';
	}
	catch(e) {
	}
};

function AddQPs(num,noDupCombos) {
	mouseWait();

	if (((_Game == 3) || (_Game == 4)) && (num > 0) & (num < 101)) {
		var obj = document.getElementById(_EntryID);
				
		if (obj) {
			var objval = obj.value;
			var limit = (_Game == 3) ? (noDupCombos ? 220 : 1000) : (noDupCombos ? 715 : 10000);
			var pat = (_Game == 3) ? /((\b|^)((\d)\D?(\d)\D?(\d))(\b|$))/g : /((\b|^)((\d)\D?(\d)\D?(\d)\D?(\d))(\b|$))/g;
			var objnumary = objval.match(pat);

			if ((objnumary) && (parseInt(objnumary.length) + parseInt(num) > parseInt(limit))) {
				alert('You cannot add ' + num + ' Quick Picks, because the ' + num + ' new Quick Picks, plus the ' + objnumary.length + ' numbers already in the entry space, would exceed the maximum of ' + limit + (noDupCombos ? ' unique box combinations.' : ' possible combinations.'));
			}
			else {
				var objnums;
				
				if (objnumary) {
					objnums = objnumary.join(' ').replace(/[^\d ]/g, '');
				}
				else {
					objnums = '';
				}
				
				var str = '';
				var qpa = new Array(_Game);
				var qp, x, y, a, b, c, d, combo, stayinloop;

				for (x=0; x<num; x++) {
					stayinloop = true;

					while (stayinloop) {
						qp = '';
						
						for (y=0; y<_Game; y++) {
							qpa[y] = Math.floor(Math.random()*10).toString();
							qp += qpa[y];
						}

						if (noDupCombos) {
							stayinloop = false;

							if (_Game == 3) {
								outerLoop3:
								for (a=0; a<_Game; a++) {
									for (b=0; b<_Game; b++) {
										for (c=0; c<_Game; c++) {
											if ((a!=b) && (a!=c) && (b!=c)) {
												combo = qpa[a] + qpa[b] + qpa[c];

												if ((objnums.indexOf(combo) != -1) || (str.indexOf(combo) != -1)) {
													stayinloop = true;
													break outerLoop3;
												}
											}
										}
									}
								}
							}
							else {
								outerLoop4:
								for (a=0; a<_Game; a++) {
									for (b=0; b<_Game; b++) {
										for (c=0; c<_Game; c++) {
											for (d=0; d<_Game; d++) {
												if ((a!=b) && (a!=c) && (a!=d) && (b!=c) && (b!=d) && (c!=d)) {
													combo = qpa[a] + qpa[b] + qpa[c] + qpa[d];

													if ((objnums.indexOf(combo) != -1) || (str.indexOf(combo) != -1)) {
														stayinloop = true;
														break outerLoop4;
													}
												}
											}
										}
									}
								}
							}
						}
						else {
							if ((objnums.indexOf(qp) == -1) && (str.indexOf(qp) == -1)) {
								stayinloop = false;
							}
						}
					}
					
					str += (x>0 ? ' ' : '') + qp;
				}

				if (objval.length == 0) {
					obj.value = str;
				}
				else {
					obj.value += '\n' + str;
				}
				
				numsType(obj);
			}
		}
	}

	mouseNormal();
};

function openDialogHelp(obj,num) {
	var txt = '';
	
	switch (num) {
		case 1:
			txt = '<h2>Help on entering your numbers to play</h2>' +
			      '<p>Enter the numbers to play in the large text entry space.</p>' +
			      '<p>You can use just about any method of entering your numbers that is convenient for you.</p>' +
			      '<p>The easiest way to enter your numbers is by typing the 3-digit number, followed by a space or comma (or any other character), and then entering your next 3-digit number.</p>' +
			      '<p>For example, if you wanted to enter five different numbers to play, you could enter:</p>' +
			      '<pre>123 456 789 654 321</pre>' +
			      '<p>You can also feel free to enter any separator character between each digit (for example, &quot;1-2-3&quot; or &quot;1,2,3&quot;), and the numbers can be entered on different lines, if you wish.</p>' +
			      '<p>If you play a &quot;triple&quot; (three of the same digits) Straight + Box, it is the same thing as placing two Straight bets on the number, because triples can only match straight.&nbsp; Also, playing a triple Box is the same as playing it Straight.</p>' +
			      '<p>Be sure to verify that the correct numbers were interpreted by our website in the &quot;Verify your selections&quot; section of the page.</p>';
			break;
		case 4:
			txt = '<h2>Help on entering your numbers to play</h2>' +
			      '<p>Enter the numbers to play in the large text entry space.</p>' +
			      '<p>You can use just about any method of entering your numbers that is convenient for you.</p>' +
			      '<p>The easiest way to enter your numbers is by typing the 4-digit number, followed by a space or comma (or any other character), and then entering your next 4-digit number.</p>' +
			      '<p>For example, if you wanted to enter five different numbers to play, you could enter:</p>' +
			      '<pre>1234 4567 7890 6543 3210</pre>' +
			      '<p>You can also feel free to enter any separator character between each digit (for example, &quot;1-2-3-4&quot; or &quot;1,2,3,4&quot;), and the numbers can be entered on different lines, if you wish.</p>' +
			      '<p>If you play a &quot;quad&quot; (four of the same digits) Straight + Box, it is the same thing as placing two Straight bets on the number, because quads can only match straight.&nbsp; Also, playing a quad Box is the same as playing it Straight.</p>' +
			      '<p>Be sure to verify that the correct numbers were interpreted by our website in the &quot;Verify your selections&quot; section of the page.</p>';
			break;
	}

	objPopup.UpdateTitleBarHTML(createDialogTitle('Help'));
	objPopup.UpdateContentHTML(createDialogContent(txt));
	objPopup.OpenWindow(obj.offsetTop+obj.offsetParent.offsetTop);
};
