﻿//-----------------------------------------------------------------------------
// XMail
//
// Copyright 2005-2010 - Xcential Group LLC.
//
//-----------------------------------------------------------------------------

//=============================================================================
// Constructor

function XMail(
   from,
   create
)
{
   from = (from == null) ? null : from;
   create = (create == null) ? true : create;

   if (create)
      return new XMail(from, false);

   //--------------------------------------------------------------------------
   // Private Interface

   //--------------------------------------------------------------------------
   // Privileged Interface

   this.valueOf = function()
   {

      return oFrom;
   }

   //--------------------------------------------------------------------------

   this.setObjectValue  = function(
      from
   )
   {
      from = (from == null) ? null : from;

      var oFrom = from;
      
      return oFrom;
   }

   //--------------------------------------------------------------------------
   // Initialization

   var oFrom = this.setObjectValue(from);
}

XMail.objectClass = "XMail";

//=============================================================================
// Static Interface

//=============================================================================

XMail.openMail = function(
   sendTo,
   cc,
   bcc,
   subject,
   body,
   link,
   linkTarget
)
{
   sendTo = (sendTo == null) ? null : sendTo;
   cc = (cc == null) ? null : cc;
   bcc = (bcc == null) ? null : bcc;
   subject = (subject == null) ? null : subject;
   body = (body == null) ? null : body;
   link = (link == null) ? null : link;
   linkTarget = (link == null) ? null : linkTarget;

   if (XApp.getMode() != XApp.MODE_CLIENT)
      throw XMsg("Can only call the openMail method of XMail from the client.");

   if (!sendTo && !link)
      throw XMsg("Cannot send email without an email address or link.");

   var mailtoRef = "/app/pkgs/xfw/xmail/XMailForm.asp";
   mailtoRef += "?sendTo=" + encodeURIComponent(sendTo);
   mailtoRef += (XString(cc).isSomething()) ? "&cc=" + encodeURIComponent(cc) : "";
   mailtoRef += (XString(bcc).isSomething()) ? "&bcc=" + encodeURIComponent(bcc) : "";
   mailtoRef += (XString(subject).isSomething()) ? "&subject=" + encodeURIComponent(subject) : "";
   mailtoRef += (XString(link).isSomething()) ? "&link=" + encodeURIComponent(link) : "";
   mailtoRef += (XString(linkTarget).isSomething()) ? "&linkTarget=" + encodeURIComponent(linkTarget) : "";
   mailtoRef += (XString(body).isSomething()) ? "&body=" + encodeURIComponent(body) : "";

   var params = "";
   params += "width=640,";
   params += "height=" + ((XString(link).isSomething()) ? 800 : 750) + ",";
   params += "toolbar=no,";
   params += "menubar=no,";
   params += "scrollbars=no,";
   params += "titlebar=no,";
   params += "status=no,";
   params += "resizable=yes";
   XUI.openWindow(mailtoRef, XApp.APPLICATION_NAME.replace(/\s/g,"_") + "_Email", params);

}

//=============================================================================
// Public Interface

XMail.prototype.send = function(
   sendTo,
   cc,
   bcc,
   subject,
   body,
   link,
   linkTarget
)
{
   cc = (cc == null) ? null : cc;
   bcc = (bcc == null) ? null : bcc;
   subject = (subject == null) ? null : subject;
   
   if (XApp.getMode() != XApp.MODE_SERVER)
      throw XMsg("Can only call the send method of XMail from the server.");
      
   var message = new ActiveXObject("CDO.Message");
   
   message.configuration.fields.item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1;
   //message.configuration.fields.item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "127.0.0.1";
   message.configuration.fields.item("http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory") = "c:\\Inetpub\\mailroot\\pickup";
   message.configuration.fields.update();
   message.from = this.valueOf();
   message.to = sendTo;
   if (cc)
      message.cc = cc;
   if (bcc)
      message.bcc = bcc;
   if (subject)
      message.subject = subject;
   message.htmlBody = body;
   message.send();

}

//=============================================================================
