<repo_name>tiamahone/TermProject1<file_sep>/CloudService/CloudSVC.asmx.cs
﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using Utilities;

namespace CloudService
{
    /// <summary>
    /// Summary description for CloudSVC
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class CloudSVC : System.Web.Services.WebService
    {

        [WebMethod]
        public int attemptLogin(string[] loginInfo)
        {
            int response;
            string email = loginInfo[0];
            string password = loginInfo[1];
            DBConnect objDB;
            SqlCommand objCommand;
            DataSet myDs;


            objDB = new DBConnect();
            objCommand = new SqlCommand();
            objCommand.CommandType = CommandType.StoredProcedure;
            objCommand.CommandText = "GetCloudUserInfo";
            objCommand.Parameters.AddWithValue("@email", email);
            myDs = objDB.GetDataSetUsingCmdObj(objCommand);

            if (myDs.Tables[0].Rows.Count != 0)
            {
                if (password != objDB.GetField("Password", 0).ToString())
                {
                    response = 0;
                }
                else
                {
                    response = 1;
                }
            }
            else
            {
                objDB = new DBConnect();
                objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "GetCloudAdminInfo";
                objCommand.Parameters.AddWithValue("@email", email);
                myDs = objDB.GetDataSetUsingCmdObj(objCommand);

                if (myDs.Tables[0].Rows.Count != 0)
                {
                    if (password != objDB.GetField("Password", 0).ToString())
                    {
                        response = 0;
                    }
                    else
                    {
                        response = 2;
                    }
                }
                else
                {
                    response = -1;
                }
            }
            return response;
        }

        [WebMethod]
        public int addUser(string[] userInfo)
        {
            int response;
            //if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            //{
            string name = userInfo[0];
            string email = userInfo[1];
            string password = userInfo[2];
            string phone = userInfo[3];
            DBConnect objDB;
            SqlCommand objCommand;
            DataSet myDs;

            objDB = new DBConnect();
            objCommand = new SqlCommand();
            objCommand.CommandType = CommandType.StoredProcedure;
            objCommand.CommandText = "GetCloudUserInfo";
            objCommand.Parameters.AddWithValue("@email", email);
            myDs = objDB.GetDataSetUsingCmdObj(objCommand);

            if (myDs.Tables[0].Rows.Count != 0)
            {
                response = -1;
            }
            else
            {
                objDB = new DBConnect();
                objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "AddCloudUser";
                objCommand.Parameters.AddWithValue("@name", name);
                objCommand.Parameters.AddWithValue("@email", email);
                objCommand.Parameters.AddWithValue("@password", <PASSWORD>);
                objCommand.Parameters.AddWithValue("@phone", phone);
                //myDs = objDB.GetDataSetUsingCmdObj(objCommand);
                objDB.DoUpdateUsingCmdObj(objCommand);
                response = 0;
            }
            //} 
            // else
            //{
            //    response = -1;
            //}
            return response;
        }

        [WebMethod]
        public int addAdmin(string[] loginInfo, string[] userInfo)
        {
            int response;
            if (attemptLogin(loginInfo) == 2)
            {
                string name = userInfo[0];
                string email = userInfo[1];
                string password = userInfo[2];
                string phone = userInfo[3];
                DBConnect objDB;
                SqlCommand objCommand;
                DataSet myDs;

                objDB = new DBConnect();
                objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "GetCloudAdminInfo";
                objCommand.Parameters.AddWithValue("@email", email);
                myDs = objDB.GetDataSetUsingCmdObj(objCommand);

                if (myDs.Tables[0].Rows.Count != 0)
                {
                    response = -1;
                }
                else
                {
                    objDB = new DBConnect();
                    objCommand = new SqlCommand();
                    objCommand.CommandType = CommandType.StoredProcedure;
                    objCommand.CommandText = "AddCloudAdmin";
                    objCommand.Parameters.AddWithValue("@name", name);
                    objCommand.Parameters.AddWithValue("@email", email);
                    objCommand.Parameters.AddWithValue("@password", <PASSWORD>);
                    objCommand.Parameters.AddWithValue("@phone", phone);
                    //myDs = objDB.GetDataSetUsingCmdObj(objCommand);
                    objDB.DoUpdateUsingCmdObj(objCommand);
                    response = 0;
                }
            }
            else
            {
                response = -1;
            }
            return response;
        }

        [WebMethod]
        public int addFile(string[] loginInfo, string[] fileInfo, byte[] fileData)
        {
            int response = 0;
            string transactionType = "Upload";
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                string fileEmail = fileInfo[0];
                string fileName = fileInfo[1];
                string fileType = fileInfo[2];
                double fileSize = Convert.ToDouble(fileInfo[3]);
                double freeStorage = getUserFreeStorage(loginInfo, fileInfo);

                DBConnect objDB;
                SqlCommand objCommand;

                //Checks to see if file already exists
                //DataSet myDs = checkForFile(loginInfo, fileInfo);
                double oldFileSize = checkForFile(loginInfo, fileInfo);
                //if (myDs.Tables[0].Rows.Count != 0
                if (oldFileSize != 0)
                {
                    string[] file = new string[3];
                    file[0] = fileName;
                    //file[1] = fileSize.ToString();
                    file[1] = oldFileSize.ToString();
                    deleteFile(loginInfo, file);
                    transactionType = "Update";
                    response = 1;

                }
                //Checks to see if enough storage is available
                if (fileSize > freeStorage)
                {
                    response = -1;
                }
                else
                {
                    //Adds file to DB
                    objDB = new DBConnect();
                    objCommand = new SqlCommand();
                    objCommand.CommandType = CommandType.StoredProcedure;
                    objCommand.CommandText = "AddCloudFile";
                    objCommand.Parameters.AddWithValue("@email", fileEmail);
                    objCommand.Parameters.AddWithValue("@fileName", fileName);
                    objCommand.Parameters.AddWithValue("@fileType", fileType);
                    objCommand.Parameters.AddWithValue("@fileSize", fileSize);
                    objCommand.Parameters.AddWithValue("@fileData", fileData);
                    objDB.DoUpdateUsingCmdObj(objCommand);

                    //Updates free storage number in CloudUsersDB
                    double newFreeStorage = freeStorage - fileSize;
                    objDB = new DBConnect();
                    objCommand = new SqlCommand();
                    objCommand.CommandType = CommandType.StoredProcedure;
                    objCommand.CommandText = "UpdateUserFreeStorage";
                    objCommand.Parameters.AddWithValue("@email", fileEmail);
                    objCommand.Parameters.AddWithValue("@newStorage", newFreeStorage);
                    objDB.DoUpdateUsingCmdObj(objCommand);

                    //Adds transaction record to CloudUserTransactions table
                    objDB = new DBConnect();
                    objCommand = new SqlCommand();
                    objCommand.CommandType = CommandType.StoredProcedure;
                    objCommand.CommandText = "AddCloudTransaction";
                    objCommand.Parameters.AddWithValue("@email", fileEmail);
                    objCommand.Parameters.AddWithValue("@type", transactionType);
                    objCommand.Parameters.AddWithValue("@fileName", fileName);
                    objCommand.Parameters.AddWithValue("@time", DateTime.Now);
                    objDB.DoUpdateUsingCmdObj(objCommand);
                }
            }
            else
            {
                response = -1;
            }
            return response;
        }

