/*******************************************************************************
 * Purpose: This file includes all the Javascript functions for the SMAS website
 * Author:  Ray Schmidt
 * Date:    March 12, 2007
*******************************************************************************/

var targetId = "";
var xmlHTTP = false;
var currentDiv = "start_div";

/*******************************************************************************
 * Function:  openPic
 * Purpose:   Opens a picture from an image link in a new window
 * Params:    picURL - the path to the image
 *            picDesc - the description to be displayed with the image
*******************************************************************************/
function openPic ( picURL, picDesc )
{
  var objWin = window.open ( picURL, "Window",
      "toolbar=0, menubar=0, scrollbars=yes, height=800, width=840" );
  objWin.document.write ( "<html>\n" );
  objWin.document.write ( "<head>\n" );
  objWin.document.write ( "  <title>Gallery</title>\n" );
  objWin.document.write ( "  <link href='popStyles.css' rel='stylesheet' " +
      "type='text/css' />\n" );
  objWin.document.write ( "</head>\n" );
  objWin.document.write ( "<body>\n" );
  objWin.document.write ( "<div class='gallery_pics'>\n" );
  objWin.document.write ( "  <img src='" + picURL + "' alt='" +
      "' width='800' height='600' />\n" );
  objWin.document.write ( "</div><br />\n" );
  objWin.document.write ( "  <p>" + picDesc + "</p>\n" );
  objWin.document.write ( "  <p>\n" );
  objWin.document.write ( "    <a href='Javascript: self.close()'>" +
      "Close</a>\n" );
  objWin.document.write ( "  </p>\n" );
  objWin.document.write ( "</body>\n" );
  objWin.document.write ( "</html>\n" );
  objWin.document.close ();
}

/*******************************************************************************
 * Function:  initDiv
 * Purpose:   Initializes the dynamic_content_pane div by hiding all the
 *              divs within
*******************************************************************************/
function initDiv ()
{
	var divs = document.getElementById
      ( "dynamic_content_pane" ).getElementsByTagName ( "div" );

	for ( i = 0; i < divs.length; i++ )
	{
	  divs[i].style.visibility = "hidden";
	}
	location.href="#top";
}

/*******************************************************************************
 * Function:  pageSwitch
 * Purpose:   Changes which divs are shown and hidden
 *            Called by clicking on the specific category links on the links
 *              page
 * Params:    id - the name of the div that we want to show
*******************************************************************************/
function pageSwitch ( id )
{
	if ( !document.getElementById ) return false;
	var div = document.getElementById ( id );
	var curDiv = document.getElementById ( currentDiv );
	curDiv.style.position = "absolute";
	curDiv.style.visibility = "hidden";
	hideDivs ( currentDiv );
	div.style.position = "static";
	div.style.visibility = "visible";
	currentDiv = id;
	showDivs ( currentDiv );
}

/*******************************************************************************
 * Function:  showDivs
 * Purpose:   Unhides a specified div by setting the visibility style to visible
 * Params:    parentDiv - the div to unhide
*******************************************************************************/
function showDivs ( parentDiv )
{
	var divs = document.getElementById ( parentDiv ).getElementsByTagName ( "div" );

	for ( i = 0; i < divs.length; i++ )
	{
		divs[i].style.visibility = "visible";
	}
}

/*******************************************************************************
 * Function:  hideDivs
 * Purpose:   Hides a specified div by setting the visibility style to hidden
 * Params:    parentDiv - the div to hide
*******************************************************************************/
function hideDivs ( parentDiv )
{
	var divs = document.getElementById ( parentDiv ).getElementsByTagName ( "div" );

	for ( i = 0; i < divs.length; i++ )
	{
		divs[i].style.visibility = "hidden";
	}
}

/*******************************************************************************
 * Function:	getNextMeetingDate
 * Purpose:		Keeps the next meeting date current
 ******************************************************************************/
 function getNextMeetingDate()
 {
		var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

		var currentDate = new Date();
 		var meetingMonth = currentDate.getMonth();
 		var meetingYear = currentDate.getFullYear();
		var nextMeetingDate = getFirstWednesday ( meetingMonth, meetingYear );

		if ( currentDate.getDate() > nextMeetingDate.getDate() )
		{
			if ( meetingMonth == 11 )
			{
				meetingYear++;
				meetingMonth = 0;
			}
			else
			{
				meetingMonth++;
			}
			nextMeetingDate = getFirstWednesday ( meetingMonth, meetingYear );
		}

		if ( ( nextMeetingDate.getMonth() == 3 ) || ( nextMeetingDate.getMonth() == 9 ) )
		{
			document.write ( months [ nextMeetingDate.getMonth() ] + " " + nextMeetingDate.getDate() );
			document.write ( "<br />" );
			document.write ( "Annual Swap & Sale" );
		}
		else
		{
			document.write ( months [ nextMeetingDate.getMonth() ] + " " + nextMeetingDate.getDate() );
		}
 }
 /*******************************************************************************
 * Function:	getFirstWednesday
 * Purpose:		Calculates the first Wednesday for a given month and year
 * Params:		month - the month used to calculate the meeting date
 *						year - the year used to calculate the meeting date
 ******************************************************************************/
 function getFirstWednesday ( month, year )
 {
		var meetingDate = new Date();
 		var meetingDateIsSet = false;
		var i = 1;

		while ( meetingDateIsSet == false )
		{
			meetingDate.setDate ( i );
			meetingDate.setMonth ( month );
			meetingDate.setFullYear ( year );

			if ( meetingDate.getDay() == 3 )
			{
				meetingDateIsSet = true;
			}
			else
			{
				i++;
			}
		}
		return meetingDate;
 }
