//public override void ViewWillAppear (bool animated)
 //{
 //}
 public PostNoteDialog(MatterDTO matter, IPostingComplete listener)
     : base(UITableViewStyle.Grouped, null, true)
 {
     this.matter = matter;
     this.listener = listener;
     selectedDate = DateTime.Now;
     //this.NavigationItem.RightBarButtonItem = new UIBarButtonItem ("Back", UIBarButtonItemStyle.Bordered, delegate(object sender, EventArgs e) {
     //	NavigationController.PopViewControllerAnimated (true);
     //});
     GetTariffCodes (0);
     BuildInterface ();
 }
        public static double getFee(MatterDTO matter, MobileTariffCodeDTO code,
		                            int duration)
        {
            try {
                Console.WriteLine ("Calculate Fee - amount: " + code.amount + " duration: " + duration + " units: " + code.units + " surcharge: " + matter.surchargeFactor);
            } catch (Exception e) {
                Console.WriteLine ("F****d up ... getting exception " + e.Message);
                return 0;
            }

            double fee = 0;

            if (duration > 0) {
                int elapsed = DivRoundUp (duration, (int) code.units);
                if (code.units > 0) {
                    fee = (code.amount / 60) * code.units * elapsed;
                } else {
                    fee = (code.amount / 60) *  elapsed;
                }

            } else {
                if (code.units > 0) {
                    fee = code.amount * code.units;
                } else {
                    fee = code.amount;
                }

            }

            if (code.surchargeApplies) {
                double surcharge = fee * matter.surchargeFactor;
                fee = surcharge;
            }

            return fee;
        }
        void DataDownloaded(IAsyncResult result)
        {
            isBusy = false;
            UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
            end = DateTime.Now;
            var request = result.AsyncState as HttpWebRequest;
            WebServiceResponseDTO dto;
            try {
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse (result);
                Stream receiveStream = response.GetResponseStream ();
                StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
                string resp = readStream.ReadToEnd ();
                Console.WriteLine ("## ASYNCResponse stream received.\n" + resp);
                response.Close ();
                readStream.Close ();
                dto = (WebServiceResponseDTO)JsonConvert.DeserializeObject (resp, typeof(WebServiceResponseDTO)
                );
                if (dto != null) {

                    Tools.SendElapsedTime (start, end, dto.activityID);
                }

                InvokeOnMainThread (delegate {
                    isBusy = false;
                    try {
                        if (dto.responseCode > 0) {
                            new UIAlertView ("Matter Details", dto.responseMessage, null, "OK").Show ();
                            return;
                        } else {
                            matter = dto.matter;
                            BuildInterface ();
                        }
                    } catch (Exception e) {
                        Console.WriteLine ("### IGNORED: " + e.Message);
                        //ignore -
                    }
                }
                );

            } catch (Exception ex) {
                Console.WriteLine ("shit " + ex.ToString ());
                InvokeOnMainThread (delegate {
                    isBusy = false;
                    new UIAlertView ("Network Error", "Problem communicating with server, \n\nCheck your network connections and try again later", null, "Close"
                    ).Show ();

                }
                );
            }
        }