﻿var tempPasswordHrefClick = function() {
    $.blockUI({ message: $('#tempPassword'),
        css: { width: '60%', left: '22%', top: '30%', cursor: 'auto' }
    });
    $('label#tempPassword_email_error').hide();
    $("#tempPasswordEmail").val("");
    $('div#setTempPasswordForm').show();
    $('#setTempPasswordSuccess').hide();
    $("#tempPasswordEmail").focus();
}
//
$(document).ready(function() {   
    
    $('span#loggedInHrefs').hide();
    $('span#loginHrefs').show();
    // check login status
    $.ajax({
        url: "userprofile.aspx/IsUserAuthenticated",
        type: "POST",
        data: "{}",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        cache: false,
        success: function(msg) {
            if (msg.d === false) {
                $('span#loggedInHrefs').hide();
                $('span#loginHrefs').show();
            }
            else {
                $('span#loginHrefs').hide();
                $('span#loggedInHrefs').show();
            }
        }
    });

    $('.loginReqError').hide();
    $('div#searchContainer input').keyup(function(e) {
        if (e.keyCode == 13) {
            $('input#searchImage').click();
        }
    });
    $('input#searchImage').click(function() {

        if ($('input#searchText').val() == "") {
            alert("Please enter text to search");
        }
        else {
            window.open('search.aspx?search=' + $('input#searchText').val());
        }
    });
    $('#loginHref').click(function() {
        $.blockUI({ message: $('#loginForm'),
            css: { width: '60%', left: '22%', top: '30%', cursor: 'auto' }
        });

    });
    // cancle login
    $('#closeLogin').click(function() {
        $.unblockUI();
    });

    $('#clickLogin').click(function() {
        var userId = $("input#userId").val();
        if (userId == "") {
            $("label#userId_req_error").show();
            $("input#userId").focus();
            return false;
        } // end check for name;
        else {
            $("label#userId_req_error").hide();
        }
        //
        var uspasswd = $('input#userPassword').val();
        if (uspasswd == "") {
            $("label#userPassword_req_error").show();
            $("input#userPassword").focus();
            return false;
        } // end check for name;
        else {
            $("label#userPassword_req_error").hide();
        }
        //
        if ($('input#userId').val() === "" || $('input#userPassword').val() === "") {
        }
        else {
            var publicLogin = new Object();
            publicLogin.EmailID = userId;
            publicLogin.Password = uspasswd;
            // make ajax call
            $.ajax({
                url: "userprofile.aspx/ValidateUser",
                type: "POST",
                data: "{'UserCreds': " + JSON.stringify(publicLogin) + "}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                cache: false,
                success: function(msg) {

                    var isAuthenticated = msg.d["IsAuthenticated"];
                    if (isAuthenticated) {
                        $.unblockUI();
                        $('span#loginHrefs').hide();
                        $('span#loggedInHrefs').show();

                    }
                    else {
                        alert('Validation failed');
                    }
                },
                error: function(xhr, status, error) {

                }
            }); // end ajax call

        } // end else for valid entries
        //return false;        

    }); // end clickLogin
    // click logout
    $('a#logoutHref').click(function() {
        $.ajax({
            url: "userprofile.aspx/PublicUserLogout",
            type: "POST",
            data: "{}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            cache: false,
            success: function(msg) {
                $('label.userName').text("");
                $('span#loginHrefs').show();
                $('span#loggedInHrefs').hide();
                window.location.href = "publichome.aspx";
            }
        });
        //return false;
    }); // end click logout
    /// register
    $('#registerHref').click(function() {
        window.location.href = "siteregistration.aspx";
        return false;

    });
    /// why register
    $('#whyRegisterHref').click(function() {
        window.location.href = "siteregistration.aspx";
        return false;

    });
    /// myprofile
    $('#myProfileHref').click(function() {
        window.location.href = "userprofile.aspx";
        return false;
    });
    // close dialog
    $('.closeDialog').click(function() {
        $.unblockUI();
        return false;
    });
    // my resources
    $('#myResourcesHref').click(function() {
        // check login status
        $.ajax({
            url: "userprofile.aspx/IsUserAuthenticated",
            type: "POST",
            data: "{}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            cache: false,
            success: function(msg) {
                if (msg.d === false) { // show login form
                    $.blockUI({ message: $('#loginForm'),
                        css: { width: '60%', left: '22%', top: '30%', cursor: 'auto' }
                    });
                }
                else {
                    window.location.href = "myresources.aspx";
                }
            }
        }); // end login status check

        return false;
    }); // end myResources href click

    // from link on registration page
    $('a#tempPasswordHref').click(function() {
        tempPasswordHrefClick();
        return false;
    });
    // from main master page
    $('a#tempPasswordHrefMainMaster').click(function() {
        setTimeout("tempPasswordHrefClick()", 0);
        return false;
    });

    // request tempPassword submit
    $('#tempPasswordRequest').click(function() {
        var tempEmail = $("#tempPasswordEmail").val();
        if (tempEmail === "") {
            $('label#tempPassword_email_error').show();
            return false;
        }
        else {
            $('label#tempPassword_email_error').hide();
        }
        // check if email exists
        $.ajax({
            url: "siteregistration.aspx/ValidateUserEmail",
            type: "POST",
            data: "{'email':'" + tempEmail + "'}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            cache: false,
            success: function(msg) {
                if (msg.d === false) { // invalid email syntax
                    $('label#tempPassword_email_error').text('please enter a valid email address');
                    $('label#tempPassword_email_error').show();
                    return false;
                }
                else {
                    $('label#tempPassword_email_error').hide();
                    //
                    checkEmailIfRegistered(tempEmail);

                } // end else on validate email
            } // end success ajax
        }); // end validate email ajax
        return false;
    }); // end submitRequest click
    // check if email has been registered
    var checkEmailIfRegistered = function(tempEmail) {
        $.ajax({
            url: "siteregistration.aspx/CheckEmailExists",
            type: "POST",
            data: "{'email':'" + tempEmail + "'}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            cache: false,
            success: function(msg) {
                if (msg.d === false) { // invalid email syntax
                    $('label#tempPassword_email_error').text('This email address has not been registered. Cannot send temporary password to an unregistered email.');
                    $('label#tempPassword_email_error').show();
                    return false;
                }
                else {
                    $('label#tempPassword_email_error').hide();
                    //
                    sendTempPassword(tempEmail);

                } // end else on validate email
            } // end success ajax
        }); // end ajax call
    } // end  checkEmailIfRegistered
    //
    // send temp password
    var sendTempPassword = function(tempEmail) {
        $.ajax({
            url: "siteregistration.aspx/SendTempPassword",
            type: "POST",
            data: "{'email':'" + tempEmail + "'}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            cache: false,
            success: function(msg) {
                if (msg.d === false) { // invalid email syntax
                    $('label#tempPassword_email_error').show();
                    $('label#tempPassword_email_error').text('There was an error sending your temporary password');
                    return false;
                }
                else {
                    //
                    $('div#setTempPasswordForm').hide();
                    $('#setTempPasswordSuccess').show();
                    $('#setTempSuccess').html('A temporary password was sent successfully to the email address you entered. We recommend that you sign in after receiving your temporary password and then change your password.');
                } // end else on validate email
            } // end success ajax
        }); // end ajax call
    } // end sendTempPassword
});              // end document.ready         