/// <summary>
    /// Initial set up for the application. We read the config file and create the instance of Oauth and addressbook object.
    /// If it's post back from getting Auth code then perform operations.
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        ReadConfigFile();
        oauth = new OAuth(endPoint, scope, apiKey, secretKey, authorizeRedirectUri, refreshTokenExpiresIn, bypassSSL);
        this.serializer = new JavaScriptSerializer();
        if (Session["cs_rest_AccessToken"] != null && this.addressbook == null)
        {
            this.addressbook = new AddressBook(this.endPoint, Session["cs_rest_AccessToken"].ToString());
        }
        if ((string)Session["cs_rest_appState"] == "GetToken" && Request["Code"] != null)
        {
            this.oauth.authCode = Request["code"].ToString();
            try
            {
                if (oauth.GetAccessToken(OAuth.AccessTokenType.AuthorizationCode) == true)
                {
                    StoreAccessTokenToSession(oauth.accessTokenJson);
                    this.addressbook = new AddressBook(this.endPoint, this.accessToken);
                    Operation operation = (Operation)Session["cs_rest_ServiceRequest"];
                    switch (operation)
                    {
                        case Operation.CreateContactOperation:
                            showCreateContact = "true";
                            try
                            {
                                if (null == addressbook.createContact(Session["JSONstring"].ToString()))
                                {
                                    this.contact_error = this.addressbook.apiService.errorResponse;
                                }
                                else
                                {
                                    this.create_contact = new Success();
                                    this.create_contact.location = this.addressbook.apiService.apiResponse.location;
                                }
                            }
                            catch (Exception ex)
                            {
                                this.contact_error = ex.Message;
                            }
                            break;
                        case Operation.UpdateContactOperation:
                            showUpdateContact = "true";
                            try
                            {
                                if (!addressbook.updateContact(Session["contactid"].ToString(), Session["JSONstring"].ToString()))
                                {
                                    this.contact_error = this.addressbook.apiService.errorResponse;
                                }
                                else
                                {
                                    this.success_contact = new Success();
                                    this.success_contact.last_modified = this.addressbook.apiService.apiResponse.lastModified;
                                }
                            }
                            catch (Exception ex)
                            {
                                this.contact_error = ex.Message;
                            }
                            break;
                        case Operation.DeleteContactOperation:
                            showDeleteContact = "true";
                            try
                            {
                                if (!addressbook.deleteContact(Session["contactid"].ToString()))
                                {
                                    this.contact_error = this.addressbook.apiService.errorResponse;
                                }
                                else
                                {
                                    this.success_contact = new Success();
                                    this.success_contact.last_modified = this.addressbook.apiService.apiResponse.lastModified;
                                }
                            }
                            catch (Exception ex)
                            {
                                this.contact_error = ex.Message;
                            }
                            break;
                        case Operation.GetContactsOperation:
                            showGetContacts = "true";
                            try
                            {
                                this.qContactResult = addressbook.getContacts(Session["querystring"].ToString());
                                if (null == this.qContactResult)
                                {
                                    this.contact_error = this.addressbook.apiService.errorResponse;
                                }
                                //else
                                //{
                                    //try
                                    //{
                                        //this.qContactResult = serializer.Deserialize<QuickContactObj.RootObject>(addressbook.JSONstring);
                                      //  this.qContactResult = addressbook.getContacts(Session["querystring"].ToString());
                                    //}
                                    //catch (Exception ex)
                                    //{
                                     //   this.contact_error = ex.Message;
                                    //}
                                //}
                            }
                            catch (Exception ex)
                            {
                                this.contact_error = ex.Message;
                            }
                            break;
                        case Operation.UpdateMyInfoOperation:
                            showUpdateMyInfo = "true";
                            try
                            {
                                if (!addressbook.updateMyInfo(Session["JSONstring"].ToString()))
                                {
                                    this.myinfo_error = this.addressbook.apiService.errorResponse;
                                }
                                else
                                {
                                    this.update_myinfo = new Success();
                                    this.update_myinfo.last_modified = this.addressbook.apiService.apiResponse.lastModified;
                                }
                            }
                            catch (Exception ex)
                            {
                                this.myinfo_error = ex.Message;
                            }
                            break;
                        case Operation.GetMyInfoOperation:
                            showGetMyInfo = "true";
                            try
                            {
                                string result = addressbook.getMyInfo();
                                if (null == result)
                                {
                                    this.myinfo_error = this.addressbook.apiService.errorResponse;
                                }
                                else
                                {
                                    //try
                                    //{
                                    this.myInfoResult = serializer.Deserialize<ContactObj.RootObject>(result);
                                    //}

                                }
                            }
                            catch (Exception ex)
                            {
                                this.myinfo_error = ex.Message;
                            }
                            break;
                        case Operation.CreateGroupOperation:
                            showCreateGroup = "true";
                            try
                            {
                                if (null == addressbook.createGroup(Session["JSONstring"].ToString()))
                                {
                                    this.group_error = this.addressbook.apiService.errorResponse;
                                }
                                else
                                {
                                    this.create_group = new Success();
                                    this.create_group.location = this.addressbook.apiService.apiResponse.location;
                                }
                            }
                            catch (Exception ex)
                            {
                                this.group_error = ex.Message;
                            }
                            break;
                        case Operation.UpdateGroupOperation:
                            showUpdateGroup = "true";
                            try
                            {
                                if (!addressbook.updateGroup(Session["groupid"].ToString(), Session["JSONstring"].ToString()))
                                {
                                    this.group_error = this.addressbook.apiService.errorResponse;
                                }
                                else
                                {
                                    this.success_group = new Success();
                                    this.success_group.last_modified = this.addressbook.apiService.apiResponse.lastModified;
                                }
                            }
                            catch (Exception ex)
                            {
                                this.group_error = ex.Message;
                            }
                            break;
                        case Operation.DeleteGroupOperation:
                            showDeleteGroup = "true";
                            if (!addressbook.deleteGroup(Session["groupid"].ToString()))
                            {
                                this.group_error = this.addressbook.apiService.errorResponse;
                            }
                            else
                            {
                                this.success_group = new Success();
                                this.success_group.last_modified = this.addressbook.apiService.apiResponse.lastModified;
                            }
                            break;
                        case Operation.GetGroupsOperation:
                            showGetGroups = "true";
                            try
                            {
                                this.groupResult = addressbook.getGroups(Session["querystring"].ToString());
                                if (null == this.groupResult)
                                {
                                    this.group_error = this.addressbook.apiService.errorResponse;
                                }
                                //else
                                //{
                                //  try
                                //{
                                //this.groupResult = serializer.Deserialize<GroupObj.RootObject>(addressbook.JSONstring);

                                //}

                                //}
                            }
                            catch (Exception ex)
                            {
                                this.group_error = ex.Message;
                            }
                            break;
                        case Operation.GetGroupContactsOperation:
                            showGetGroupContacts = "true";
                            try
                            {
                                String result = addressbook.getGroupContacts(Session["groupid"].ToString());
                                if (null == result)
                                {
                                    this.manage_groups_error = this.addressbook.apiService.errorResponse;
                                }
                                //else
                                //{
                                //  try
                                //{

                                //}
                                this.contactIdResult = serializer.Deserialize<ContactIdObj.RootObject>(result);

                            }
                            catch (Exception ex)
                            {
                                this.manage_groups_error = ex.Message;
                            }
                            break;
                        case Operation.AddContctsToGroupOperation:
                            showAddContactsToGroup = "true";
                            try
                            {

                                if (!addressbook.addContactToGroup(Session["groupid"].ToString(), Session["contactids"].ToString()))
                                {
                                    this.manage_groups_error = this.addressbook.apiService.errorResponse;
                                }
                                else
                                {
                                    this.manage_groups = new Success();
                                    this.manage_groups.last_modified = this.addressbook.apiService.apiResponse.lastModified;
                                }
                            }
                            catch (Exception ex)
                            {
                                this.manage_groups_error = ex.Message;
                            }
                            break;
                        case Operation.RemoveContactsFromGroupOperation:
                            showRemoveContactsfrmGroup = "true";
                            try
                            {
                                if (!addressbook.removeContactsFromGroup(Session["groupid"].ToString(), Session["contactids"].ToString()))
                                {
                                    this.manage_groups_error = this.addressbook.apiService.errorResponse;
                                }
                                else
                                {
                                    this.manage_groups = new Success();
                                    this.manage_groups.last_modified = this.addressbook.apiService.apiResponse.lastModified;
                                }
                            }
                            catch (Exception ex)
                            {
                                this.manage_groups_error = ex.Message;
                            }
                            break;
                        case Operation.GetContactGroupsOperation:
                            showGetContactGroups = "true";
                            try
                            {
                                String result = addressbook.getContactGroups(Session["contactid"].ToString());
                                if (null == result)
                                {
                                    this.manage_groups_error = this.addressbook.apiService.errorResponse;
                                }
                                else
                                {
                                    //   try
                                    //  {
                                    //this.contactGroupResult = serializer.Deserialize<GroupObj.RootObject>(result);
                                    //}

                                    //}

                                    this.contactGroupResult = serializer.Deserialize<GroupObj.RootObject>(result);
                                }
                            }
                            catch (Exception ex)
                            {
                                this.manage_groups_error = ex.Message;
                            }

                            break;
                    }
                    ResetRequestSessionVariables(operation);
                }
                else
                {
                    if (oauth.getAuthCodeError != null)
                    {
                        this.oauth_error = "GetAuthCodeError: " + oauth.getAuthCodeError;
                    }
                    if (oauth.GetAccessTokenError != null)
                    {
                        this.oauth_error = "GetAccessTokenError: " + oauth.GetAccessTokenError;
                    }
                    this.ResetTokenSessionVariables();
                    return;
                }
            }
            catch (Exception ex)
            {
                this.oauth_error = "GetAuthCodeError: " + ex.Message;
                this.ResetTokenSessionVariables();
            }

        }
    }
    public void getContacts_Click(object sender, EventArgs e)
    {
        showGetContacts = "true";
        try
        {
            checkAccessToken(Operation.GetContactsOperation);
            var qs = searchVal.Text;
            this.qContactResult = addressbook.getContacts(qs);
            if (null == this.qContactResult)
            {
                this.contact_error = this.addressbook.apiService.errorResponse;
            }
            //else
            //{
            //try
            //{
            //  this.qContactResult = serializer.Deserialize<QuickContactObj.RootObject>(addressbook.JSONstring);
            //}

            //}
        }
        catch (Exception ex)
        {
            this.contact_error = ex.Message;
        }
    }