        [WebMethod]
        public double checkForFile(string[] loginInfo, string[] userInfo)
        {
            double size = 0;
            DataSet myDS = null;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                SqlCommand objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "checkForFile";
                objCommand.Parameters.AddWithValue("@email", userInfo[0]);
                objCommand.Parameters.AddWithValue("@fileName", userInfo[1]);
                DBConnect objDB = new DBConnect();
                myDS = objDB.GetDataSetUsingCmdObj(objCommand);
                if (myDS.Tables[0].Rows.Count != 0)
                {
                    size = Convert.ToDouble(objDB.GetField("File Size", 0));
                }
            }
            //return myDS;
            return size;
        }

        [WebMethod]
        public DataSet getFilesByUser(string[] loginInfo, string[] userInfo)
        {
            DataSet myDS = null;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                SqlCommand objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "GetCloudFilesByUser";
                objCommand.Parameters.AddWithValue("@email", userInfo[0]);
                DBConnect objDB = new DBConnect();
                myDS = objDB.GetDataSetUsingCmdObj(objCommand);        
            }
            return myDS;

        }

        [WebMethod]
        public double getUserFreeStorage(string[] loginInfo, string[] userInfo)
        {
            double freeStorage = 0;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                SqlCommand objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "GetCloudUserFreeStorage";
                objCommand.Parameters.AddWithValue("@email", userInfo[0]);
                DBConnect objDB = new DBConnect();
                DataSet myDS = objDB.GetDataSetUsingCmdObj(objCommand);
                freeStorage = Convert.ToDouble(objDB.GetField("Free Storage", 0));
            }
            return freeStorage;
        }

        [WebMethod]
        public double getUserTotalStorage(string[] loginInfo, string[] userInfo)
        {
            double totalStorage = 0;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                SqlCommand objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "GetCloudUserTotalStorage";
                objCommand.Parameters.AddWithValue("@email", userInfo[0]);
                DBConnect objDB = new DBConnect();
                DataSet myDS = objDB.GetDataSetUsingCmdObj(objCommand);
                totalStorage = Convert.ToDouble(objDB.GetField("Total Storage", 0));
            }
            return totalStorage;
        }

        [WebMethod]
        public DataSet getCloudUsers(string[] loginInfo)
        {
            DataSet myDS = null;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                SqlCommand objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "GetCloudUsers";
                DBConnect objDB = new DBConnect();
                myDS = objDB.GetDataSetUsingCmdObj(objCommand);
            }
            return myDS;
        }

        [WebMethod]
        public DataSet getCloudUsersInfo(string[] loginInfo)
        {
            DataSet myDS = null;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                SqlCommand objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "GetCloudUsersInfo";
                DBConnect objDB = new DBConnect();
                myDS = objDB.GetDataSetUsingCmdObj(objCommand);
            }
            return myDS;
        }

        [WebMethod]
        public DataSet getTransactions(string[] loginInfo, string[] userInfo)
        {
            DataSet myDS = null;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                SqlCommand objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "GetCloudTransactions";
                objCommand.Parameters.AddWithValue("@email", userInfo[0]);
                if (userInfo[1] == "All")
                {
                    objCommand.Parameters.AddWithValue("@timePeriod", "1/01/1900 12:00:01 AM");
                }
                else if (userInfo[1] == "Past Day")
                {
                    DateTime timePeriod = (Convert.ToDateTime(userInfo[1])).AddDays(-1);
                    objCommand.Parameters.AddWithValue("@timePeriod", userInfo[1]);
                }
                else if (userInfo[1] == "Past Week")
                {
                    DateTime timePeriod = (Convert.ToDateTime(userInfo[1])).AddDays(-7);
                    objCommand.Parameters.AddWithValue("@timePeriod", userInfo[1]);
                }
                else if (userInfo[1] == "Past Month")
                {
                    DateTime timePeriod = (Convert.ToDateTime(userInfo[1])).AddDays(-30);
                    objCommand.Parameters.AddWithValue("@timePeriod", userInfo[1]);
                }

                DBConnect objDB = new DBConnect();
                myDS = objDB.GetDataSetUsingCmdObj(objCommand);
            }
            return myDS;
        }

        [WebMethod]
        public int adminUpdateUser(string[] loginInfo, string[] userInfo)
        {
            int response = -1;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                double freeStorage = getUserFreeStorage(loginInfo, userInfo);
                double oldTotalStorage = getUserTotalStorage(loginInfo, userInfo);
                double difference = (Convert.ToDouble(userInfo[3])) - oldTotalStorage;
                freeStorage += difference;

                SqlCommand objCommand = new SqlCommand();
                DBConnect objDB = new DBConnect();
                objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "UpdateCloudUser";
                objCommand.Parameters.AddWithValue("@email", userInfo[0]);
                objCommand.Parameters.AddWithValue("@password", <PASSWORD>[1]);
                objCommand.Parameters.AddWithValue("@phone", userInfo[2]);
                objCommand.Parameters.AddWithValue("@totalStorage", userInfo[3]);
                objCommand.Parameters.AddWithValue("@freeStorage", freeStorage);
                objDB.DoUpdateUsingCmdObj(objCommand);
                response = 0;
            }
            return response;
        }

        [WebMethod]
        public int deleteUser(string[] loginInfo, string[] userInfo)
        {
            int response = -1;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                SqlCommand objCommand = new SqlCommand();
                DBConnect objDB = new DBConnect();
                objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "DeleteCloudUser";
                objCommand.Parameters.AddWithValue("@email", userInfo[0]);
                objDB.DoUpdateUsingCmdObj(objCommand);
                response = 0;
            }
            return response;
        }

        [WebMethod]
        public DataSet getSingleUserInfo(string[] loginInfo, string[] userInfo)
        {
            DataSet myDS = null;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                SqlCommand objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "GetSingleCloudUser";
                objCommand.Parameters.AddWithValue("@email", userInfo[0]);
                DBConnect objDB = new DBConnect();
                myDS = objDB.GetDataSetUsingCmdObj(objCommand);
            }
            return myDS;
        }

        [WebMethod]
        public int userUpdateUser(string[] loginInfo, string[] userInfo)
        {
            int response = -1;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                SqlCommand objCommand = new SqlCommand();
                DBConnect objDB = new DBConnect();
                objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "UpdateCloudUserPersonal";
                objCommand.Parameters.AddWithValue("@id", Convert.ToInt32(userInfo[0]));
                objCommand.Parameters.AddWithValue("@name", userInfo[1]);
                objCommand.Parameters.AddWithValue("@email", userInfo[2]);
                objCommand.Parameters.AddWithValue("@password", userInfo[3]);
                objCommand.Parameters.AddWithValue("@phone", userInfo[4]);
                objDB.DoUpdateUsingCmdObj(objCommand);
                response = 0;
            }

            return response;
        }

        [WebMethod]
        public int deleteFile(string[] loginInfo, string[] userInfo)
        {
            int response = -1;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                SqlCommand objCommand = new SqlCommand();
                DBConnect objDB = new DBConnect();

                //GET FILE
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "GetFile";
                objCommand.Parameters.AddWithValue("@email", loginInfo[0]);
                objCommand.Parameters.AddWithValue("@fileName", userInfo[0]);
                DataSet myDS = objDB.GetDataSetUsingCmdObj(objCommand);

                string email = objDB.GetField("Email", 0).ToString();
                string fileName = objDB.GetField("File Name", 0).ToString();
                string fileType = objDB.GetField("File Type", 0).ToString();
                double fileSizeOld = Convert.ToDouble(objDB.GetField("File Size", 0));
                byte[] fileData = (byte[])objDB.GetField("File Data", 0);
                DateTime timeStamp = DateTime.Now;
                //byte[] image = (byte[])objDB.GetField("File Image", 0);

                //Put file in trash
                objDB = new DBConnect();
                objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "AddToTrash";
                objCommand.Parameters.AddWithValue("@email", email);
                objCommand.Parameters.AddWithValue("@fileName", fileName);
                objCommand.Parameters.AddWithValue("@fileType", fileType);
                objCommand.Parameters.AddWithValue("@fileSize", fileSizeOld);
                objCommand.Parameters.AddWithValue("@fileData", fileData);
                objCommand.Parameters.AddWithValue("@timeStamp", timeStamp);
                //objCommand.Parameters.AddWithValue("@image", image);
                objDB.DoUpdateUsingCmdObj(objCommand);


                //Delete File
                objDB = new DBConnect();
                objCommand = new SqlCommand();

                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "DeleteFile";
                objCommand.Parameters.AddWithValue("@email", loginInfo[0]);
                objCommand.Parameters.AddWithValue("@fileName", userInfo[0]);
                objDB.DoUpdateUsingCmdObj(objCommand);

                double oldFreeSpace = Convert.ToDouble(getUserFreeStorage(loginInfo, loginInfo));
                double fileSize = Convert.ToDouble(userInfo[1]);
                double newFreeSpace = oldFreeSpace + fileSize;


                objDB = new DBConnect();
                objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "UpdateFreeSpace";
                objCommand.Parameters.AddWithValue("@email", loginInfo[0]);
                objCommand.Parameters.AddWithValue("@freeStorage", newFreeSpace);
                objDB.DoUpdateUsingCmdObj(objCommand);

                // add transaction
                if (userInfo[2] == "delete")
                {
                    objDB = new DBConnect();
                    objCommand = new SqlCommand();
                    objCommand.CommandType = CommandType.StoredProcedure;
                    objCommand.CommandText = "AddCloudTransaction";
                    objCommand.Parameters.AddWithValue("@email", email);
                    objCommand.Parameters.AddWithValue("@type", "Delete");
                    objCommand.Parameters.AddWithValue("@fileName", fileName);
                    objCommand.Parameters.AddWithValue("@time", DateTime.Now);
                    objDB.DoUpdateUsingCmdObj(objCommand);
                }

                response = 0;
            }
            return response;
        }

        [WebMethod]
        public DataSet getUserTrash(string[] loginInfo, string[] userInfo)
        {
            DataSet myDS = null;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                SqlCommand objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "GetTrashByUser ";
                objCommand.Parameters.AddWithValue("@email", userInfo[0]);
                DBConnect objDB = new DBConnect();
                myDS = objDB.GetDataSetUsingCmdObj(objCommand);
            }
            return myDS;
        }

        [WebMethod]
        public int changeStoragePlan(string[] loginInfo, string[] userInfo)
        {
            int response = 0;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                double freeStorage = getUserFreeStorage(loginInfo, userInfo);
                double totalStorage = getUserTotalStorage(loginInfo, userInfo);
                double newTotalStorage = Convert.ToDouble(userInfo[1]);
                double newFreeStorage = freeStorage + (newTotalStorage - totalStorage);
                DBConnect objDB = new DBConnect();
                SqlCommand objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "ChangeStoragePlan";
                objCommand.Parameters.AddWithValue("@email", userInfo[0]);
                objCommand.Parameters.AddWithValue("@freeStorage", newFreeStorage);
                objCommand.Parameters.AddWithValue("@totalStorage", newTotalStorage);
                objDB.DoUpdateUsingCmdObj(objCommand);
            }
            return response;
        }

        [WebMethod]
        public int recoverFile(string[] loginInfo, string[] userInfo)
        {
            int response = 0;
            if (attemptLogin(loginInfo) == 1 || attemptLogin(loginInfo) == 2)
            {
                DBConnect objDB = new DBConnect();
                SqlCommand objCommand = new SqlCommand();
                //GET FILE
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "GetFileFromTrash";
                objCommand.Parameters.AddWithValue("@id", userInfo[1]);
                DataSet myDS = objDB.GetDataSetUsingCmdObj(objCommand);
                string email = objDB.GetField("Email", 0).ToString();
                string fileName = objDB.GetField("File Name", 0).ToString();
                fileName += "-R";
                string fileType = objDB.GetField("File Type", 0).ToString();
                double fileSizeOld = Convert.ToDouble(objDB.GetField("File Size", 0));
                byte[] fileData = (byte[])objDB.GetField("File Data", 0);
                DateTime timeStamp = DateTime.Now;

                //Update Free Space
                double oldFreeSpace = Convert.ToDouble(getUserFreeStorage(loginInfo, loginInfo));
                double newFreeSpace = oldFreeSpace - fileSizeOld;
                if (oldFreeSpace < fileSizeOld)
                {
                    return -1;
                }

                objDB = new DBConnect();
                objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "UpdateFreeSpace";
                objCommand.Parameters.AddWithValue("@email", loginInfo[0]);
                objCommand.Parameters.AddWithValue("@freeStorage", newFreeSpace);
                objDB.DoUpdateUsingCmdObj(objCommand);

                //Put file back
                objDB = new DBConnect();
                objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "AddCloudFile";
                objCommand.Parameters.AddWithValue("@email", email);
                objCommand.Parameters.AddWithValue("@fileName", fileName);
                objCommand.Parameters.AddWithValue("@fileType", fileType);
                objCommand.Parameters.AddWithValue("@fileSize", fileSizeOld);
                objCommand.Parameters.AddWithValue("@fileData", fileData);
                objDB.DoUpdateUsingCmdObj(objCommand);


                //Delete File From Trash
                objDB = new DBConnect();
                objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "DeleteFileFromTrash";
                objCommand.Parameters.AddWithValue("@id", userInfo[1]);
                objDB.DoUpdateUsingCmdObj(objCommand);

                //Add transaction
                objDB = new DBConnect();
                objCommand = new SqlCommand();
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.CommandText = "AddCloudTransaction";
                objCommand.Parameters.AddWithValue("@email", email);
                objCommand.Parameters.AddWithValue("@type", "Recover");
                objCommand.Parameters.AddWithValue("@fileName", fileName);
                objCommand.Parameters.AddWithValue("@time", DateTime.Now);
                objDB.DoUpdateUsingCmdObj(objCommand);

            }
            return response;
        }
        [WebMethod]
        public DataSet getQuestions()
        {
            DBConnect objDB = new DBConnect();
            SqlCommand objCommand = new SqlCommand();
            objCommand.CommandType = CommandType.StoredProcedure;
            objCommand.CommandText = "GetCloudQuestions";
            DataSet myDS = objDB.GetDataSetUsingCmdObj(objCommand);
            return myDS;
        }

        [WebMethod]
        public void askQuestion(string[] loginInfo, string question)
        {
            DBConnect objDB = new DBConnect();
            SqlCommand objCommand = new SqlCommand();
            objCommand.CommandType = CommandType.StoredProcedure;
            objCommand.CommandText = "AskCloudQuestion";
            objCommand.Parameters.AddWithValue("@email", loginInfo[0]);
            objCommand.Parameters.AddWithValue("@question", question);
            objDB.DoUpdateUsingCmdObj(objCommand);
        }
    }
}
<file_sep>/TermProject/StorageOptions.aspx.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Class_Library;

