//Copyright (c) 2000 Microsoft Corporation.  All rights reserved.
//<script>
var g_WinDocAll			=	window.document.all;
var g_szCurrentApptURL	=	null;

function window.onload()
{
	var m_oCalViewer = idCalendarViewer.addBehavior(g_szVirtualRoot+"/controls/CalendarView.htc");
	var m_oCalendar = idCalendarControl.addBehavior(g_szVirtualRoot+"/controls/ExchangeCalendar.htc");
	ToggleViewButtons(0);
	window.document.body.onkeydown = onKeydownBody;
}

function onKeydownBody()
{
	if (46 == event.keyCode) //Delete Key
	{
		if(null != g_szCurrentApptURL && 0 != g_szCurrentApptURL.length)
		{
			event.cancelBubble=true;
			DeleteAppointment();
		}
	}
}
//fires when a new date is selected in the calendar
function idCalendarControl_OnDateChange()
{
	idCalendarViewer.update(event.date,event.xml,event.view);
	g_szCurrentApptURL=null;
	ToggleViewButtons(event.view)
}

/***Begin Calendar View Event Handlers*********/
function idCalendarViewer_OnSelectAppointment()
{
	g_szCurrentApptURL = event.URL;
}

GetISODateObj.re=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):\d{2}\.\d{3}Z?$/;
function GetISODateObj(szISODate)
{
	var arr = GetISODateObj.re.exec( szISODate );
	if (null == arr)
	{
		return(null);
	}
	var objD = new Date( Number(arr[1]) , Number(arr[2])-1 , Number(arr[3]) );	
	objD.setUTCHours( Number(arr[4]) , Number(arr[5]),0 );
	return(objD);
}

var m_objXMLhttp_search;
event_onRSC_searchMasterStart.szURL;
event_onRSC_searchMasterStart.szType;
function event_onRSC_searchMasterStart()
{
	if(4 == m_objXMLhttp_search.readyState)
	{
		if(207!== m_objXMLhttp_search.status)	
		{
			return;
		}
		var dtstart =  m_objXMLhttp_search.responseXML.selectSingleNode("a:multistatus/a:response/a:propstat/a:prop/dtstart").text;	
		var oDate = GetISODateObj(dtstart);			
		window.open(event_onRSC_searchMasterStart.szURL + "?Cmd=open&Type=" + event_onRSC_searchMasterStart.szType + "&tzoffset=" + oDate.getTimezoneOffset(),String(Math.round(Math.random() * 100000)),"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=680,height=500");
	}
}

function idCalendarViewer_OnDoubleClickAppointment()
{
	if (event.command == 'open')
	{
		var szURL = event.URL;
		var rgParams = new Array();
		rgParams["CancelOpen"]=false;
		rgParams["InstanceType"] ="";
		if("2" == event.instancetype || ("3" == event.instancetype && "" != event.pr_cdormid))
		{
			var szPath	=	g_szUserBase+"?Cmd=dialog&template=dlg_openappt";
			var hWin	=	window.showModalDialog(szPath, rgParams,"dialogWidth:300px;dialogHeight:180px;status:no;resizable:yes;help:no;center:yes;")
			if(false != rgParams["CancelOpen"])
			{
				return(false);
			}
		}		
		var szType = rgParams["InstanceType"];
		var oDate = event.startDate;
		if("master" == szType.toLowerCase())
		{
		//need to get dtstart of the master appointment in order to send across the correct timezoneoffset
			var szQuery='<a:searchrequest xmlns:a="DAV:" > ' +
			'<a:sql>Select ' +
			 '"urn:schemas:calendar:dtstart" AS dtstart ' +
			'FROM Scope(\'SHALLOW TRAVERSAL OF ""\') ' +
			'WHERE "urn:schemas:calendar:instancetype" = 1 ' +
			'AND "urn:schemas:calendar:uid" = \'' + event.uid + '\' ' +
			'ORDER BY "urn:schemas:calendar:dtstart" ASC ' +
			'</></>';
			m_objXMLhttp_search = new ActiveXObject("Microsoft.XMLHTTP");
			m_objXMLhttp_search.open( "SEARCH", g_szUserBase + g_szFolder,true);
			m_objXMLhttp_search.setRequestHeader( "Content-type:", "text/xml");
			m_objXMLhttp_search.onreadystatechange = event_onRSC_searchMasterStart;
			event_onRSC_searchMasterStart.szURL = szURL;
			event_onRSC_searchMasterStart.szType = szType;			
			m_objXMLhttp_search.send(szQuery);
		}
		else
		{
			window.open(szURL + "?Cmd=open&Type=" + szType + "&tzoffset=" + oDate.getTimezoneOffset(),String(Math.round(Math.random() * 100000)),"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=680,height=500");
		}
	}
	else if (event.command == 'newallday')
	{
		openNewWindow(g_szUserBase + '/' + g_szFolder + "/?Cmd=new&allday=1&mm="+idCalendarControl.month+"&dd="+idCalendarControl.day+"&yy="+idCalendarControl.fullyear,"",680, 500);	
	}
}

