//*********************Globals for calendar****************************

//a date object that is created with arguments passed to loadCalendar
var dateInfo;
var linkInfo;

//lookup arrays for formatting
var day_of_week = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); 
var month_of_year = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

//current date info for initial date (i.e. today)
var currentDate = new Date();
var currentMonth = currentDate.getMonth();

//set flag to false intially 
//used to increase font size on old browsers
var old_IE = false;
var isIE = false;

//used for formatting chice when conference is scheduled today
var confToday = false;

//If it is Opera adjust the year returned
if ((navigator.userAgent.indexOf("Opera")!=-1) || (navigator.userAgent.indexOf("Opera")!=-1))
	currentYear = currentDate.getYear() + 1900;
//if its IE then the year is correct already otherwise adjust
else if ((navigator.appName.indexOf("Microsoft") != -1)){
	//flag it if it is ie 
	var currentYear = currentDate.getYear();
	isIE = true;
	//if it is a pre 5.0 i.e. browser flag it
	if ((navigator.appVersion.indexOf("MSIE 4.0") != -1) || (navigator.appVersion.indexOf("MSIE 4.5") != -1))
		old_IE = true;
}
else
	var currentYear = currentDate.getYear() + 1900;

//************************End Globals******************************************

//**********************code for pop-ups***************************************

function Start(page) {
	OpenWin = this.open(page, "CtrlWindow", "toolbar=no,location=no,status=no,menubar=no,directories=no,scrollbars=yes,resizable=yes,width=400,height=400");
}

function Start2(page) {
	OpenWin = this.open(page, "Exhibittest", "toolbar=no,location=no,status=no,menubar=no,directories=no,scrollbars=no,resizable=no,width=818,height=488");
}

function Close() {
	onClick=window.close
}

function playVideo(name, start, end){
	var page = "popup.php?loc=" + name + "&start=" + start + "&end=" + end;
	OpenWin = this.open(page, "Exhibittest", "toolbar=no,location=no,status=no,menubar=no,directories=no,scrollbars=no,resizable=no,width=818,height=488");
}
//***********************end pop-up code**********************************************

//***********************Calendar functions*******************************************

function isConferenceDay(day) {
	//returns true if there is a conference scheduled on the
	//same day as the day argument, false otherwise.
	for (j = 0; j < conferences.length; j++){
		if ((conferences[j].date[0] == dateInfo.month) && (conferences[j].date[2] == dateInfo.year) && (conferences[j].date[1] + (dateInfo.startPos -1) == day)){
			linkInfo = conferences[j].link;
			return true;
		}
	}
	return false;
}

function build_dropdown(){
	//builds the dropdown menu for the calendar
	//looks at conference object array and adds
	//an entry for each unique month
	
	var menu = '<select name="dropDown" onChange="loadCalendar(this.value.substr(0,this.value.indexOf(' + "'|'" + ')), this.value.substr(this.value.indexOf(' + "'|'" + ') + 1));">';
	
	//create 2005 menu
	menu += '<option class="year" value="' + dateInfo.month + '|' + dateInfo.year + '">2005</option>';
	if ((dateInfo.year == 2005) && (dateInfo.month == 10))
		menu += '<option value="10|2005" selected>November</option>';
	else
		menu += '<option value="10|2005">November</option>';
	if ((dateInfo.year == 2005) && (dateInfo.month == 11))
		menu += '<option value="11|2005" selected >December</option>';
	else
		menu += '<option value="11|2005">December</option>';
	
	//start 2006 menu
	menu += '<option value="' + dateInfo.month + '|' + dateInfo.year + '"></option>';
	menu += '<option class="year" value="' + dateInfo.month + '|' + dateInfo.year + '">2006</option>';
	
	//add entires for 2005
	for (var i = 0; i < 9; i++){
		if ((i == dateInfo.month) && (dateInfo.year == currentYear))
			menu += '<option value="' + i + '|2006" selected>' + month_of_year[i] + '</option>';
		else
			menu += '<option value="' + i + '|2006">' + month_of_year[i] + '</option>';
	}
	return menu + '</select>';
	
}

function build_nav_group(){
	//builds the next "conference on..menu below calendar.
	//looks for next two scheduled dates. appends this below calendar table
	
	var nav_group = "";
	for (i = 0; i < conferences.length; i++){
		//find first date greater than today that has a month >= to this month and a year >=
		if((currentYear <= parseInt(conferences[i].date[2])) && (parseInt(conferences[i].date[0]) >= currentMonth) && (dateInfo.today <= parseInt(conferences[i].date[1]))){
			if (confToday == true)
				nav_group += '<p class="next_conference">Today&#39s Conference:</p>';
			else
				nav_group += '<p class="next_conference">Next Conference:</p>';
			nav_group += '<p><a href="' + conferences[i].link + '">' + conferences[i].title + '</a></p>';
			nav_group += '<p class="calendar_date">' + month_of_year[conferences[i].date[0]] + ' ' + conferences[i].date[1] + '</p>';
			break;
		}
		else if ((parseInt(conferences[i].date[0]) > currentMonth) && (currentYear <= parseInt(conferences[i].date[2]))){
			if (confToday == true)
				nav_group += '<p class="next_conference">Today&#39s Conference:</p>';
			else
				nav_group += '<p class="next_conference">Next Conference:</p>';
			nav_group += '<p><a href="' + conferences[i].link + '">' + conferences[i].title + '</a></p>';
			nav_group += '<p class="calendar_date">' + month_of_year[conferences[i].date[0]] + ' ' + conferences[i].date[1] + '</p>';
			break;
		}
	}
	
	return nav_group;

}

