﻿$(document).ready(function () {

    SetupSearchForm();

    //Start Slides
    $('.homeSlideShow').cycle({ fx: 'fade', speed: 1500, timeout: 7000 });

    //Setup Homepage tab content behaviors
    $('td.TabTitle').click(function () { ShowTab($(this).attr("id")); });

    //Show the second from the left by default.
    ShowTab("Tab2");

    //Setup homepage newsletter signup form
    SetupNewsletterForm();
});

function SetupSearchForm() {
    SetupWatermark($('#txtSearch'), "Search");

    //Setup enterkey detection in header search
    $('#txtSearch').keypress(function (e) {
        if (e.which == 13 || e.keyCode == 13) {
            Search();
            return false;
        }
    });

    $(".searchBtn").click(Search);
}

function SetupNewsletterForm() {
    SetupWatermark($('#txtNewsletterName'), "Name");
    SetupWatermark($('#txtNewsletterEmail'), "Email address");

    //Setup enterkey detection in header search
    $('#txtNewsletterName').keypress(function (e) {
        if (e.which == 13 || e.keyCode == 13) {
            NewsletterSignup();
            return false;
        }
    });
    $('#txtNewsletterEmail').keypress(function (e) {
        if (e.which == 13 || e.keyCode == 13) {
            NewsletterSignup();
            return false;
        }
    });

    $("#btnNewsletterSignup").click(NewsletterSignup);
}

function NewsletterSignup() {
    var name = $('#txtNewsletterName').val();
    var email = $('#txtNewsletterEmail').val();

    //Validate name
    if (name.Length == 0 || name == "Name") {
        alert('Name is required.');
        $('#txtNewsletterName').focus();
        return;
    }

    //Validate email
    if (name.Length == 0) {
        alert('E-mail is required.');
        $('#txtNewsletterEmail').focus();
        return;
    }

    //Validate email
    var reEmail = new RegExp("^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
    if (!reEmail.test(email)) {
        alert('A valid e-mail address is required.');
        $('#txtNewsletterEmail').focus();
        return;
    }

    $.ajax({ type: "POST", url: "default.aspx", data: "name=" + name + "&email=" + email, success: function (msg) { NewsletterSignupResponse(msg) } });
}

function NewsletterSignupResponse(result) {

    if (result == "SUCCESS") {
        var name = $('#txtNewsletterName');
        var email = $('#txtNewsletterEmail');
        name.val("");
        email.val("");
        name.blur();
        email.blur();
        alert("Thank you for subscribing to our newsletter!");
    }
    else {
        alert("An error has occurred. No action was taken. Please try again at another time.");
    }
}

function ShowTab(id) {
    //Hide all
    $('div.TabContent').hide(); //hide all tab content
    $('.tblTabTitles td div').attr('class', ''); //reset all tab title class names
    $('#' + id + ' div').attr('class','Selected');
    $('#TabContent_' + id).show();
}

function Search() {
    var terms = document.getElementById("txtSearch").value;
    if (terms.length > 0 && terms != "Enter keyword") {
        window.open('search.aspx?q=' + terms, '_self', '');
    }

    return false;
}

function SetupWatermark(jqueryElementObject, waterMarkText) {
    // Define what happens when the textbox comes under focus
    // Remove the watermark class and clear the box
    jqueryElementObject.focus(function () {

        $(this).filter(function () {

            // We only want this to apply if there's not 
            // something actually entered
            return $(this).val() == "" || $(this).val() == waterMarkText

        }).removeClass("watermarkOn").val("");

    });

    // Define what happens when the textbox loses focus
    // Add the watermark class and default text
    jqueryElementObject.blur(function () {

        $(this).filter(function () {

            // We only want this to apply if there's not
            // something actually entered
            return $(this).val() == ""

        }).addClass("watermarkOn").val(waterMarkText);

    });
}
