﻿function app_doLocationSearch(q) {
    if (q.length > 0) {
        window.location = "/about/locations/?postalcode=" + q + "&radius=10&page=1";
    }
}

function app_showEmmaForm(a, b, c, d) {
    a.css({ display: "none" });
    b.css({ display: "block" });
    
    if (d.toLowerCase() == c.toLowerCase()) { d = ""; }
    // Push the email address that was entered into the emma form
    // and set focus on the first name field.
    var $member_email = $("#signup-ifrm").contents().find("input:text[name='emma_member_email']", "#e2ma_signup").val(d);
    var $member_first = $("#signup-ifrm").contents().find("input:text[name='emma_member_name_first']", "#e2ma_signup").focus();
}

$(function () {
    // Setup the location search widget on home page.
    var $q = $("#txtZipBoxQ"),
        $g = $("#btnZipBoxGo");
    if ($q.length > 0 && $g.length > 0) {
        $q.focus(function() { this.select(); }).keypress(function (e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                app_doLocationSearch($q.val());
                return false;
            } else {
                return true;
            }
        });

        $g.css({ cursor: "pointer" }).click(function () {
            app_doLocationSearch($q.val());
        });
    }

    // Setup the email opt-in stuff.
    var default_text = "Enter Email",
        $e = $("#txtEmailSignup"),
        $b = $("#btnEmailSignup"),
        $f1 = $("#signup"),
        $f2 = $("#signup-form");

    if ($e.length > 0 && $b.length > 0 && $f1.length > 0 && $f2.length > 0) {
        $e.focus(function () {
            this.select();
        }).keypress(function (e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                app_showEmmaForm($f1, $f2, default_text, $e.val());
                return false;
            } else {
                return true;
            }
        }); ;

        $b.css({ cursor: "pointer" }).click(function () {
            app_showEmmaForm($f1, $f2, default_text, $e.val());
        });
    }
});
