// class to add an event to MS Outlook calendar
//=============================================================================================================\
// see i:/web/olj.asp for more info...
 // CHM help file for Outlook - f:\msoffice\office\1033\Vbaoutl9.chm
 // http://support.microsoft.com/default.aspx?scid=KB;en-us;q208520
 //
 //    var yyy  = new addToOutlook;                            //create new class
 //    yyy.createObject();
 //    yyy.Subject                    = eSubject.value;
 //    yyy.Start                      = 'mm/dd/yy hh:mm';
 //    yyy.Location                   = eLocation.value;
 //    yyy.End                        = 'mm/dd/yy hh:mm';
 //    yyy.ReminderSet                = true;
 //    yyy.ReminderOverrideDefault    = true;
 //    yyy.ReminderMinutesBeforeStart = 90;
 //    yyy.AllDayEvent                = true;
 //    yyy.Body                       = eBody.value;
 //    yyy.Categories                 = 'VYSL';
 //    yyy.ae();                                               //AddEvent - class methods require ()
 //
//=============================================================================================================/
     function addToOutlookCalendar()
     {
      /* properties...*/
      this.e         = '';
      this.item      = '';
      this.olns      = '';
      this.defFolder = '';
      this.myEvent   = '';

     this.EventId       = '';
     this.Subject       = '';
     this.Start         = 'mm/dd/yyyy';
     this.Location      = '';
     this.PDANotes      = '';
     this.End           = 'mm/dd/yyyy';
     this.AllDayEvent   = true;
     this.Body          = '';
     this.Categories    = '';
      /* methods...apparently do not need () in prototype*/
      this.ae = addEvent;
     }

     function addEvent()
     {
      var key;
      var e;
      try{
          this.item = new ActiveXObject("Outlook.Application");
      }
      catch(e){
              alert(
              'cannot open Outlook Application...' + 
              '\n...maybe its not installed on your machine?\n' +
              'or\n' +
              'your browser security settings are set to disable ActiveX Control Scripts not marked as safe\n' +
              'under Tools; Internet Options; try changing it to "Prompt" under the Security TAB' +
              '\n\nerr: ' + e.number + '-' + e.description
              );
              return;
      }
      this.olns          = this.item.Application.GetNameSpace('MAPI');
      var e;
      try{
          this.defFolder = this.olns.GetDefaultFolder(9); //get Calendar
      }
      catch(e){
              alert(
              'unable to find Calendar folder in Outlook: ' + e.number + '-' + e.description
              );
              return;
      }
      if(this.defFolder != 'Calendar')
      {
         alert("unable to find calendar folder...found " + this.defFolder + ' instead');
         return;
      }

      // try and find it...if found, then delete it...before adding a new one
      key = this.defFolder.Items.Find("[Categories] = '" + this.EventId + "'");
      if(key == null)
      {
      }
      else
      {
        key.Delete()
        alert('existing event deleted first...before adding it in again...');
      }
      //you can do this too...without having to find the Calendar folder...
      //this.item = Item.CreateItem(1);  // 1==olAppointmentItem
      this.myEvent             = this.defFolder.Items.Add;
      this.myEvent.Subject     = this.Subject;
      this.myEvent.Start       = this.Start;
      this.myEvent.Location    = this.Location;
      this.myEvent.End         = this.End;
      this.myEvent.AllDayEvent = this.AllDayEvent;
      this.myEvent.Body        = this.Body + "\n\n" + this.PDANotes;
      this.myEvent.Categories  = this.EventId + ", " + this.Categories;
      try{
          this.myEvent.Save();
      }
      catch(e){
              alert(
              'unable to save new event in Calendar in Outlook: ' + e.number + '-' + e.description
              );
              return;
      }
      alert("Outlook has been updated with new Event");
      //this.e = new Error;
      //alert('any errors:' + this.e.number + '-' + this.e.description);
     }

     function addToOutlookAddressBook()
     {
      /* properties...*/
      this.e         = '';
      this.item      = '';
      this.olns      = '';
      this.defFolder = '';
      this.myContact = '';
      this.myNote    = '';

     this.ContactId           = '';
     this.FirstName           = '';
     this.LastName            = '';
     this.HomeTelephoneNumber = '';
     this.Email1Address       = '';
     this.JobTitle            = '';
     this.Notes               = '';
     this.Categories          = '';
      /* methods...apparently do not need () in prototype*/
      this.ac = addContact;
     }

     function addContact()
     {
      var key;
      var e;
      try{
          this.item = new ActiveXObject("Outlook.Application");
      }
      catch(e){
              alert(
              'cannot open Outlook Application...' + 
              '\n...maybe its not installed on your machine?\n' +
              'or\n' +
              'your browser security settings are set to disable ActiveX Control Scripts not marked as safe\n' +
              'under Tools; Interent Options; try changing it to "Prompt" under the Security TAB' +
              '\n\nerr: ' + e.number + '-' + e.description
              );
              return;
      }
      this.olns          = this.item.Application.GetNameSpace('MAPI');
      var e;
      try{
          this.defFolder = this.olns.GetDefaultFolder(10); // 10==olFolderContacts
      }
      catch(e){
              alert(
              'unable to find Contacts folder in Outlook: ' + e.number + '-' + e.description
              );
              return;
      }
      if(this.defFolder != 'Contacts')
      {
         alert("unable to find calendar folder...found " + this.defFolder + ' instead');
         return;
      }

      // try and find it...if found, then delete it...before adding a new one
      var fn;
      fn = this.FirstName + " " + this.LastName
      key = this.defFolder.Items.Find("[Categories] = '" + this.ContactId + "'");
      if(key == null)
      {
      }
      else
      {
        key.Delete()
        alert('existing contact deleted first...before adding it in again...');
      }
      //you can do this too...without having to find the Calendar folder...
      //myItem = Item.CreateItem(2); //2==olContactItem
      this.myContact                     = this.defFolder.Items.Add;
      this.myContact.FirstName           = this.FirstName;
      this.myContact.LastName            = this.LastName;
      this.myContact.FullName            = fn;
      this.myContact.HomeTelephoneNumber = this.HomeTelephoneNumber;
      this.myContact.Email1Address       = this.Email1Address;
      this.myContact.JobTitle            = this.JobTitle;
      this.myContact.User1               = this.Notes;
      this.myContact.Categories          = this.ContactId + ", " + this.Categories;
      try{
          this.myContact.Save();
      }
      catch(e){
              alert(
              'unable to save new contact in Address Book in Outlook: ' + e.number + '-' + e.description
              );
              return;
      }
      alert("Outlook has been updated with new Contact");
      //this.e = new Error;
      //alert('any errors:' + this.e.number + '-' + this.e.description);
     }

