function calendar_page_exists(YYYYMM)
//*****************************************************************************
//* Determines whether a calendar page exists for the year and month 
//* specified by parameter YYYYMM and if so, returns a value of 1. Othwerwise 
//* returns a value of 0.
//*****************************************************************************
{
if (YYYYMM <= "201202")
  return X = 1
else
  return X = 0;
}

function tournament_scheduled(YYMMDD)
//*****************************************************************************
//* Determines whether a tournament was scheduled on the date indicated by 
//* parameter YYMMDD and if so, returns a value of 1. Othwerwise returns a 
//* value of 0.
//*
//* All tournament dates must be coded in this module. This is the only 
//* module where tournament dates are hard-coded.
//*****************************************************************************
{
if (YYMMDD >= "120607" && YYMMDD <= "120610")
  return X = 1;
if (YYMMDD >= "111013" && YYMMDD <= "111016") // event ID coded on line 286
  return X = 1;
return X = 0;
}

function bookmarksite(TITLE,URL)
//*****************************************************************************
//* Aids the user in bookmarking the website.
//*
//* Called directly from HTML code. 
//*****************************************************************************
{
if (window.sidebar) // firefox
  window.sidebar.addPanel(TITLE,URL,"")
else 
  if (window.opera && window.print) // opera
    { 
	var elem = document.createElement('a');
	elem.setAttribute('href',URL);
	elem.setAttribute('title',TITLE);
	elem.setAttribute('rel','sidebar');
	elem.click();
    } 
    else 
      if (document.all) // ie
        window.external.AddFavorite(URL,TITLE);
}

function numstr(DD)
//*****************************************************************************
//* Returns a 2-digit number based on the value of parameter DD. 
//*****************************************************************************
{
var NUM_STR = new Array(32);
NUM_STR[0] = "00";
NUM_STR[1] = "01";
NUM_STR[2] = "02";
NUM_STR[3] = "03";
NUM_STR[4] = "04";
NUM_STR[5] = "05";
NUM_STR[6] = "06";
NUM_STR[7] = "07";
NUM_STR[8] = "08";
NUM_STR[9] = "09";
NUM_STR[10] = "10";
NUM_STR[11] = "11";
NUM_STR[12] = "12";
NUM_STR[13] = "13";
NUM_STR[14] = "14";
NUM_STR[15] = "15";
NUM_STR[16] = "16";
NUM_STR[17] = "17";
NUM_STR[18] = "18";
NUM_STR[19] = "19";
NUM_STR[20] = "20";
NUM_STR[21] = "21";
NUM_STR[22] = "22";
NUM_STR[23] = "23";
NUM_STR[24] = "24";
NUM_STR[25] = "25";
NUM_STR[26] = "26";
NUM_STR[27] = "27";
NUM_STR[28] = "28";
NUM_STR[29] = "29";
NUM_STR[30] = "30";
NUM_STR[31] = "31";
return X = NUM_STR[DD];
}

function monthname(MM)
//*****************************************************************************
//* Returns the full month name based on the value of parameter MM. 
//*****************************************************************************
{
var NAME = new Array(13);
NAME[0] = "---";
NAME[1] = "January";
NAME[2] = "February";
NAME[3] = "March";
NAME[4] = "April";
NAME[5] = "May";
NAME[6] = "June";
NAME[7] = "July";
NAME[8] = "August";
NAME[9] = "September";
NAME[10] = "October";
NAME[11] = "November";
NAME[12] = "December";
return X = NAME[MM];
}

function monthname3(MM)
//*****************************************************************************
//* Returns a 3-character month name based on the value of parameter MM. 
//*****************************************************************************
{
var NAME = monthname(MM);
return X = NAME.substr(0,3);
}

