//****************************************************************
//****************************************************************
//	COPYRIGHT 2000, Vertex Software
//****************************************************************
//****************************************************************

//----------------------------------------------------------------
//----------------------------------------------------------------
// <FONT COLOR=red>Requires client to have Microsoft Outlook 2000 and IE installed.<BR></FONT>
// See <A HREF='/global/scripts/SpellChecker/Example.asp'>EXAMPLE</A>.
//----------------------------------------------------------------
//----------------------------------------------------------------

var olAppointmentItem  = 1;
var olFolderContacts = 10;
var olFolderCalendar = 9;
var olSave = 0;

//----------------------------------------------------------------
// GetOutlookApplication
//----------------------------------------------------------------
var OlDaysOfWeek = {
	olSunday:1,
	olMonday:2,
	olTuesday:4,
	olWednesday:8,
	olThursday:16,
	olFriday:32,
	olSaturday:64
	}


//----------------------------------------------------------------
// GetOutlookApplication
//----------------------------------------------------------------
var OlRecurrenceType = {
	olRecursDaily:0,
	olRecursWeekly:1,
	olRecursMonthly:2,
	olRecursMonthNth:3,
	olRecursYearly:5,
	olRecursYearNth:6
	}


//----------------------------------------------------------------
// GetOutlookApplication
//----------------------------------------------------------------
function GetOutlookApplication() {
	var application = null;
	try {
		application = new ActiveXObject("Outlook.Application");
		return application;
		}
	catch (error) {
		alert( "There must be a copy of Microsoft Outlook 2000 installed on this computer to use the Outlook 2000 functions and you must add this site to your 'Trusted Sites' list under security in Internet Options." );
		return null;;
		}
	}


//---------------------------------------------------------------
//	AddAppointment
//---------------------------------------------------------------
function AddAppointment( eventSubject, eventText, startDate, endDate, eventCategory, eventLocation, allDay ) {
	try {
		var outlookApplication = GetOutlookApplication();
		if (!outlookApplication) return;
		var appointment = outlookApplication.createItem( olAppointmentItem );
		with (appointment) {
			Start = new Date(startDate).getVarDate();
			End = new Date(endDate).getVarDate();
			Subject = "" + unescape(eventSubject);
			if (eventText) Body = "" + unescape(eventText);
			if (eventCategory) Category = "" + unescape(eventCategory);
			if (eventLocation) Location = "" + unescape( eventLocation );
			if (allDay) AllDayEvent = true; 
			}
		appointment.Close(olSave);
		//outlookApplication.Quit();
		outlookApplication = null;
		}
	catch (error) {
		alert( error.description );
		if (outlookApplication) outlookApplication.Quit(false);
		return;
		}
	}


//===========================================================
// GetSelectedAppointments
//===========================================================
function GetSelectedAppointments( includeRecurrences ) {
	var appointments = "";
	try {
		var properties = {};
		properties.windowTitle = "Select Appointments"
		var result = ShowDialog( "custom", "/global/scripts/Outlook2000/AppointmentFilterForm.asp", properties );
		if (result[ "OK" ] == "SELECTED") {
			appointments = GetAppointments( result.filter || "", includeRecurrences );
			}
		}
	catch( error ) {
		alert( "SelectAppointmentRange: " + error.description );
		}
	return appointments;
	}



//===========================================================
// GetAppointments
//===========================================================
function GetAppointments( filter, includeRecurrences ) {
	var appointments = new Array();
	try {
		var outlookApplication = GetOutlookApplication();
		if (!outlookApplication) return;
		var nameSpace = outlookApplication.GetNameSpace("MAPI");
		var calendar = nameSpace.GetDefaultFolder(olFolderCalendar);
		var calendarItems = calendar.Items;
		if (filter)  {
			calendarItems.Sort( "[Start]" );
			calendarItems.IncludeRecurrences = includeRecurrences;
			calendarItems = calendarItems.Restrict( filter );
			}
		var appointment = calendarItems.GetFirst();
		var appointmentProperties = "EntryID,Subject,AllDayEvent,Body,Categories,Duration,IsRecurring,Start,End";
		var contactProperties = "Email1Address,Email2Address";
		var recurrenceProperties = "DayOfMonth,DayOfWeekMask,Duration,Interval,MonthOfYear,PatternEndDate,PatternStartDate,RecurrenceType,EndTime,NoEndDate";
		while (appointment) {
			var appointmentObject = ExtractObjectAttributes(appointment, appointmentProperties);
			appointmentObject.Start = new Date( String(appointmentObject.Start) );
			appointmentObject.End = new Date( String(appointmentObject.End) );
			appointmentObject.contact = new Array();
			if (appointment.IsRecurring) {
				var pattern = appointment.GetRecurrencePattern();
				if( pattern ) {
					pattern = ExtractObjectAttributes(pattern, recurrenceProperties);
					pattern.EndTime = new Date( String(pattern.EndTime) );
					pattern.PatternEndDate = new Date( String(pattern.PatternEndDate) );
					pattern.PatternStartDate = new Date( String(pattern.PatternStartDate) );
					appointmentObject.recurrencePattern = pattern;
					}
				}
			for (var item=1; item<=appointment.Links.Count; item++) {
				var link = appointment.Links.Item(item);
				var contactObject = {};
				try {
					var contact = link.Item;
					var contactObject = ExtractObjectAttributes(contact, contactProperties);
					}
				catch (error) {
					}
				contactObject.Name = link.Name;
				appointmentObject.contact.push( contactObject );
				}
			appointments.push(appointmentObject);
			appointment = calendarItems.GetNext();
			}
		// Dont' do this! Closes user's original instance if it was open.
		//if (outlookApplication) outlookApplication.Quit();
		}
	catch( error ) {
		alert( "GetAppointments: " + error.description );
		}
	return appointments;
	}


//===========================================================
// SendMail
//===========================================================
function SendMail( recipients, subject, message, asHTML ) {
	try {
		var olMailItem = 0;
		var olFormatHTML = 2;
		var olFormatPlain = 1;
		var outlookApplication = GetOutlookApplication();
		if (!outlookApplication) return;
		var mailItem = outlookApplication.CreateItem( olMailItem );
		if (asHTML) {
			mailItem.BodyFormat = olFormatHTML;
			mailItem.Body = "";
			mailItem.HTMLBody = message;
			}
		else {
			mailItem.Body = message;
			mailItem.BodyFormat = olFormatPlain;
			}
		//mailItem.To = recipients;
		mailItem.Recipients.Add(recipients)
		mailItem.Subject = subject;
		mailItem.Send();
		outlookApplication = null;
		}
	catch( error ) {
		alert( "SendMail: " + error.description );
		}
	}