//
        // Asynchronous HTTP request
        //
        public void getAsyncData()
        {
            isBusy = true;
            start = DateTime.Now;
            UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
            GhostRequestDTO cr = new GhostRequestDTO ();
            string json, encodedJSON, url;

            cr.requestType = GhostRequestDTO.FIND_MATTER;
            cr.searchString = searchBar.Text;
            cr.appID = NSUserDefaults.StandardUserDefaults.IntForKey ("appID");
            cr.userID = NSUserDefaults.StandardUserDefaults.IntForKey ("userID");
            cr.companyID = NSUserDefaults.StandardUserDefaults.IntForKey ("companyID");
            cr.deviceID = NSUserDefaults.StandardUserDefaults.StringForKey ("deviceID");

            json = JsonConvert.SerializeObject (cr);
            Console.WriteLine ("Async JSON = " + json);
            encodedJSON = HttpUtility.UrlEncode (json);
            url = Tools.CONSOLE_URL + encodedJSON;

            try {
                var request = (HttpWebRequest)WebRequest.Create (url);
                request.BeginGetResponse (DataDownloaded, request);
            } catch (WebException e) {
                isBusy = false;
                Console.WriteLine ("Exception - " + e.Message);
                new UIAlertView ("Error", "Server Unavailable at this time.\nPlease try later.", null, "OK").Show ();
            }
        }
        // Asynchronous HTTP request
        //
        public static void SendElapsedTime(DateTime start, DateTime end, int activityID)
        {
            GhostRequestDTO cr = new GhostRequestDTO ();
            string json, encodedJSON, url;

            cr.requestType = GhostRequestDTO.POST_DEVICE_ELAPSED_TIME;
            cr.activityID = activityID;
            cr.deviceElapsedSeconds = getElapsed (start, end);

            json = JsonConvert.SerializeObject (cr);
            //Console.WriteLine ("SendElapsed JSON = " + json);
            encodedJSON = HttpUtility.UrlEncode (json);
            url = Tools.CONSOLE_URL + encodedJSON;

            var request = (HttpWebRequest)WebRequest.Create (url);
            request.BeginGetResponse (PlatformDownloaded, request);
        }
        //
        // Asynchronous HTTP request
        //
        public void getAsyncAppAndPlatform()
        {
            UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
            isBusy = true;
            GhostRequestDTO cr = new GhostRequestDTO ();
            string json, encodedJSON, url;

            cr.requestType = GhostRequestDTO.GET_APP_PLATFORM_IDS;
            cr.appName = "GhostPractice Mobile";
            cr.platformName = "iPhone";

            json = JsonConvert.SerializeObject (cr);
            Console.WriteLine ("Async JSON = " + json);
            encodedJSON = HttpUtility.UrlEncode (json);
            url = Tools.CONSOLE_URL + encodedJSON;

            var request = (HttpWebRequest)WebRequest.Create (url);
            request.BeginGetResponse (PlatformDownloaded, request);
        }
        //
        // Asynchronous HTTP request
        //
        public void getAsyncProvisionDevice()
        {
            UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
            isBusy = true;
            start = DateTime.Now;
            GhostRequestDTO cr = new GhostRequestDTO ();
            string json, encodedJSON, url;

            cr.requestType = GhostRequestDTO.PROVISION_NEW_USER;
            cr.activationCode = activation.Value;
            cr.appID = NSUserDefaults.StandardUserDefaults.IntForKey ("appID");
            cr.platformID = NSUserDefaults.StandardUserDefaults.IntForKey ("platformID");

            json = JsonConvert.SerializeObject (cr);
            Console.WriteLine ("Async JSON = " + json);
            encodedJSON = HttpUtility.UrlEncode (json);
            url = Tools.CONSOLE_URL + encodedJSON;

            var request = (HttpWebRequest)WebRequest.Create (url);
            request.BeginGetResponse (ProvisioningDownloaded, request);
        }
        public void getAsyncReportData()
        {
            UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
            isBusy = true;
            start = DateTime.Now;
            GhostRequestDTO cr = new GhostRequestDTO ();
            string json, encodedJSON, url;

            cr.requestType = GhostRequestDTO.GET_REPORT;
            cr.reportType = reportType;
            cr.appID = NSUserDefaults.StandardUserDefaults.IntForKey ("appID");
            cr.userID = NSUserDefaults.StandardUserDefaults.IntForKey ("userID");
            cr.companyID = NSUserDefaults.StandardUserDefaults.IntForKey ("companyID");
            cr.deviceID = NSUserDefaults.StandardUserDefaults.StringForKey ("deviceID");
            json = JsonConvert.SerializeObject (cr);
            Console.WriteLine ("##GetReport JSON = " + json);
            encodedJSON = HttpUtility.UrlEncode (json);
            url = Tools.CONSOLE_URL + encodedJSON;

            var request = (HttpWebRequest)WebRequest.Create (url);
            request.BeginGetResponse (DataDownloaded, request);
        }
        public void GetTariffCodes(int duration)
        {
            if (isBusy) {
                Console.WriteLine ("##GetTariffCodes: comms are busy, slow down!");
                return;
            }
            if (matter == null) {
                return;
            }

            isBusy = true;
            BuildInterface ();
            UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
            start = DateTime.Now;
            GhostRequestDTO cr = new GhostRequestDTO ();
            string json, encodedJSON, url;

            cr.requestType = GhostRequestDTO.GET_TARIFF_CODES;
            cr.matterID = matter.id;
            cr.duration = duration;
            cr.tarrifCodeType = DataUtil.TARIFF_CODE_TYPE_NOTES;
            cr.appID = NSUserDefaults.StandardUserDefaults.IntForKey ("appID");
            cr.userID = NSUserDefaults.StandardUserDefaults.IntForKey ("userID");
            cr.companyID = NSUserDefaults.StandardUserDefaults.IntForKey ("companyID");
            cr.deviceID = NSUserDefaults.StandardUserDefaults.StringForKey ("deviceID");

            json = JsonConvert.SerializeObject (cr);
            Console.WriteLine ("$$ GetTariffCodes JSON = " + json);
            encodedJSON = HttpUtility.UrlEncode (json);
            url = Tools.CONSOLE_URL + encodedJSON;

            var request = (HttpWebRequest)WebRequest.Create (url);
            request.BeginGetResponse (DataDownloaded, request);
        }
        /*
         * Methods to access web services async
         *
         */
        public void PostNoteToOffice(MatterNoteDTO note)
        {
            if (isBusy) {
                Console.WriteLine ("##PostNote: comms are busy, slow down!");
                return;
            }
            isBusy = true;
            BuildInterface ();
            UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
            start = DateTime.Now;
            GhostRequestDTO cr = new GhostRequestDTO ();
            string json, encodedJSON, url;

            cr.requestType = GhostRequestDTO.POST_NOTE;
            cr.note = note;
            cr.appID = NSUserDefaults.StandardUserDefaults.IntForKey ("appID");
            cr.userID = NSUserDefaults.StandardUserDefaults.IntForKey ("userID");
            cr.companyID = NSUserDefaults.StandardUserDefaults.IntForKey ("companyID");
            cr.deviceID = NSUserDefaults.StandardUserDefaults.StringForKey ("deviceID");

            json = JsonConvert.SerializeObject (cr);
            Console.WriteLine ("@@ PostNote JSON = " + json);
            encodedJSON = HttpUtility.UrlEncode (json);
            url = Tools.CONSOLE_URL + encodedJSON;

            var request = (HttpWebRequest)WebRequest.Create (url);
            request.BeginGetResponse (PostComplete, request);
        }