﻿$(document).ready(function() {

    disableImageButton("SaveChangesButton");

    $("div[id$='SignedInPreferencesPanel'] input")
        .click(function() { enableImageButton("SaveChangesButton") })
        .keypress(function() { enableImageButton("SaveChangesButton") });

    function handleTabSelect(event, tab) {
        if (tab.index == 0) {
            //tab 1 Selected
            $('img#imgTab1').attr({ src: "Images/Nav/FriendRequest_sel.png" });
            $('#link1').css({ color: "White" });
            $('img#imgTab2').attr({ src: "Images/Nav/GameNetworks.png" });
            $('img#imgTab3').attr({ src: "Images/Nav/FindFriends.png" });
            $('img#imgTab4').attr({ src: "Images/Nav/Share.png" });
        }
        else if (tab.index == 1) {
            //tab 2 Selected
            $('img#imgTab1').attr({ src: "Images/Nav/FriendRequest.png" });
            $('#link1').css({ color: "#004280" });
            $('img#imgTab2').attr({ src: "Images/Nav/GameNetworks_sel.png" });
            $('img#imgTab3').attr({ src: "Images/Nav/FindFriends.png" });
            $('img#imgTab4').attr({ src: "Images/Nav/Share.png" });
        }
        else if (tab.index == 2) {
            //tab 3 Selected
            $('img#imgTab1').attr({ src: "Images/Nav/FriendRequest.png" });
            $('#link1').css({ color: "#004280" });
            $('img#imgTab2').attr({ src: "Images/Nav/GameNetworks.png" });
            $('img#imgTab3').attr({ src: "Images/Nav/FindFriends_sel.png" });
            $('img#imgTab4').attr({ src: "Images/Nav/Share.png" });
        }
        else if (tab.index == 3) {
            //tab 4 Selected
            $('img#imgTab1').attr({ src: "Images/Nav/FriendRequest.png" });
            $('#link1').css({ color: "#004280" });
            $('img#imgTab2').attr({ src: "Images/Nav/GameNetworks.png" });
            $('img#imgTab3').attr({ src: "Images/Nav/FindFriends.png" });
            $('img#imgTab4').attr({ src: "Images/Nav/Share_sel.png" });
        }
    }

    $("#getConnectedTabs").tabs({ select: handleTabSelect });

    hideSpecial("FriendRequestStartingPointSpecial", true);

    /*************************************************************************************
    * START: Game Networks - "Add Friend" support (for updating the Pending Friends list *
    **************************************************************************************/
    var memberInvites = $("input[name$='memberInvite']");

    memberInvites.click(function() {
        // Find the clicked object's parent container
        var memberContainer = $(this).parents("[id$='memberContainer']");

        // Get the hyperlink with the member name
        var memberObject = $("a[id$='memberView']", memberContainer);

        // If the member name is not shown using the expected hyperlink
        if (memberObject.length == 0) {
            // Get the span with the member name
            memberObject = $("span[id$='memberName']", memberContainer);
        }

        // Add the friend the pending friend list
        $.getJSON("/PendingFriends.ashx", { command: "add", username: memberObject.text(), dt: pendingFriends.GetCacheBurst() });
    });
    /***********************************************************************************
    * END: Game Networks - "Add Friend" support (for updating the Pending Friends list *
    ************************************************************************************/

    // Function to enable/disable notify checkbox and email textbox based on user selection
    var notifyForFriendRequests = $("input:checkbox[name$='NotifyForFriendRequests']");
    notifyForFriendRequests.click(function() {
        if (notifyForFriendRequests.is(":checked")) {
            // clear notify checkbox and email textbox
            $("input:text[name$='NotifyEmailAddress']").val('');
            $("input:text[name$='NotifyEmailAddress']").attr('disabled', true);

            $("input:checkbox[name$='NotifyMe']").attr('checked', false);
            $("input:checkbox[name$='NotifyMe']").attr('disabled', true);
        }
        else {
            // Find the object's parent container first
            $("input:checkbox[name$='NotifyMe']").parent().removeAttr('disabled');
            $("input:checkbox[name$='NotifyMe']").removeAttr('disabled');
            $("input:text[name$='NotifyEmailAddress']").removeAttr('disabled');
        }
    });
});

function Validate_NotifyEmailAddressCustom(source, args)
{
    var notifyMe = $("input:checkbox[name$='NotifyMe']").is(":checked");
    var notifyEmailAddress = $("input:text[name$='NotifyEmailAddress']").val();

    if (!notifyMe)
    {
        args.IsValid = true;
    }
    else if (notifyMe && notifyEmailAddress.length > 0)
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }
}
