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.Net.Mail;
using System.Text.RegularExpressions;
using digioz;

public partial class controls_ContactUs : System.Web.UI.UserControl
{
    ContactInfo coContactInfo = new ContactInfo();

    protected void Page_Load(object sender, EventArgs e)
    {
        coContactInfo = ContactInfoManagement.GetContactInfo();
                
        lblContact.Text = coContactInfo.ContactName;
        lblContactEMail.Text = coContactInfo.ContactEMail;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //validate fields

        if (txtSenderName.Text.Length <= 0)
        {
            lblErrorMessage.Text = "Sender Name is a required field and can not be empty.";
            txtSenderName.Focus();
            return;
        }
        else if (txtSenderEMail.Text.Length <= 0)
        {
            lblErrorMessage.Text = "EMail Address is a required field and can not be empty.";
            txtSenderEMail.Focus();
            return;
        }
        else if (isEmail(txtSenderEMail.Text) == false)
        {
            lblErrorMessage.Text = "You have entered an invalid EMail address.";
            txtSenderEMail.Focus();
            return;
        }
        else if (txtSenderMessage.Text.Length <= 0)
        {
            lblErrorMessage.Text = "Message is a required field and can not be empty.";
            txtSenderMessage.Focus();
            return;
        }
        else if (ImageVerifier1.Text != txtImgVerifyCode.Text)
        {
            lblErrorMessage.Text = "You have entered a wrong code. Please enter the new one correctly.";
            txtImgVerifyCode.Focus();
            return;
        }
        else
        {
            lblErrorMessage.Text = "";
        }

        coContactInfo.ContactName = lblContact.Text;
        coContactInfo.ContactEMail = lblContactEMail.Text;
        coContactInfo.SenderName = txtSenderName.Text;
        coContactInfo.SenderEMail = txtSenderEMail.Text;
        coContactInfo.SenderSubject = "Feedback from Portal";
        coContactInfo.Attachment = String.Empty;
        coContactInfo.SenderMessage = "From  " + coContactInfo.SenderName + ":  " + txtSenderMessage.Text;

        bool lbResponse = false;

        lbResponse = digioz.Utility.SendMail.SubmitMail(coContactInfo);

        if (lbResponse == true)
        {
            lblStatus.ForeColor = System.Drawing.Color.Green;
            lblStatus.Text = "Message successfully sent.";
        }
        else
        {
            lblStatus.ForeColor = System.Drawing.Color.Red;
            lblStatus.Text = "Message submission failed!";
        }
    }
    public static bool isEmail(string inputEmail)
    {
        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})(\]?)$";
        Regex re = new Regex(strRegex);
        if (re.IsMatch(inputEmail))
            return (true);
        else
            return (false);
    }
}
