protected void OnButtonInvoiceEditSaveClicked(object sender, EventArgs e)
    {
        if (btnEditSave.Text == ResourceManager.GetString("editText"))
        {
            //Change mode to Edit mode.
            //btnEditSave.Text = ResourceManager.GetString("saveText");
            //EnableInvoiceControls(true);
            //lnkAddInvoiceDetail.OnClientClick = "return OnAddNewInvoiceDetailClientClicked('"
            //    + SessionManager.CurrentInvoice.InvoiceIdPK + "')";
            //lnkAddNewPayment.OnClientClick = "return OnAddNewInvoicePaymentClientClicked('"
            //    + SessionManager.CurrentInvoice.InvoiceIdPK + "')";
            //btnExport.Visible = false;
            string url = Request.Url.PathAndQuery;

            if (!string.IsNullOrEmpty(Request.QueryString["mode"]))
                url = url.Replace(Request.QueryString["mode"], "edit");
            else
                url += "&mode=edit";
            Response.Redirect(url, true);
        }
        else if (Request.QueryString["type"] == "copy")
        {
            string message;
            SessionManager.CurrentInvoice = null;
            Invoices saveItem = GetInvoice(out message);
            if (!string.IsNullOrEmpty(message))
            {
                string script = "<script type=\"text/javascript\">";
                script += " alert(\"" + message + "\")";
                script += " </script>";

                if (!ClientScript.IsClientScriptBlockRegistered("redirectUser"))
                    ClientScript.RegisterStartupScript(this.GetType(), "redirectUser", script);
                return;
            }
            InvoicesRepository repo = new InvoicesRepository();
            repo.InserNewInvoices(saveItem);
            //insert invoice details
            if (!string.IsNullOrEmpty(Request.QueryString["from"]))
            {
                string invoideIDPK = Request.QueryString["from"];
                string[] key = invoideIDPK.Split('-');
                int idFactNumber = int.Parse(key[0]);
                string type = key[1];
                int idYear = int.Parse(key[2]);

                InvoiceDetailsRepository detailRepo = new InvoiceDetailsRepository();
                IList<InvoiceDetails> detailList = detailRepo.GetInvoiceDetailsOfInvoice(idFactNumber, type, idYear, null);
                foreach (InvoiceDetails detail in detailList)
                {
                    detail.IdFactNumber = saveItem.IdFactNumber;
                    detail.IdTypeInvoice = saveItem.IdTypeInvoice;
                    detail.IdYear = saveItem.IdYear;

                    detailRepo.InserNewInvoiceDetails(detail);
                }
            }

            Response.Redirect("~/InvoiceProfile.aspx?InvoiceIdPK=" + saveItem.InvoiceIdPK + "&mode=edit", true);
        }
        else
        {
            string message;
            //Save data.
            Invoices saveItem = GetInvoice(out message);
            if (!string.IsNullOrEmpty(message))
            {
                string script = "<script type=\"text/javascript\">";
                script += " alert(\"" + message + "\")";
                script += " </script>";

                if (!ClientScript.IsClientScriptBlockRegistered("redirectUser"))
                    ClientScript.RegisterStartupScript(this.GetType(), "redirectUser", script);
                return;
            }
            InvoicesRepository repo = new InvoicesRepository();

            //if (SessionManager.CurrentInvoice != null)
            if (!string.IsNullOrEmpty(Request.QueryString["InvoiceIdPK"]))
            {
                repo.UpdateInvoices(saveItem);
            }
            else
            {
                repo.InserNewInvoices(saveItem);
            }

            if (saveItem.IdFactNumberNew.HasValue)
                SessionManager.CurrentInvoice = repo.GetInvoiceByID(saveItem.IdFactNumberNew.Value, saveItem.IdTypeInvoice, saveItem.IdYear);
            else
                SessionManager.CurrentInvoice = repo.GetInvoiceByID(saveItem.IdFactNumber, saveItem.IdTypeInvoice, saveItem.IdYear);

            if (SessionManager.CurrentInvoice.CompanyId.HasValue)
            {
                txtIdCustomer.Text = SessionManager.CurrentInvoice.CompanyId.ToString();
                txtCustomerName.Text = SessionManager.CurrentInvoice.CompanyName.Trim();
            }
            txtInvoiceNumber.Text = SessionManager.CurrentInvoice.IdFactNumber.ToString();
            //    //Change mode to View mode

            string addBackUrl = string.Empty;
            if (!string.IsNullOrEmpty(Request.QueryString["backurl"]) && Request.QueryString["backurl"] == "visible")
            {
                addBackUrl = "&backurl=visible";
            }
            Response.Redirect(string.Format("~/InvoiceProfile.aspx?InvoiceIdPK={0}&mode=view" + addBackUrl, SessionManager.CurrentInvoice.InvoiceIdPK), true);
        }
    }
    protected void OnInvoiceProfileAjaxManagerAjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        if (e.Argument.IndexOf("DataBindCompanyAddress") != -1)
        {
            string[] param = e.Argument.Split('-');
            if (param.Length == 2)
            {
                invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, divHeader);
                //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, txtAddressName);
                //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, txtAddress);
                //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, txtZipCode);
                //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, txtCity);
                //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, txtVatNumber);
                //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, btnChooseAddress);

                invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, hiddenCompanyAddressId);
                int companyID = int.Parse(param[1]);
                Company company = new CompanyRepository().FindOne(new Company(companyID));
                txtCustomerName.Text = company.CompanyName;
                txtIdCustomer.Text = companyID.ToString();

                IList<CompanyAddress> comAdrList = new CompanyAddressRepository().GetAddressesOfCompany(companyID);
                hiddenCompanyAddressId.Value = null;

                txtAddressName.Text = null;
                txtAddress.Text = null;
                txtZipCode.Text = null;
                txtCity.Text = null;
                txtVatNumber.Text = null;
                foreach (CompanyAddress item in comAdrList)
                {
                    if (item.IsDefault)
                    {
                        hiddenCompanyAddressId.Value = item.AddressID.ToString();
                        txtAddressName.Text = item.Name;
                        txtAddress.Text = item.Address;
                        txtZipCode.Text = item.ZipCode;
                        txtCity.Text = item.City;
                        txtVatNumber.Text = item.VatNumber;
                        break;
                    }
                }
                btnChooseAddress.Enabled = true;
            }
        }

        else if (e.Argument.IndexOf("RebindInvoiceDetailData") != -1)
        {
            invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, divTotal);
            //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, txtTotalHTVA);
            //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, txtTotalVAT);
            //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, txtTotal);
            //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, txtPaymentDate);
            //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, txtPayment);
            //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, chkPayment);
            invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, gridInvoiceDetails);
            gridInvoiceDetails.Rebind();
        }
        else if (e.Argument.IndexOf("RebindInvoicePaymentData") != -1)
        {
            invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, divTotal);
            //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, txtPaymentDate);
            //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, txtPayment);
            //invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, chkPayment);
            invoiceProfileAjaxManager.AjaxSettings.AddAjaxSetting(invoiceProfileAjaxManager, gridInvoicePayments);
            gridInvoicePayments.Rebind();
        }
        else if (e.Argument.IndexOf("ViewEditInvoice") != -1)
        {
            string url = Request.Url.PathAndQuery;

            if (hidMode.Value == "view")
            {
                if (!string.IsNullOrEmpty(Request.QueryString["mode"]))
                    url = url.Replace(Request.QueryString["mode"], "edit");
                else
                    url += "&mode=edit";
                Response.Redirect(url, true);
            }
            else
            {
                if (!string.IsNullOrEmpty(Request.QueryString["mode"]))
                    url = url.Replace(Request.QueryString["mode"], "view");
                else
                    url += "&mode=view";
                Response.Redirect(url, true);
            }
        }
        else if (e.Argument.IndexOf("SaveInvoice") != -1)
        {
            string message;
            Invoices saveItem = GetInvoice(out message);
            if (!string.IsNullOrEmpty(message))
            {
                string script = "<script type=\"text/javascript\">";
                script += " alert(\"" + message + "\")";
                script += " </script>";

                if (!ClientScript.IsClientScriptBlockRegistered("redirectUser"))
                    ClientScript.RegisterStartupScript(this.GetType(), "redirectUser", script);
                return;
            }
            InvoicesRepository repo = new InvoicesRepository();
            if (SessionManager.CurrentInvoice != null)
            {
                repo.UpdateInvoices(saveItem);
            }
            else
            {
                repo.InserNewInvoices(saveItem);
            }
            SessionManager.CurrentInvoice = saveItem;

            string addBackUrl = string.Empty;
            if (!string.IsNullOrEmpty(Request.QueryString["backurl"]) && Request.QueryString["backurl"] == "visible")
            {
                addBackUrl = "&backurl=visible";
            }
            Response.Redirect(string.Format("~/InvoiceProfile.aspx?InvoiceIdPK={0}&mode=view" + addBackUrl, SessionManager.CurrentInvoice.InvoiceIdPK), true);
            //string url = Request.Url.PathAndQuery;
            //if (!string.IsNullOrEmpty(Request.QueryString["mode"]))
            //    url = url.Replace(Request.QueryString["mode"], "view");
            //else
            //    url += "&mode=view";
            //Response.Redirect(url, true);
        }
        else if (e.Argument.IndexOf("PrintInvoice") != -1)
        {
           /* Common.ExportInvoices(SessionManager.CurrentInvoice,
            WebConfig.AddressFillInInvoice, WebConfig.AbsoluteExportDirectory);
            string script = " alert(\"" + ResourceManager.GetString("messageExportSuccessfully") + "\")";
            invoiceProfileAjaxManager.ResponseScripts.Add(script);*/
        }
        else if (e.Argument.IndexOf("EmailInvoice") != -1)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["InvoiceIdPK"]))
            {
                string selectedInvoiceIDs = Request.QueryString["InvoiceIdPK"];
                string url = "SendEmail.aspx?type=invoice&ids=" + selectedInvoiceIDs;
                invoiceProfileAjaxManager.ResponseScripts.Add(string.Format("OnSendInvoiceByEmail('{0}')", url));
            }
        }
    }