// JavaScript for Trips on the JHOC Website

//====================UPDATE NUMBER OF TRIPS HERE====================
var numTrips = 6;
//This is the total number of trips that will need to appear on the "Trip Sign Ups" page


var timeNow = new Date();

//Won't actually store the trip dates, but the date of the nearest Sunday
var tripDates = new Array(numTrips);
var tripNames = new Array(numTrips);
var signUpsBeginDaysB4Trip = new Array(numTrips);
var signUpsEndDaysB4Trip = new Array(numTrips);
var signUpsFull = new Array(numTrips);

//Initialize Arrays
for (i = 0; i < numTrips; i++)
	{
		tripDates[i] = new Date();
		
		//Sign ups begin and end at 11:59pm on the specified day
		tripDates[i].setHours(23,59,0,0); //hours, minutes, seconds, milliseconds
		signUpsBeginDaysB4Trip[i] = 15; //Two Saturdays before
		signUpsEndDaysB4Trip[i] = 7; //Sunday before
		signUpsFull[i] = new Boolean(false); //trips are not full by default
	}

//====================UPDATE TRIP SCHEDULE HERE====================

//Set date as nearest Sunday in the format: (year,month-1,day)

//To make a trip full, insert "signUpsFull[j] = true;" on the line BEFORE "tripDates[j++]..."

//To change the sign up period, insert either or both of the following on the line BEFORE "tripDates[j++]..."
//   "signUpsBeginDaysB4Trip[j] = xx;" where "xx" is the number of days before the trip that the sign ups should begin (default = 15)
//   "signUpsEndDaysB4Trip[j] = xx;" where "xx" is the number of days before the trip that the sign ups should end (default = 7)

var j = 0;
//Sep. 19 - Day Canoeing
tripNames[j] = "9/19 - Day Canoeing";
signUpsFull[j] = true;
tripDates[j++].setFullYear(2009,9-1,20);

//Sep. 26-27 - Weekend Biking
tripNames[j] = "9/26-9/27 - Weekend Biking";
tripDates[j++].setFullYear(2009,9-1,27);

//Oct. 3 - Rappelling
tripNames[j] = "10/3 - Rappelling";
tripDates[j++].setFullYear(2009,10-1,4);

//Oct. 10-11 - Weekend Backpacking
tripNames[j] = "10/17-10/18 - Weekend Backpacking";
tripDates[j++].setFullYear(2009,10-1,18);

//Oct. 24 - Day Canoeing
tripNames[j] = "10/24 - Day Canoeing";
tripDates[j++].setFullYear(2009,10-1,25);

//Nov. 1 - Day Biking
tripNames[j] = "11/1 - Day Biking";
tripDates[j++].setFullYear(2009,11-1,1);

/*
//Mar. 10-12 - White Water Rafting
tripNames[j] = "3/10-3/12 - White Water Rafting";
signUpsBeginDaysB4Trip[j] = 29; //Four Saturdays before
signUpsEndDaysB4Trip[j] = 11; //Two Wednesdays before
tripDates[j++].setFullYear(2007,3-1,11);
*/

//====================BELOW IS THE CODE====================

//Sign up status? -> -1 = past, 0 = current, 1 = future, 2 = full
function signUpStatus(tripNum)
{
//alert("trip " + tripNum);
	var status;
	var now = timeNow.getTime();
	var start = signUpsBeginDate(tripNum).getTime();
	var end = signUpsEndDate(tripNum).getTime();

		if (now > end)
			{status = -1;}
		else if (now < start)
			{status = 1;}
		else if (signUpsFull[tripNum].valueOf())
			{status = 2;}
		else
			{status = 0;}

	return status;
}

function signUpsBeginDate(tripNum)
{
	var beginDate = new Date();
	beginDate.setTime(tripDates[tripNum].getTime());
	
	beginDate.setDate(beginDate.getDate()-signUpsBeginDaysB4Trip[tripNum]);
	return beginDate;
}

function signUpsEndDate(tripNum)
{
	var endDate = new Date();
	endDate.setTime(tripDates[tripNum].getTime());
	
	endDate.setDate(endDate.getDate()-signUpsEndDaysB4Trip[tripNum]);
	return endDate;
}

function insertSignUpStatus(tripNum,id)
{
	var type = signUpStatus(tripNum);
	var text;
	var newText;
	var bodyRef = document.getElementById(id);
	var signUpLink = "trip_signups.html";

	if (type == 1)
		{
		var newLink = document.createElement("span");
		var month = signUpsBeginDate(tripNum).getMonth() + 1;
		var day = signUpsBeginDate(tripNum).getDate() + 1;
		
		text = "Opens " + month + "/" + day;
		
		newText = document.createTextNode(text);
		
    		newLink.appendChild(newText);
    		bodyRef.appendChild(newLink);
		}
	else if (type == 0)
		{
		var newLink = document.createElement("a");
		
		text = "Available";
		newText = document.createTextNode(text);
		
		newLink.href = signUpLink;
    		newLink.appendChild(newText);
    		bodyRef.appendChild(newLink);
		}
	else if (type == 2)
		{
		var newLink = document.createElement("span");
		
		text = "Full";
		newText = document.createTextNode(text);
		
    		newLink.appendChild(newText);
    		bodyRef.appendChild(newLink);
		}
	else if (type == -1)
		{
		var newLink = document.createElement("span");
		
		text = "Closed";
		newText = document.createTextNode(text);
		
    		newLink.appendChild(newText);
    		bodyRef.appendChild(newLink);
		}
}

function insertSignUpStatusAll()
{
	var id;
	
	for (i = 0; i < numTrips; i++)
	{
		id = "tripStatus" + i;
		insertSignUpStatus(i,id);
	}
}

function insertAvailableTrips()
{
	var numAvailableTrips = 0;
	var tripInput = document.getElementById("trip");

	for (i = 0; i < numTrips; i++)
	{
		if (signUpStatus(i) == 0)
		{
		var newOption = document.createElement("option");
	
		newText = document.createTextNode(tripNames[i]);
		newOption.appendChild(newText);
		tripInput.appendChild(newOption);
		
		numAvailableTrips++;
		}
	}
	
	if (numAvailableTrips == 0)
		tripInput.options[tripInput.selectedIndex].text="None available";
}