/***End Calendar View Event Handlers*********/
function ToggleViewButtons(iViewMode)
{
	var objButton;
	switch(iViewMode)
	{
		default:
		case 0:
			objButton	=	AccessFld(g_WinDocAll["DAYVIEW"]);
		break;
		case 1:
			objButton	=	AccessFld(g_WinDocAll["WEEKVIEW"]);
		break;
		case 2:
			objButton = AccessFld(g_WinDocAll["MONTHVIEW"]);
		break;
	}
	if(null != objButton)
	{
		onCbStickyClickEvent(objButton, true);
	}
}

function DeleteAppointment()
{

	if(null == g_szCurrentApptURL || 0 == g_szCurrentApptURL.length)
	{
		return(false);
	}
	else
	{
		DeleteMessage(g_szCurrentApptURL, g_szUserBase+"/", DeleteMessage.iACT_NO_CLOSE);
	}
}

function ShowHideFolders()
{
	if(window.parent && window.parent.navbar)
	{
		var rgFrmColl	=	window.parent.document.all.tags("FRAMESET");
		if(rgFrmColl.length == 0)
		{
			return;
		}
		var objFrame = rgFrmColl[0];
		if(null != objFrame)
		{
			if("0,*" == objFrame.cols)
			{
				objFrame.cols = "150,*";
			}
			else
			{
				objFrame.cols = "0,*";
			}
		}
	}
}

function onCbClick(szCommand, fState)
{
	if(event && event.button == 2)
	{
		return(false);
	}

	try
	{
		switch(szCommand.toUpperCase())
		{
			case "NEWMESSAGE":
				openNewWindow(g_szUserBase + g_szDraftsURL + "/?Cmd=new","",680, 500);
			break;
			case "NEWCONTACT":
				openNewWindow(g_szUserBase + g_szContactsURL + "/?Cmd=new","",680, 500);
			break;
			case "NEWITEM":
			case "NEWAPPOINTMENT":
				//$$NOTE:use current date for new appt when from monthly view
				if(2 == idCalendarControl.viewMode)
				{
					var oDate = new Date();
					openNewWindow(g_szUserBase + '/' + g_szFolder + "/?Cmd=new&mm="+oDate.getMonth()+"&dd="+oDate.getDate()+"&yy="+oDate.getFullYear(),"",680, 500);
					return;	
				}
				openNewWindow(g_szUserBase + '/' + g_szFolder + "/?Cmd=new&mm="+idCalendarControl.month+"&dd="+idCalendarControl.day+"&yy="+idCalendarControl.fullyear,"",680, 500);
			break;
			case "MOVECOPY":
			break;
			case "DELETE":
				DeleteAppointment();
			break;
			case "SHOWFOLDERS":
				ShowHideFolders();
			break;
			case "SHOWCALENDAR":
				ShowCalendar(toolBar_ShowHideCalendar);
			break;	
			case "DAYVIEW":
				idCalendarControl.setView(0)
			break;
			case "WEEKVIEW":
				idCalendarControl.setView(1)
			break;
			case "MONTHVIEW":
				idCalendarControl.setView(2)
			break;	
			case "CHECKMESSAGES":
				idCalendarControl.refresh();
			break;
			case "ADDRESSBOOK":
				openGal();
			break;
			case "HELP":
				LaunchHelp();
			break;
		}
	}
	catch(e)
	{
		alert("Calendar.js >> Toolbar command == " + szCommand + "\n" + "Error : " + e.description);
	}
}
//NOTE: do not rename RefreshMessageList()
//Generic callback function used in the composenotes by MSGCTRL.HTC to
//tell the current viewer to refresh on delete/move/save etc
function RefreshMessageList()
{
	if (null != idCalendarControl)
	{
		idCalendarControl.refresh();
	}
}

