using System;
using MySql.Data.MySqlClient;
using System.Text.RegularExpressions;
using System.Threading;
using System.Collections;
namespace WebApplication4
{
    public partial class WebForm1 : System.Web.UI.Page
    {        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (DateTime.Now.Month < 10)
            {
                TextBox4.Text = DateTime.Now.Day.ToString() + "-" + "0" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString();
            }
            else
            {
                TextBox4.Text = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString();
            }
            TextBox4.ReadOnly = true;                        
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";
        }
        public bool isEmail()
        {
            string inputEmail = TextBox6.Text.ToString();                        
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                  @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                  @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";            
            //indbygget metode til at tjekke en regular expression
            Regex mail = new Regex(strRegex);
            if (mail.IsMatch(inputEmail))
                return true;
            else
                return false;
        }
        public bool isValidNavn()
        {
            string inputText = TextBox2.Text.ToString();
            if (inputText.Length > 45)
            {
                return false;
            }
            else
            {
                return true;
            }
        }     
        public bool isValidOverskrift()
        {
            string inputText = TextBox1.Text.ToString();
            if (inputText.Length > 45)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static void Thread2()
        {            
                Thread.Sleep(2000);
                System.Web.HttpResponse.Redirect("
http://www.google.com");                
            An object             
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            isEmail();
            isValidNavn();
            isValidOverskrift();           
            if (TextBox1.Text.Equals("") || TextBox2.Text.Equals("") || TextBox3.Text.Equals("") || TextBox4.Text.Equals("") || TextBox5.Text.Equals("") || TextBox6.Text.Equals(""))
            {
                //doNothing
            }
            else if (isEmail() == false)
            {
                TextBox6.Text = "Din email var ikke korrekt indtastet!";
            }
            else if (isValidNavn() == false)
            {
                Label1.Text = "Dit navn er for langt";
            }
            else if(isValidOverskrift() == false)
            {
                Label1.Text = "Dit overskrift er for lang";
            }            
            else
            {
                Thread trd = new Thread(new ThreadStart(Thread2));
                trd.IsBackground = true;
                trd.Start();                
                //connection string
                string MyConString = "SERVER=localhost;" + "DATABASE=CMS;" + "UID=root;" + "PASSWORD=password;";                
                //ny connection
                MySqlConnection connection = new MySqlConnection(MyConString);
                MySqlCommand command = connection.CreateCommand();
                //åben connection
                connection.Open();
                //SQL kald
                command.CommandText = "INSERT INTO Nyhed(Overskrift, SkrevetAf, Undertitel, Dato, Artikel, Email) VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')";                
                //Betyder at det er et kald som skriver TIL databasen.
                command.ExecuteNonQuery();
                //Lukker forbindelsen
                connection.Close();                
            }            
        }
        protected void Button3_Click(object sender, EventArgs e)
        {            
            isEmail();
            isValidNavn();
            isValidOverskrift();
            if (TextBox1.Text.Equals("") || TextBox2.Text.Equals("") || TextBox3.Text.Equals("") || TextBox4.Text.Equals("") || TextBox5.Text.Equals("") || TextBox6.Text.Equals(""))
            {
                //doNothing
            }
            else if (isEmail() == false)
            {
                Label1.Text = "Din email var ikke korrekt indtastet!";
            }
            else if (isValidNavn() == false)
            {
                Label1.Text = "Dit navn må have max 50 karakterer";
            }
            else if (isValidOverskrift() == false)
            {
                Label1.Text = "Din overskrift må være på maks 45 tegn";
            }
            else
            {
                Label1.Text = "Det ser fint ud";
            }            
        }
    }
}