using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using System.Resources;

public partial class login : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        if (Session["culture"] != null)
        {
            UICulture = Session["culture"].ToString();
            Culture = Session["culture"].ToString();

            Thread.CurrentThread.CurrentCulture =
              CultureInfo.CreateSpecificCulture(Session["culture"].ToString());
            Thread.CurrentThread.CurrentUICulture =
              new CultureInfo(Session["culture"].ToString());
        }
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        FakturaImageButton.ImageUrl = Resources.Common.Faktura;
        if (!IsPostBack) { SetFocus(NIPTextBox); }        
    }

    protected void ZalogujButton_Click(object sender, EventArgs e)
    {
        string nip = NIPTextBox.Text;
        string nrZlec = NrZlecTextBox.Text;
        bool czyZalogowany = Zaloguj(nip, nrZlec);
        if (czyZalogowany)
        {
            FormsAuthentication.RedirectFromLoginPage(nip, false);            
        }
        else
        {
            errorMsg.Text = GetLocalResourceObject("errorMsg").ToString();
        }
    }

    bool Zaloguj(string nip, string nrZlec)
    {
        string connString = ConfigurationManager.ConnectionStrings["BazaReklamConn"].ConnectionString;
        string cmdText = "SELECT COUNT(*) FROM dbo.vDanePlatnosciEcard WHERE nip=@nip AND [ID Reklamy]=@nrZlec";
        nip = nip.Replace(" ", "");
        nip = nip.Replace("-", "");
        int znalezione = 0;
        using(SqlConnection conn = new SqlConnection(connString))
        {
            SqlCommand cmd = new SqlCommand(cmdText, conn);
            cmd.Parameters.Add("@nip", SqlDbType.VarChar, 50).Value = nip;
            cmd.Parameters.Add("@nrZlec", SqlDbType.VarChar, 20).Value = nrZlec;
            conn.Open();
            znalezione = (int)cmd.ExecuteScalar();
            if (znalezione > 0)
            {
                Session["nip"] = nip;
                Session["nrZlec"] = nrZlec;
            }
            conn.Close();
        }
        return (znalezione > 0);
    }
    
}