function adjust_date(YYMMDD,ACTION,DAYS)
//*****************************************************************************
//* Returns the specified date adjusted by the number of days specified in 
//* DAYS. If an ACTION of "-" is specified the date is decreased otherwise it 
//* is increased.
//*****************************************************************************
{
var ADJ = DAYS * 86400000; // ms per day
var YYYY = 2000 + Number(YYMMDD.substr(0,2));
var MM = Number(YYMMDD.substr(2,2)) - 1;
var DD = Number(YYMMDD.substr(4,2));
var D = new Date();
D.setFullYear(YYYY);
D.setMonth(MM);
D.setDate(DD);
var MS = D.getTime();
if (ACTION == "-")
  MS = MS - ADJ
else
  MS = MS + ADJ;
D.setTime(MS);
DD = D.getDate();
MM = D.getMonth() + 1;
YYYY = D.getFullYear();
var YYYY_STR = YYYY.toString();
return X = YYYY_STR.substr(2,2) + numstr(MM) + numstr(DD);
}

function acbl_report_page(YYYY,MM)
//*****************************************************************************
//* Opens the page containing the masterpoint report for the month indicated 
//* by parameters YYYY and MM. If the user attempts to view the report before 
//* midnight on the last day of the month, he or she will be alerted.
//*
//* Example URL 
//*        http://www.tcdbc.com/Archives/08Mar/moRptMar2008.html
//*
//* Called directly from HTML code. 
//*****************************************************************************
{
var YY = YYYY.substr(2,2);
var MM_STR = numstr(MM);
var AVAIL_YYMMDD = YY + MM_STR + "28";
// loop until AVAIL_YYMMDD is set to 1st of following month
while (AVAIL_YYMMDD.substr(2,2) == MM_STR) // month unchanged
  AVAIL_YYMMDD = adjust_date(AVAIL_YYMMDD,"+",1);
if (future_date(AVAIL_YYMMDD) == 1)
  alert_user("Report will be available " 
               + AVAIL_YYMMDD.substr(2,2) 
               + "/" + AVAIL_YYMMDD.substr(4,2) 
               + "/20" + AVAIL_YYMMDD.substr(0,2) + ".")
else
  window.location = "http://www.tcdbc.com/Archives/" + YY + monthname3(MM) 
               + "/moRpt" + monthname3(MM) + YYYY + ".html";
return;
}

function archivepage()
//*****************************************************************************
//* Opens the archive index page for the current year. 
//*
//* Called directly from HTML code. 
//*****************************************************************************
{
var D = new Date();
YYYY = D.getFullYear();
window.location = "archive_index_" + YYYY.toString() + ".htm";
return;
}

function calendarindex()
//*****************************************************************************
//* Opens a calendar selection page based on the curent month. 
//*
//* Called directly from HTML code. 
//*****************************************************************************
{
var CURR_YYMMDD = getYYMMDD();
var CURR_MM = CURR_YYMMDD.substr(2,2);
window.location = "calendar/index_" + CURR_MM + ".htm";  
return;
}

function calendarpage(MM)
//*****************************************************************************
//* Opens the calendar page for the specified month. A value of zero in 
//* parameter MM specifies the current month. If the month specified is neither 
//* the current nor immediately prior month, the future is assumed and the 
//* year will be calculated accordingly.
//*
//* Called directly from HTML code. 
//*****************************************************************************
{
var CURR_YYMMDD = getYYMMDD();
var CURR_MM = Number(CURR_YYMMDD.substr(2,2));
var YY = Number(CURR_YYMMDD.substr(0,2));
if (MM == 0)
  MM = CURR_MM
else
  if (CURR_MM == 1 && MM == 12) // in january wanting december of prior year
   YY = YY - 1
  else
    if (MM < CURR_MM-1) // wanting earlier month of later year
      YY = YY + 1;
var YYYYMM = "20" + numstr(YY) + numstr(MM);
if (calendar_page_exists(YYYYMM) == 0)
  {
  var MSG = "Calendar for " + monthname(MM) + " " + YYYYMM.substr(0,4) + " not yet available. Please call the club (210 375-0500) for assistance.";
  alert_user(MSG);
  return;
  }
//URL = "http://www.tcdbc.com/calendar/" + YYYYMM + ".pdf";
URL = "http://www.spsimpson.com/tcdbc/calendar/" + YYYYMM + ".pdf";
//URL = "calendar/" + YYYYMM + ".pdf";
window.location = URL;  
return;
}

