﻿$(document).ready(function () {
    var tabContainers = $('div.podWrapper > div.newProdContent');
    tabContainers.hide().filter(':first').show();

    $('ul#navtabs a').click(function () {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('ul#navtabs a').removeClass('hilitea');
        $('ul#navtabs li').removeClass('hilite');
        $(this).addClass('hilitea');
        $(this).parent('li').addClass('hilite');
        return false;
    });

    $("#footer a").mouseover(function (e) {
        e.preventDefault();
        var footerTop = $("div#footer").offset().top;
        var footerLeft = $("div#footer").offset().left;
        $("#mapplacerholder").attr("src", $(this).attr('href'));
        $("#mapplacerholder").css({ "top": footerTop - 287, "left": footerLeft });
        $("#mapplacerholder").toggle();
    }).mouseout(function () {
        $("#mapplacerholder").toggle();
        $("#mapplacerholder").css({ "top": "-1000px", "left": "-1000px" });
    });
    $("#footer a").click(function (e) {
        e.preventDefault();
    });

    $("#signup").click(function (e) {
        addSubscriber();
        e.preventDefault();
    });

    $("a.close").click(function () {
        $(".handle").click();
    });

    $("#txtName, #txtEmail").focus(function () {
        clearErrors();
        $(this).val("");
    });
    $("#preferredStore, #chkAgeVerification").focus(function () {
        clearErrors();
    });
    $("#txtName").blur(function () {
        if ($(this).val() == "") {
            $(this).val("Name");
        }
    });
    $("#txtEmail").blur(function () {
        if ($(this).val() == "") {
            $(this).val("Email");
        }
    });
});

function addSubscriber() {
    clearErrors();
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    var flagTest = true;
    if (!$("#txtName").val() || $("#txtName").val() == "Name") {
        $("#txtName").addClass("error");
        flagTest = false;
    }
    if (!filter.test($("#txtEmail").val())) {
        $("#txtEmail").addClass("error");
        flagTest = false;
    }

    if ($("#preferredStore").val() == "0") {
        $("#preferredStore").addClass("error");
        flagTest = false;
    }

    if (!$("#chkAgeVerification").is(":checked")) {
        $("#spanAgeVerification").addClass("error");
        flagTest = false;
    }

    if (flagTest) {
        $.ajax({ type: "POST",
            async: false,
            url: "/WebService.asmx/addSubscriber",
            data: "name=" + $("#txtName").val() + "&email=" + $("#txtEmail").val() + "&preferredStore=" + $("#preferredStore").val() + "&signupType=website&ageVerify=Yes",
            success: function (xml) {
                var returnOutput = $(xml).find('boolean').text();
                if (returnOutput == "true") {
                    $("#signUpform").hide();
                    $("#subscribed").show();
                    $("#alreadysubscribed").hide();
                    $("#txtName").val("");
                    $("#txtEmail").val("");
                    $("#preferredStore").val("0");
                } else {
                    $("#signUpform").hide();
                    $("#subscribed").hide();
                    $("#alreadysubscribed").show();
                    $("#txtName").val("");
                    $("#txtEmail").val("");
                    $("#preferredStore").val("0");
                    $('#chkAgeVerification').attr('checked', false);
                }
            },
            error: function () {
                $("#signUpform").hide();
                $("#subscribed").hide();
                $("#alreadysubscribed").hide();
                $("#error").show();
                $("#txtName").val("");
                $("#txtEmail").val("");
                $("#preferredStore").val("0");
                $('#chkAgeVerification').attr('checked', false);
            }
        });
    } else {
        return false;
    }
}

function clearErrors() {
    $("input, select").removeClass("error");
    $("#spanAgeVerification").removeClass("error");
}

startList = function () {
    if (document.all && document.getElementById) {
        navRoot = document.getElementById("siteNav");
        for (i = 0; i < navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            if (node.nodeName == "LI") {
                node.onmouseover = function () {
                    this.className += " over";
                }
                node.onmouseout = function () {
                    this.className = this.className.replace(" over", "");
                }
            }
        }
    }
}
window.onload = startList;