namespace TermProject
{
    public partial class StorageOptions : System.Web.UI.Page
    {
        double storage;
        
        string[] loginInfo = new string[2];
        string email = "";
        int index;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Login"] != null)
            {
                lblDisplayText.Text = Session["User"].ToString() + "'s Storage Options";
                loginInfo[0] = Session["User"].ToString();
                loginInfo[1] = Session["Password"].ToString();
                email = Session["User"].ToString();
                if(Session["Index"] != null)
                {
                    index = Convert.ToInt32(Session["Index"]);
                }
                // if (!IsPostBack)
                // {
                stateLoggedIn();
                //}
            }
            else
            {
                lblDisplayText.Text = "Error: Must Login in";
            }
            
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Main.aspx");
        }

        public void stateLoggedIn()
        {
            rdoStorageOptions.Visible = true;
            lblDisplayText2.Text = "";
            if (!IsPostBack)
            {
                storage = Convert.ToDouble(Functions.getUserTotalSpace(loginInfo, email));
                storage = storage / 1000000;
                if (storage == 100)
                {
                    rdoStorageOptions.SelectedIndex = 0; ;
                    rdoStorageOptions.SelectedItem.Text = "100 MB (Free) -- Current Plan";
                    index = 0;
                }
                else if (storage == 1000)
                {
                    rdoStorageOptions.SelectedIndex = 1; ;
                    rdoStorageOptions.SelectedItem.Text = "1 GB ($0.99/Month) -- Current Plan";
                    index = 1;
                }
                else if (storage == 2000)
                {
                    rdoStorageOptions.SelectedIndex = 2; ;
                    rdoStorageOptions.SelectedItem.Text = "2 GB ($1.99/Month) -- Current Plan";
                    index = 2;
                }
                else if (storage == 5000)
                {
                    rdoStorageOptions.SelectedIndex = 3; ;
                    rdoStorageOptions.SelectedItem.Text = "5 GB ($2.99/Month) -- Current Plan";
                    index = 3;
                }
                else if (storage == 10000)
                {
                    rdoStorageOptions.SelectedIndex = 4; ;
                    rdoStorageOptions.SelectedItem.Text = "10 GB ($4.99/Month) -- Current Plan";
                    index = 4;
                }
                else if (storage == 50000)
                {
                    rdoStorageOptions.SelectedIndex = 5; ;
                    rdoStorageOptions.SelectedItem.Text = "50 GB ($9.99/Month) -- Current Plan";
                    index = 5;
                }
                Session["Index"] = index;
            }

        }

