<%@ Page Language="JScript" %>
<%@ Register TagPrefix="IBuySpy" TagName="Menu" Src="_Menu.ascx" %>
<%@ Register TagPrefix="IBuySpy" TagName="Header" Src="_Header.ascx" %>

<script runat="server">

    //*******************************************************
    //
    // The LoginBtn_Click event is used on this page to
    // authenticate a customer's supplied username/password
    // credentials against a database.
    //
    // If the supplied username/password are valid, then
    // the event handler adds a cookie to the client
    // (so that we can personalize the home page's welcome
    // message), migrates any items stored in the user's
    // temporary (non-persistent) shopping cart to their
    // permanent customer account, and then redirects the browser
    // back to the originating page.
    //
    //*******************************************************

    function LoginBtn_Click(sender: Object, e: ImageClickEventArgs) : void {
        // Only attempt a login if all form fields on the page are valid
        if (Page.IsValid == true) {

            // Save old ShoppingCartID
			var shoppingCart : IBuySpy.ShoppingCartDB = new IBuySpy.ShoppingCartDB();
            var tempCartID : String = shoppingCart.GetShoppingCartId();

            // Attempt to Validate User Credentials using CustomersDB
            var accountSystem : IBuySpy.CustomersDB  = new IBuySpy.CustomersDB();
            var customerId : String  = accountSystem.Login(email.Text, password.Text);

            if (customerId != null) {

                // Migrate any existing shopping cart items into the permanent shopping cart
                shoppingCart.MigrateCart(tempCartID, customerId);

                // Lookup the customer's full account details
                var customerDetails : IBuySpy.CustomerDetails  = accountSystem.GetCustomerDetails(customerId);

                // Store the user's fullname in a cookie for personalization purposes
                Response.Cookies["IBuySpy_FullName"].Value = customerDetails.FullName;

                // Make the cookie persistent only if the user selects "persistent" login checkbox
                if (RememberLogin.Checked ==  true) {
                    Response.Cookies["IBuySpy_FullName"].Expires = DateTime.Now.AddMonths(1);
                }

                // Redirect browser back to originating page
                FormsAuthentication.RedirectFromLoginPage(customerId, RememberLogin.Checked);
            }
            else {
                Message.Text = "Login Failed!";
            }
        }
    }


</script>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="IBuySpy.css">
    </head>
    <body background="images/sitebkgrd.gif" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginheight="0" marginwidth="0">
        <table cellspacing="0" cellpadding="0" width="100%" border="0">
            <tr>
                <td colspan="2">
                    <IBuySpy:Header ID="Header1" runat="server" />
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <IBuySpy:Menu id="Menu1" runat="server" />
                    <img height="1" src="images/1x1.gif" width="145">
                </td>
                <td align="left" valign="top" width="100%" nowrap>
                    <table height="100%" align="left" cellspacing="0" cellpadding="0" width="100%" border="0">
                        <tr valign="top">
                            <td nowrap>
                                <br>
                                <form runat="server">
                                    <img align="left" width="24" height="1" src="images/1x1.gif">
                                    <table cellspacing="0" cellpadding="0" width="100%" border="0">
                                        <tr>
                                            <td class="ContentHead">
                                                <img align="left" height="32" width="60" src="images/1x1.gif">Sign Into Your Account
                                                <br>
                                            </td>
                                        </tr>
                                    </table>
                                    <img align="left" height="1" width="92" src="images/1x1.gif">
                                    <table height="100%" cellspacing="0" cellpadding="0" border="0">
                                        <tr valign="top">
                                            <td width="550">
                                                <asp:Label id="Message" class="ErrorText" runat="server" />
                                                <br>
                                                <br>
                                                 <span class="NormalBold">Email</span>
                                                <br>
                                                 <asp:TextBox size="25" id="email" runat="server" /> 
                                                <asp:RequiredFieldValidator id="emailRequired" ControlToValidate="email" Display="dynamic" Font-Name="verdana" Font-Size="9pt" ErrorMessage="'Name' must not be left blank." runat="server" />
                                                <asp:RegularExpressionValidator id="emailValid" ControlToValidate="email" ValidationExpression="[\w\.-]+(\+[\w-]*)?@([\w-]+\.)+[\w-]+" Display="Dynamic" ErrorMessage="Must use a valid email address." runat="server" />
                                                <br>
                                                <br>
                                                 <span class="NormalBold">Password</span>
                                                <br>
                                                 <asp:TextBox id="password" textmode="password" size="25" runat="server" /> 
                                                <asp:RequiredFieldValidator id="passwordRequired" ControlToValidate="password" Display="Static" Font-Name="verdana" Font-Size="9pt" ErrorMessage="'Password' must not be left blank." runat="server" />
                                                <br>
                                                <br>
                                                <asp:checkbox id="RememberLogin" runat="server" />
                                                <span class="NormalBold">Remember My Sign-In Across Browser Restarts</span>
                                                <br>
                                                <br>
                                                <br>
                                                <asp:ImageButton id="LoginBtn" ImageURL="images/sign_in_now.gif" OnClick="LoginBtn_Click" runat="server" />
                                                <br>
                                                <br>
                                                <span class="Normal"> If you are a new user and you don't have an account with IBuySpy, then register for one now.</span>
                                                <br>
                                                <br>
                                                <a href="register.aspx"><img border="0" src="images/register.gif"></a>
                                            </td>
                                        </tr>
                                    </table>
                                </form>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </body>
</html>