var _roleId;
var _Email;
var _UserId;
var _IsProfile;

User =
{
    DeleteDisplay: function (userId) {
        // Hide the dialog area container
        Common.DialogAreaCloseAll();

        // Displays the dialog area for Deleting User
        _UserId = userId;
        AJAX.HTTPRequest("AJAXServiceGet.ashx"
			, "Action=UserDeleteDisplay"
			+ "&UserId=" + encodeURIComponent(userId)
			, User.DeleteDisplayCallback);
    }
	,
    DeleteDisplayCallback: function (HTTPStatus, HTTPResponseText) {
        // Check HTTP request was successful
        if (!AJAX.HTTPRequestCheck(HTTPStatus, HTTPResponseText))
            return;

        // Show the dialog area container
        var button = document.getElementById("Delete_" + _UserId);
        if (button)
            Common.DialogAreaOpen("DeleteUserContainer", HTTPResponseText, Common.GetBottom(button), null);
    }
	,
    DeleteUser: function (userId) {
        var select = document.getElementById("perManBox");
        var selectedReviewer;
        if (select)
            selectedReviewer = select.options[select.selectedIndex].value;


        if (select && selectedReviewer == "0") {
            AJAX.MessageSetFail("Please select a replacement Reviewer", "");
        }
        else {
            AJAX.HTTPRequest("AJAXServiceSet.ashx"
			    , "Action=UserDelete"
			    + "&UserId=" + encodeURIComponent(userId)
			    + "&ReviewerUserId=" + encodeURIComponent(selectedReviewer)
			    , User.DeleteUserCallback);
        }
    }
	,
    DeleteUserCallback: function (HTTPStatus, HTTPResponseText) {
        // Check HTTP request was successful
        if (!AJAX.HTTPRequestCheck(HTTPStatus, HTTPResponseText))
            return;

        // refresh page
        window.location = "UserList.aspx";
    }
	,
    ChangePasswordDisplay: function (userId) {
        // Hide the dialog area container
        Common.DialogAreaCloseAll();

        // Displays the dialog area for changing password
        AJAX.HTTPRequest("AJAXServiceGet.ashx"
			, "Action=UserChangePasswordDisplay"
			+ "&UserId=" + encodeURIComponent(userId)
			, User.ChangePasswordDisplayCallback);
    }
	,
    ChangePasswordDisplayCallback: function (HTTPStatus, HTTPResponseText) {

        // Check HTTP request was successful
        if (!AJAX.HTTPRequestCheck(HTTPStatus, HTTPResponseText))
            return;

        // Show the dialog area container
        Common.DialogAreaOpen("PasswordDialogAreaContainer", HTTPResponseText, null, null);
    }
	,
    ChangePasswordSave: function (userId) {
        // Get section edit - text
        var text1 = document.getElementById("Password");
        var text1value = text1.value;
        var text2 = document.getElementById("PasswordConfirm");
        var text2value = text2.value;

        // Save to database
        AJAX.HTTPRequest("AJAXServiceSet.ashx"
			, "Action=UserChangePassword"
			+ "&UserId=" + encodeURIComponent(userId)
			+ "&Password1=" + encodeURIComponent(text1value)
			+ "&Password2=" + encodeURIComponent(text2value)
			, User.ChangePasswordSaveCallback);
    }
	,
    ChangePasswordSaveCallback: function (HTTPStatus, HTTPResponseText) {
        AJAX.Message(HTTPStatus, HTTPResponseText);

        if (HTTPStatus == 200) // Ok
        {
            // Hide the dialog area container
            Common.DialogAreaCloseAll();

            // show success popup
            alert("Password changed successfully!");
        }
    }
	,
    ChangeNameDisplay: function (userId) {
        // Hide the dialog area container
        Common.DialogAreaCloseAll();

        // Displays the dialog area for changing password
        AJAX.HTTPRequest("AJAXServiceGet.ashx"
			, "Action=UserChangeNameDisplay"
			+ "&UserId=" + encodeURIComponent(userId)
			, User.ChangeNameDisplayCallback);
    }
	,
    ChangeNameDisplayCallback: function (HTTPStatus, HTTPResponseText) {
        // Check HTTP request was successful
        if (!AJAX.HTTPRequestCheck(HTTPStatus, HTTPResponseText))
            return;

        // Show the dialog area container
        Common.DialogAreaOpen("NameDialogAreaContainer", HTTPResponseText, null, null);
    }
	,
    ChangeNameSave: function (userId) {
        // Get section edit - text
        var text1 = document.getElementById("FirstName");
        var text1value = text1.value;
        var text2 = document.getElementById("Surname");
        var text2value = text2.value;

        // Save to database
        AJAX.HTTPRequest("AJAXServiceSet.ashx"
			, "Action=UserChangeName"
			+ "&UserId=" + encodeURIComponent(userId)
			+ "&FirstName=" + encodeURIComponent(text1value)
			+ "&Surname=" + encodeURIComponent(text2value)
			, User.ChangeNameSaveCallback);
    }
	,
    ChangeNameSaveCallback: function (HTTPStatus, HTTPResponseText) {
        //AJAX.Message(HTTPStatus, HTTPResponseText);

        if (HTTPStatus == 200) // Ok
        {
            // Hide the dialog area container
            Common.DialogAreaCloseAll();

            // show success popup
            alert("Name changed successfully!");

            //update name display
            var name = document.getElementById(Common.GetBaseClientId() + "uxUserNameLabel");
            if (name)
                name.innerText = HTTPResponseText;
        }
    }
	,
    ChangeEmailDisplay: function (userId, isProfile) {
        // Hide the dialog area container
        Common.DialogAreaCloseAll();
        _IsProfile = isProfile;
        // Displays the dialog area for changing email
        AJAX.HTTPRequest("AJAXServiceGet.ashx"
			, "Action=UserChangeEmailDisplay"
			+ "&UserId=" + encodeURIComponent(userId)
			, User.ChangeEmailDisplayCallback);
    }
	,
    ChangeEmailDisplayCallback: function (HTTPStatus, HTTPResponseText) {
        // Check HTTP request was successful
        if (!AJAX.HTTPRequestCheck(HTTPStatus, HTTPResponseText))
            return;

        // Show the dialog area container
        Common.DialogAreaOpen("EmailDialogAreaContainer", HTTPResponseText, null, null);
    }
	,
    ChangeEmailSave: function (userId) {
        // Get section edit - text
        var text1 = document.getElementById("Email");
        var text1value = text1.value;
        var text2 = document.getElementById("EmailConfirm");
        var text2value = text2.value;

        _Email = text2.value; //for use with the AlertAdminPage (see ChangeEmailSaveCallback:function)

        // IF IT IS A "PROFILE" ACCOUNT THEN CHECK THE VALIDILITY OF THE EMAIL 
        if (_IsProfile == "True") {
            // Checks for the presence of an @ sign and a . for an email address
            var filter = /^.+@.+\..{2,3}$/;

            //
            if (!filter.test(text1value)) {
                alert("Please input a valid email address!")
                return;
            }
        }

        // Save to database
        AJAX.HTTPRequest("AJAXServiceSet.ashx"
			, "Action=UserChangeEmail"
			+ "&UserId=" + encodeURIComponent(userId)
			+ "&Email1=" + encodeURIComponent(text1value)
			+ "&Email2=" + encodeURIComponent(text2value)
			, User.ChangeEmailSaveCallback);
    }
	,
    ChangeEmailSaveCallback: function (HTTPStatus, HTTPResponseText) {
        AJAX.Message(HTTPStatus, HTTPResponseText);

        if (HTTPStatus == 200) // Ok
        {
            // Hide the dialog area container
            Common.DialogAreaCloseAll();

            // show success popup
            alert("Email changed successfully!");

            // if on the settings page (alertAdmin) then set the span to the email address so no postback is required
            var textbox = document.getElementById(Common.GetBaseClientId() + "uxEmailSpan");
            if (textbox) {
                textbox.innerHTML = _Email;
            }
        }
    }
	,
    ChangeDisplayClose: function () {
        // Hide the dialog area container
        Common.DialogAreaCloseAll();
    }
	,
    PasswordReminderDisplay: function () {
        var textbox = document.getElementById("uxEmailTextBox");

        var html = '<div>'
	            + '<div class="Heading">Login Assistance</div>'
	            + '<hr class="Separator" /><br />'
	            + '<div class="SubHeading">Some ideas that may help you:</div>'
	            + '<ul>'
	            + '<li>Check your Username/Email <u>closely</u> to see if you have added/missed a letter or punctuation mark.</li>'
	            + '<li>The login page is case-sensitive, so please make sure that you have entered upper and lower-case characters where necessary.</li>'
	            + '</ul>'
                + '<div class="SubHeading">If your <i>SWIFT</i> username is an email address:</div>'
                + '&nbsp;&nbsp;&nbsp;&nbsp;- please enter it here to receive an email reminder of your login details: '
                + '<input type="text" id="uxReminderEmail" value="' + (textbox ? textbox.value : '') + '" onkeydown="Common.SearchTextEntered(event, \'PasswordSendButton\')" /><br /><br />'
                + '<div class="SubHeading">Otherwise:</div>'
                + '&nbsp;&nbsp;&nbsp;&nbsp;- please call us on 0845 4900 447 for further assistance.'
                + '<div class="ButtonPanel"><span id="AJAXMessage"></span>'
                + '<button class="ButtonWide" onclick="User.PasswordReminderSend(); return false" id="PasswordSendButton">Send Reminder</button>'
                + '<button class="Button" onclick="Common.DialogAreaCloseAll(); return false">Cancel</button>'
                + '</div>'
                + '</div>';

        Common.DialogAreaOpen("DialogAreaContainer", html);
    }
	,
    PasswordReminderSend: function () {
        var textbox = document.getElementById("uxReminderEmail");

        if (textbox) {
            // check db for email
            AJAX.HTTPRequest("Default.aspx"
			    , "Action=UserPasswordReminderSend"
			    + "&Email=" + encodeURIComponent(textbox.value)
			    , User.PasswordReminderSendCallback);
        }
    }
	,
    PasswordReminderSendCallback: function (HTTPStatus, HTTPResponseText) {
        if (HTTPStatus == 200 && HTTPResponseText == "success") // Ok
        {
            // Hide the dialog area container
            Common.DialogAreaCloseAll();
            alert("Password reminder sent!");
        }
        else
            alert(HTTPResponseText);
    }
	,
    AddRole: function (RoleId) {
        //get selected project
        var select = document.getElementById("userSelect_" + RoleId);
        var projectId = select.options[select.selectedIndex].value;

        // add project to group
        _roleId = RoleId;
        AJAX.HTTPRequest("AJAXServiceSet.ashx"
		, "Action=PermissionAdd"
		+ "&RoleId=" + encodeURIComponent(RoleId)
		+ "&UserId=" + encodeURIComponent(projectId)
		, User.UpdateRoles);
    }
	,
    RemoveRole: function (roleId, userId) {
        // remove project from group
        _roleId = roleId;
        AJAX.HTTPRequest("AJAXServiceSet.ashx"
		, "Action=PermissionDelete"
		+ "&RoleId=" + encodeURIComponent(roleId)
		+ "&UserId=" + encodeURIComponent(userId)
		, User.UpdateRoles);
    }
	,
    UpdateRoles: function (HTTPStatus, HTTPResponseText) {
        // Check HTTP request was successful
        if (!AJAX.HTTPRequestCheck(HTTPStatus, HTTPResponseText))
            return;

        //update display for permissions
        AJAX.HTTPRequest("AJAXServiceGet.ashx"
			, "Action=PermissionsView"
			+ "&RoleId=" + encodeURIComponent(_roleId)
			, User.UpdateRolesCallback);
    }
	,
    UpdateRolesCallback: function (HTTPStatus, HTTPResponseText) {
        // Check HTTP request was successful
        if (!AJAX.HTTPRequestCheck(HTTPStatus, HTTPResponseText))
            return;

        //update display for permissions
        var td = document.getElementById("roles_" + _roleId);
        td.innerHTML = HTTPResponseText;
    }
	,
    SendTestEmail: function () {
        // Send a test e-mail to the user
        AJAX.HTTPRequest("AJAXServiceSet.ashx"
			, "Action=SendTestEmail"
			, User.SendTestEmailCallback);
    }
	,
    SendTestEmailCallback: function (HTTPStatus, HTTPResponseText) {
        // Check HTTP request was successful
        if (AJAX.HTTPRequestCheck(HTTPStatus, HTTPResponseText))
            alert("an e-mail has been sent");
        else
            alert("an e-mail has has failed to send, Please try again.");
    }
    ,
    ProjectSelected: function (select) {
        var projectId = select.options[select.selectedIndex].value;
        window.location = "ProjectHome.aspx?ProjectId=" + projectId;
    }
    ,
    ObservationSelected: function (select) {
        var observationId = select.options[select.selectedIndex].value;
        window.location = "PerManObservationView.aspx?ObservationId=" + observationId;
    }
}