        public void paymentFormOn()
        {
            lblAddress.Visible = true; lblCardNumber.Visible = true; lblCardType.Visible = true;
            lblCity.Visible = true; lblExpiration.Visible = true; lblName.Visible = true; lblSecurityCode.Visible = true;
            lblState.Visible = true; lblZipCode.Visible = true; txtAddress.Visible = true; txtCardNumber.Visible = true;
            txtCity.Visible = true; txtName.Visible = true; txtSecurityCode.Visible = true; txtZipCode.Visible = true;
            ddlCardType.Visible = true; ddlMonth.Visible = true; ddlState.Visible = true; ddlYear.Visible = true;
            btnSubmit.Visible = true;
        }
        public void paymentFormOff()
        {
            lblAddress.Visible = false; lblCardNumber.Visible = false; lblCardType.Visible = false;
            lblCity.Visible = false; lblExpiration.Visible = false; lblName.Visible = false; lblSecurityCode.Visible = false;
            lblState.Visible = false; lblZipCode.Visible = false; txtAddress.Visible = false; txtCardNumber.Visible = false;
            txtCity.Visible = false; txtName.Visible = false; txtSecurityCode.Visible = false; txtZipCode.Visible = false;
            ddlCardType.Visible = false; ddlMonth.Visible = false; ddlState.Visible = false; ddlYear.Visible = false;
            btnSubmit.Visible = false;
        }

        protected void btnChangePlan_Click(object sender, EventArgs e)
        {
            if (rdoStorageOptions.SelectedIndex == index)
            {
                lblDisplayText2.Text = "Must choose plan different from current plan";

            }
            else if (rdoStorageOptions.SelectedIndex == 0) 
            {
                lblDisplayText2.Text = "Plan Successfully changed";
            }
            
            else
            {
                paymentFormOn();
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Validations())
            {
                lblDisplayText2.Text = "Plan Successfully changed";
                paymentFormOff();
                Functions.changeStoragePlan(loginInfo, email, rdoStorageOptions.SelectedIndex);
            }
        }


        public bool Validations()
        {
            if (txtAddress.Text == String.Empty ||
                txtCardNumber.Text == String.Empty ||
                txtCity.Text == String.Empty||
                txtName.Text == String.Empty ||
                txtSecurityCode.Text == String.Empty ||
                txtZipCode.Text == String.Empty
                )
            {
                lblDisplayText2.Text = "Enter all fields above!";
                return false;
            }
            else
            {
                return true;
            }
        }


    }
}<file_sep>/TermProject/Trash.aspx.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Class_Library;

namespace TermProject
{
    public partial class Trash : System.Web.UI.Page
    {
        string[] loginInfo = new string[2];

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Login"] != null)
            {
                lblDisplayText.Text = Session["User"].ToString() + "'s Trash Can";
                
                loginInfo[0] = Session["User"].ToString();
                loginInfo[1] = Session["Password"].ToString();

                gvTrash.DataSource = Functions.getUserTrash(loginInfo, Session["User"].ToString());
                gvTrash.DataBind();
            }
            else
            {
                lblDisplayText.Text = "Error: Must Login in";
            }

        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Main.aspx");
        }

