﻿$(document).ready(function() {

    $("form input").keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            //$('#frmLogon').submit();

            var domain = $('#currentDomain').val();
            window.location.href = 'http://' + domain + '/results/general/' + cleanString($('#tbSearch').val()) + '/0';
            return false;
        }
    });

    $('#aSearchSubmit').click(function() {
        if ($('#tbSearch').length > 0) {
            //$('#frmSearch').submit();

            var domain = $('#currentDomain').val();
            window.location.href = 'http://' + domain + '/results/general/' + cleanString($('#tbSearch').val()) + '/0';
            
        }
    });

    $('#aSignInSubmit').click(function() {
        if (
                ($('#tbEmailAddress').val().toLowerCase() == 'email address') || ($('#tbPassword').val().toLowerCase() == 'password') ||
                ($('#tbEmailAddress').val().length <= 0) || ($('#tbPassword').val().length <= 0)
            ) {
            $('#signInMessage').html('All fields are required.');
            $('#signInMessage').show();
        }
        else {
            var SignInObject = {};
            SignInObject.Email = $('#tbEmailAddress').val();
            SignInObject.Password = $('#tbPassword').val();
            var DTOSignInObject = { "SignInObject": SignInObject };

            $.ajax({
                type: "POST",
                url: "/Services/Security.asmx/IsUserValid",
                data: JSON.stringify(DTOSignInObject),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(res) {
                    if ((res != null) && (res.d == true)) {
                        $('#signInMessage').hide();
                        $('#frmSignIn').submit();
                    }
                    else {
                        $('#signInMessage').html('Sorry, could not find a user with the email address and password entered.').show();
                        $('#signInMessage').show();
                    }
                },
                error: function(res) {
                    $('#signInMessage').html('error: ' + res.responseText).show();
                }
            })
        }
    });
}); 
