protected void btnSave_Click(object sender, System.EventArgs e)
        {
            bool error = false;
            string webURL = null;
            StringBuilder message = new StringBuilder();
            //Check fields for valid input
            if (!(txtServiceName.Text != null && txtServiceName.Text.Length > 0))
            {
                error = true;
                message.Append(" You must enter a Service Name<br/>");
            }
            try
            {
                if (!checkGuid())
                {
                    error = true;
                    message.Append("There is an unspecified error with the GUID string.<br/>");
                }
            }
            catch (Exception exc)
            {
                error = true;
                message.Append("GUID Error: ");
                message.Append(exc.Message);
                message.Append("<br/>");
            }
            if (!(txtOutPasskey.Text != null && txtOutPasskey.Text.Length > 0))
            {
                error = true;
                message.Append(" You must enter a default passkey<br/>");
            }
            if (!(txtCodebaseUrl.Text != null && txtCodebaseUrl.Text.Length > 0))
            {
                error = true;
                message.Append(" You must enter the base URL for the Web Site<br/>");
            }
            else if (txtCodebaseUrl.Text.Contains("localhost"))
            {
                error = true;
                message.Append(" You must not use localhost in a codebase URL, if you must test on the local machine please use '127.0.0.1'.<br/>");
            }
            if (!(txtServiceUrl.Text != null && txtServiceUrl.Text.Length > 0))
            {
                error = true;
                message.Append(" You must enter full or relative URL of the Web Service page<br/>");
            }
            else if (txtServiceUrl.Text.Contains("localhost"))
            {
                error = true;
                message.Append(" You must not use localhost in a web service URL, if you must test only on the local machine please use '127.0.0.1'.<br/>");
            }
            else
            {
                //Construct webServiceUrl

                string testURL = txtServiceUrl.Text.Trim();
                if (testURL.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
                    || testURL.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
                {
                    webURL = testURL;
                }
                else if (testURL.StartsWith("~/", StringComparison.OrdinalIgnoreCase))
                {
                    webURL = txtCodebaseUrl.Text.Trim() + testURL.Substring(1);
                }
                else if (testURL.StartsWith("/", StringComparison.OrdinalIgnoreCase))
                {
                    webURL = txtCodebaseUrl.Text.Trim() + testURL;
                }
                else
                {
                    webURL = txtCodebaseUrl.Text.Trim() + "/" + testURL;
                }

                // Test for valid webService URL
                ProcessAgentProxy paProxy = new ProcessAgentProxy();
                paProxy.Url = webURL;
                try
                {
                    DateTime serviceTime = paProxy.GetServiceTime();
                }
                catch
                {
                    error = true;
                    message.Append(" There is an error with the web service URL: " + webURL + " Please check that it is valid and the web service is configured correctly.<br/>");
                }
            }
            if (error)
            {
                lblResponse.Text = Utilities.FormatErrorMessage(message.ToString());
                lblResponse.Visible = true;
                return;
            }
            else
            {
                //if(txtOutPasskey.Text.CompareTo(ConfigurationManager.AppSettings["defaultPasskey"]) != 0){
                //    ConfigurationManager.AppSettings.Set("defaultPasskey",txtOutPasskey.Text);
                //}

                // Check if domain is set if so only update mutable Fields
                dbTicketing.SelfRegisterProcessAgent(txtServiceGuid.Text.Trim(),
                    txtServiceName.Text, lblServiceType.Text, null,
                    txtCodebaseUrl.Text.Trim(), webURL);
                txtServiceUrl.Text = webURL;
                if (AgentType == ProcessAgentType.SERVICE_BROKER)
                {
                    dbTicketing.SetDomainGuid(txtServiceGuid.Text.Trim());
                    AuthorityUpdateSelf(txtServiceName.Text, txtServiceGuid.Text.Trim(),
                                 txtCodebaseUrl.Text.Trim(), txtDescription.Text, txtContactInfo.Text,
                                 txtBugEmail.Text, txtLocation.Text);
                }
                dbTicketing.SaveSystemSupport(ProcessAgentDB.ServiceAgent.agentGuid, txtContactInfo.Text, txtBugEmail.Text,
                            txtInfoUrl.Text, txtDescription.Text, txtLocation.Text);

                //DisplayForm();
                lblResponse.Text = Utilities.FormatConfirmationMessage("Self registration has completed!");
                lblResponse.Visible = true;
                ProcessAgentDB.RefreshServiceAgent();
            }
        }
        protected void btnModify_Click(object sender, System.EventArgs e)
        {
            bool error = false;
            StringBuilder message = new StringBuilder();
            try
            {
                if (ProcessAgentDB.ServiceAgent != null)
                {
                    string originalGuid = ProcessAgentDB.ServiceAgent.agentGuid;
                    if (!(txtServiceName.Text != null && txtServiceName.Text.Length > 0))
                    {
                        error = true;
                        message.Append(" You must enter a Service Name<br/>");
                    }
                    try
                    {
                        if (!checkGuid())
                        {
                            error = true;
                            message.Append("There is an unspecified error with the GUID string.<br/>");
                        }
                    }
                    catch (Exception ex)
                    {
                        error = true;
                        message.Append("GUID Error: ");
                        message.Append(ex.Message);
                        message.Append("<br/>");
                    }
                    if (!(txtOutPasskey.Text != null && txtOutPasskey.Text.Length > 0))
                    {
                        error = true;
                        message.Append(" You must enter a default passkey<br/>");
                    }
                    if (!(txtCodebaseUrl.Text != null && txtCodebaseUrl.Text.Length > 0))
                    {
                        error = true;
                        message.Append(" You must enter the base URL for the Web Site<br/>");
                    }
                    else if (txtCodebaseUrl.Text.Contains("localhost"))
                    {
                        error = true;
                        message.Append(" You must not use localhost in a codebase URL, if you must test on the local machine please use '127.0.0.1'.<br/>");
                    }
                    if (!(txtServiceUrl.Text != null && txtServiceUrl.Text.Length > 0))
                    {
                        error = true;
                        message.Append(" You must enter the web Ssrvice URL for the Web Site<br/>");
                    }
                    else if (txtServiceUrl.Text.Contains("localhost"))
                    {
                        error = true;
                        message.Append(" You must not use localhost in a web service URL, if you must test only on the local machine please use '127.0.0.1'.<br/>");
                    }
                    else
                    { // Test for valid webService URL
                        ProcessAgentProxy paProxy = new ProcessAgentProxy();
                        paProxy.Url = txtServiceUrl.Text.Trim();
                        try
                        {
                            DateTime serTime = paProxy.GetServiceTime();
                        }
                        catch
                        {
                                error = true;
                                message.Append(" There is an error with the web service URL: " + txtServiceUrl.Text.Trim() + " Please check that it is valid and the web service is configured correctly.<br/>");
                        }
                    }
                    if (error)
                    {
                        lblResponse.Text = Utilities.FormatErrorMessage(message.ToString());
                        lblResponse.Visible = true;
                        return;
                    }
                    if (ProcessAgentDB.ServiceAgent.domainGuid != null)
                    {
                        ProcessAgentInfo originalAgent = dbTicketing.GetProcessAgentInfo(originalGuid);
                        ProcessAgentInfo sb = dbTicketing.GetProcessAgentInfo(ProcessAgentDB.ServiceAgent.domainGuid);
                        if ((sb != null) && !sb.retired)
                        {
                            ProcessAgentProxy psProxy = new ProcessAgentProxy();
                            AgentAuthHeader header = new AgentAuthHeader();
                            header.agentGuid = ProcessAgentDB.ServiceAgent.agentGuid;
                            header.coupon = sb.identOut;
                            psProxy.AgentAuthHeaderValue = header;
                            psProxy.Url = sb.webServiceUrl;
                            ProcessAgent pa = new ProcessAgent();
                            pa.agentGuid = txtServiceGuid.Text;
                            pa.agentName = txtServiceName.Text;
                            pa.domainGuid = ProcessAgentDB.ServiceAgent.domainGuid;
                            pa.codeBaseUrl = txtCodebaseUrl.Text;
                            pa.webServiceUrl = txtServiceUrl.Text;
                            pa.type = AgentType;
                            //dbTicketing.SelfRegisterProcessAgent(pa.agentGuid, pa.agentName, agentType,
                            //    pa.domainGuid, pa.codeBaseUrl, pa.webServiceUrl);
                            //message.Append("Local information has been saved. ");
                            int returnValue = psProxy.ModifyProcessAgent(originalGuid, pa, null);
                            message.Append("The changes have been sent to the ServiceBroker");
                            if (returnValue > 0)
                            {
                                dbTicketing.SelfRegisterProcessAgent(pa.agentGuid, pa.agentName, AgentType,
                                pa.domainGuid, pa.codeBaseUrl, pa.webServiceUrl);

                                message.Append(".<br />Local information has been saved. ");
                                lblResponse.Text = Utilities.FormatConfirmationMessage(message.ToString());
                                lblResponse.Visible = true;

                            }
                            else
                            {
                                message.Append(", but did not process correctly!");
                                message.Append("<br />Local information has not been saved. ");
                                lblResponse.Text = Utilities.FormatErrorMessage(message.ToString());
                                lblResponse.Visible = true;
                            }
                        }
                    }
                    else
                    {
                        string tmpGuid = null;
                        if (AgentType == ProcessAgentType.SERVICE_BROKER)
                        {
                            tmpGuid = ProcessAgentDB.ServiceAgent.agentGuid;
                            AuthorityUpdateSelf(txtServiceName.Text,ProcessAgentDB.ServiceAgent.agentGuid,
                                 txtCodebaseUrl.Text.Trim(), txtDescription.Text, txtContactInfo.Text,
                                 txtBugEmail.Text, txtLocation.Text);
                        }
                        dbTicketing.SelfRegisterProcessAgent(ProcessAgentDB.ServiceAgent.agentGuid, txtServiceName.Text, AgentType,
                                tmpGuid, txtCodebaseUrl.Text.Trim(), txtServiceUrl.Text.Trim());
                        dbTicketing.SaveSystemSupport(ProcessAgentDB.ServiceAgent.agentGuid, txtContactInfo.Text, txtBugEmail.Text,
                           txtInfoUrl.Text, txtDescription.Text, txtLocation.Text);
                    }
                    ProcessAgentDB.RefreshServiceAgent();
                //    if (txtOutPasskey.Text.CompareTo(ConfigurationManager.AppSettings["defaultPasskey"]) != 0)
                //    {
                //        ConfigurationManager.AppSettings.Set("defaultPasskey", txtOutPasskey.Text);
                //    }
                }
            }
            catch (Exception ex)
            {
                Exception ex2 = new Exception("Error in  selfRegistration.modify()", ex);
               Logger.WriteLine(Utilities.DumpException(ex2));
                throw ex2;
            }
        }