﻿var selectedID = "";
var enabled = true;
var myScroller;

$(document).ready(function () {
    // Create a panel scroller
    myScroller = Scroller("newUserNameContainer", "newUserName");
    // Add the new scroller to scroller array, used in the scroller object
    myScroller.addScroller(myScroller);
});

function closeWindow() {
    // Check enabled to make sure a save isn't in progress
    if (enabled) {
        // Hide this popup window
        parent.hideModalWindow("editUsernameModal");
    }
}

function setTextBox(element, type) {
    // Empty the text box on click if it has the default value, set it back to default text if fcocus is lost and the field is empty
    if (type == "clear") {
        if (element.value == "[New-Username]") {
            element.value = "";
        }
    }
    else {
        if (element.value == "") {
            element.value = type;
        }
    }
}

function setSelection(id) {
    // Get the selected item by it's ID
    var selectedItem = document.getElementById(id);
    // Make sure that an unavailable username hasn't been selected
    if (selectedItem.className == "resultsItemLightPink" || selectedItem.className == "resultsItemDarkPink") {
        parent.showMessage2("small", "The new username you selected<br/>is already taken");
    }
    else {
        // Save the selected item for later
        selectedID = id;
        // get the number of results, passed through from server side
        var resultsCount = parseInt(document.getElementById('hfResultCount').value);
        // Loop through all the items and unselect them
        for (var i = 0; i < resultsCount; i++) {
            var elem = document.getElementById('newUserName' + i);
            if (elem.className != "resultsItemLightPink" && elem.className != "resultsItemDarkPink") {
                elem.style.backgroundImage = "none";
                elem.style.border = "solid 0px #00ff00";
            }
        }
        // Show the selected tick and set the green border
        selectedItem.style.backgroundImage = "url(Images/loc_add_button.png)";
        selectedItem.style.border = "solid 1px #00ff00";
    }
}

function saveUName() {
    // Check enabled to make sure a save isn't in progress
    if (enabled) {
        // set enabled to false so they cannot press save or cancel until the new name has been set and confirmed
        enabled = false;
        // Make sure a new username has been selected
        if (selectedID != "") {
            //Get the selected username
            var newUserName = document.getElementById(selectedID).innerHTML;
            // Set the new username using the handler
            var strURL = parent.document.getElementById('hfHandlerURL').value + "/setNewUserPass.ashx"; //  call the handler to set the text in the database too
            $.post(strURL, {
                userID: parent.document.getElementById('hfUserID').value,
                type: "user",
                value: newUserName
            }, function (data) {
                // Turn enabled back on to allow cancelling or a retry
                enabled = true;
                if (data == "success") {
                    // Hide this popup window
                    parent.hideModalWindow("editUsernameModal");
                    // Show confirmation
                    parent.showMessage2("small", "Your new username has now<br/>been set");
                }
                else {
                    // Show failed message
                    parent.showMessage2("small", "Setting your new username has failed.<br/>Please contact technical support on 01524 230 250<br/>or try again.");
                }
            });
        }
    }
}
