public static bool Transaction(string account, string referenceNo, decimal amount, bool IsDeposit, int status)
    {
        db = new SMSDataClassesDataContext();
        SMS_VirtualMoney vMoney = new SMS_VirtualMoney();

        vMoney.refno = referenceNo;
        vMoney.account = account;
        if (IsDeposit){
            vMoney.type = 21;
        }
        else{
            vMoney.type = 22;
        }
        vMoney.amount = amount;
        vMoney.status = status;
        vMoney.datetime = config.current_DateTime();
        try{
            db.SMS_VirtualMoneys.InsertOnSubmit(vMoney);
            db.SubmitChanges();
            return true;
        }
        catch (Exception ex){
        }
        return false;
    }
    public static string Registration(string number, string input, string launcher_id)
    {
        db = new SMSDataClassesDataContext();

        if (checkNumber(number))
        {
            process.save(number, "Oop, Your number already exist to our record!PleasE try a new number");
            return "OK";
        }

        if (checkCode(populate(input, launcher_id)))
        {
            string MemberName = string.Empty;
            string refCode = "CIA" + number.Substring(1, 10) + "-" + DateTime.Now.Year + "-" + DateTime.Now.Month;
            SMS_Member m = new SMS_Member();
            m.ReferenceNo = refCode;

            foreach (var l in objList)
            {
                MemberName = l.Firstname;
                m.Group_Name = l.Group;
                m.Account_Number = number;
                m.Family_Name = l.Lastname.ToUpper();
                m.First_Name = l.Firstname.ToUpper();
                m.Town = l.Town.ToUpper();
                m.Province = l.Province.ToUpper();
                m.Sponsor_ID = l.Sponsor;
                m.CP_Number = number;
                m.Section_A = (short)l.Section_a;
                m.Section_B = (short)l.Section_b;
                m.Pin_Code = l.Pin;
            }

            m.DateReg = config.current_DateTime();
            m.Status = true;
            m.monthOf = DateTime.Now.Month;
            m.yearOf = DateTime.Now.Year;

            try
            {
                db.SMS_Members.InsertOnSubmit(m);
                db.SubmitChanges();

                balance.Transaction(number, "CIA00001", 100, true, 25);
                process.save(number, replyMessage("NREG", "OK", MemberName));
                return "OK";
            }
            catch (Exception ex)
            {
                process.save(number, "Sorry, our system has encountered an error, please try again later. thank you!");
                return "OK";
            }
        }
        process.save(number, replyMessage("NREG", "NOK0"));
        return "OK";
    }
    public static string now(string number, string message)
    {
        db = new SMSDataClassesDataContext();
        string bayanihan = check_If_open(config.current_DateTime());
        if (IsSender_Exist(number))
        {
            if (bayanihan == "Closed" || bayanihan == "NULL")
            {
                process.save(number, "U hav reached the cut offtime to donate. ur entry isn't valid. try donating after 11pm 4 da nxt draw. Thank you!");
                return "OK";
            }

            string[] x = message.Split(' ');
            string[] y = x[1].Split('/');
            Donation = Convert.ToDouble(y[0]);
            Combination = y[1];
            string refCode = config.current_DateTime().ToString("MMdd") + config.generateReferenceNo(4);

            if (balance.current_amount(number) >= Donation)
            {
                SMS_Bayanihan b = new SMS_Bayanihan();
                b.refNo = refCode;
                b.bayanihan_ref = bayanihan;
                b.donator = number;
                b.combination = Combination;
                b.donation = (decimal)Donation;
                b.date_danated = config.current_DateTime();
                b.status = "-";

                try
                {
                    db.SMS_Bayanihans.InsertOnSubmit(b);
                    db.SubmitChanges();

                    sqlServer.Update("UPDATE SMS_BayanihanSum SET played = '" + count_donators(bayanihan) + "', " +
                                "donation = '" + total_donation(bayanihan) + "' " +
                                "WHERE bayanihan_ref = '" + bayanihan + "' AND status = 'Open';");

                    balance.Transaction(number, refCode, (decimal)Donation, false, 24);
                    process.save(number, reply("OK").Replace("[AMOUNT]", Donation.ToString()).Replace("[COMBINATION]", Combination).Replace("[BALANCE]", config.format_currency((decimal)balance.current_amount(number))).Replace("[REFNO]", refCode));
                    return "OK";
                }
                catch (Exception ex)
                {
                    process.save(number, "The system is not available at this time, please try again later");
                    return "OK";
                }
            }
            process.save(number, "Transaction cannot be processed because do to insufficient funds, to donate please reload now.");
            return "OK";
        }
        process.save(number, "You are not registerd in our record, please register first! thank you...");
        return "OK";
    }
    public static bool delete(long id)
    {
        db = new SMSDataClassesDataContext();
        var x = (from i in db.SMS_ReceivedMsgs.Where(i => i.Id == id) select i).Take(1).FirstOrDefault();

        try
        {
            db.SMS_ReceivedMsgs.DeleteOnSubmit(x);
            db.SubmitChanges();
            return true;
        }
        catch (Exception ex)
        {}
        return false;
    }
    public static bool toQueuedbox(string number, string message, short net)
    {
        db = new SMSDataClassesDataContext();
        SMS_QueuedBox que = new SMS_QueuedBox();
        que.Number = number;
        que.Message = config.encrypt(message);
        que.Network = net;
        que.Status = false;

        try
        {
            db.SMS_QueuedBoxes.InsertOnSubmit(que);
            db.SubmitChanges();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
 public void save(string newCode)
 {
     try
     {
         SMSDataClassesDataContext db = new SMSDataClassesDataContext();
         SMS_ActivationCode sm;
         var chk = (from c in db.SMS_ActivationCodes.Where(c => c.PINCode == newCode) select c.PINCode).FirstOrDefault();
         if (chk == null)
         {
             sm = new SMS_ActivationCode();
             sm.PINCode = newCode;
             sm.Status = true;
             db.SMS_ActivationCodes.InsertOnSubmit(sm);
             db.SubmitChanges();
             Console.Write("<br />" + newCode);
         }
     }
     catch (Exception ex)
     {
     }
 }
    public static bool save(string number, string message, string launcher_id, bool process_now = false)
    {
        db = new SMSDataClassesDataContext();
        SMS_Inbox inbox = new SMS_Inbox();
        SMS_ReceivedMsg receivedMsg = new SMS_ReceivedMsg();

        inbox.Sender = number;
        inbox.Message = message;
        inbox.DateReceived = config.current_DateTime();
        inbox.MonthOf = DateTime.Now.Month;
        inbox.YearOf = DateTime.Now.Year;
        inbox.Status = false;

        receivedMsg.Sender = number;
        receivedMsg.Message = message;
        receivedMsg.DateReceived = config.current_DateTime();
        receivedMsg.MonthOf = DateTime.Now.Month;
        receivedMsg.YearOf = DateTime.Now.Year;
        receivedMsg.Status = false;

        try
        {
            db.SMS_Inboxes.InsertOnSubmit(inbox);
            db.SMS_ReceivedMsgs.InsertOnSubmit(receivedMsg);
            db.SubmitChanges();

            if (process_now)
            {
                process.now(launcher_id);
            }

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    protected void btnProceed_Click(object sender, EventArgs e)
    {
        db = new SMSDataClassesDataContext();
        string refNo = Request.QueryString["ref"].Trim();
        string winCode = txtWinCode.Text.Trim();

        SMS_BayanihanSum d = new SMS_BayanihanSum();
        d.bayanihan_ref = "BD" + DateTime.Now.AddDays(1).ToString("Mddyyyy");
        d.played = 0;
        d.donation = 0;
        d.combination_no_win = "-";
        d.total_win = 0;
        d.total_win_amount = 0;
        d.open_started = DateTime.Parse(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + " 01:00:00.000");
        d.closing_time = DateTime.Parse(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + " 18:00:00.000");
        d.status = "Open";

        sqlServer.Update("UPDATE SMS_Bayanihan SET " +
                            "status = 'Winner'" +
                            "WHERE bayanihan_ref = '" + refNo + "' AND combination = '" + winCode + "'");

        sqlServer.Update("UPDATE SMS_BayanihanSum SET " +
                             "combination_no_win = '" + winCode + "', " +
                             "total_win = '" + totalWon(refNo, winCode) + "', " +
                             "total_win_amount = '" + wonAllTotalAmount(refNo, winCode) + "', " +
                             "status = 'Closed' " +
                             "WHERE bayanihan_ref = '" + refNo + "'");
        try
        {
            db.SMS_BayanihanSums.InsertOnSubmit(d);
            db.SubmitChanges();
        }
        catch (Exception ex)
        {
        }
        Response.Redirect(HelpController.Refresh);
    }
    public static bool save(string number, string message)
    {
        if (message != "OK")
        {
            db = new SMSDataClassesDataContext();
            SMS_QueuedBox que = new SMS_QueuedBox();
            que.Message = config.encrypt(message);
            que.Number = number;
            que.Network = (short)config.prefixes(number);
            que.Status = false;

            try
            {
                db.SMS_QueuedBoxes.InsertOnSubmit(que);
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            { }
        }
        return false;
    }
    public static bool saveToSentItemThenDelete(long id, bool status)
    {
        db = new SMSDataClassesDataContext();
        SMS_SentItem sentItems = new SMS_SentItem();
        var x = (from i in db.SMS_QueuedBoxes.Where(i => i.ID == id) select i).Take(1).FirstOrDefault();
        if (x != null)
        {
            sentItems.Number = x.Number;
            sentItems.Message = x.Message;
            sentItems.DateTime = config.current_DateTime();
            sentItems.Status = status;
            sentItems.MonthOf = DateTime.Now.Month;
            sentItems.YearOf = DateTime.Now.Year;

            try
            {
                db.SMS_SentItems.InsertOnSubmit(sentItems);
                db.SMS_QueuedBoxes.DeleteOnSubmit(x);
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {}
        }
        return false;
    }
    private string add_user()
    {
        db = new SMSDataClassesDataContext();
        SMS_User u = new SMS_User();
        u.admin = txtUsername.Text;
        u.password = config.encrypt(txtPassword.Text);
        u.email = txtEmail.Text;
        u.role = ddlRole.SelectedValue;
        u.date_added = config.current_DateTime();
        u.added_by = Session["user"].ToString();

        try
        {
            db.SMS_Users.InsertOnSubmit(u);
            db.SubmitChanges();
            config.send_email("User Account",
                    config.email_UsernameAndPassword("User Account", txtUsername.Text, txtPassword.Text),
                    txtEmail.Text);
            return txtUsername .Text + " was successfully added!";
        }
        catch (Exception ex)
        { }
        return txtUsername.Text + " wasn't successfully added!";
    }