protected void SaveButton_Click(object sender, EventArgs e)
        {
			Page.Validate("");
			if (Page.IsValid)
			{
				if (this.OverrideTaxDateCheckBox.Checked == true && !this.TaxDateCal.ValidateNow())
				{
					this.TaxDateCustomValidator.IsValid = false;
					return;
				}

				bool succeeded = false;
				bool previouslyPaid = CurrentCredit.Paid;
				
				LoadCreditFromScreen();

				try
				{
					if (!Invoice.DoesDuplicateGuidExistInDb((Guid)this.ViewState["DuplicateGuid"]) || CurrentCredit.K > 0)
					{
						if (this.CreditItemDataHolderList.Count == 0)
							throw new Exception("Must have at least one credit item!");

						bool newCredit = CurrentCredit.K == 0;
						//Invoice invoiceToCredit = new Invoice(Convert.ToInt32(InvoiceKValueLabel.Text));

						if (CurrentCredit.K == 0 && CurrentInvoice.AmountAllowedToCredit < Math.Abs(CurrentCredit.Total))
							throw new Exception("Cannot credit more than " + CurrentInvoice.AmountAllowedToCredit.ToString("c") + " for Invoice #" + CurrentInvoice.K.ToString());

						CurrentCredit.Update();

						foreach (InvoiceItemDataHolder creditItemDataHolder in this.CreditItemDataHolderList)
						{
							creditItemDataHolder.InvoiceK = CurrentCredit.K;
							creditItemDataHolder.UpdateInsertDelete();
						}
						foreach (InvoiceTransferDataHolder creditTransferDataHolder in this.CreditTransferDataHolderList)
						{
							creditTransferDataHolder.InvoiceK = CurrentCredit.K;
							creditTransferDataHolder.UpdateInsertDelete();
						}
						foreach (InvoiceTransferDataHolder creditTransferDataHolderDelete in this.CreditTransferDataHolderDeleteList)
						{
							bool toBeDeleted = true;
							foreach (InvoiceTransferDataHolder creditTransferDataHolder in this.CreditTransferDataHolderList)
							{
								// Do not delete ones that are confirmed to be saved.  This resolves issues when saved items are marked for deletion, then added again.
								if (creditTransferDataHolder.TransferK == creditTransferDataHolderDelete.TransferK)
								{
									toBeDeleted = false;
									break;
								}
							}

							if (toBeDeleted == true)
							{
								creditTransferDataHolderDelete.State = DataHolderState.Deleted;
								creditTransferDataHolderDelete.UpdateInsertDelete();
							}
						}

						CurrentInvoice.ApplyCreditToThisInvoice(CurrentCredit);

						if (newCredit)
						{
							// Refresh CurrentCredit
							CurrentCredit = new Invoice(CurrentCredit.K);
                            CurrentCredit.AssignBuyerType();
							CurrentCredit.UpdatePromoterStatusAndSalesStatus();
                            CurrentCredit.Update();
						}

						succeeded = true;
					}
					// Do not process if duplicate exists. User probably tried refreshing the page.
					else
					{
						throw new Exception("Duplicate credit already exists in the database.");
					}
				}
			
				catch (Exception ex)
				{
					// Display error message
					this.ProcessingVal.ErrorMessage = ex.Message;
					this.ProcessingVal.IsValid = false;
				}

				// Having Server.Transfer or Response.Redirect caused an error during debugging.
				if (succeeded == true)
				{
					// Send email to promoter and to DSI accounts
					bool creditCreated = false;
					if (CreditK == 0)
						creditCreated = true;

					// Only send out emails when invoice is created or when Paid status changes
					if (creditCreated || previouslyPaid != CurrentCredit.Paid)
						Utilities.EmailInvoice(CurrentCredit, creditCreated);

					string response = "<script type=\"text/javascript\">alert('Credit #" + CurrentCredit.K.ToString() + " saved successfully'); open('" + CurrentCredit.UrlAdmin() + "?" + Cambro.Misc.Utility.GenRandomText(5) + "', '_self');</script>";
					ViewState["DuplicateGuid"] = Guid.NewGuid();
					Response.Write(response);
					Response.End();
				}
			}
        }