        protected void gvTrash_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("recover"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string id = gvTrash.Rows[index].Cells[1].Text;
                string[] userInfo = new string[2];
                userInfo[0] = Session["User"].ToString(); userInfo[1] = id;
                int response = Functions.recoverFile(loginInfo, userInfo);
                gvTrash.DataSource = Functions.getUserTrash(loginInfo, Session["User"].ToString());
                gvTrash.DataBind();
                if (response == -1)
                {
                    lblDisplayText2.Text = "Not enough free space to recover file.";
                }

            }
        }

    }
}<file_sep>/Class Library/CloudStorageObject.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Class_Library
{
    public class CloudStorageObject
    {
         private List<File> cloudItems = new List<File>();

            public CloudStorageObject()
            {
            }

            public List<File> LineItems
            {
                get { return cloudItems; }
                set { cloudItems = value; }
            }

            public void Add(File file)
            {
                if (file != null)
                {
                    cloudItems.Add(file);
                }
            }

            public void clear()
            {
                cloudItems.Clear();
            }
        
    }
}<file_sep>/Class Library/File.cs
﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;

namespace Class_Library
{
    public class File
    {
        private string email;
        private string fileName;
        private string fileType;
        private double fileSize;
        private byte[] fileData;

        public File()
        {
        }

        public File(string email, string fileName, string fileType, double fileSize, byte[] fileData)
        {
            this.email = email;
            this.fileName = fileName;
            this.fileType = fileType;
            this.fileSize = fileSize;
            this.fileData = fileData;
        }

        public string Email
        {
            get { return email; }
            set { email = value; }
        }
        public string FileName
        {
            get { return fileName; }
            set { fileName = value; }
        }
        public string FileType
        {
            get { return fileType; }
            set { fileType = value; }
        }
        public double FileSize
        {
            get { return fileSize; }
            set { fileSize = value; }
        }
        public byte[] FileData
        {
            get { return fileData; }
            set { fileData = value; }
        }
        

    }
}<file_sep>/Class Library/UserInformation.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Class_Library
{
    public class UserInformation
    {
        public string name { get; set; }
        public string email { get; set; }
        public string password { get; set; }
        public string passwordConfirm { get; set; }
    }
}<file_sep>/TermProject/Login.aspx.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Class_Library;

namespace TermProject
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && Request.Cookies["Login_Cookie"] != null)
            {
                HttpCookie cookie = Request.Cookies["Login_Cookie"];
                lblEmail.Text = "Welcome back " + cookie.Values["userName"].ToString() + "!";
                txtEmail.Text = cookie.Values["userName"].ToString();
                txtEmail.Visible = false; btnRegister.Visible = false; btnOtherUser.Visible = true;
            }
            else
            {
                lblEmail.Text = "Email Address:";
                txtEmail.Visible = true; btnRegister.Visible = true; btnOtherUser.Visible = false;
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Response.Redirect("Registration.aspx");
        }

        protected void btnSignin_Click(object sender, EventArgs e)
        {
            string email = txtEmail.Text;
            string password = <PASSWORD>.Text;
            string response = Functions.attemptLogin(email, password);

            if (Validations())
            {

                if (response == "Success User" || response == "Success Admin")
                {
                    // Adds cookie if remember me is checked
                    if (chkRemember.Checked)
                    {
                        HttpCookie myCookie = new HttpCookie("Login_Cookie");
                        myCookie.Values["userName"] = txtEmail.Text;
                        Response.Cookies.Add(myCookie);
                    }
                    // Deletes cookie if remember me is not checked
                    else
                    {
                        if (Request.Cookies["Login_Cookie"] != null)
                        {
                            HttpCookie myCookie = new HttpCookie("Login_Cookie");
                            myCookie.Expires = DateTime.Now.AddDays(-1d);
                            Response.Cookies.Add(myCookie);
                        }
                    }

                    Session["Login"] = response;
                    Session["User"] = email;
                    Session["Password"] = <PASSWORD>;
                    Response.Redirect("Main.aspx");
                }
                else
                {
                    lblDisplayText1.Text = response;
                }
            }
        }

        protected void btnOtherUser_Click(object sender, EventArgs e)
        {
            // Deletes cookie if Not Me is clicked
            HttpCookie myCookie = new HttpCookie("Login_Cookie");
            myCookie.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(myCookie);
            txtEmail.Text = "";
        }

        public bool Validations()
        {
            if (txtEmail.Text == String.Empty ||
                txtPassword.Text == String.Empty)
            {
                lblDisplayText2.Text = "Enter all fields above!";
                return false;
            }
            else
            {
                return true;
            }
        }

    }
}<file_sep>/TermProject/Registration.aspx.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Class_Library;

namespace TermProject
{
    public partial class Registraion : System.Web.UI.Page
    {
        string[] loginInfo = new string[2];
        protected void Page_Load(object sender, EventArgs e) 
        {
            if (Session["Login"] != null)
            {
                loginInfo[0] = Session["User"].ToString();
                loginInfo[1] = Session["Password"].ToString();
                if (Session["Login"].ToString() == "Success Admin")
                {
                    btnRegister.Text = "Register Admin";
                    lblRemember.Visible = false;
                    chkRemember.Checked = false;
                    chkRemember.Visible = false;
                }
            }
           
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Session["Login"] = null;
            Response.Redirect("Login.aspx");
        }

        protected void btnRegister_Click1(object sender, EventArgs e)
        {
            string result = "";
            if (Validation())
            {
                if (txtPassword.Text != txtConfirm.Text)
                {
                    lblDisplayText.Text = "Passwords must match";
                }
                else
                {
                    if (Session["Login"] != null)
                    {
                        if (Session["Login"].ToString() == "Success Admin")
                        {
                            result = Functions.addAdmin(loginInfo, txtName.Text, txtEmail.Text,
                            txtPassword.Text, txtPhone.Text);
                        }
                    }
                    else
                    {
                        result = Functions.addUser(txtName.Text, txtEmail.Text,
                            txtPassword.Text, txtPhone.Text);
                    }

                    lblDisplayText.Text = result;

                    // Adds cookie if remember me is checked
                    if (chkRemember.Checked)
                    {
                        HttpCookie myCookie = new HttpCookie("Login_Cookie");
                        myCookie.Values["userName"] = txtEmail.Text;
                        Response.Cookies.Add(myCookie);
                    }
                    // Deletes cookie if remember me is not checked
                    else
                    {
                        if (Request.Cookies["Login_Cookie"] != null)
                        {
                            HttpCookie myCookie = new HttpCookie("Login_Cookie");
                            myCookie.Expires = DateTime.Now.AddDays(-1d);
                            Response.Cookies.Add(myCookie);
                        }
                    }
                }
            }

        }

