<!-- start cal.js -->

var	zeros = "00000000000000000000"

function ZeroPadNum(n, length)
{
	var		s = "";

	s = "" + n;
	return (zeros.substring(0, length - s.length) + s);
}


function StdStringToDate(ds)
{
	var		s = "";
	
	// convert to MM/DD/YYYY HH:MM:SS (e.g. 06/19/1997 10:16:41)
	s = ds.substring(4,6) + "/" + ds.substring(6,8) + "/" + ds.substring(0,4) + " " + ds.substring(8,10) + ":" + ds.substring(10,12) + ":" + ds.substring(12,14);
		
	return new Date(s);
}


function DateToDateString(date)
{
	var		s;

	s = date.toString();

    year = date.getYear() < 1900 ? date.getYear() + 1900 : date.getYear();

	// Standard date format is YYYYMMDD	
	return  ZeroPadNum(year, 4) +
            ZeroPadNum(date.getMonth() + 1, 2) + 
            ZeroPadNum(date.getDate(), 2);
}


function DateToDateTimeString(date)
{
    if (isNaN(date.getYear()))
        return "";

    year = date.getYear() < 1900 ? date.getYear() + 1900 : date.getYear();

	// Standard date format is YYYYMMDDHHMMSS	
	return  ZeroPadNum(year, 4) +
            ZeroPadNum(date.getMonth() + 1, 2) + 
            ZeroPadNum(date.getDate(), 2) + 
            ZeroPadNum(date.getHours(), 2) + 
            ZeroPadNum(date.getMinutes(), 2) + 
            ZeroPadNum(date.getSeconds(), 2);
}


function GetSelectedDate(querydate)
{
    var     curdate = new Date();
    var     ds = DateToDateString(curdate);
    
    month = document.nvoForm.selmonth.selectedIndex + 1;
    if (month <= 12)
    {    
        day = document.nvoForm.selday.selectedIndex + 1;
        if (day > 31) day = 1;
    
        ds = ZeroPadNum(document.nvoForm.selyear.selectedIndex + 1997, 4) + ZeroPadNum(month, 2) + ZeroPadNum(day, 2);
        
        odate = StdStringToDate(ds);
        newds = DateToDateString(odate);
        if (newds != ds)
        {
            alert("The date selected is invalid.  The closest valid date will be used instead.");
            ds = newds;
        }

    }
    else
    {
        // if a invalid date is selected then use the date on the query string if provided
        if (querydate != "")
        {
            ds = querydate;
        }
    }

    return ds;   
}


function GotoYear(querydate)
{
	location.href = "year.nhtml?date=" + GetSelectedDate(querydate);
}


function GotoMonth(querydate)
{
	location.href = "index.nhtml?date=" + GetSelectedDate(querydate);
}


function GotoWeek(querydate)
{
	location.href = "week.nhtml?date=" + GetSelectedDate(querydate);
}


function GotoDay(querydate)
{
	location.href = "day.nhtml?date=" + GetSelectedDate(querydate);
}


function GotoDate(uri)
{
    // if this is an event then go to day view
    if (uri.indexOf("event.nhtml") >= 0)
    {
        uri = "day.nhtml";
    }
    
    location.href = uri + "?date=" + GetSelectedDate('');
}


function DoViewToday(uri)
{
    // if this is an event then go to day view
    if (uri.indexOf("event.nhtml") >= 0)
    {
        uri = "day.nhtml";
    }
    
    date = new Date;
    location.href = uri + "?date=" + DateToDateString(date);
}


function DoNewEvent(querydate)
{
    location.href = "eventedit.nhtml?date=" + GetSelectedDate(querydate);
}


function DoSearch(querydate)
{
    location.href = "search.nhtml?date=" + GetSelectedDate(querydate);
}


function SetSelectedDay(datestr)
{
    date = StdStringToDate(datestr);
        
    year = date.getYear() < 1900 ? date.getYear() + 1900 : date.getYear();
    
    document.nvoForm.selday.selectedIndex = date.getDate() - 1;
    document.nvoForm.selyear.selectedIndex = year - 1997;
    document.nvoForm.selmonth.selectedIndex = date.getMonth();
}