// class to retrieve XML structure from server in non-asynchronous mode
//=============================================================================================================\
 //
 //    modified (greatly): 2005/10/28
 //
 //    var xmlisland;
 //    var yyy  = new ajax;                                    create new class
 //    yyy.async = true;                                       optional...if used though, you may want to use CallBack else you will not know when its finished
                                                               // unless you are not interested in any return
 //    ajax.prototype.acb = aftercallback;                     prototype a new method for my own object, which will get called from the callback function
 //    yyy.url  = host.value;                                  assign URL
 //    yyy.sxml = ixml.value;                                  assign xml to send
 //    if(yyy.launch() == true)                                initiate XML retrieval passing a parameter of where to put the xml structure
 //    {
 //    }                                                       do whatever with xmlisland.........
 //    else
 //    {
 //       badxml.innerText = yyy.rawText;                      if mal-formed xml, then raw data is in rawText
 //       badxml.style.display = "";
 //    }
 //                 T e s t e d  w i t h  AJAXtester.asp
//=============================================================================================================/
     function ajax()
     {
        /* properties... */
           this.url      = "";    // target URL...should be same domain or we may see "access denied"
           this.rv       = "";    // Return Value to signify if request was successful
           this.sxml     = "";    // XML to send to server
           this.rxml     = "";    // XML returned from server
           this.rawtext  = "";    // raw text received in .ResponseText
           this.oX       = "";    // object that handles server request
           this.success  = "";    // flag which signifies if ResponseXML is indeed valid XML
           this.async    = false; // flag to request sync (false) or async (true) request
           this.callback = "";    // function to execute after return from server
           this.callbackdone = false;
           //this.acb = null;
        /* methods... */
           this.launch = getxml;
           this.cb = returnFromServer;
     }

           function returnFromServer()
     {
                       switch (this.oX.readyState)
                       {
                              /*
                              //Value (4 byte integer) Definition from MSDN 2000/11/01
                              //0 (uninitialized) - The object has been created but has not been initialized (open has not been called).
                              //1 (loading)       - The object has been created but the send method has not been called.
                              //2 (loaded)        - The send method has been called and the status and headers are available, but the response is not yet available.
                              //3 (interactive)   - Some data has been received. You can call responseBody and responseText to get the current partial results.
                              //4 (completed)     - All the data has been received, and the complete data is available in responseBody and responseText.

                              Access data returned from the server via the responseText or responseXML properties.
                              The former provides only a string representation of the data.
                              More powerful, however, is the XML document object in the responseXML property.
                              This object is a full-fledged document node object (a DOM nodeType of 9),
                              which can be examined and parsed using W3C Document Object Model (DOM) node tree methods and properties.
                              Note, however, that this is an XML, rather than HTML, document,
                              meaning that you cannot count on the DOM's HTML module methods and properties.
                              This is not really a restriction because the Core DOM module gives you ample ways of finding element nodes,
                              element attribute values, and text nodes nested inside elements.

                              */
                       case 4:

                              // prime raw text return value
                              this.rawtext = this.oX.responseText;
                              this.rv = true;

                              // prime XML return object
                              this.success = false;
                              if (window.XMLHttpRequest)
                              {
                                 if (this.oX.responseXML != null)
                                 {
                                    this.rxml = this.oX.responseXML;
                                    this.success = true;
                                 }
                              }
                              else
                              {
                                 if (this.oX.responseXML.documentElement != null)
                                 {
                                    this.rxml = this.oX.responseXML;
                                    this.success = true;
                                 }
                              }
                              this.callbackdone = true;
                              if (this.acb != undefined && this.acb != null)
                              {
                                 this.acb(); //call the AfterCallBack function now that the data is received
                              }
                              break;
                       default:
                              //exit do //only execute this if true is used - asynchronous
                       } // switch (this.oX.readyState)
     }


           function getxml()
     {
                  var oCB;
                  var action;
                  oCB = this;                 //apparently required to assign correct reference to callback function
                  //alert("url=" + this.url);
                  this.oX = assignXMLHTTP(); // declared in client.js
                  action = "POST";
                  if (this.sxml == "" || this.sxml == null)
                  {
                     action = "GET";
                  }
                  this.oX.open (action, this.url , this.async);
                  this.oX.setRequestHeader ("content-type", "text/xml");
                  if (this.async == true)
                  {
                     this.oX.onreadystatechange = function() { oCB.cb(); };
                  }
                  this.oX.send (this.sxml);
                  // gone off to the server....

                  if (this.async == true)
                  {
                     this.rv   = true;
                  }
                  else
                  {
                     this.rv   = false;
                     do  // this will loop for all 4 status types
                     {
                       oCB.cb();
                       if (this.rv == true)
                       {
                          break;
                       }
                     } // do
                     while(true);
                  } // if (this.async == true)

                  //alert(this.oX.responseXML.documentElement.nodeName);    // works IF content is XML
                                                                            // html in IE
                                                                            // html in Firefox
                  //alert(this.oX.responseXML.documentElement.childNodes.length);    // works IF content is XML
                                                                                     // 2 in IE
                                                                                     // 5 in Firefox
                  //alert(this.oX.responseXML); // works IF content is XML
                                                // [object] in IE
                                                // [object XMLDocument] in Firefox
                  //alert(this.oX.responseXML);                 // if no XML...null in FF; [object] in MSIE
                  //alert(this.oX.responseXML.documentElement); // if no XML..."no properties" error in FF; null in MSIE

                  /*
                  all above tested with following ASP page with content-type == xml
                  <html>
                    <head>
                    </head>
                    <body>
                    Test ASP page
                    </body>
                  </html>
                  */
                  return (this.rv);
     }

