﻿    function ajLogin(u,p,pr) {
        var smsg = 'Your login was successful.';
        var fmsg = 'your login was not successful';
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "/webServices/Authentication.asmx/AjaxLogin",
            data: "{username:'" + u.replace('\'', '\\\'') + "',password:'" + p.replace('\'', '\\\'') + "',persist:'" + pr.replace('\'', '\\\'') + "'}",
            dataType: "json",
            success: function (msg) {
                if (msg.d.success == 'true') {
                    $('#btnGlobalLogin').colorbox.close();
                    $('.globalUserName').html(msg.d.fullname);
                    $('.globalHeaderMembership').css('display', 'none');
                    $('.globalHeaderLoggedIn').css('display', '');
                    if (msg.refresh == 'true') {
                        window.location.reload();
                    }
                } else {
                    $('#LoginBoxError').html(fmsg);
                }
            },
            error: function () {
                $('#LoginBoxError').html('Error contacting login service');
            }
        })
    }; 
    function ajPword(em) {
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "/webServices/PasswordServices.asmx/AjaxForgotPassword",
            data: "{email:'"+em.replace('\'','\\\'')+"'}",
            dataType: "json",
            success: function(msg) {
                if (msg.d=='true') {
                    $('#PwordBoxError').html('Password Sent');
                    $('#btnLoginBoxPword').colorbox.close();
                } else {
                    $('#PwordBoxError').html('Error retreiving password');
                }
            },
            error: function() {
                $('#PwordBoxError').html('Error contacting password service');
            }
        })
    }; 
    function ajEmail(to,from,name,msg,cc) {
        var smsg = 'Your message was successfully sent.';
        var fmsg = 'There was a problem sending your email, check the email addresses you entered and try again.';
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "/webServices/SendEmail.asmx/Message",
            data: "{t:'"+to+"',f:'"+from+"',n:'"+name+"',m:'"+msg+"',u:'"+location.href+"',ti:'"+document.title+"',c:'"+cc+"'}",
            dataType: "json",
            success: function(msg) {if (msg.d=='True') {$("#divRMsg").html(smsg)} else {$("#divRMsg").html(fmsg)}}
        })
    };
    $(document).ready(function () {
        $('#ibtnGlobalLogin').click(function() {
            ajLogin($('#txtLoginBoxUsername').attr("value"),$('#txtLoginBoxPasswd').attr("value"),$('#chkLoginBoxPersist').attr("value"));
        });
        $('#ibtnPwordBoxSubmit').click(function() {
            ajPword($('#txtPwordBoxEmail').attr("value"));
        });
        $('#ibtnEmailBoxSubmit').click(function() {
            ajEmail(
                $('#txtEmailTo').attr("value").replace('\'','\\\''),
                $('#txtEmailFrom').attr("value").replace('\'','\\\''),
                $('#txtEmailName').attr("value").replace('\'','\\\''),
                $('#txtEmailMessage').attr("value").replace('\'','\\\''),
                $('#chkEmailCC').attr("checked")
            );
        });
        $('#btnLoginBoxClose').click(function() {$('#btnLoginBoxPword').colorbox.close();});
        $('#btnPwordBoxClose').click(function() {$('#btnGlobalLogin').colorbox.close();});
        $('#btnEmailBoxClose').click(function() {$('.lnkEmailPage').colorbox.close();});
        $('#btnGlobalLogin').colorbox({transition:"fade", inline:"true", href:"#shadow_login_box"});
        $('#btnLoginBoxPword').colorbox({transition:"fade", inline:"true", href:"#shadow_pword_box"});
        $('.lnkEmailPage').colorbox({transition:"fade", inline:"true", href:"#shadow_email_box"});
        $('#btnGlobalSearch').click(function() {
            window.location.href='/search/results/?q='+escape($('#tbGlobalSearch').attr('value'));
        });
        $('#globalHeaderSearch').keypress(function(event) {
            if (event.keyCode == '13') {
                event.preventDefault();
                window.location.href='/search/results/?q='+escape($('#tbGlobalSearch').attr('value'));
            }
        });
        $('#LoginBoxContent').keypress(function(event) {
            if (event.keyCode == '13') {
                event.preventDefault();
                ajLogin($('#txtLoginBoxUsername').attr("value"),$('#txtLoginBoxPasswd').attr("value"),$('#chkLoginBoxPersist').attr("value"));
            }
        });
        $('#PwordBoxContent').keypress(function(event) {
            if (event.keyCode == '13') {
                event.preventDefault();
                ajPword($('#txtPwordBoxEmail').attr("value"));
            }
        });
        $('#EmailBoxContent').keypress(function(event) {
            if (event.keyCode == '13') {
                event.preventDefault();
                ajEmail(
                    $('#txtEmailTo').attr("value").replace('\'','\\\''),
                    $('#txtEmailFrom').attr("value").replace('\'','\\\''),
                    $('#txtEmailName').attr("value").replace('\'','\\\''),
                    $('#txtEmailMessage').attr("value").replace('\'','\\\''),
                    $('#chkEmailCC').attr("checked")
                );
            }
        });
        $('#tbGlobalSearch').autocomplete({ serviceUrl:'/webServices/RecentSearchTerms.asmx/GetCompletionList',minChars:2,width:170, delimiter: /(,|;)\s*/ });
 
        // handler for select boxes in filter boxes on canned searches
        $('.FiltersContainer > select').change(function() {
            var s = window.location.pathname + '?'
            // if (window.location.search.length > 0) {s=window.location.pathname + window.location.search}
            $('.FiltersContainer > select').each(function() {
                x = $(this).val().split('|');
                if (x[1] != '-10') {s=s+x[0]+'='+x[1]+'&';}
            });
            window.location.replace(s);
        });
    });
