// JavaScript Document

var browser = navigator.userAgent.toLowerCase();
var browserIE = ( browser.indexOf("msie") != -1 );
var today = new Date();
var thisYear = today.getFullYear();
var FestivalDate = "July 15, 2012";

/*
 * --------------------------------------------------------------------------------
 */

function f_element( p_index, p_deliminator, p_string ) {
  /* 
   * JavaScript version of OpenVMS DCL F$ELEMENT lexical
   */
  var currIndex = p_string.indexOf(p_deliminator);
  var i = 0;
  var outChar = '';
  var outPoint = 0;
  var outString = p_string;
  var trackIndex = 0;

  if ( currIndex >= 0 ) {
      while ( ( trackIndex != p_index ) && ( currIndex != -1 ) ) {
        outPoint = ++currIndex;
        currIndex = p_string.indexOf(p_deliminator,outPoint);
        trackIndex++;
      } // end while ( ( trackIndex != p_index ) && ( currIndex != -1 ) )
      if ( trackIndex != p_index ) {
        outString = p_deliminator;
      } else {
        outString = '';
        for ( i=outPoint; i<p_string.length; i++ ) { 
          outChar = p_string.charAt(i);
          if ( outChar == p_deliminator ) {
            break;
          } else {
            outString = ( outString + outChar );
          } // end if ( outChar == p_deliminator )
        } // for ( i=outPoint; i<p_string.length; i++ )
      } // end if ( trackIndex != p_index )
    } else if ( p_index >= 1 )
      outString = p_deliminator;
    // end if ( currIndex >= 0 )
  
  return outString;
  } // end f_element function
  
/*
 * --------------------------------------------------------------------------------
 */

function replace(p_string,p_text,p_by) {
// Replaces p_text with p_by in p_string
// From:  http://tech.irt.org/articles/js037/
    var strLength = p_string.length, txtLength = p_text.length;
    if ((strLength == 0) || (txtLength == 0)) return p_string;

    var i = p_string.indexOf(p_text);
    if ((!i) && (p_text != p_string.substring(0,txtLength))) return p_string;
    if (i == -1) return p_string;

    var newstr = p_string.substring(0,i) + p_by;

    if (i+txtLength < strLength)
        newstr += replace(p_string.substring(i+txtLength,strLength),p_text,p_by);

    return newstr;
}  // end replace function

/*
 * --------------------------------------------------------------------------------
 * addLoadEvent will execute a JavaScript command/function when a page is
 * brought up in a browser.  Ordinarily only one such command/function can be
 * performed at that time, but addLoadEvent overrides that limitation.
 * --------------------------------------------------------------------------------
 */

