﻿Imports com.racadtech.Framework.Database
Imports com.racadtech.Framework.Web.Security
Imports com.racadtech.services

Partial Class order_centre_login
    Inherits System.Web.UI.Page
    Private _webAdapter As New WebWS()
    Private _webUserControl As New com.racadtech.Framework.Web.Security.UserControl()
    Private _goPrint2Adapter As New com.racadtech.services.GoPrint2Client()

    Public ReadOnly Property ErrorMessage() As String
        Get
            If (Session("NewUser.ErrorMessage") Is Nothing) Then
                Return ""
            Else
                Return Session("NewUser.ErrorMessage")
            End If
        End Get
    End Property

    Public ReadOnly Property DatabaseInstance() As Database
        Get
            Return DatabaseManager.CreateDatabase("PrintIt")
        End Get
    End Property

    Public ReadOnly Property WebUserControl() As com.racadtech.Framework.Web.Security.UserControl
        Get
            Return _webUserControl
        End Get
    End Property

    Public ReadOnly Property GoPrint2Adapter() As com.racadtech.services.GoPrint2Client
        Get
            Return _goPrint2Adapter
        End Get
    End Property

    Public ReadOnly Property CurrentActivationId() As String
        Get
            Return WebAdapter.GetActivationIdByStoreId(Session("StoreId")).ToString()
        End Get
    End Property

    Public ReadOnly Property WebAdapter() As WebWS
        Get
            Return _webAdapter
        End Get
    End Property

    Public ReadOnly Property MembershipProviderInstance() As MembershipProvider
        Get
            Return Membership.Provider
        End Get
    End Property

    Public ReadOnly Property RoleProviderInstance() As RoleProvider
        Get
            Return Roles.Provider
        End Get
    End Property

    Public ReadOnly Property MasterKey() As String
        Get
            Return "877k31hg333-d03423332wq-312dkazlfs-433398j543"
        End Get
    End Property

    Public ReadOnly Property BaseImageURL() As String
        Get
            If Not Session("BaseImageURL") Is Nothing Then
                Return Session("BaseImageURL")
            Else
                Return "themes/orange/"
            End If
        End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Session("NewUser.ErrorMessage") Is Nothing Then
            Me.ErrorLabel.Text = Session("NewUser.ErrorMessage").ToString()
        End If
        If Not Page.IsPostBack Then
            Me.registered_user.Visible = True
            Me.new_user.Visible = False
            Me.guestForm.Visible = False
            Me.Panel1.DefaultButton = "LoginButton"
            If Request.QueryString("user") = "new" Then
                Me.registered_user.Visible = False
                Me.new_user.Visible = True
                Me.guestForm.Visible = False
                Me.Panel1.DefaultButton = "NewUserButton"
            ElseIf Request.QueryString("user") = "guest" Then
                Me.registered_user.Visible = False
                Me.new_user.Visible = False
                Me.guestForm.Visible = True
                If Not Request.Cookies("FNameTextBox") Is Nothing Then FNameTextBox.Text = Request.Cookies("FNameTextBox").Value
                If Not Request.Cookies("LNameTextBox") Is Nothing Then LNameTextBox.Text = Request.Cookies("LNameTextBox").Value
                If Not Request.Cookies("CompanyNameTextBox") Is Nothing Then CompanyNameTextBox.Text = Request.Cookies("CompanyNameTextBox").Value
                If Not Request.Cookies("AddressTextBox") Is Nothing Then AddressTextBox.Text = Request.Cookies("AddressTextBox").Value
                If Not Request.Cookies("CityTextBox") Is Nothing Then CityTextBox.Text = Request.Cookies("CityTextBox").Value
                If Not Request.Cookies("StateTextBoxAlt") Is Nothing Then StateTextBoxAlt.Text = Request.Cookies("StateTextBoxAlt").Value
                If Not Request.Cookies("ZipTextBox") Is Nothing Then ZipTextBox.Text = Request.Cookies("ZipTextBox").Value

                'Response.Write(Request.Cookies("CountryDropDownList"))

                'If (Not Request.Cookies("CountryDropDownList") Is Nothing) Or (Request.Cookies("CountryDropDownList").ToString <> "") Then CountryDropDownList.SelectedValue = Request.Cookies("CountryDropDownList").Value
                If Not Request.Cookies("PhoneTextBox") Is Nothing Then PhoneTextBox.Text = Request.Cookies("PhoneTextBox").Value
                If Not Request.Cookies("FaxTextBox") Is Nothing Then FaxTextBox.Text = Request.Cookies("FaxTextBox").Value
                If Not Request.Cookies("EmailTextBox") Is Nothing Then EmailTextBox.Text = Request.Cookies("EmailTextBox").Value
                Me.Panel1.DefaultButton = "GuestLoginButton"
            End If
            Dim _ds As System.Data.DataSet = WebAdapter.SelectWithSQLQuery("Select * From PI_StoreOrderCentreSettings Where StoreId='" & Session("StoreId") & "'")

            If _ds.Tables(0).Rows.Count > 0 Then
                If Not _ds.Tables(0).Rows(0)("ShowTermsAndConditions") Is DBNull.Value Then
                    TermsAndConditionsTr.Visible = _ds.Tables(0).Rows(0)("ShowTermsAndConditions")
                    TermsAndConditionsTr2.Visible = _ds.Tables(0).Rows(0)("ShowTermsAndConditions")
                End If
                If Not _ds.Tables(0).Rows(0)("SimpleLogin") Is DBNull.Value Then
                    Dim _fieldsVisibility As Boolean = True
                    If _ds.Tables(0).Rows(0)("SimpleLogin") Then _fieldsVisibility = False
                    CompanyNameTr.Visible = _fieldsVisibility
                    AddressTr.Visible = _fieldsVisibility
                    CityTr.Visible = _fieldsVisibility
                    CountryTr.Visible = _fieldsVisibility
                    StateTr.Visible = _fieldsVisibility
                    ZipTr.Visible = _fieldsVisibility
                    PhoneTr.Visible = _fieldsVisibility
                    FaxTr.Visible = _fieldsVisibility
                End If
            End If
        End If

        '<script language="javascript" type="text/javascript">
        'function TermsCheckBoxFunc2(source, args)
        '{
        '    args.IsValid = document.getElementById('<%= TermsCheckBox2.ClientID %>').checked;
        '}
        'function TermsCheckBoxFunc(source, args)
        '{
        '    args.IsValid = document.getElementById('<%= TermsCheckBox.ClientID %>').checked;
        '}
        '</script>

    End Sub


    Protected Sub NewUserButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NewUserButton.Click
        Dim _userName As String = Session("StoreId") & "_" & UserNameTextBox.Text.ToString()
        Dim _userDetails As UserDetails = WebUserControl.CreateUser(MembershipProviderInstance, _
                          RoleProviderInstance, _userName, _
                          PasswordTextBox.Text.ToString(), EmailTextBox2.Text.ToString(), "PrintItWebCustomer")

        If (_userDetails.ErrorCode > 0) Then ' Failed
            Session("NewUser.ErrorMessage") = _userDetails.ErrorMessage
            Response.Redirect("~/order_centre/login.aspx?user=new")
        Else
	    Dim _storeId As String = Session("StoreId")
            FormsAuthentication.SetAuthCookie(_userName, True)
            Dim _userProfile As ProfileCommon
            _userProfile = ProfileCommon.Create(_userName)
	    '_userProfile.StoreId = 999999
            _userProfile.StoreId = _storeId
            _userProfile.Save()

            If (_storeId.Equals("2066")) Then
                Response.Redirect("~/order_centre/customer_contact_info_carters.aspx")
            Else
                Response.Redirect("~/order_centre/customer_contact_info.aspx")
            End If

        End If
    End Sub

    Protected Sub GuestLoginButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles GuestLoginButton.Click
        ' Fake User Session with pre-existing user in the system.
        FormsAuthentication.SetAuthCookie("GoPrint2Guest", True)

        ' Insert Guest Contact Information.
        ' 

        Dim _companyName As String = ""
        Dim _address As String = ""
        Dim _city As String = ""
        Dim _country As String = ""
        Dim _state As String = ""
        Dim _zip As String = ""
        Dim _phone As String = ""
        Dim _fax As String = ""

        If CompanyNameTr.Visible Then
            '' CompanyNameTr.Visible = False means that other Tr.Visible = False
            _companyName = CompanyNameTextBox.Text
            _address = AddressTextBox.Text
            _city = CityTextBox.Text
            _country = CountryDropDownList.SelectedValue
            _state = StateTextBoxAlt.Text
            _zip = ZipTextBox.Text
            _phone = PhoneTextBox.Text
            _fax = FaxTextBox.Text
        End If



        Dim _customerDetails As CustomerDetails = _goPrint2Adapter.UpdateCustomerDetailsWithUserId("", "c12e2abe-d778-4a21-882f-00c247c5b01b", _
                                                         CurrentActivationId, FNameTextBox.Text, _
                                                         LNameTextBox.Text, _companyName, _
                                                         _address, "", _city, _state, _zip, _
                                                         _country, _phone, _fax, EmailTextBox.Text, MasterKey)

        Dim newCookie As HttpCookie = New HttpCookie("FNameTextBox")
        newCookie.Value = FNameTextBox.Text
        newCookie.Expires = #12/31/2099#
        Response.Cookies.Add(newCookie)

        newCookie = New HttpCookie("LNameTextBox")
        newCookie.Value = LNameTextBox.Text
        newCookie.Expires = #12/31/2099#
        Response.Cookies.Add(newCookie)

        newCookie = New HttpCookie("CompanyNameTextBox")
        newCookie.Value = _companyName
        newCookie.Expires = #12/31/2099#
        Response.Cookies.Add(newCookie)

        newCookie = New HttpCookie("AddressTextBox")
        newCookie.Value = _address
        newCookie.Expires = #12/31/2099#
        Response.Cookies.Add(newCookie)

        newCookie = New HttpCookie("CityTextBox")
        newCookie.Value = _city
        newCookie.Expires = #12/31/2099#
        Response.Cookies.Add(newCookie)

        newCookie = New HttpCookie("StateTextBoxAlt")
        newCookie.Value = _state
        newCookie.Expires = #12/31/2099#
        Response.Cookies.Add(newCookie)

        newCookie = New HttpCookie("ZipTextBox")
        newCookie.Value = _zip
        newCookie.Expires = #12/31/2099#
        Response.Cookies.Add(newCookie)

        If _country <> "" Then
            newCookie = New HttpCookie("CountryDropDownList")
            newCookie.Value = _country
            newCookie.Expires = #12/31/2099#
            Response.Cookies.Add(newCookie)
        Else
            Response.Cookies.Remove("CountryDropDownList")
        End If

        newCookie = New HttpCookie("PhoneTextBox")
        newCookie.Value = _phone
        newCookie.Expires = #12/31/2099#
        Response.Cookies.Add(newCookie)

        newCookie = New HttpCookie("FaxTextBox")
        newCookie.Value = _fax
        newCookie.Expires = #12/31/2099#
        Response.Cookies.Add(newCookie)

        newCookie = New HttpCookie("EmailTextBox")
        newCookie.Value = EmailTextBox.Text
        newCookie.Expires = #12/31/2099#
        Response.Cookies.Add(newCookie)

        Session("CustomerId") = _customerDetails.CustomerID
        Response.Redirect("~/order_centre/job_options.aspx")
    End Sub


    Protected Sub NotRegisteredLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.registered_user.Visible = False
        Me.new_user.Visible = True
        Me.guestForm.Visible = False
    End Sub

    Protected Sub RegisteredLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RegisteredLinkButton.Click
        Me.registered_user.Visible = True
        Me.new_user.Visible = False
        Me.guestForm.Visible = False
    End Sub



    'Protected Sub Login1_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoggedIn
    '    If Not Roles.IsUserInRole(Login1.UserName, "PrintItWebCustomer") Then
    '        Response.Redirect("~/order_centre/login.aspx")
    '    End If
    '    Dim _userProfile As ProfileCommon = Profile.GetProfile(Login1.UserName)
    '    'Session("StoreId") = _userProfile.StoreId
    'End Sub

    Protected Sub LoginButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginButton.Click
        Dim _userName As String = Session("StoreId") & "_" & UserName.Text.ToString()
        If WebUserControl.ValidateUser(MembershipProviderInstance, _userName, Password.Text.ToString()) Then
            'Dim _user As MembershipUser = New MembershipUser(MembershipProviderInstance.Name, _userName, _
            '                                                MembershipProviderInstance.GetUser(_userName, True).ProviderUserKey, _
            '                                                MembershipProviderInstance.GetUser(_userName, True).Email, _
            '                                                MembershipProviderInstance.GetUser(_userName, True).PasswordQuestion, _
            '                                                MembershipProviderInstance.GetUser(_userName, True).Comment, _
            '                                                 MembershipProviderInstance.GetUser(_userName, True).IsApproved, _
            '                                                 MembershipProviderInstance.GetUser(_userName, True).IsLockedOut, _
            '                                                 MembershipProviderInstance.GetUser(_userName, True).CreationDate, _
            '                                                 MembershipProviderInstance.GetUser(_userName, True).LastLoginDate, _
            '                                                 MembershipProviderInstance.GetUser(_userName, True).LastActivityDate, _
            '                                                 MembershipProviderInstance.GetUser(_userName, True).LastPasswordChangedDate, _
            '                                                 MembershipProviderInstance.GetUser(_userName, True).LastLockoutDate)
            If Not Roles.IsUserInRole(_userName, "PrintItWebCustomer") Then
                Response.Redirect("~/order_centre/login.aspx")
            Else
                FormsAuthentication.SetAuthCookie(_userName, True)
                ' FormsAuthentication.RedirectFromLoginPage(_userName, True)
                Dim _userProfile As ProfileCommon = Profile.GetProfile(_userName)

                Dim _storeId As String = Session("StoreId")

                If (_storeId.Equals("2066")) Then
                    Response.Redirect("~/order_centre/customer_contact_info_carters.aspx")
                End If

                Response.Redirect("~/order_centre/customer_contact_info.aspx")
            End If
        Else
            Response.Redirect("~/order_centre/login.aspx")
        End If

        'Response.Write(Session("StoreId") & "_" & UserName.Text.ToString())

    End Sub

End Class
