//+---------------------------------------------------------------------------
 //
 //  function:   GetAccountInfo
 //
 //  Synopsis:   Get the account object.
 //
 //  Arguments:  [objFaxAccSet] - FaxAccountSet object having the list of all Fax Accounts
 //                [strAccountName] - Account whose info is to be printed.
 //
 //  Returns:    bool: true if passed successfully
 //
 //----------------------------------------------------------------------------
 static bool GetAccountInfo(FAXCOMEXLib.IFaxAccountSet objFaxAccSet, string strAccountName)
 {
     if ((objFaxAccSet != null) && (String.IsNullOrEmpty(strAccountName) != false))
                 {
                         FAXCOMEXLib.IFaxAccount objFaxAccount;
                         //Get the account with the name lptstrAccountName
                         objFaxAccount = objFaxAccSet.GetAccount(strAccountName);
                         DisplayFaxAccount(objFaxAccount);
                         return true;
                 }
                 System.Console.WriteLine("GetAccountInfo: Parameter is NULL");
                 return false;
 }
        //+---------------------------------------------------------------------------
        //
        //  function:   DeleteAccount
        //
        //  Synopsis:   Deletes a Fax Account
        //
        //  Arguments:  [objFaxAccSet] - FaxAccountSet object having the list of all Fax Accounts
        //                [strAccountName] - name of the account to be deleted.
        //
        //  Returns:    bool: true if passed successfully
        //
        //----------------------------------------------------------------------------
        static bool DeleteAccount(FAXCOMEXLib.IFaxAccountSet objFaxAccSet, string strAccountName)
        {
            if ((objFaxAccSet != null) && (String.IsNullOrEmpty(strAccountName) == false))
                        {
                                bool bFound = false;
                                //now lets enumerate the existing accounts first.
                                if (!FaxEnumAccounts(objFaxAccSet, false, null, ref bFound))
                                {
                                        //FaxEnumAccounts failed
                                        System.Console.WriteLine("FaxEnumAccounts failed before deleting");
                                }

                                objFaxAccSet.RemoveAccount(strAccountName);
                                //now enumerate to see if account exists
                                if (!FaxEnumAccounts(objFaxAccSet, true, strAccountName, ref bFound))
                                {
                                        //we can properly validate if this call fails, hence log an error
                                        System.Console.WriteLine("FaxEnumAccounts failed during validation");
                                        return false;
                                }
                                if (bFound)
                                {
                                        //we just deleted the account but still the enumeration shows the account. hecen log error
                                        System.Console.WriteLine("Account exists after deleteion");
                                        return false;
                                }
                                return true;
                        }
                        System.Console.WriteLine("DeleteAccount: Parameter is NULL");
                        return false;
        }
        //+---------------------------------------------------------------------------
        //
        //  function:   IFaxIncomingMessageIterator
        //
        //  Synopsis:   Get the incoming archive folder of the current account
        //
        //  Arguments:  [objFaxFolders] - List of folders for the current account
        //
        //  Returns:    IFaxIncomingMessageIterator: Iterator to the messages in inbox folder
        //
        //----------------------------------------------------------------------------
        static FAXCOMEXLib.IFaxIncomingMessageIterator FaxAccountIncomingArchive(FAXCOMEXLib.IFaxAccountFolders objFaxFolders)
        {
            int NUM_MSGS = 100;
                        FAXCOMEXLib.IFaxAccountIncomingArchive objFaxInbox;
                        FAXCOMEXLib.IFaxIncomingMessageIterator objIncomingMsgIterator;

                        if (objFaxFolders != null)
                        {
                                //Initialize MsgArchive Object
                                objFaxInbox = objFaxFolders.IncomingArchive;
                                //Initialize Msg Iterator
                                objIncomingMsgIterator = objFaxInbox.GetMessages(NUM_MSGS);
                                return objIncomingMsgIterator;
                        }
                        System.Console.WriteLine("FaxAccountIncomingArchive: Parameter is NULL");
                        return null;
        }
 //+---------------------------------------------------------------------------
 //
 //  function:   AddAccount
 //
 //  Synopsis:   Adds a Fax Account
 //
 //  Arguments:  [objFaxAccSet] - FaxAccountSet object having the list of all Fax Accounts
 //                [strAccName] - name of the account to be added. Must be a valid NT/Domain user.
 //
 //  Returns:    bool: true if passed successfully
 //
 //----------------------------------------------------------------------------
 static bool AddAccount(FAXCOMEXLib.IFaxAccountSet objFaxAccSet, string strAccName)
 {
     if ((objFaxAccSet != null) && (String.IsNullOrEmpty(strAccName) == false))
                 {
                         bool bFound = true;
                         //first enum the existing accounts
                         if (!FaxEnumAccounts(objFaxAccSet, false, null, ref bFound))
                         {
                                 //enum failed
                                 System.Console.WriteLine("FaxEnumAccounts failed");
                         }
                         //now add the account
                         FAXCOMEXLib.IFaxAccount objFaxAccount = objFaxAccSet.AddAccount(strAccName);
                         //Display Info on added account.
                         DisplayFaxAccount(objFaxAccount);
                         return true;
                 }
                 System.Console.WriteLine("AddAccount: Parameter is NULL");
                 return false;
 }
        //+---------------------------------------------------------------------------
        //
        //  function:   getUnassignedMsg
        //
        //  Synopsis:   Get unassigned msgs
        //
        //  Arguments:  [objIncomingMsgIterator] - Iterator to the messages in inbox folder
        //                [pCount] - Referenced variable containing the number of reassignable faxes.
        //
        //  Returns:    ArrayList: Array of strings containing the mesg ids of reassignable faxes
        //
        //----------------------------------------------------------------------------
        static ArrayList getUnassignedMsg(FAXCOMEXLib.IFaxIncomingMessageIterator objIncomingMsgIterator, ref int pCount)
        {
            //Get the number of reassignable messages
                        ArrayList arrMsgIds = new ArrayList();
                        if (objIncomingMsgIterator != null)
                        {

                                //Goto first Msg
                                objIncomingMsgIterator.MoveFirst();

                                //Loop thru all msgs
                                int i = 0;
                                while (true)
                                {
                                        FAXCOMEXLib.IFaxIncomingMessage objIncomingMessage;
                                        if (objIncomingMsgIterator.AtEOF)
                                        {
                                                break;
                                        }
                                        objIncomingMessage = objIncomingMsgIterator.Message;
                                        FAXCOMEXLib.IFaxIncomingMessage2 objIncomingMessage2 = (FAXCOMEXLib.IFaxIncomingMessage2)(objIncomingMessage);
                                        //if not reassigned
                                        if (!objIncomingMessage2.WasReAssigned)
                                        {
                                                arrMsgIds.Add(objIncomingMessage2.Id);
                                                i++;
                                        }
                                        objIncomingMsgIterator.MoveNext();
                                }
                                pCount = i;

                                return arrMsgIds;
                        }
                        System.Console.WriteLine("getUnassignedMsg: Parameter is NULL");
                        return null;
        }
        //+---------------------------------------------------------------------------
        //
        //  function:   Reassign
        //
        //  Synopsis:   Reassign the Msg
        //
        //  Arguments:  [objIncomingMsgIterator] - Iterator to the messages in inbox folder
        //                [strMsgId] - Id of the message to be reassigned
        //                [strRecipients] - Recipients to whom the message is to be assigned.
        //
        //  Returns:    bool : true if reassign was successful
        //
        //----------------------------------------------------------------------------
        static bool Reassign(FAXCOMEXLib.IFaxIncomingMessageIterator objIncomingMsgIterator, string strMsgId, string strRecipients)
        {
            bool bRetVal = false;
                        if ((objIncomingMsgIterator != null) && (String.IsNullOrEmpty(strMsgId) != true) && (String.IsNullOrEmpty(strRecipients) != true))
                        {
                                //Goto first Msg
                                objIncomingMsgIterator.MoveFirst();
                                while (true)
                                {
                                        FAXCOMEXLib.IFaxIncomingMessage objIncomingMessage;
                                        if (objIncomingMsgIterator.AtEOF)
                                        {
                                                System.Console.WriteLine("Reassign Message Id not found");
                                                break;
                                        }
                                        //Get current Msg
                                        objIncomingMessage = objIncomingMsgIterator.Message;
                                        FAXCOMEXLib.IFaxIncomingMessage2 objIncomingMessage2 = (FAXCOMEXLib.IFaxIncomingMessage2)(objIncomingMessage);

                                        if (String.Compare(objIncomingMessage2.Id, strMsgId, true, CultureInfo.CurrentCulture) == 0)
                                        {
                                                //Set the Msg Parameters
                                                objIncomingMessage2.Subject = SUBJECT;
                                                objIncomingMessage2.SenderName = SENDER_NAME;
                                                objIncomingMessage2.Recipients = strRecipients;
                                                objIncomingMessage2.SenderFaxNumber = SENDER_FAXNUMBER;
                                                //Reassign
                                                objIncomingMessage2.ReAssign();
                                                System.Console.WriteLine("Reassign was successful");
                                                bRetVal = true;
                                                break;
                                        }
                                        //Next Msg
                                        objIncomingMsgIterator.MoveNext();
                                }
                                return bRetVal;
                        }
                        System.Console.WriteLine("Reassign: Parameter is NULL");
                        return false;
        }
 //+---------------------------------------------------------------------------
 //
 //  function:   hasReassignPermission
 //
 //  Synopsis:   Check if the current user has ReAssign Permission
 //
 //  Arguments:  [objFaxServer] - Fax Server object
 //
 //  Returns:    bool: true if it has reassign permissions
 //
 //----------------------------------------------------------------------------
 static bool hasReassignPermission(FAXCOMEXLib.FaxServerClass objFaxServer)
 {
     FAXCOMEXLib.IFaxSecurity2 objFaxSecurity2;
                 if (objFaxServer != null)
                 {
                         //Get the Security Object
                         objFaxSecurity2 = objFaxServer.Security2;
                         FAXCOMEXLib.FAX_ACCESS_RIGHTS_ENUM_2 enumFaxRights;
                         //Get the Access Rights of the user
                         enumFaxRights = objFaxSecurity2.GrantedRights;
                         if ((enumFaxRights & FAXCOMEXLib.FAX_ACCESS_RIGHTS_ENUM_2.far2MANAGE_RECEIVE_FOLDER) == FAXCOMEXLib.FAX_ACCESS_RIGHTS_ENUM_2.far2MANAGE_RECEIVE_FOLDER)
                         {
                                 return true;
                         }
                         else
                         {
                                 return false;
                         }
                 }
                 System.Console.WriteLine("hasReassignPermission: Parameter is NULL");
                 return false;
 }
        //+---------------------------------------------------------------------------
        //
        //  function:   PrintJobStatus
        //
        //  Synopsis:   prints the jobs status
        //
        //  Arguments:  [objFaxOutgoingJob] - FaxOutgoingJob object pointing to the fax that was sent.
        //
        //  Returns:    bool: true is passed successfully
        //
        //----------------------------------------------------------------------------
        static bool PrintJobStatus(FAXCOMEXLib.IFaxOutgoingJob2 objFaxOutgoingJob)
        {
            bool bRetVal = false;
                        long lDeviceId = 0;
                        FAX_JOB_STATUS_ENUM faxStatus;
                        FAX_PRIORITY_TYPE_ENUM faxPriority;
                        if (objFaxOutgoingJob != null)
                        {

                                lDeviceId = objFaxOutgoingJob.DeviceId;
                                System.Console.Write("Device Id : ");
                                System.Console.Write(lDeviceId);
                                System.Console.WriteLine();

                                faxStatus = objFaxOutgoingJob.Status;
                                if (faxStatus == FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsCANCELED)
                                        System.Console.WriteLine("Status :  Canceled ");

                                if (faxStatus == FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsCANCELING)
                                        System.Console.WriteLine("Status :  Canceling ");

                                if (faxStatus == FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsCOMPLETED)
                                        System.Console.WriteLine("Status :  Completed ");

                                if (faxStatus == FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsFAILED)
                                        System.Console.WriteLine("Status :  Failed ");

                                if (faxStatus == FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsINPROGRESS)
                                        System.Console.WriteLine("Status :  In Progress ");

                                if (faxStatus == FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsNOLINE)
                                        System.Console.WriteLine("Status :  No Line ");

                                if (faxStatus == FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsPAUSED)
                                        System.Console.WriteLine("Status :  Paused ");

                                if (faxStatus == FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsPENDING)
                                        System.Console.WriteLine("Status :  Pending ");

                                if (faxStatus == FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsRETRIES_EXCEEDED)
                                        System.Console.WriteLine("Status :  Retries Exceeded ");

                                if (faxStatus == FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsRETRYING)
                                        System.Console.WriteLine("Status :  Retrying ");

                                if (faxStatus == FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsROUTING)
                                        System.Console.WriteLine("Status :  Routing ");

                                faxPriority = objFaxOutgoingJob.Priority;
                                if (faxPriority == FAX_PRIORITY_TYPE_ENUM.fptLOW)
                                        System.Console.WriteLine("Priority :  Low ");

                                if (faxPriority == FAX_PRIORITY_TYPE_ENUM.fptNORMAL)
                                        System.Console.WriteLine("Priority :  Normal ");

                                if (faxPriority == FAX_PRIORITY_TYPE_ENUM.fptHIGH)
                                        System.Console.WriteLine("Priority :  High ");

                                bRetVal = true;
                                return bRetVal;
                        }
                        System.Console.WriteLine("PrintJobStatus: Parameter is NULL");
                        return false;
        }
        //+---------------------------------------------------------------------------
        //
        //  function:   FaxEnumAccounts
        //
        //  Synopsis:   Enumerates the list of accounts
        //
        //  Arguments:  [objFaxAccSet] - FaxAccountSet object having the list of all Fax Accounts
        //                [bCheck] - if set to true then is verifies if the account with name strAccName is present.
        //                [strAccName] - name of the account that is to be verified.
        //                [pbFound] - used to return the result of whether the account is present or not
        //
        //  Returns:    bool: true if passed successfully
        //
        //  Modifies:    pbFound : if the account with the name strAccName is found then pbFound is set to true.
        //
        //----------------------------------------------------------------------------
        static bool FaxEnumAccounts(FAXCOMEXLib.IFaxAccountSet objFaxAccSet, bool bCheck, string strAccName, ref bool pbFound)
        {
            if(objFaxAccSet !=null)
                        {
                                //Get the FaxAccounts object
                                FAXCOMEXLib.IFaxAccounts objFaxAccounts = objFaxAccSet.GetAccounts();
                                //Print the number of FaxAccounts
                                System.Console.WriteLine("Number of accounts: " + objFaxAccounts.Count);

                                //start enumerating each account.
                                System.Collections.IEnumerator objFaxEnum = objFaxAccounts.GetEnumerator();

                                while (true)
                                {
                                        bool bLast = objFaxEnum.MoveNext();
                                        if (bLast == false)
                                        {
                                                //enumeration is done
                                                System.Console.WriteLine("Enumeration of accounts done.");
                                                break;
                                        }

                                        FAXCOMEXLib.IFaxAccount objFaxAccount = (IFaxAccount)objFaxEnum.Current;
                                        //if check that a account is present.
                                        if (bCheck)
                                        {
                                                if (String.IsNullOrEmpty(strAccName) == false)
                                                {
                                                        if (String.Compare(objFaxAccount.AccountName.ToLower(CultureInfo.CurrentCulture), strAccName.ToLower(CultureInfo.CurrentCulture), true, CultureInfo.CurrentCulture) == 0)
                                                        {
                                                                pbFound = true;
                                                        }
                                                }
                                                else
                                                {
                                                        System.Console.WriteLine("FaxEnumAccounts: strAccName Parameter is NULL");
                                                        return false;
                                                }

                                        }
                                        //Display the current account info.
                                        DisplayFaxAccount(objFaxAccount);
                                }
                                return true;
                        }
                        System.Console.WriteLine("FaxEnumAccounts: Parameter is NULL");
                        return false;
        }
 //+---------------------------------------------------------------------------
 //
 //  function:   DisplayFaxAccount
 //
 //  Synopsis:   prints the display name of a Fax Account
 //
 //  Arguments:  [objFaxAcc] - FaxAccount object whose display name is to be printed
 //
 //  Returns:    bool: true if passed successfully
 //
 //----------------------------------------------------------------------------
 static bool DisplayFaxAccount(FAXCOMEXLib.IFaxAccount objFaxAcc)
 {
     if(objFaxAcc != null)
                 {
                         //print the accountname
                         System.Console.WriteLine("Fax Account Name: " + objFaxAcc.AccountName);
                         return true;
                 }
                 System.Console.WriteLine("DisplayFaxAccount: Parameter is NULL");
                 return false;
 }
 //+---------------------------------------------------------------------------
 //
 //  function:    setTSID
 //
 //  Synopsis:    sets the value of TSID for a FaxDevice
 //
 //  Arguments:  [objFaxDevices] - FaxDevices object pointing to the list of devices on the server
 //                [lDeviceId] - Device Id of the device to be set
 //                [strTSID] -	value of the TSID
 //
 //  Returns:     bool: true if passed successfully
 //
 //----------------------------------------------------------------------------
 static bool setTSID(FAXCOMEXLib.IFaxDevices objFaxDevices, int iDeviceId, string strTSID)
 {
     FaxDevice objFaxDevice = null;
                 if(objFaxDevices != null && String.IsNullOrEmpty(strTSID) == false)
                 {
                         objFaxDevice = objFaxDevices.get_ItemById(iDeviceId);
                         //set TSID
                         objFaxDevice.TSID = strTSID;
                         //Save it
                         objFaxDevice.Save();
                         System.Console.WriteLine("New TSID is set");
                         return true;
                 }
                 System.Console.WriteLine("setTSID: Parameter is NULL");
                 return false;
 }