public static AjaxResult AddPhoto()
 {
     int gId = int.Parse(HttpContext.Current.Request.Form["galleryId"]);
     AjaxResult ar = new AjaxResult();
     ar.Message = string.Empty;
     ar.ReturnCode = 0;
     ar.Json = Utility.AddPhoto(gId).ToString();
     return ar;
 }
        public static AjaxResult AddAccount()
        {
            HttpRequest req = HttpContext.Current.Request;
            string firstName = req.Form["firstName"];
            string lastName = req.Form["lastName"];
            string email = req.Form["email"];

            AjaxResult ar = new AjaxResult { ReturnCode = 0, Message = string.Empty, Json = "{}" };
            if (Utility.CheckAccountExist(email))
            {
                ar.ReturnCode = -1;
                ar.Message = "The current email has been regeistered before!";
                return ar;
            }

            Utility.AddAccount(firstName, lastName, email);
            return ar;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //string FILE_ID = "fileToUpload";
            string FILE_ID = Request.QueryString["name"];
            string photoType = Request.QueryString["ptype"];
            int photoId = int.Parse(Request.QueryString["pid"]);

            AjaxResult ar = new AjaxResult { ReturnCode = 0, Message = string.Empty };
            if (Request.Files[FILE_ID] == null || Request.Files[FILE_ID].ContentLength == 0)
            {
                ar.Message = "No file found";
                ar.ReturnCode = 1;
            }
            else
            {
                HttpPostedFile file = Request.Files[FILE_ID];
                string fileName = Guid.NewGuid().ToString() + file.FileName.Substring(file.FileName.LastIndexOf("."));
                string folder = Server.MapPath(TARGET_FOLDER);
                string combined = string.Empty;
                if (folder.EndsWith("\\"))
                {
                    combined = folder + fileName;
                }
                else
                {
                    combined = folder + "\\" + fileName;
                }
                try
                {
                    //if (folder.EndsWith("\\"))
                    //{
                    //    file.SaveAs(folder + fileName);
                    //}
                    //else
                    //{
                    //    file.SaveAs(folder + "\\" + fileName);
                    //}

                    file.SaveAs(combined);

                    ar.Message = fileName;

                    // update db
                    Utility.UpdatePhotoFile(photoId, fileName, photoType);

                    if (photoType.Equals("s2"))
                    {
                        Utility.UpdatePhotoSize(combined);

                        System.Drawing.Size photoSize = Utility.GetPhotoSizeById(photoId);
                        ar.Json = "{" + string.Format("\"width\":{0},\"height\":{1}", photoSize.Width, photoSize.Height) + "}";
                    }

                }
                catch (System.Exception ex)
                {
                    ar.ReturnCode = 2;
                    ar.Message = ex.Message + " and the file name is :" + combined.Replace("\\", "\\\\");
                    ar.Message.Replace("\\", "?");
                }
            }
            //Response.ContentType = "application/x-javascript;charset=UTF-8";
            Response.Clear();
            WebMethods.OutputResult(ar);
            Response.End();
        }
        public static AjaxResult DeleteClient()
        {
            int clientId = int.Parse(HttpContext.Current.Request.Form["clientId"]);

            string sql = "DELETE FROM [TAccount] WHERE [ID] = " + clientId;
            SqlCommand com = Utility.GetCommand(sql);
            com.ExecuteNonQuery();
            com.Connection.Close();
            com.Dispose();

            AjaxResult ar = new AjaxResult { ReturnCode = 0, Message = string.Empty, Json = "{}" };
            return ar;
        }
        public static AjaxResult UpdateGallery()
        {
            AjaxResult ar = new AjaxResult { ReturnCode = 0, Message = string.Empty, Json = "{}" };
            HttpRequest req = HttpContext.Current.Request;
            try
            {
                int galleryId = int.Parse(req.Form["galleryId"]);
                int show = int.Parse(req.Form["Show"]);
                string galleryName = req.Form["galleryName"];
                string comments = req.Form["galleryDescription"];

                Utility.UpdateGallery(galleryId, galleryName, show, comments);
            }
            catch (System.Exception ex)
            {
                ar.ReturnCode = 1;
                ar.Message = ex.Message;
            }
            return ar;
        }
        public static AjaxResult UpdatePriceSettings()
        {
            AjaxResult ar = new AjaxResult { ReturnCode = 0, Message = string.Empty, Json = "{}" };

            Utility.UpdatePriceSettigns(HttpContext.Current.Request.Form["priceSettings"]);

            return ar;
        }
        public static AjaxResult RemovePassword()
        {
            AjaxResult ar = new AjaxResult { ReturnCode = 0, Message = string.Empty, Json = "{}" };

            int id = int.Parse(HttpContext.Current.Request.Form["id"]);

            /*
            SqlCommand command = Utility.GetCommand(string.Format("DELETE FROM [TAccount] WHERE [ID] = {0}", id));
            command.ExecuteNonQuery();
            command.Connection.Close();
            command.Dispose();
            */

            Utility.SetClientPassword(id, string.Empty, -1);

            return ar;
        }
        public static AjaxResult SetClientPassword()
        {
            HttpRequest req = HttpContext.Current.Request;
            string password = req.Form["password"];
            int id = int.Parse(req.Form["id"]);
            int galleryId = int.Parse(req.Form["galleryId"]);

            AjaxResult ar = new AjaxResult { ReturnCode = 0, Message = string.Empty, Json = "{}" };

            if (Utility.CheckPasswordExist(password))
            {
                ar.ReturnCode = -1;
                ar.Message = "The password has been used. Please select another onee!";
                return ar;
            }
            Utility.SetClientPassword(id, password, galleryId);
            return ar;
        }
 public static void OutputResult(AjaxResult ar)
 {
     if (string.IsNullOrEmpty(ar.Json))
     {
         ar.Json = "{}";
     }
     HttpContext.Current.Response.Write("{" + string.Format("\"msgId\":{0},\"message\" :\"{1}\",\"data\":{2}",
                   ar.ReturnCode,
                   ar.Message.Replace("\"", "\\\""),
                   ar.Json) + "}");
 }
        public static AjaxResult Process(bool needAuthentication)
        {
            HttpResponse response = System.Web.HttpContext.Current.Response;
            response.ContentType = "application/x-javascript;charset=UTF-8";
            response.Clear();
            string methodName = HttpContext.Current.Request.QueryString["method"];

            AjaxResult ar = new AjaxResult { ReturnCode = 0, Message = string.Empty };
            if (string.IsNullOrEmpty(methodName))
            {
                ar.ReturnCode = -1001;
                ar.Message = "No method found!";
            }
            else
            {
                System.Reflection.MethodInfo method = typeof(WebMethods).GetMethod(methodName);
                try
                {
                    ar = (AjaxResult)method.Invoke(null, null);
                }
                catch (System.Exception ex)
                {
                    ar.ReturnCode = -1;
                    ar.Message = ex.Message;
                }
            }
            //response.Write("{" + string.Format("\"msgId\":{0},\"message\" :\"{1}\"",
            //       ar.ReturnCode,
            //       ar.Message.Replace("\"", "\\\"")) + "}");
            //ar.Json = jsonData;
            OutputResult(ar);

            //response.Write("{\"msgId\":0, \"message\":\"hi\"}");
            //response.Write("{msgId:0}");
            response.End();
            return ar;
        }
        public static AjaxResult GetPhotosFromClient()
        {
            AjaxResult ar = new AjaxResult { ReturnCode = 0, Message = string.Empty, Json = "[]" };
            HttpRequest req = HttpContext.Current.Request;

            string password = req.Form["password"];
            if (string.IsNullOrEmpty(password))
            {
                return ar;
            }

            string sql = "SELECT TOP 1 [GalleryId] FROM [TAccount] WHERE Password = '******'", "''") + "';";
            DataTable t = Utility.GetTable(sql);
            if (t.Rows.Count == 0)
                return ar;

            int galleryId = int.Parse(t.Rows[0][0].ToString());
            //req.Form.Add("galleryId", galleryId.ToString() );
            //req.Form.Set("galleryId", galleryId.ToString());

            ar.Json = JsonlizePhotoList(Utility.GetPhotos(galleryId));
            ar.Message = Utility.GetGalleryDescription(galleryId);
            if (ar.Message == null)
            {
                ar.Message = string.Empty;
            }
            return ar;
        }
        public static AjaxResult GetPhotos()
        {
            AjaxResult ar = new AjaxResult { ReturnCode = 0, Message = string.Empty };
            HttpRequest req = HttpContext.Current.Request;

            if (string.IsNullOrEmpty(req["galleryId"]))
            //if (string.IsNullOrEmpty(req.Form["galleryId"]))
            {
                ar.ReturnCode = -1;
                ar.Message = "NO Gallery Found!";
                OutputResult(ar);
            }

            //int gId = int.Parse(req.Form["galleryId"]);
            int gId = int.Parse(req["galleryId"]);
            DataTable t = Utility.GetPhotos(gId);
            /*
            System.Text.StringBuilder json = new System.Text.StringBuilder();
            json.Append("[");
            if (t.Rows.Count > 0)
            {
                int count = 0;
                foreach (DataRow row in t.Rows)
                {
                    if (count > 0)
                    {
                        json.Append(",");
                    }
                    json.Append("{");

                    // id
                    json.AppendFormat("\"id\":{0}", row["ID"].ToString());
                    json.AppendFormat(",\"s1\":\"{0}\"", row["ThumbName"]);
                    json.AppendFormat(",\"s2\":\"{0}\"", row["PhotoName"]);
                    json.AppendFormat(",\"order\":{0}", row["OrderIndex"].ToString());
                    json.AppendFormat(",\"width\":{0}", row["Width"].ToString());
                    json.AppendFormat(",\"height\":{0}", row["Height"].ToString());
                    json.Append("}");
                    ++count;
                }
            }
            json.Append("]");

            //ar.Message = json.ToString();
            */
            ar.Json = JsonlizePhotoList(t);
            //HttpContext.Current.Response.Write("{\"msgId:0,\"message\":" + json.ToString() + "}");
            return ar;
        }
        public static AjaxResult GetGalleries()
        {
            //DataTable t = Utility.GetGalleries();
            //DataRow[] rows = t.Select("Show=1");
            //for (int i = 0; i < rows.Count; ++i)
            //{
            //}

            AjaxResult ar = new AjaxResult { ReturnCode = 0, Message = string.Empty, Json = Utility.GetGalleriesJson() };
            return ar;
        }
        public static AjaxResult GetClients()
        {
            DataTable t = Utility.GetTable("SELECT [ID],[FirstName],[LastName],[Email],[Password],[GalleryId] FROM [TAccount]");
            AjaxResult ar = new AjaxResult();
            ar.ReturnCode = 0;
            ar.Message = string.Empty;

            if (t.Rows.Count == 0)
            {
                ar.Json = "{}";
                return ar;
            }

            System.Text.StringBuilder json = new System.Text.StringBuilder();
            json.Append("[");
            int count = 0;
            foreach (DataRow row in t.Rows)
            {
                if (count > 0)
                {
                    json.Append(",");
                }

                //string firstName =   row["FirstName"] == DBNull.Value ? string.Empty : row["FirstName"].ToString().Replace("\"", "\\\"");
                //string lastName = row["LastName

                json.Append("{");
                json.AppendFormat("\"id\":{0}", row["ID"]);
                json.AppendFormat(",\"firstName\":\"{0}\"", Utility.JsonCellData(row["FirstName"]));
                json.AppendFormat(",\"lastName\":\"{0}\"", Utility.JsonCellData(row["LastName"]));
                json.AppendFormat(",\"email\":\"{0}\"", Utility.JsonCellData(row["Email"]));
                json.AppendFormat(",\"password\":\"{0}\"", Utility.JsonCellData(row["Password"]));
                json.AppendFormat(",\"galleryId\":{0}", row["GalleryId"]);
                json.Append("}");

                ++count;
            }
            json.Append("]");

            ar.Json = json.ToString();
            return ar;
        }
        public static AjaxResult DeletePhoto()
        {
            AjaxResult ar = new AjaxResult { ReturnCode = 0, Message = string.Empty, Json = "{}" };
            //DataTable = Utility.GetTable(

            try
            {
                int photoId = int.Parse(HttpContext.Current.Request.Form["photoId"]);
                ar.ReturnCode = Utility.DeletePhoto(photoId);
            }
            catch (System.Exception ex)
            {
                ar.Message = ex.Message;
            }
            return ar;
        }