//====================================================================================================
//====================================================================================================
//====================================================================================================






//====================================================================================================
// class to retrieve XML structure from server in asynchronous mode
//=============================================================================================================\
 //var jxmlisland;
 //function jsaclass()
 //{
 //    jyyy = new xxxa;
 //    xxxa.prototype.acb = aftercallback;        //prototype a new method for my own object, which will get called from the callback function
 //    jyyy.url  = host.value;                                  //assign URL
 //    jyyy.sxml = ixml.value;                                  //assign xml to send
 //    jyyy.gx()                                  //initiate XML retrieval passing a parameter of where to put the xml structure
 //}
 //function aftercallback()
 //{
 //    while (false == false)
 //    {
 //      if(jyyy.callbackdone == true)
 //      {
 //         //do whatever...
 //         refreshPage();
 //         break;
 //      }
 //      else
 //      {
 //         badxml.innerText = jyyy.rawtext;
 //      }
 //    }
 //}
//=============================================================================================================/
     function xxxa()
     {
           /* properties... */
           this.url      = "";
           this.rv       = "";
           this.sxml     = "";
           this.rxml     = "";
           this.rawtext  = "";
           this.oTbl     = "";
           this.success  = "";
           this.async    = true;
           this.callback = "";
           this.callbackdone = false;
           /* methods... */
           this.gx = getxmla;
           this.cb = callback;
     }

           function getxmla()
     {
                  var obj;
                  obj = this;                 //apparently required to assign correct reference to callback function
                  //this.oTbl = new ActiveXObject("Microsoft.xmlHTTP");
                  this.oTbl = assignXMLHTTP(); // declared in client.js
                  this.oTbl.open ("POST", this.url , this.async);
                  this.oTbl.setRequestHeader ("content-type", "text/xml");
                  if (this.async == true)
                  {
                     this.oTbl.onreadystatechange = function() { obj.cb(); };
                  }
                  this.oTbl.send (this.sxml);
                  this.rv   = true;
                  return (this.rv);
     }

           function callback()
     {
           switch (this.oTbl.readyState)
           {
           case 4:
                  this.rxml     = new ActiveXObject("Microsoft.XMLDOM");
                  if (this.oTbl.responseText == "" | this.oTbl.responseText.length == 0)
                  {
                     this.rxml.loadXML ("<warning>finished, but no response</warning>");
                     this.rv = false;
                  }
                  else
                  {
                     this.rawtext = this.oTbl.responseText;
                     this.success = this.rxml.loadXML(this.rawtext);
                     if (this.success == false)
                     {
                        this.rxml.loadXML ("<warning>possible mal-formed XML</warning>");
                        this.rv = false;
                     }
                     else
                     {
                     }
                  }
                  this.callbackdone = true;
                  this.acb(); //call the AfterCallBack function now that the data is received
           default:
           }
        }