function summarypage(YYMMDD)
//*****************************************************************************
//* Returns the URL of the relevant tournament summary page based on the date
//* passed via parameter YYMMDD.
//*****************************************************************************
{
return X = "results_20" + YYMMDD.substr(0,2) + YYMMDD.substr(2,2) + ".htm";
}

function priortournament()
//*****************************************************************************
//* Searches backwards from the current date until a date is found when a 
//* tournament was scheduled, then takes the user to the relevant summary page.
//*
//* Called directly from HTML code. 
//*****************************************************************************
{
var YYMMDD = getYYMMDD();
var LOW_MM = YYMMDD.substr(2,2); // work around for apparent bug
// loop until a tournament date is found
while (tournament_scheduled(YYMMDD) == 0)
  {
  YYMMDD = adjust_date(YYMMDD,"-",1);
  if (YYMMDD.substr(2,2) < LOW_MM)
    LOW_MM = YYMMDD.substr(2,2);
  YYMMDD = YYMMDD.substr(0,2) + LOW_MM + YYMMDD.substr(4,2);
  }
window.location = summarypage(YYMMDD);
return;
}

function tournament_fwd(YYMMDD,FN,TYPE)
//*****************************************************************************
//* If the normal schedule for the date indicated by parameter YYMMDD was 
//* preempted by a tournament, alerts the user and opens the relevant 
//* tournament summary page. Otherwise, opens the indicated file. 
//*****************************************************************************
{
if (tournament_scheduled(YYMMDD) == 1)
  {
//  alert("A tournament was running. Forwarding to tournament results page.");
//  window.location = summarypage(YYMMDD);
  alert("A tournament was running ... forwarding to TournaVision website.");
window.location = "http://www.spsimpson.com/tournavision/results.php?id=99";
  return;
  }
window.location = "../bp/fileswitch.php?type=" + TYPE.substr(0,1) + "&fn=" + FN;
return;
}

function getYYMMDD()
//*****************************************************************************
//* Returns the current date in YYMMDD format.
//*****************************************************************************
{
var D = new Date();
var DD = D.getDate();
var MM = D.getMonth() + 1;
var YY = D.getFullYear();
if (YY >= 2000)
  YY = YY - 2000;
return X = numstr(YY) + numstr(MM) + numstr(DD);
}

function alert_user(MESSAGE)
//*****************************************************************************
//* Alerts the user, displaying the text of parameter MESSAGE. 
//*****************************************************************************
{
alert(MESSAGE);
}
  
function future_date_alert()
//*****************************************************************************
//* Alerts a future date using a standardized message. 
//*****************************************************************************
{
alert_user("Future date - nothing posted yet.");
}
  
function date_trap(YYMMDD,URL)
//*****************************************************************************
//* Checks the date specified in parameter YYMMDD and either forwards to the
//* URL specified in parameter URL or denies access and alerts the user that a 
//* future date was selected.
//* 
//* Called directly from HTML code. 
//*****************************************************************************
{
if (future_date(YYMMDD) == 1)
  future_date_alert()
else
  window.location = URL;
return;
}

function date_trap_with_msg(YYMMDD,URL,MESSAGE)
//*****************************************************************************
//* Checks the date specified in parameter YYMMDD and either forwards to the
//* URL specified in parameter URL or denies access and alerts the user with 
//* the message specified in parameter MESSAGE.
//* 
//* Called directly from HTML code. 
//*****************************************************************************
{
if (future_date(YYMMDD) == 1)
  alert_user(MESSAGE)
else
  window.location = URL;
return;
}

function future_date(YYMMDD)
//*****************************************************************************
//* Determines whether the date indicated by parameter YYMMDD is a future 
//* date and if so, returns a value of 1. Otherwise returns a value of 0.
//*****************************************************************************
{
var X = 0;
TODAY_YYMMDD = getYYMMDD();
if (YYMMDD > TODAY_YYMMDD)
  X = 1;
return X;
}