        public Boolean Validation()
        {

            if (txtName.Text == String.Empty || 
                txtEmail.Text == String.Empty || 
                txtPassword.Text == String.Empty || 
                txtConfirm.Text == String.Empty || 
                txtPhone.Text == String.Empty)
            {
                lblDisplayText.Text = "Enter all fields above!";
                return false;
            }
            else
            {
                return true;
            }

            
        }
    }
}<file_sep>/TermProject/Main.aspx.cs
﻿using Class_Library;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace TermProject
{
    public partial class Main : System.Web.UI.Page
    {
        string[] loginInfo = new string[2];
        CloudStorageObject cloudObj = new CloudStorageObject();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Login"] != null)
            {
                if (Session["cloudObj"] != null)
                {
                    cloudObj = (CloudStorageObject)Session["cloudObj"];
                }
                lblDisplayText.Text = "Welcome " + Session["User"].ToString() + "!";
                if (Session["Login"].ToString() == "Success Admin")
                {
                    stateAdmin();                  
                }
                else if (Session["Login"].ToString() == "Success User")
                {
                    stateUser();                   
                }
                loginInfo[0] = Session["User"].ToString();
                loginInfo[1] = Session["Password"].ToString();
            }
            else
            {
                lblDisplayText.Text = "Error: Must Login in";
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }

        protected void btnAddAdmin_Click(object sender, EventArgs e)
        {
            Response.Redirect("Registration.aspx");
        }
        public void stateUser()
        {
            btnUserEditUser.Visible = true;
            btnMyFiles.Visible = true;
            btnDeleteFiles.Visible = true;
            btnStorageOptions.Visible = true;
            btnViewTrash.Visible = true;
            btnAskQuestion.Visible = true;
        }
        public void stateAdmin()
        {
            btnAddAdmin.Visible = true;
            btnViewTransactions.Visible = true;
            btnAdminEditUser.Visible = true;
            btnDeleteUser.Visible = true;
            btnAdminViewUserFiles.Visible = true;
            btnAnswerQuestion.Visible = true;
        }

        protected void btnFile_Click(object sender, EventArgs e)
        {
            userFormsOff();
            int fileSize;
            string fileType, fileName;
            if (fileUp.HasFile)
            {
                fileSize = fileUp.PostedFile.ContentLength;
                byte[] fileData = new byte[fileSize];

                fileUp.PostedFile.InputStream.Read(fileData, 0, fileSize);
                fileName = fileUp.PostedFile.FileName;
                fileType = fileUp.PostedFile.ContentType;

                File file = new File();
                file.Email = Session["User"].ToString();
                file.FileName = fileName;
                file.FileType = fileType;
                file.FileSize = fileSize;
                file.FileData = fileData;
                cloudObj.Add(file);

                string response = Functions.fileUpload(loginInfo, Session["User"].ToString(), fileName, fileType, 
                    fileSize, fileData);
                lblDisplayText.Text = response;
                stateUser();
                gvUserFiles.DataSource = Functions.getFilesByUser(loginInfo, Session["User"].ToString());
                gvUserFiles.DataBind(); gvUserFiles.Visible = true; lblFile.Visible = true; fileUp.Visible = true;
                lblFreeUserSpace.Visible = true; btnFile.Visible = true;
                lblFreeUserSpace.Text = "Free Space Remaining: " +
                Functions.getUserFreeSpace(loginInfo, Session["User"].ToString()) +
                " Bytes";

            }    
        }

        protected void gvTransactions_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void btnViewTransactions_Click(object sender, EventArgs e)
        {
            adminFormsOff();
            dropUser.DataSource = Functions.getCloudUsers(loginInfo);
            dropUser.DataTextField = "Email";
            dropUser.DataBind();
            transactionFormOn();       
        }

        protected void btnGetTransactions_Click(object sender, EventArgs e)
        {
            gvTransactions.DataSource = Functions.getTransactions(loginInfo, dropUser.SelectedItem.ToString(),
                dropTimePeriod.SelectedItem.ToString());
            gvTransactions.DataBind();
            gvTransactions.Visible = true;
        }
        
        public void transactionFormOn()
        {
            lblSelectUser.Visible = true;
            dropUser.Visible = true; lblSelectTimePeriod.Visible = true;
            dropTimePeriod.Visible = true;
            btnGetTransactions.Visible = true;
        }
        public void adminFormsOff()
        {
            lblSelectUser.Visible = false;
            dropUser.Visible = false; lblSelectTimePeriod.Visible = false;
            dropTimePeriod.Visible = false;
            btnGetTransactions.Visible = false;
            gvTransactions.Visible = false;
            gvAdminModify.Visible = false;
            btnDeleteSelection.Visible = false;
            gvDelete.Visible = false;
            gvAdminUserFilesView.Visible = false; lblSelectUserFA.Visible = false;
            btnGetUserFilesView.Visible = false;
            gvQuestions.Visible = false;
            lblQuestion.Visible = false;
            txtQuestions.Visible = false; btnAnswer.Visible = false;
        }

        public void userFormsOff()
        {
            gvUserFiles.Visible = false;
            lblFile.Visible = false; fileUp.Visible = false;
            lblFreeUserSpace.Visible = false; btnFile.Visible = false;
            gvUserModify.Visible = false;
            btnDeleteFile.Visible = false;
            gvDeleteFile.Visible = false;
            gvQuestions.Visible = false;
            lblQuestion.Visible = false;
            txtQuestions.Visible = false; btnAsk.Visible = false;
        }


        protected void gvAdminModify_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void gvAdminModify_PageIndexChanging(Object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
        {
            gvAdminModify.PageIndex = e.NewPageIndex;
            gvAdminModify.DataSource = Functions.getCloudUsersInfo(loginInfo);
            gvAdminModify.DataBind();
        }
        protected void gvAdminModify_RowEditing(Object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
        {
            gvAdminModify.EditIndex = e.NewEditIndex;
            gvAdminModify.DataSource = Functions.getCloudUsersInfo(loginInfo);
            gvAdminModify.DataBind();
        }

        protected void gvAdminModify_RowCancelingEdit(Object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
        {
            gvAdminModify.EditIndex = -1;
            gvAdminModify.DataSource = Functions.getCloudUsersInfo(loginInfo);
            gvAdminModify.DataBind();
        }
        protected void gvAdminModify_RowUpdating(Object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
        {
            int rowIndex = e.RowIndex;
            string email = gvAdminModify.Rows[rowIndex].Cells[2].Text;
            TextBox passwordBox = (TextBox)gvAdminModify.Rows[rowIndex].Cells[3].Controls[0];
            TextBox phoneBox = (TextBox)gvAdminModify.Rows[rowIndex].Cells[4].Controls[0];
            TextBox totalBox = (TextBox)gvAdminModify.Rows[rowIndex].Cells[6].Controls[0];
            Functions.adminUpdateUser(loginInfo, email, passwordBox.Text, phoneBox.Text, totalBox.Text);
            gvAdminModify.EditIndex = -1;
            gvAdminModify.DataSource = Functions.getCloudUsersInfo(loginInfo);
            gvAdminModify.DataBind();

         }

        protected void btnAdminEditUser_Click(object sender, EventArgs e)
        {
            adminFormsOff();
            gvAdminModify.Visible = true;
            gvAdminModify.DataSource = Functions.getCloudUsersInfo(loginInfo);
            gvAdminModify.DataBind();
        }
        protected void btnDeleteUser_Click(object sender, EventArgs e)
        {
            adminFormsOff();
            gvDelete.Visible = true;
            gvDelete.DataSource = Functions.getCloudUsersInfo(loginInfo);
            gvDelete.DataBind();
            btnDeleteSelection.Visible = true;
        }
        protected void btnDeleteSelection_Click(object sender, EventArgs e)
        {
            int checkCount = 0;
            for (int row = 0; row < gvDelete.Rows.Count; row++)
            {
                CheckBox CBox = (CheckBox)gvDelete.Rows[row].FindControl("chkSelect");
                if (CBox.Checked)
                {
                    checkCount++;
                    string email = gvDelete.Rows[row].Cells[3].Text;
                    Functions.deleteUser(loginInfo, email);
                }
            }
            if (checkCount == 0)
            {
                //tell user to check something
            }
            else
            {
                gvDelete.DataSource = Functions.getCloudUsersInfo(loginInfo);
                gvDelete.DataBind();
            }
        }

        protected void btnUserEditUser_Click(object sender, EventArgs e)
        {
            userFormsOff();
            gvUserModify.Visible = true;
            gvUserModify.DataSource = Functions.getSingleUserInfo(loginInfo, Session["User"].ToString());
            gvUserModify.DataBind(); 
        }
        protected void gvUserModify_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void gvUserModify_RowEditing(Object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
        {
            gvUserModify.EditIndex = e.NewEditIndex;
            gvUserModify.DataSource = Functions.getSingleUserInfo(loginInfo, Session["User"].ToString());
            gvUserModify.DataBind();
        }

        protected void gvUserModify_RowCancelingEdit(Object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
        {
            gvUserModify.EditIndex = -1;
            gvUserModify.DataSource = Functions.getSingleUserInfo(loginInfo, Session["User"].ToString());
            gvUserModify.DataBind();
        }
        protected void gvUserModify_RowUpdating(Object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
        {
            int rowIndex = e.RowIndex;
            int id = Convert.ToInt32(gvUserModify.Rows[rowIndex].Cells[0].Text);
            TextBox nameBox = (TextBox)gvUserModify.Rows[rowIndex].Cells[1].Controls[0];
            TextBox emailBox = (TextBox)gvUserModify.Rows[rowIndex].Cells[2].Controls[0];
            TextBox passwordBox = (TextBox)gvUserModify.Rows[rowIndex].Cells[3].Controls[0];
            TextBox phoneBox = (TextBox)gvUserModify.Rows[rowIndex].Cells[4].Controls[0];
            Functions.userUpdateUser(loginInfo, id, nameBox.Text, emailBox.Text, passwordBox.Text, phoneBox.Text);
            gvUserModify.EditIndex = -1;
            gvUserModify.DataSource = Functions.getSingleUserInfo(loginInfo, Session["User"].ToString());
            gvUserModify.DataBind();

        }

        protected void btnMyFiles_Click(object sender, EventArgs e)
        {
            userFormsOff();
            lblFile.Visible = true; fileUp.Visible = true;
            lblFreeUserSpace.Visible = true; btnFile.Visible = true;
            lblFreeUserSpace.Text = "Free Space Remaining: " +
                Functions.getUserFreeSpace(loginInfo, Session["User"].ToString()) +
                " Bytes";                           
            gvUserFiles.DataSource = Functions.getFilesByUser(loginInfo, Session["User"].ToString());
            gvUserFiles.DataBind(); gvUserFiles.Visible = true;
        }
        protected void btnDeleteFiles_Click(object sender, EventArgs e)
        {
            userFormsOff();
            gvDeleteFile.Visible = true;
            gvDeleteFile.DataSource = Functions.getFilesByUser(loginInfo, Session["User"].ToString());
            gvDeleteFile.DataBind();
            btnDeleteFile.Visible = true;
        }

        protected void btnDeleteFile_Click(object sender, EventArgs e)
        {
            int checkCount = 0;
            for (int row = 0; row < gvDeleteFile.Rows.Count; row++)
            {
                CheckBox CBox = (CheckBox)gvDeleteFile.Rows[row].FindControl("chkSelectFile");
                if (CBox.Checked)
                {
                    checkCount++;
                    string fileName = gvDeleteFile.Rows[row].Cells[1].Text;
                    string fileSize = gvDeleteFile.Rows[row].Cells[3].Text;
                    Functions.deleteFile(loginInfo, fileName, fileSize);
                }
            }
            if (checkCount == 0)
            {
                //tell user to check something
            }
            else
            {
                gvDeleteFile.DataSource = Functions.getFilesByUser(loginInfo, Session["User"].ToString());
                gvDeleteFile.DataBind();
            }
        }

        protected void btnStorageOptions_Click(object sender, EventArgs e)
        {
            Response.Redirect("StorageOptions.aspx");
        }

        protected void btnViewTrash_Click(object sender, EventArgs e)
        {
            Response.Redirect("Trash.aspx");
        }

        protected void btnViewUserFiles_Click(object sender, EventArgs e)
        {
            adminFormsOff();
            ddlAdminUserFilesView.DataSource = Functions.getCloudUsers(loginInfo);
            ddlAdminUserFilesView.DataTextField = "Email";
            ddlAdminUserFilesView.DataBind(); ddlAdminUserFilesView.Visible = true;
            gvAdminUserFilesView.Visible = true; lblSelectUserFA.Visible = true;
            btnGetUserFilesView.Visible = true;
        }
        protected void btnGetUserFiles_Click(object sender, EventArgs e)
        {
            gvAdminUserFilesView.DataSource = Functions.getFilesByUser(loginInfo, ddlAdminUserFilesView.SelectedItem.ToString());
            gvAdminUserFilesView.DataBind(); gvAdminUserFilesView.Visible = true;
        }

        protected void btnaskQuestion_Click(object sender, EventArgs e)
        {
            userFormsOff();
            gvQuestions.Visible = true;
            gvQuestions.DataSource = Functions.getQuestions();
            gvQuestions.DataBind();
            lblQuestion.Visible = true; lblQuestion.Text = "Ask Question: ";
            txtQuestions.Visible = true; btnAsk.Visible = true;


        }

        protected void btnAnswerQuestion_Click(object sender, EventArgs e)
        {
            adminFormsOff();
            gvQuestions.Visible = true;
            gvQuestions.DataSource = Functions.getQuestions();
            gvQuestions.DataBind();
            lblQuestion.Visible = true; lblQuestion.Text = "Answer Question: ";
            txtQuestions.Visible = true; btnAnswer.Visible = true;
        }
        protected void btnAsk_Click(object sender, EventArgs e)
        {
            
            Functions.askQuestion(loginInfo, txtQuestions.Text);
            gvQuestions.DataSource = Functions.getQuestions();
            gvQuestions.DataBind();
        }
        protected void btnAnswer_Click(object sender, EventArgs e)
        {
            
        }




    }
}<file_sep>/Class Library/Functions.aspx.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utilities;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Class_Library
{
    public partial class Functions : System.Web.UI.Page
    {
        CloudStorageObject cloudObj = new CloudStorageObject();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["cloudObj"] != null)
            {
                cloudObj = (CloudStorageObject)Session["cloudObj"];
            }
        }

        public static string attemptLogin(string email, string password)
        {
            string[] loginInfo = new string[2];
            string response = "";
            loginInfo[0] = email; loginInfo[1] = password;
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();

            int result = pxy.attemptLogin(loginInfo);
            if (result == -1)
            {
                response = "Not Found";
            }
            else if (result == 0)
            {
                response = "Invalid Password";
            }
            else if (result == 1)
            {
                response = "Success User";
            }
            else if (result == 2)
            {
                response = "Success Admin";
            }

            return response;

        }

        public static string addUser(string name, string email, string password, string phone)
        {
            string[] userInfo = new string[4];
            string response = "";
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            userInfo[0] = name; userInfo[1] = email;
            userInfo[2] = password; userInfo[3] = phone;
            int result = pxy.addUser(userInfo);

            if (result == 0)
            {
                response = "User successfully registered!";
            }
            else if (result == -1)
            {
                response = "Error: User already exists";
            }

            return response;
        }

        public static string addAdmin(string[] loginInfo, string name, string email, string password, string phone)
        {
            string[] userInfo = new string[4];
            string response = "";
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            userInfo[0] = name; userInfo[1] = email;
            userInfo[2] = password; userInfo[3] = phone;
            int result = pxy.addAdmin(loginInfo, userInfo);

            if (result == 0)
            {
                response = "Admin successfully registered!";
            }
            else if (result == -1)
            {
                response = "Error: Admin already exists";
            }

            return response;

        }

        public static string fileUpload(string[] loginInfo, string email, string fileName, string fileType, int fileSize, byte[] fileData)
        {
            string response = "";
            string[] fileInfo = new string[4];
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            fileInfo[0] = email; fileInfo[1] = fileName;
            fileInfo[2] = fileType; fileInfo[3] = fileSize.ToString();

            int result = pxy.addFile(loginInfo, fileInfo, fileData);

            if (result == 0)
            {
                response = "File successfully added!";
            }
            else if (result == 1)
            {
                response = "File updated, previous version moved to trash";
            }
            else if (result == -1)
            {
                response = "Error: Not Enough Free Storage";
            }

            return response;

        }

        public static DataSet getFilesByUser(string[] loginInfo, string email)
        {
            string[] userInfo = new string[1];
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            userInfo[0] = email;
            DataSet myDS = pxy.getFilesByUser(loginInfo, userInfo); 
            return myDS;
        }

        public static string getUserFreeSpace(string[] loginInfo, string email)
        {
            string response = "";
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            string[] userInfo = new string[1];
            userInfo[0] = email;
            response = pxy.getUserFreeStorage(loginInfo, userInfo).ToString();
            return response;
        }

        public static string getUserTotalSpace(string[] loginInfo, string email)
        {
            string response = "";
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            string[] userInfo = new string[1];
            userInfo[0] = email;
            response = pxy.getUserTotalStorage(loginInfo, userInfo).ToString();
            return response;
        }

        public static DataSet getTransactions(string[] loginInfo, string user, string timePeriod)
        {
            string[] userInfo = new string[2];
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            userInfo[0] = user; userInfo[1] = timePeriod;
            DataSet myDS = pxy.getTransactions(loginInfo, userInfo);
            return myDS;
        }

        public static DataSet getCloudUsers(string[] loginInfo)
        {
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            DataSet myDS = pxy.getCloudUsers(loginInfo);
            return myDS;
        }
        public static DataSet getCloudUsersInfo(string[] loginInfo)
        {
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            DataSet myDS = pxy.getCloudUsersInfo(loginInfo);
            return myDS;
        }

        public static int adminUpdateUser(string[] loginInfo, string email, string password, string phone, string total)
        {
            string[] userInfo = new string[4];
            userInfo[0] = email;
            userInfo[1] = password;
            userInfo[2] = phone;
            userInfo[3] = total;
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            int response = pxy.adminUpdateUser(loginInfo, userInfo);
            return response;
        }

        public static int deleteUser(string[] loginInfo, string email)
        {
            string[] userInfo = new string[1];
            userInfo[0] = email;
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            int response = pxy.deleteUser(loginInfo, userInfo);
            return response;
        }

        public static DataSet getSingleUserInfo(string[] loginInfo, string email)
        {
            string[] userInfo = new string[1];
            userInfo[0] = email;
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            DataSet myDS = pxy.getSingleUserInfo(loginInfo, userInfo);
            return myDS;
        }

        public static int userUpdateUser(string[] loginInfo, int id, string name, string email, string password, string phone)
        {
            string[] userInfo = new string[5];
            userInfo[0] = id.ToString();
            userInfo[1] = name;
            userInfo[2] = email;
            userInfo[3] = password;
            userInfo[4] = phone;
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            int response = pxy.userUpdateUser(loginInfo, userInfo);
            return response;
        }

        public static int deleteFile(string[] loginInfo, string fileName, string fileSize)
        {
            string[] userInfo = new string[3];
            userInfo[0] = fileName;
            userInfo[1] = fileSize;
            userInfo[2] = "delete";
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            int response = pxy.deleteFile(loginInfo, userInfo);
            return response;
        }

        public static DataSet getUserTrash(string[] loginInfo, string email)
        {
            string[] userInfo = new string[1];
            userInfo[0] = email;
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            DataSet myDS = pxy.getUserTrash(loginInfo, userInfo);
            return myDS;
        }
        public static int changeStoragePlan(string[] loginInfo, string email, int plan)
        {
            int response = 0;
            double storage = 0;
            string[] userInfo = new string[2];
            if (plan == 0) {storage = 100000000; }
            else if (plan == 1) { storage = 1000000000; }
            else if (plan == 2) { storage = 2000000000; }
            else if (plan == 3) { storage = 5000000000; }
            else if (plan == 4) { storage = 10000000000; }
            else if (plan == 5) { storage = 50000000000; }
            userInfo[0] = email; userInfo[1] = storage.ToString(); ;
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            response = pxy.changeStoragePlan(loginInfo, userInfo);

            return response;
        }

        public static int recoverFile(string[] loginInfo, string[] userInfo)
        {
            int response = 0;
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            response = pxy.recoverFile(loginInfo, userInfo);
            return response;
        }

        public static DataSet getQuestions()
        {
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            DataSet myDs = pxy.getQuestions();
            return myDs;
        }

        public static void askQuestion(string[] loginInfo, string question)
        {
            CloudSVCRef.CloudSVC pxy = new CloudSVCRef.CloudSVC();
            pxy.askQuestion(loginInfo, question);
        }
    }
}