using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Collections.Generic;


public partial class Controls_PledgeBox : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        pnlEmailRequired.Visible = false;
    }

    protected int TotalPledges
    {
        get {
            int pledgeCount = 0;
            if (Cache.Get("TotalPledges") == null)
            {
                pledgeCount = Int32.Parse(SqlHelper.ExecuteScalar(CommandType.Text, "SELECT count(ID) FROM Pledges ").ToString());
                pledgeCount += 252;
                Cache.Insert("TotalPledges", pledgeCount);
            }
            else pledgeCount = (int)Cache.Get("TotalPledges");
            return pledgeCount;
        }
    }

    protected void btnAddToMailingList_Click(object sender, EventArgs e)
    {
        if (Utility.IsNull(txtEmail.Text))
        {
            pnlEmailRequired.Visible = true;
            return;
        }

        int numberPledges = Int32.Parse(SqlHelper.ExecuteScalar(CommandType.Text, "SELECT count(ID) FROM Pledges WHERE EmailAddress = '" + txtEmail.Text + "'").ToString());

        if (numberPledges < 1)
        {
            SqlHelper.ExecuteNonQuery(CommandType.Text, "INSERT INTO Pledges ( EmailAddress,User_IP,DateAdded ) VALUES(  '" + txtEmail.Text + "', '" + Request.ServerVariables["REMOTE_ADDR"].ToString() + "', GETDATE())");
            Cache.Remove("TotalPledges");
        }

        pnlButton.Visible = false;
        pnlResults.Visible = true;
        lblResults.Text = "Thank you for pledging your support of Dr. Sabrin!";
    }
}
