protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["PageName"] = "Unsubscribe";

            ServiceReference objServiceReference = new ServiceReference();

            ScriptReference objScriptReference = new ScriptReference();

            objServiceReference.Path = "~/CarsService.asmx";

            objScriptReference.Path = "~/Static/Js/CarsJScriptNew.js";

            scrptmgr.Services.Add(objServiceReference);
            scrptmgr.Scripts.Add(objScriptReference);

            if (Request.QueryString.Keys.Count > 0)
            {
                string URL = string.Empty;
                string URL1 = string.Empty;
                if (Request.QueryString.Keys.Count > 0)
                {
                    if (URL != null)
                    {
                        PreferencesBL objPreferencesBL = new PreferencesBL();

                        DataSet dsPreferences = new DataSet();

                        dsPreferences = objPreferencesBL.GetEmailPreferences(Request.QueryString["PreferID"].ToString());

                        if (dsPreferences.Tables[0].Rows.Count > 0)
                        {
                            if (dsPreferences.Tables[0].Rows[0]["IsActive"].ToString() == "True")
                            {
                                objPreferencesBL.SubScribeStatus(Request.QueryString["PreferID"].ToString(), 0);

                                lblalert.Text = "Email Alerts has Unsubscribed Successfully...";
                            }
                            else
                            {
                                lblalert.Text = "Email Alerts has Already Unsubscribed ";

                                //mpealteruser.Show();
                            }
                        }

                        Type cstype = GetType();
                        ClientScriptManager cs = Page.ClientScript;
                        cs.RegisterStartupScript(cstype, "KyRST", "<script type='text/javascript' language='javascript'>pageLoad();</script>");

                    }
                }

            }

        }
    }
    public string CheckAlreadySubscribeEmail(string sEmail)
    {
        PreferencesBL objPreferencesBL = new PreferencesBL();
        DataSet dssub = new DataSet();

        dssub = objPreferencesBL.GetEmailPreferencesbyEmail(sEmail);
        string sPreferenceID = string.Empty;

        if (dssub.Tables[0].Rows.Count > 0)
        {
            sPreferenceID = dssub.Tables[0].Rows[0]["UserPreferID"].ToString();
        }
        else
        {
            sPreferenceID = "";
        }

        return sPreferenceID;
    }
    protected void btnSubscribe_Click(object sender, EventArgs e)
    {
        PreferencesBL objPreferencesBL = new PreferencesBL();

        DataSet dssub = new DataSet();

        PreferenceInfo ObjPreferncesInfo = new PreferenceInfo();

        PreferncesItemsInfo ObjPreferncesItemsInfo = new PreferncesItemsInfo();

        ObjPreferncesInfo.PreferenceID = hdnUserPrefernceID.Value;

        if (txtZip.Text != "Zip")
        {
            ObjPreferncesInfo.Zip = txtZip.Text;
        }
        if (txtName.Text != "Your Name")
        {
            ObjPreferncesInfo.Name = txtName.Text;
        }

        if (txtEmailAlert.Text != "Your Email")
        {
            ObjPreferncesInfo.Email = txtEmailAlert.Text;
        }

        if (txtPhoneNo.Text != "Your Phone")
        {
            ObjPreferncesInfo.Phone = txtPhoneNo.Text;
        }

        String strHostName = HttpContext.Current.Request.UserHostAddress.ToString();

        ObjPreferncesInfo.IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

        DataSet dsPreferences = new DataSet();

        dsPreferences = objPreferencesBL.SaveSubscribe(ObjPreferncesInfo, 1);

        for (int i = 1; i < 6; i++)
        {
            string SelectedMake = string.Empty;
            string SelectedModel = string.Empty;
            string SelectedRange = string.Empty;

            if (i == 1)
            {
                SelectedMake = ddlMakes1.SelectedValue;
                SelectedModel = ddlModels1.SelectedValue;
                SelectedRange = ddlRanges1.SelectedValue;
            }
            else if (i == 2)
            {
                SelectedMake = ddlMakes2.SelectedValue;
                SelectedModel = ddlModels2.SelectedValue;
                SelectedRange = ddlRanges2.SelectedValue;
            }
            else if (i == 3)
            {
                SelectedMake = ddlMakes3.SelectedValue;
                SelectedModel = ddlModels3.SelectedValue;
                SelectedRange = ddlRanges3.SelectedValue;
            }
            else if (i == 4)
            {
                SelectedMake = ddlMakes4.SelectedValue;
                SelectedModel = ddlModels4.SelectedValue;
                SelectedRange = ddlRanges4.SelectedValue;
            }
            else if (i == 5)
            {
                SelectedMake = ddlMakes5.SelectedValue;
                SelectedModel = ddlModels5.SelectedValue;
                SelectedRange = ddlRanges5.SelectedValue;
            }
            if (SelectedMake != "0" && SelectedModel != "0")
            {

                ObjPreferncesItemsInfo.Makeid = SelectedMake;

                ObjPreferncesItemsInfo.ModelID = SelectedModel;

                ObjPreferncesItemsInfo.PriceRange = SelectedRange;

                ObjPreferncesItemsInfo.UserPreferID = dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString();

                HiddenField hdnPreferenceID = (HiddenField)Page.FindControl("hdnPreferenceID" + i);

                ObjPreferncesItemsInfo.PreferenceID = hdnPreferenceID.Value;

                dssub = objPreferencesBL.SaveSubScribeItems(ObjPreferncesItemsInfo, true);
            }
            else
            {

                HiddenField hdnPreferenceID = (HiddenField)Page.FindControl("hdnPreferenceID" + i);

                if (hdnPreferenceID.Value != "")
                {
                    if (SelectedMake != "0" && SelectedModel != "0")
                    {
                        ObjPreferncesItemsInfo.Makeid = SelectedMake;

                        ObjPreferncesItemsInfo.ModelID = SelectedModel;

                        ObjPreferncesItemsInfo.PriceRange = SelectedRange;

                        ObjPreferncesItemsInfo.UserPreferID = dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString();

                        ObjPreferncesItemsInfo.PreferenceID = hdnPreferenceID.Value;

                        dssub = objPreferencesBL.SaveSubScribeItems(ObjPreferncesItemsInfo, false);
                    }
                }
            }
        }

        clsMailFormats format = new clsMailFormats();
        MailMessage msg = new MailMessage();

        msg.From = new MailAddress(CommonVariable.FromInfoMail);

        msg.To.Add(txtEmailAlert.Text);

        //msg.Bcc.Add("[email protected]");

        msg.Subject = "Successfully modified your personalized weekly email alerts - United Car Exchange.";

        msg.IsBodyHtml = true;

        string text = string.Empty;

        string VerifyPreferences = string.Empty;

        string ModifyPreferences = string.Empty;

        string PreferencesID = string.Empty;

        //VerifyPreferences = "www.unitedcarexchange.com/VerifyPreferences.aspx?Preferce=" + dssub.Tables[0].Rows[0]["ID"].ToString();

        //ModifyPreferences = "www.unitedcarexchange.com/EmailPreferences.aspx?Preferce=" + dssub.Tables[0].Rows[0]["ID"].ToString();

        PreferencesID = dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString();

        text = format.SendEMailPreferencesEdit(dsPreferences.Tables[0].Rows[0]["Name"].ToString(), dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString(), ref text);

        msg.Body = text.ToString();

        SmtpClient smtp = new SmtpClient();

        //smtp.Host = "smtp.gmail.com";
        //smtp.Port = 587;
        //smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "hugomirad");
        //smtp.EnableSsl = true;
        //smtp.Send(msg);

        smtp.Host = "127.0.0.1";
        smtp.Port = 25;
        smtp.Send(msg);

        lblalert.Text = "Your Preferences Has been Updated according to your selection";

        mpealteruser.Show();

        Page.ClientScript.RegisterStartupScript(typeof(Page), "KyRST", "<script type='text/javascript' language='javascript'>pageLoad();</script>");
    }
    private void GetEmailPreferences(string PreferenceID)
    {
        PreferencesBL objPreferencesBL = new PreferencesBL();

        DataSet dsPreferences = new DataSet();

        dsPreferences = objPreferencesBL.GetEmailPreferences(PreferenceID);

        if (dsPreferences.Tables[0].Rows.Count > 0)
        {

            if (dsPreferences.Tables[0].Rows[0]["isactive"].ToString() == "0")
            {

                lblalert.Text = "Your email address Already Unsubscribed";

                mpealteruser.Show();

            }
            else
            {

                if (dsPreferences.Tables[0].Rows[0]["Zip"].ToString() != "")
                {
                    txtZip.Text = dsPreferences.Tables[0].Rows[0]["Zip"].ToString();
                }
                if (dsPreferences.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    txtName.Text = dsPreferences.Tables[0].Rows[0]["Name"].ToString();
                }

                if (dsPreferences.Tables[0].Rows[0]["Email"].ToString() != "Your Email")
                {
                    txtEmailAlert.Text = dsPreferences.Tables[0].Rows[0]["Email"].ToString();
                }

                if (dsPreferences.Tables[0].Rows[0]["Phone"].ToString() != "")
                {
                    txtPhoneNo.Text = dsPreferences.Tables[0].Rows[0]["Phone"].ToString();
                }

                hdnUserPrefernceID.Value = dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString();

                if (dsPreferences.Tables[1].Rows.Count > 0)
                {

                    int k = 1;

                    TotalRowcount.Value = dsPreferences.Tables[1].Rows.Count.ToString();

                    for (int i = 0; i < dsPreferences.Tables[1].Rows.Count; i++)
                    {

                        DropDownList ddlDropddlMake = (DropDownList)Page.FindControl("ddlMakes" + k);
                        DropDownList ddlDropddlModel = (DropDownList)Page.FindControl("ddlModels" + k);
                        DropDownList ddlDropddlRange = (DropDownList)Page.FindControl("ddlRanges" + k);

                        HiddenField hdnPreferenceID = (HiddenField)Page.FindControl("hdnPreferenceID" + k);

                        ddlDropddlRange.SelectedIndex = -1;

                        ddlDropddlMake.SelectedIndex = ddlDropddlMake.Items.IndexOf(ddlDropddlMake.Items.FindByValue(dsPreferences.Tables[1].Rows[i]["Makeid"].ToString()));

                        GetModelsInfo(ddlDropddlMake.SelectedValue, ddlDropddlModel);

                        ddlDropddlModel.SelectedIndex = ddlDropddlModel.Items.IndexOf(ddlDropddlModel.Items.FindByValue(dsPreferences.Tables[1].Rows[i]["ModelID"].ToString()));

                        //ddlDropddlRange.SelectedIndex = ddlDropddlRange.Items.IndexOf(ddlDropddlRange.Items.FindByValue(dsPreferences.Tables[1].Rows[i]["PriceRange"].ToString()));

                        ddlDropddlRange.SelectedIndex = Convert.ToInt32(dsPreferences.Tables[1].Rows[i]["PriceRange"].ToString());

                        hdnPreferenceID.Value = dsPreferences.Tables[1].Rows[i]["PreferenceID"].ToString();

                        k = k + 1;
                    }
                }
                else
                {
                    TotalRowcount.Value = "1";
                }
            }
        }
    }
    public string SubscribeItems(string SelectedMake, string SelectedModel, string SelectedRange, string sPrefernceID, string sPrefernceItem, int i)
    {
        PreferencesBL objPreferencesBL = new PreferencesBL();

        Session[Constants.Subscribe] = 1;

        PreferncesItemsInfo ObjPreferncesItemsInfo = new PreferncesItemsInfo();

        DataSet dssub = new DataSet();

        if (SelectedMake != "0" && SelectedModel != "0")
        {

            ObjPreferncesItemsInfo.Makeid = SelectedMake;

            ObjPreferncesItemsInfo.ModelID = SelectedModel;

            ObjPreferncesItemsInfo.PriceRange = SelectedRange;

            ObjPreferncesItemsInfo.UserPreferID = sPrefernceID;

            //HiddenField hdnPreferenceID = (HiddenField)Page.FindControl("hdnPreferenceID" + i);

            ObjPreferncesItemsInfo.PreferenceID = sPrefernceItem;

            dssub = objPreferencesBL.SaveSubScribeItems(ObjPreferncesItemsInfo, true);
        }
        else
        {
            if (sPrefernceID != "")
            {
                if (SelectedMake != "0" && SelectedModel != "0")
                {
                    ObjPreferncesItemsInfo.Makeid = SelectedMake;

                    ObjPreferncesItemsInfo.ModelID = SelectedModel;

                    ObjPreferncesItemsInfo.PriceRange = SelectedRange;

                    ObjPreferncesItemsInfo.UserPreferID = sPrefernceID;

                    ObjPreferncesItemsInfo.PreferenceID = sPrefernceID;

                    dssub = objPreferencesBL.SaveSubScribeItems(ObjPreferncesItemsInfo, false);
                }
            }
        }
        return i.ToString();
    }
    public string SubScribe(string PreferenceID, string sZip, string sName, string sEmail, string sPhoneNo)
    {
        Session[Constants.Subscribe] = 1;

        PreferencesBL objPreferencesBL = new PreferencesBL();

        DataSet dssub = new DataSet();

        PreferenceInfo ObjPreferncesInfo = new PreferenceInfo();

        PreferncesItemsInfo ObjPreferncesItemsInfo = new PreferncesItemsInfo();

        dssub = objPreferencesBL.GetEmailPreferencesbyEmail(sEmail);

        string sPreferenceID = string.Empty;

        if (dssub.Tables[0].Rows.Count == 0)
        {
            ObjPreferncesInfo.PreferenceID = PreferenceID;

            if (sZip != "Zip")
            {
                ObjPreferncesInfo.Zip = sZip;
            }
            if (sName != "Your Name")
            {
                ObjPreferncesInfo.Name = sName;
            }

            if (sEmail != "Your Email")
            {
                ObjPreferncesInfo.Email = sEmail;
            }

            if (sPhoneNo != "Your Phone")
            {
                ObjPreferncesInfo.Phone = sPhoneNo;
            }

            String strHostName = HttpContext.Current.Request.UserHostAddress.ToString();

            ObjPreferncesInfo.IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

            if (HttpContext.Current.Request.Cookies["UserSettings"] == null)
            {
                HttpCookie myCookie = new HttpCookie("UserSettings");

                myCookie.Expires = DateTime.Now.AddDays(500);

                myCookie.Values.Add("Email", ObjPreferncesInfo.Email);
                myCookie.Values.Add("Name", ObjPreferncesInfo.Name);
                myCookie.Values.Add("Phone", ObjPreferncesInfo.Phone);

                HttpContext.Current.Response.Cookies.Add(myCookie);
            }

            DataSet dsPreferences = new DataSet();

            dsPreferences = objPreferencesBL.SaveSubscribe(ObjPreferncesInfo, 1);

            clsMailFormats format = new clsMailFormats();
            MailMessage msg = new MailMessage();

            msg.From = new MailAddress(CommonVariable.FromInfoMail);

            msg.To.Add(sEmail);

            //msg.Bcc.Add("[email protected]");

            msg.Subject = "Successfully modified your personalized weekly email alerts - United Car Exchange.";

            msg.IsBodyHtml = true;

            string text = string.Empty;

            string VerifyPreferences = string.Empty;

            string ModifyPreferences = string.Empty;

            string PreferencesID = string.Empty;

            //VerifyPreferences = "www.unitedcarexchange.com/VerifyPreferences.aspx?Preferce=" + dssub.Tables[0].Rows[0]["ID"].ToString();

            //ModifyPreferences = "www.unitedcarexchange.com/EmailPreferences.aspx?Preferce=" + dssub.Tables[0].Rows[0]["ID"].ToString();

            PreferencesID = dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString();

            text = format.SendEMailPreferencesEdit(dsPreferences.Tables[0].Rows[0]["Name"].ToString(), dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString(), ref text);

            msg.Body = text.ToString();

            SmtpClient smtp = new SmtpClient();

            //smtp.Host = "smtp.gmail.com";
            //smtp.Port = 587;
            //smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "hugomirad");
            //smtp.EnableSsl = true;
            //smtp.Send(msg);

            smtp.Host = "127.0.0.1";
            smtp.Port = 25;
            smtp.Send(msg);

            return PreferencesID;
        }
        else
        {
            return "true";

        }
    }
    protected void imgBtnSubscribe_Click(object sender, ImageClickEventArgs e)
    {
        PreferencesBL objPreferencesBL = new PreferencesBL();

        DataSet dssub = new DataSet();

        dssub = objPreferencesBL.GetEmailPreferencesbyEmail(txtSub.Text);

        if (dssub.Tables[0].Rows.Count > 0)
        {
            Response.Redirect("EmailPreferences.aspx?PreferID=" + dssub.Tables[0].Rows[0]["UserPreferID"].ToString() + "");
        }
        else
        {
            FillMakes();

            FillWithin();

            txtEmailAlert.Text = txtSub.Text;

            mpesubscribe.Show();
        }
    }
    protected void btnSubscribe_Click(object sender, EventArgs e)
    {
        mpesubscribe.Hide();

        PreferencesBL objPreferencesBL = new PreferencesBL();

        DataSet dssub = new DataSet();

        PreferenceInfo ObjPreferncesInfo = new PreferenceInfo();

        PreferncesItemsInfo ObjPreferncesItemsInfo = new PreferncesItemsInfo();

        ObjPreferncesInfo.PreferenceID = "0";

        ObjPreferncesInfo.Zip = txtZip.Text;

        ObjPreferncesInfo.Name = txtName.Text;

        if (txtEmailAlert.Text != "Your Email")
        {
            ObjPreferncesInfo.Email = txtEmailAlert.Text;
        }

        if (txtPhoneNo.Text != "Your Phone")
        {
            ObjPreferncesInfo.Phone = txtPhoneNo.Text;
        }

        String strHostName = HttpContext.Current.Request.UserHostAddress.ToString();

        ObjPreferncesInfo.IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

        if (Request.Cookies["UserSettings"] == null)
        {
            HttpCookie myCookie = new HttpCookie("UserSettings");

            myCookie.Expires = DateTime.Now.AddDays(500);

            myCookie.Values.Add("Email", ObjPreferncesInfo.Email);
            myCookie.Values.Add("Name", ObjPreferncesInfo.Name);
            myCookie.Values.Add("Phone", ObjPreferncesInfo.Phone);

            Response.Cookies.Add(myCookie);
        }

        ObjPreferncesItemsInfo.Makeid = ddlMake1.SelectedValue;
        ObjPreferncesItemsInfo.ModelID = ddlModel1.SelectedValue;
        ObjPreferncesItemsInfo.PriceRange = ddlRange1.SelectedValue;

        DataSet dsEmailExist = new DataSet();

        DataSet dsPreferences = new DataSet();

        dsEmailExist = objPreferencesBL.GetEmailPreferencesbyEmail(txtEmailAlert.Text);

        if (dsEmailExist.Tables[0].Rows.Count == 0)
        {
            dsPreferences = objPreferencesBL.SaveSubscribe(ObjPreferncesInfo, 1);

            for (int i = 1; i < 6; i++)
            {
                string SelectedMake = string.Empty;
                string SelectedModel = string.Empty;
                string SelectedRange = string.Empty;

                if (i == 1)
                {
                    SelectedMake = ddlMake1.SelectedValue;
                    SelectedModel = ddlModel1.SelectedValue;
                    SelectedRange = ddlRange1.SelectedValue;
                }
                else if (i == 2)
                {
                    SelectedMake = ddlMake2.SelectedValue;
                    SelectedModel = ddlModel2.SelectedValue;
                    SelectedRange = ddlRange2.SelectedValue;
                }
                else if (i == 3)
                {
                    SelectedMake = ddlMake3.SelectedValue;
                    SelectedModel = ddlModel3.SelectedValue;
                    SelectedRange = ddlRange3.SelectedValue;
                }
                else if (i == 4)
                {
                    SelectedMake = ddlMake4.SelectedValue;
                    SelectedModel = ddlModel4.SelectedValue;
                    SelectedRange = ddlRange4.SelectedValue;
                }
                else if (i == 5)
                {
                    SelectedMake = ddlMake5.SelectedValue;
                    SelectedModel = ddlModel5.SelectedValue;
                    SelectedRange = ddlRange5.SelectedValue;
                }
                if (SelectedMake != "0" && SelectedModel != "0")
                {

                    ObjPreferncesItemsInfo.Makeid = SelectedMake;

                    ObjPreferncesItemsInfo.ModelID = SelectedModel;

                    ObjPreferncesItemsInfo.PreferenceID = "0";

                    ObjPreferncesItemsInfo.PriceRange = SelectedRange;

                    ObjPreferncesItemsInfo.UserPreferID = dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString();

                    dssub = objPreferencesBL.SaveSubScribeItems(ObjPreferncesItemsInfo, true);
                }
            }
            clsMailFormats format = new clsMailFormats();

            MailMessage msg = new MailMessage();

            msg.From = new MailAddress(txtEmailAlert.Text);

            msg.To.Add("[email protected]");

            //msg.Bcc.Add();

            msg.Subject = "Welcome to your personalized weekly email alerts preferences.";

            msg.IsBodyHtml = true;

            string text = string.Empty;

            string VerifyPreferences = string.Empty;

            string ModifyPreferences = string.Empty;

            string PreferencesID = string.Empty;

            //VerifyPreferences = "www.unitedcarexchange.com/VerifyPreferences.aspx?Preferce=" + dssub.Tables[0].Rows[0]["ID"].ToString();

            //ModifyPreferences = "www.unitedcarexchange.com/EmailPreferences.aspx?Preferce=" + dssub.Tables[0].Rows[0]["ID"].ToString();

            PreferencesID = dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString();

            GeneralFunc obj = new GeneralFunc();

            text = format.SendEMailPreferences(GeneralFunc.ToProper(dsPreferences.Tables[0].Rows[0]["Name"].ToString()), dsPreferences.Tables[0].Rows[0]["UserPreferID"].ToString(), ref text);

            msg.Body = text.ToString();

            SmtpClient smtp = new SmtpClient();

            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "hugomirad");
            smtp.EnableSsl = true;
            smtp.Send(msg);

            //smtp.Host = "127.0.0.1";
            //smtp.Port = 25;
            //smtp.Send(msg);

            // Progress111.Visible = false;

            Type cstype = GetType();

            ClientScriptManager cs = Page.ClientScript;

            cs.RegisterStartupScript(cstype, "KyRST", "<script type='text/javascript' language='javascript'>pageLoad();</script>");

            lblAlertMsg.Text = "Thank you for signing up for automatic email alerts..";
            //mpealert.Show();
        }
        else
        {

            Type cstype = GetType();

            ClientScriptManager cs = Page.ClientScript;

            cs.RegisterStartupScript(cstype, "KyRST", "<script type='text/javascript' language='javascript'>pageLoad();</script>");

            lblAlertMsg.Text = "Email ID Already Subscribed..";
            //mpealert.Show();

        }
    }