﻿$(document).ready(function() {
    function keyboardSubmit(event) {
        if (event.keyCode == 13)
            submit();
    }

    function submit() {
        if ($('#txtSearch').val().length == 0) {
            Smith.base.ShowModal('Input Required', 'Please enter a value in the search box.');
            $('#txtSearch').blur();
        }
        else{
            var formattedQuery = $('#txtSearch').val().replace(' ', '+');
            formattedQuery = formattedQuery.replace(/&/g,"-amp-");
            if (window.location.href.indexOf("search") != -1  && typeof Smith.SearchResults != 'undefined')
            {
                Smith.SearchResults.AjaxSubmit();
            }
            else
            {
                Smith.base.showBottomMessage('Please wait while your search is being processed.');
                window.location.href = Smith.base.SiteRoot + 'search/{' + formattedQuery + '}';
            }
        }
    }
    $('#btnSearch').click(submit);

    // allow the user to press the enter key to submit when the
    // textbox has focus.
    $('#txtSearch').focus(function() {
        $(document).keyup(keyboardSubmit);
    })
      .blur(function() {
          $(document).unbind('keyup', keyboardSubmit);
      });
});