function addLoadEvent(func) {
// Multiple onload function created by: Simon Willison
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

/*
 * --------------------------------------------------------------------------------
 * The following functions control the left column menu display
 * --------------------------------------------------------------------------------
 */

/*
 * --------------------------------------------------------------------------------
 */

function yyyymmddFormat(p_date) {
  /* 
   * ++
   * Name: yyyymmddFormat
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 30-Mar-2009
   * Abstract: To return the specified date in YYYYMMDD format.
   * Description: 
   * Parameters: None
   * History:
   * --
   */
  
  function takeYear(theDate) {
    // Source: http://www.quirksmode.org/js/introdate.html
    var x = theDate.getYear();
    var y = x % 100;
    y += (y < 38) ? 2000 : 1900;
    return y;
  }

  // [ yyyymmddFormat Constants ]
  var specifiedDate = ( p_date == undefined )
        ? new Date()
        : new Date(p_date);

  // [ yyyymmddFormat Variables ]
  var dd = ( specifiedDate.getDate() );
  var mm = ( specifiedDate.getMonth() + 1 );
  var yyyy = ( takeYear(specifiedDate) + '' );
  var yyyymmdd = '';

  // [ yyyymmddFormat Main Line ]
  if ( mm <= 9 )
    mm = ( '0' +  mm.toString() );

  if ( dd <= 9 )
    dd = ( '0' +  dd.toString() );

  yyyymmdd = ( yyyy + mm + dd );

  return yyyymmdd;
  } // end yyyymmddFormat function

/*
 * --------------------------------------------------------------------------------
 */
 
function hideId( p_id, p_date ) {
 /* 
 * ++
 * Name: hideId
 * Author: Alvin Orzechowski, MyFirstWebPage.net
 * Creation Date: 21-Oct-2010
 * Abstract: To hide a specified id after a specified date.
 * Description: 
 * Parameters: p_id - the id that will be hidden if today's date falls after 
 *                    p_date.
 *             p_date - the date in yyyymmdd format that is the last day p_id is
 *                      to appear.
 * Requirement: yyyymmddFormat function
 * History:
 * - Created
 * --
 */
 // [ hideId Main Line ]
 if ( yyyymmddFormat() > p_date ) 
    document.getElementById(p_id).style.display = 'none';

 return;
 } // end hideId function

/*
 * --------------------------------------------------------------------------------
 */
 
function getDaysUntil(p_targetDate) {
 /* 
 * ++
 * Name: getDaysUntil
 * Author: Alvin Orzechowski, MyFirstWebPage.net
 * Creation Date: 30-Mar-2009
 * Abstract: To display the number of days until the Woodstock Folk Festival 
 *           date.
 * Description: This function returns the number of days from today until a 
 *              specified end date.  If the target date is past, the function 
 *              returns a -1.
 * Parameters: p_targetDate - End date to be used (Required)
 * History:
 * - Created
 * --
 */
 // [ getDaysUntil Constants ]
 var dateTargetDate = new Date(p_targetDate);
 var dateToday = ( browser.indexOf("msie") != -1 )
       ? new Date( f_element(1," ",Date())+' '+f_element(2," ",Date())+', '+f_element(4," ",Date()) )
       : new Date( f_element(1," ",Date())+' '+f_element(2," ",Date())+', '+f_element(3," ",Date()) );
 var one_day = ( 1000 * 60 * 60 * 24 );
 var yyyymmddTargetDate = yyyymmddFormat(p_targetDate);
 var yyyymmddToday = yyyymmddFormat();

 // [ getDaysUntil Variables ]
 var daysUntilTargetDate = ( yyyymmddToday <= yyyymmddTargetDate )
       ? Math.round( ( dateTargetDate.getTime() - dateToday.getTime() ) / one_day )
       : -1;

 // [ getDaysUntil Main Line ]

  return daysUntilTargetDate;
 } // end getDaysUntil function

function displayDaysUntilFestival(p_festivalDate) {
 /* 
 * ++
 * Name: displayDaysUntilFestival
 * Author: Alvin Orzechowski, MyFirstWebPage.net
 * Creation Date: 31-Mar-2009
 * Abstract: To display the number of day until the Festival in a box.
 * Description: 
 * Parameters: p_festivalDate - The date of the Festival (required)
 * History:
 * - Created
 * --
 */
 // [ displayDaysUntilFestival Constants ]
 var daysUntilFestival = getDaysUntil(p_festivalDate);

 // [ displayDaysUntilFestival Variables ]
 var displayNotice = "";

 // [ displayDaysUntilFestival Main Line ]
 if ( daysUntilFestival >= 0 ) {
	 if ( daysUntilFestival == 0 ) {
		 displayNotice = "<strong>The Festival is<br />\nToday!</strong>";
	 } else if ( daysUntilFestival == 1 ) {
		 displayNotice = "The Festival is<br />\ntomorrow.";
	 }else if ( daysUntilFestival <= 6 ) {
		 displayNotice = ( "The Festival is in<br />\n" + daysUntilFestival + " days." );
	 } else if ( daysUntilFestival == 7 ) {
		 displayNotice = "The Festival is<br />\none week from today.";
	 } else displayNotice = ( "It is " + daysUntilFestival + " days until <br />\n" + p_festivalDate );
 } else displayNotice = "&nbsp;See you&nbsp;<br />\n&nbsp;next year.&nbsp;"
 if ( displayNotice != "" )
   displayNotice = ( '<table width="75%" style="margin-left: 0.5em; margin-bottom: 1em;"><tbody><tr><td align="center"><table style="border: 1px solid #999999; background-color: #ffffff;"><tbody><tr><td align="center" style="font-size: 8pt !important;">' + displayNotice + '</td></tr></tbody></table></td></tr></tbody></table>' );
   document.write("<br />\n"+displayNotice);

 return;
 } // end displayDaysUntilFestival function


function displayOption( pPageId, pOption ) {
  tdClass = "";
  pageName = f_element( 0, '.', pPageId );
  pageType = '.'+f_element( 1, '.', pPageId );
  if ( pageType == '..' )
     pageType = '.html';
  if ( pPageId == 'index' )
    pageFile = homePage 
  else
    pageFile = ( pageName + pageType );
  /* pageFile = '#' */
  if ( PgID == pageName ) {
    aValue = 'class="youAreHere" title="You are here"';
	  tdClass = ' class="youAreHere"';
	}
  else {
    aValue = ( 'href="' + pageFile + '" title="Go to the ' + pOption + ' page"' );
    if ( pOption == 'Press Release' )
      aValue = ( 'href="' + pPageId + '" title="Read the Press Release"' )
  }
  document.write('<tr><td' + tdClass + '><a ' + aValue + '>' + pOption + '</a></td></tr>\n');
  return;
}

function displayMenuBreak() {
   document.write('<tr>\n');
   document.write('<td colspan="2" class="menuBreak">&nbsp;</td>\n');
   document.write('</tr>\n');
   return;
}

function WFMlink() {
    document.write('<style type="text/css">\n');
    document.write('p.WFMlink { text-align: center; font-size: 8pt !important;}\n'); 
    document.write('.WFMlink img { border: 0 solid #ff0000; margin: 0; padding: 0; }\n');
    document.write('.WFMlink a { font-size: 8pt !important;}\n');
    document.write('.WFMlink a:hover { }\n');
    document.write('</style>\n');
    document.write('<p class="WFMlink">Member of the<br \>\n');
    document.write('<a href="http://www.woodstockfolkmusic.com/"><img src="images/wfmTriplets w24.gif"><br />WoodstockFolkMusic.com Sites</a><br />\n');
    document.write('</p>\n');
    return;
    }

function displayMenu() {
  displayDaysUntilFestival(FestivalDate);
  document.write('<table id="menu" cellpadding="0" cellspacing="1">\n');
  displayOption( 'index', 'Home' );
  // displayOption( '2011schedule', 'Schedule' );
  // displayOption( '2011mainstage', 'Main Stage' );
  // displayOption( '2011workshops', "Workshops" );
  // displayOption( '2011openmike', "Open Mike" );
  // displayOption( '2011kidsarea', "Kids Area" );
  // displayOption( '2011map', "Map" );
  displayOption( 'directions', "Directions" );
  displayOption( 'lodging', "Lodging" );
  // displayOption( 'businesses', "Businesses" ); <-- last used in 2007
  displayOption( '2011radio-partners', "Radio Partners" );
  displayOption( '2011sponsors', "Sponsors" );
  displayMenuBreak();

  displayOption( 'wffconcerts', "WFF Concerts" );
  /*
  displayOption( 'wffconcerts-20090315', "WFF Concerts" );
  */
  displayMenuBreak();
  displayOption( 'aboutus', "About Us" );
  displayOption( 'history', "History" );
  displayOption( 'volunteers', "Volunteers" );
  displayOption( 'donations', "Donations" );
  displayOption( 'booking', "Booking" );
  displayOption( 'sendmsgform.php?id=5', "Contact Us" );
  document.write ('</table>\n');
  WFMlink();
  return;
}

function displayConcertsMenu(pPressRelease) {
  displayDaysUntilFestival(FestivalDate);
  document.write('<table id="menu" cellpadding="0" cellspacing="1">\n');
  displayOption( 'wffconcerts', "WFF Concerts" );
  if ( pPressRelease != null )
      displayOption( pPressRelease, 'Press Release' );
  displayOption( 'index', 'Festival Site' );
  displayOption( 'aboutus', "About Us" );
  displayOption( 'sendmsgform.php?id=5', "Contact Us" );
  document.write ('</table>\n');
  WFMlink();
  return;
}

/*
 * --------------------------------------------------------------------------------
 */
 
function displayFestivalDate() {
  /*
  document.write('<h3>Sunday,<br>\n');
  document.write(FestivalDate+'<br>\n');
  document.write('Woodstock Square<br>\n');
  document.write('12:30&nbsp;p.m. to 6&nbsp;p.m. </h3>\n');
  document.write('<hr>\n');
  */
  return;
}

/*
 * --------------------------------------------------------------------------------
 */
 
function getPassedValue(p_id) {
  /* 
   * +++
   *
   * Function Name: getPassedValue
   *        Author: Alvin Orzechowski MyFirstWebPage.net
   * Creation Date: 15-Aug-2004
   *      Abstract: To return the passed value or the specified portion of it
   *   Description: 
   *    Parameters: p_id - Value found to the left of an equal sign.  If this is
   *                    found, this function will return the value found to the
   *                    right of the equal sign.  If this parameter is not 
   *                    provided, this function will return the whole value 
   *                    passed.
   *       History: 
   * 
   * ---
   */

  // [ getPassedValue Constants ]
  var passedValue = unescape(location.search.substring(1));

  // [ getPassedValue Variables ]
  var allSubValues = location.search.substring(1,location.search.length).split('&');
  var i;
  var idValue = passedValue;
  var subValue;

  // [ getPassedValue Main Line ]
  if ( p_id != '' )
    idValue = '';
  for (i=0; i<allSubValues.length; i++) {
    subValue = allSubValues[i].split('=');
    if ( subValue[0] == p_id ) {
      idValue = unescape(subValue[1]);
      i = allSubValues.length;
    } // end if ( subValue[0] == p_id )  
  } // end for (i=0; i<allSubValues.length; i++)

  // [ getPassedValue End of Job ]
  return idValue;

} // end getPassedValue function

/*
 * --------------------------------------------------------------------------------
 */

function ckboxDisplay ( p_ckbox, p_topic ) {
	checked = ( getPassedValue('ckbox', 'parent') == p_ckbox )
	          ? ' checked="yes"'
	          : "";
	document.write('<INPUT type="checkbox" name="' + p_ckbox + '" value="Yes"' + checked + '> ' + p_topic);
} // end function ckboxDisplay

/*
 * --------------------------------------------------------------------------------
 */

function subjectDisplay() {
	subjectFound = getPassedValue('subject', 'parent');
	inputValue = ( subjectFound != "" )
	               ? ( ' value="' + subjectFound + '" ' )
		       : "";
	document.write('<INPUT type="text" name="subject" size="75"' + inputValue + '>');
} // end function ckboxDisplay

/*
 * --------------------------------------------------------------------------------
 */
 
function preloadAllImages() {
  a = document.images.length;
  document.write( a + '<br>' );
  /*
  for (x=0; x<a; x+) {
     document.write( document.images[x].src + '<br>' );
  }
  */
  return;
} // end preloadAllImages function

function pageLink(p_anchor,p_link) {
  // [ pageLink Constants ]
  var defaultLink = 'ReedyForCongress.com';

  // [ pageLink Variables ]
  var linkHere = p_link;

  // [ pageLink Main Line ]
  if ( !linkHere || ( linkHere = '' ) )
    linkHere = defaultLink;
  document.write('<a href="' + p_anchor + '">' + linkHere + '</a>');
  return;
  }


/*
 * --------------------------------------------------------------------------------
 * The following function calls the mail form
 * --------------------------------------------------------------------------------
 */

function msgForm(p_to,p_subject,p_toName) {
  /* 
   * +++
   *
   * Function Name: msgForm
   *        Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 16-Aug-2004
   *      Abstract: To bring up the msgform.html file in its own window
   *   Description: 
   *    Parameters: p_to - Value of username getting the e-mail address
   *                p_subject - Value of Subject to be put in the form
   *       History: 
   * 
   * ---
   */

  // [ msgForm Constants ]

  var day = new Date();

  // [ msgForm Variables ]

  var id = day.getTime();
  var params = '';
  var URL = 'msgform.html';

  // [ msgForm Main Line ]

  if ( p_to != '' )
    params = ( '?to=' + p_to );
  if ( p_subject != '' ) {
    if ( params == '' )
      params = '?'
    else
      params = ( params + '&' );
    params = ( params + 'subject=' + p_subject );
  }
  if ( p_toName )
    if ( p_toName != '' )
	  params = ( params + '&toName=' + p_toName );
  URL = ( URL + params );
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=685,height=490');");

  // [ msgForm End of Job ]
  return;

} // end msgForm function

/*
 * --------------------------------------------------------------------------------
 */

function econtact( pWhere, pWho ) {
    function changeStr(pStr) {
        var changedStr = "";
        
        for ( i in pStr )
          changedStr += ( '&#'  + pStr.charCodeAt(i++).toString(10) + ';' );
        return changedStr;
    }
    return ( changeStr(pWho) + '&#64;' + changeStr(pWhere) );
    }


/*
 * --------------------------------------------------------------------------------
 * MyFirstWebPage.net specific functions
 * Leave this section at the end
 * --------------------------------------------------------------------------------
 */

function get_mfwpnLogo(pTM) {
  var mfwpnLogo = '<span class="MFWPNlogo">' +
  '<span style="color: #000000;">My</span>' +
  '<span style="color: #ff0000;">First</span>' +
  '<span style="color: #00ff00;">Web</span>' +
  '<span style="color: #0000ff;">Page</span>' +
  '<span style="color: #000000;">.net';

  if ( pTM == 'tm' )
    mfwpnLogo = ( mfwpnLogo + '&#153;' );
  mfwpnLogo = ( mfwpnLogo + '</span></span>' );

return mfwpnLogo;
} // end get_mfwpnLogo function
