///////////////////////////////////////////////////////////////////////////////
//
//	File:			incTimeZone.js		
//	Author(s):		ks
//  Created:		03/16/2004	
//	Description:	This file provides functions that complement incTimeZone.asp
//
//	Dependencies:	
//	Functions:		
//	Notes:
//	TODO:		
//	History:
//
///////////////////////////////////////////////////////////////////////////////


/*--------------------------------------------------------------------
function changeDays()
Usage:	Updates the Year, Month, and Day select boxes when any of these boxes are changed
		
Input:  frmYear - a list box that lists the days of a year
		frmMonth - the month list box
		frmDay - the day list box

--------------------------------------------------------------------*/
function changeDays(frmYear, frmMonth, frmDay){
	
	//var frm = document.frmLMEdit;
	//var frmYear = frm.selYear;
	//var frmMonth = frm.selMonth;
	//var frmDay = frm.selDay;
	
	var i;
	var l;
	var m;
	var y;
	var s;

	l = frmDay.length;
	
	if(frmMonth.selectedIndex == -1)
		frmMonth.selectedIndex = 0;
	if(frmYear.selectedIndex == -1)
		frmYear.selectedIndex = 0;

	m = frmMonth.options[frmMonth.selectedIndex].value;
	y = frmYear.options[frmYear.selectedIndex].value;

	if(m == "2"){		
		if(y/4 == parseInt(y/4)){			
			if(l == 28)
				addDay(frmDay, 28);
			else{
				s = frmDay.selectedIndex + 1;
				for(i=l-1;i>28;i--)
					frmDay.options[i] = null;
				if(s > 28)
					frmDay.options[0].selected = true;	
			}	
		}
		else{
			if(l > 28){
				s = frmDay.selectedIndex + 1;
				for(i=l-1;i>27;i--)
					frmDay.options[i] = null;			
				if(s > 28)
					frmDay.options[0].selected = true;	
			}		
		}
	}
	else if(m=="4"|| m=="6"|| m=="9"|| m=="11"){
		if(l < 30){
			for(i=l;i<30;i++)
				addDay(frmDay, i);											
		}
		else if(l > 30){
			s = frmDay.selectedIndex + 1;
			frmDay.options[30] = null;
			if(s > 30)
				frmDay.options[0].selected = true;	
		}		
	}
	else if(m=="1"|| m=="3"|| m=="5"|| m=="7"|| m=="8"|| m=="10"|| m=="12"){
		if(l < 31){
			for(i=l;i<31;i++)
				addDay(frmDay, i);
		}
	}
}

/*--------------------------------------------------------------------
function addDay()
Usage:	Adds an option to a select list if the days should be increased (leap year)
		
Input:  frmDay - a list box that lists the days of a month
		i -	the number of the day to add

--------------------------------------------------------------------*/
function addDay(frmDay, i){
	var opt;
	
	if (navigator.appName == "Netscape"){
		frmDay.options[i] = new Option(i+1);
	}			
	else{
		opt = document.createElement("option");			
		opt.value = i+1;
		opt.text = i+1;
		frmDay.options.add(opt);
	}				
}

/*--------------------------------------------------------------------
function SetTimeAndZone()
Usage:	Adjusts the time, date, and timezone fields, assumes there are starting and ending time fields
		
Input:  selTimeStartHour - the hour portion of the start time
		selTimeEndHour - the hour portion of the end time
		selYear			- the year select box
		selMonth		- the month select box
		selDay			- the day select box
		selTimeZone		- the select box that holds the time zone
		
Notes:	This function was designed with the live meetings component in mind.
		Pass null in place of any of these select boxes to skip

--------------------------------------------------------------------*/
function SetTimeAndZone(selTimeStartHour, selTimeEndHour, selYear, selMonth, selDay, selTimeZone)
{
	var d = new Date();
	
	var tz = (d.getTimezoneOffset() - 420) / 60 + ""; // get the time zone relative to GMT, then offset to PST
					
	var strTZID = "1"; // default to PST
	var i;
				
	// set the id for known time zones
	if (tz=="-4.5") strTZID = "2";
	if (tz=="-1") strTZID = "3";
	if (tz=="2") strTZID = "4";
	if (tz=="-2") strTZID = "5";
	if (tz=="-4") strTZID = "6";
	if (tz=="1") strTZID = "7";
	if (tz=="-3") strTZID = "8";
	
	// get all the relevant dates and times
	var dHour = d.getHours() + "";
	var dDate = d.getDate() + "";
	var dMonth = d.getMonth() + 1 + "";
	var dYear = d.getFullYear() + "";
		
	// pad the hours
	if (dHour.length==1) dHour = "0" + dHour;
			
	// set the dates and times relative to the user's computer
	if (selTimeStartHour!=null) SelectOption(selTimeStartHour, dHour);
	
	if (selTimeStartHour.selectedIndex < selTimeStartHour.length) 
	{
		selTimeEndHour.selectedIndex = selTimeStartHour.selectedIndex + 1; // increment the end hour by one
	}
	
	if (selYear!=null) SelectOption(selYear, dYear);
	if (selMonth!=null) SelectOption(selMonth, dMonth);
	if (selDay!=null) SelectOption(selDay, dDate);
	if (selTimeZone!=null) SelectOption(selTimeZone, strTZID);
		
	// update the days for the selected month
	changeDays(selYear, selMonth, selDay)
}

// loops through a select box and sets the option to be selected if found
function SelectOption(obj, selValue)
{
	
	var j;
	
	for (j=0; j<obj.length; j++)
	{
		if (obj.options[j].value==selValue) obj.options[j].selected=true;
	}
}