function getDaysInMonth(month,year) {
	//Returns integer for the # days in month
	//based on month and year arguments passed in
	
	var days;
	if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)  
		days=31;
	else if (month==4 || month==6 || month==9 || month==11)
		days=30;
	else if (month==2){
		if (isLeapYear(year)) 
			days=29;
		else
			days=28;
	}
	return (days);
	
}
function isLeapYear (Year) {
	//returns true if the year argument is a leap
	//year false otherwise
	
	if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) 
		return true;
	else 
		return (false); 
}

function loadCalendar(intMonth,intYear){
//creates a javascript calendar as a html string
//and writes it to the innerHTML property of the 
//"wrapper" div element on the index.php page

//clean them up if coming from selection element
intMonth = parseInt(intMonth);
intYear = parseInt(intYear);

//set up default font sizro for calendar
var f_size = "75%";

//create date object to use throught script month, year, startPos, days, today, and weekday properties
dateInfo = {month : intMonth, year : intYear, startPos : new Date (intYear, intMonth, 1).getDay(), 
			days : getDaysInMonth(intMonth + 1,intYear), today : currentDate.getDate(), weekday : currentDate.getDay()};
//add a couple days based on first day of the month
dateInfo.days += dateInfo.startPos;

//override font size based on whether
//it is IE old_IE or other
if (old_IE && isIE)
	f_size = "80%";
else if (isIE)
	f_size = "60%";

//start calendar
var calendar ='<div id="calendar_border" style="font-size:' + f_size + '"><table id="calendar_text" cellspacing="0" cellpadding="0">';

//create dropdown menu navigation box and display year
calendar +=  '<tr><td colspan="4" bgcolor="#e12008">' + build_dropdown() + '</td><td colspan="3" bgcolor="#e12008" valign="middle" align="left"><font weight="bold" color="white" size="2">&nbsp;' + dateInfo.year + '</font></td></tr>';

//Label each day of week
calendar += '<tr height="20">';
			for(i=0; i < 7; i++){
				// bold today
				if((dateInfo.weekday == i) && (dateInfo.year == currentYear) && (dateInfo.month == currentMonth))
					calendar += '<td width="25"><div id="today_day"><center><b>' + day_of_week[i] +'</b></center></div></td>';
				else
					calendar += '<td width="25"><center>' + day_of_week[i] + '</center></td>';
			}
calendar += '</tr>';

// fill in blanks on first row
	//first row all blank?
	if (dateInfo.startPos != 0){
		calendar += '<tr height="20">'; 
		for(i = 0; i < dateInfo.startPos; i++) {
			calendar += '<td width="25">&nbsp;</td>';
		}
	}
//create rest of first row and remaining rows
for (i = dateInfo.startPos; i < dateInfo.days; i++) {
	//is it the last day of the week?
	if ( i%7 == 0 ) {
		//adjust if first row all blanks
		if ((i == dateInfo.startPos) && (dateInfo.startPos == 0))
			calendar += '<tr height="20">';
		else
			calendar += '</tr><tr height="20">';
	}
	//highlight todays date
	if ((dateInfo.today + dateInfo.startPos - 1 == i) && (dateInfo.year == currentYear) && (dateInfo.month == currentMonth)){
		//if it is a conference day override the day formatting for conference formatting otherwise format the day
		if (isConferenceDay(i)){
			//update the linkInfo data
			isConferenceDay(i);
			confToday = true;
			calendar += '<td width="25"<div class="conference_highlight"><center><a href="' +  linkInfo +'" class="date_link">' + (i-dateInfo.startPos+1) + '<a></div></center></td>'; 
		}
		else{
			calendar += '<td width="25"><b><div id="today_num"><center>' + (i - dateInfo.startPos + 1  ) + '</center></div></b></td>'; 
		}
	}
	else{
		//format the day appropriately if there is a conference scheduled
		if (isConferenceDay(i)){
			calendar += '<td width="25"<div class="conference_highlight"><center><a href="' +  linkInfo +'" class="date_link">' + (i-dateInfo.startPos+1) + '<a></div></center></td>'; 
		}
		else{
			calendar += '<td width="25"><center>' + (i - dateInfo.startPos + 1) + '</center></td>'; 
		}
	}
}

//close off the calendar's table and append a nav group below it
calendar += '</tr></table></div>' + build_nav_group();

//find placeholder div "wrapper" checking for all the old browsers out there
if (document.all)
	var wrap = document.all["wrapper"];
else if (document.layers)
	var wrap = document.layers['wrapper'];
else
	var wrap = document.getElementById("wrapper");
wrap.innerHTML = calendar;

}

//************************End Calendar functions**************************************

function verify(frm) {
	
	conTitle = frm.myTitle.value;
	conLink = frm.link.value;
	
	if ((conTitle == " ") || (conTitle == "") || (conLink == " ") || (conLink == "")){
		alert("Please enter both a title and link name");
		return false;
	}
	else
		return true;
}