function archive_by_day(DAY,SESSION,TYPE)
//*****************************************************************************
//* Calculates a date in YYMMDD format (based on the current day and the value 
//* of parameter DAY) and calls function ARCHIVE_BY_DATE to display the data.
//*
//* On entry ...
//*     DAY contains a value from 0 (for Sunday) thru 6 (for Saturday). 
//*     SESSION contains either "D" (for day) or "E" (for evening). 
//*     TYPE contains either "R" (for results) or "H" (for hands).
//*
//* Called directly from HTML code. 
//*****************************************************************************
{
var YYMMDD = getYYMMDD();
var D = new Date();
var DAY_OF_WEEK = D.getDay();
var HOUR_OF_DAY = D.getHours();
var ADJ = DAY_OF_WEEK - DAY;
if (ADJ < 0)
  ADJ = ADJ + 7;
if (ADJ == 0 && HOUR_OF_DAY <= 11) 
  ADJ = 7;
if (ADJ > 0)
  YYMMDD = adjust_date(YYMMDD,"-",ADJ);
archive_by_date(YYMMDD,SESSION,TYPE);
return;
}

function archive_by_date(YYMMDD,SESSION,TYPE)
//*****************************************************************************
//* Opens a page showing the required data - results or hand records - for 
//* the specified date and session. 
//*
//* On entry ... 
//*   YYMMDD contains a valid date (e.g. "080401").
//*   SESSION contains either "D" (for day) or "E" (for evening) 
//*     or "DAY" or "EVE". 
//*   TYPE contains either "R" (for results) or "H" (for hands)
//*     or "RESULTS" or "HANDS".
//*
//* Example URLs ...
//*    http://www.tcdbc.com/Archives/08Apr/01AprLR.html
//*        (results for April 1, 2008 daytime session).
//*
//*    http://www.tcdbc.com/Archives/08Apr/E01AprLR.html
//*        (results for April 1, 2008 evening session).
//* 
//*    http://www.tcdbc.com/Archives/08Apr/01AprHR.html
//*        (hand records for April 1, 2008 daytime session).
//*
//*    http://www.tcdbc.com/Archives/08Apr/E01AprHR.html
//*        (hand records for April 1, 2008 evening session).
//*
//* Effective May 1, 2009 new naming standards for results files were 
//* implemented. Folders for each month were discontinued and filenames 
//* in the form yymmddx.TXT were introduced.
//*
//* Effective Jan 1, 2010 new naming standards for hand record files were 
//* implemented. Folders for each month were discontinued and filenames 
//* in the form Hyymmddx.HTML were introduced.
//*
//* Example URLs ...
//*    http://www.spsimpson.com/tcdbc/Archives/090501M.TXT
//*        (results for May 1, 2009 morning session).
//*
//*    http://www.spsimpson.com/tcdbc/Archives/090501A.TXT
//*        (results for May 1, 2009 afternoon session).
//*
//*    http://www.spsimpson.com/tcdbc/Archives/090501E.TXT
//*        (results for May 1, 2009 evening session).
//*
//*    http://www.spsimpson.com/tcdbc/Archives/H100101M.HTML
//*        (hand records for Jan 1, 2010 morning session).
//*
//* Called directly from HTML code. 
//*****************************************************************************
{
if (future_date(YYMMDD) == 1)
  {
  future_date_alert();
  return;
  }

if (SESSION == "A")
  SUFFIX = "A"
else
  if (SESSION.substr(0,1) == "E")
    SUFFIX = "E"
  else
    SUFFIX = "M";
if (TYPE.substr(0,1) == "R")
  FN = YYMMDD + "C188573" + SUFFIX + "1.TXT"
else
  FN = YYMMDD + "C188573" + SUFFIX + "1.HTML"
tournament_fwd(YYMMDD,FN,TYPE);
return;
}

