public async Task<JsonResult> Register(RegisterViewModel model)
        {
            try
            {
                if (!ModelState.IsValid
                    || !String.IsNullOrEmpty(model.submit) && model.submit != "submit")
                {
                    return Json(new { errors = Errors(ModelState) });
                }

                var api = GoPlayApi.Instance;
                string partnerId = ConfigurationManager.AppSettings["GTOKEN_PARTNER_UID"];

                //Calling to Gtoken api to create user and adding friend in Gtoken
                var gtokenResultRegister = await api.GTokenAPIAccount(new GtokenModelAccountAction
                {
                    enumAction = EGtokenAction.Register,
                    username = model.username,
                    pwd = model.password,
                    partnerId = partnerId,
                    email = model.email,
                    referral_code = model.referralID,
                    ip_address = WebIpHelper.GetClientIp(Request).ToString(),
                    country_code = model.countryCode,
                    country_name = model.countryName
                });


                if (!gtokenResultRegister.HasData)
                {
                    ModelState.AddModelError("CustomError", gtokenResultRegister.Error.ToErrorMessage());
                    return Json(new { errors = Errors(ModelState) });
                }

                //store Gtoken session for futher using ( used in edit profile)
                HttpContext.Session["gtoken_section"] = gtokenResultRegister.Data.session;

                //Create user and adding friend
                var user = api.UpdateProfile(gtokenResultRegister.Data).Data;

                CreateLoginOauth();
                var applicationUser = new ApplicationUser()
                {
                    Id = user.id,
                    Email = user.email,
                    UserName = user.username
                };

                await SignInAsync(applicationUser, false);

                //sending mail
                string rootUrl = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, String.Empty);
                var template = new WelcomeMessageTemplate
                {
                    main_index = rootUrl + Url.Action("index", "main"),
                    logoImg_src = rootUrl + ConstantValues.S_IMAGE_IN_EMAIL_LOGO_GOPLAY,
                    username = user.username,
                    login_link = rootUrl + Url.Action("login", "account"),
                    lastestGame_link = rootUrl + Url.Action("index", "game"),
                    facebook_link = ConfigurationManager.AppSettings["LINK_TO_FACEBOOK_GO_PLAY"],
                    twitter_link = ConfigurationManager.AppSettings["LINK_TO_TWITTER_GO_PLAY"],
                    KennethImg_src = rootUrl + ConstantValues.S_IMAGE_IN_EMAIL_KENNETH,
                    home_link = rootUrl
                };
                await EmailHelper.SendMailWelcome(user.email, template);
                //end sending mail
                return Json(new { success = true, redirect = Url.Action("profile", "account") });
            }
            catch
            {
                ModelState.AddModelError("CustomError", Messages.MISSING_FIELDS);
                return Json(new { errors = Errors(ModelState) });
            }
        }
        public ActionResult Register(string referralID = null)
        {
            var api = GoPlayApi.Instance;
            if (!api.IsUserExist(referralID))
            {
                referralID = string.Empty;
            }
            var countryCode = string.Empty;
            var countryName = string.Empty;

            IPAddress ip = WebIpHelper.GetClientIp(Request);
            if (ip.Equals(IPAddress.Parse("127.0.0.1")))
            {
                countryCode = "SG";
                countryName = "Singapore";
            }
            else
            {
                ip.GetCountryCode(c => countryCode = c, n => countryName = n);
            }

            string facebook_email = string.Empty;
            if (HttpContext.Session["facebook_email"] != null)
            {
                facebook_email = HttpContext.Session["facebook_email"].ToString();
            }

            var model = new RegisterViewModel
            {
                referralID = referralID,
                countryCode = countryCode,
                countryName = countryName,
                facebook_email = facebook_email
            };
            return View(model);
        }