var dialogs = 
{
    signInDialog: null,
    signInForm: function() 
    {        
        this.signInDialog = new Dialog(
        {
            width: 265,
            height: 100,
            content: '<div style="color: white; padding-left: 45px; padding-top: 30px;"><table><tr><td colspan="2" align="left"><img style="position: relative;" src="' + getServerRoot() + 'public/images/portal/pctlogo137x42.png" width="137" height="42"/></td></tr>' +
         '<tr height="10"><td colspan="2" align="center"></td></tr>' + 
          '<tr><td align="right"><label>username:</label></td><td><input type="text" name="login" id="login" onkeypress="handleDialogEnter(event);"/></td></tr>' +
           '<tr height="5"><td colspan="2" align="center"></td></tr>' +
           '<tr><td align="right"><label>password:</label></td><td><input type="password" type="text" name="password" id="password" onkeypress="handleDialogEnter(event);"/></td></tr>' +
           '<tr><td colspan="2" align="center"><br/></td></tr>' +
           '<tr><td colspan="2" align="center"><button id="loginBtn" type="submit" onclick="return validCredentials()">sign in</button>&nbsp;&nbsp;&nbsp;&nbsp;<button id="cancelBtn" type="submit" onclick="dialogs.signInDialog.close();">cancel</button></td></tr>' +
           '<tr><td colspan="2" align="center"><div id="signInError" style="color: orange; width: 200px;"></div></td></tr></table></div>',
            focus: "login"
        });
    },
    deleteCommentDialog: null,
    deleteCommentForm: function() 
    {        
        this.deleteCommentDialog = new Dialog(
        {
            width: 265,
            height: 100,
            content: '<div style="color: white; padding-left: 35px; padding-top: 30px;">' +
                     '<table border="0" width="260"><tr><td><div style="color: white; font-size: 18px;">Delete a Comment</div></td/></tr><tr><td colspan="2" align="center"><br/></td></tr><tr><td>Are you sure you want to delete this comment?</td></tr>' +
                     '<tr><td colspan="2" align="center" style="margin-right: 40px;"><br/></td></tr>' +
                      '<tr><td><p align="center"><button type="submit" onclick="deleteComment()">delete</button>&nbsp;&nbsp;&nbsp;&nbsp;<button type="submit" onclick="dialogs.deleteCommentDialog.close();">cancel</button></p></td></tr></table></div>'
        });
    }
};


function showSignInDialog()
{
    dialogs.signInForm();
}

function showDeleteCommentDialog()
{
    dialogs.deleteCommentForm();
}


    function validCredentials()
    {
        // Throw an alert if the user id or password are empty
        var username = document.getElementById("login");
        var password = document.getElementById("password");
                
        if (username.value == "" || password.value == "")
        {
            var errorLine = document.getElementById("signInError");     

            if (errorLine)
                errorLine.innerHTML = "username and/or password is empty";

            return false;
        }
        else
        {
            slmSignIn();
            return true;
        }
    }

    function slmSignIn()
    {           
        var username = document.getElementById("login");
        var password = document.getElementById("password");
        
            new Ajax.Request(getServerRoot() + "public/j_spring_security_check",
            {
                method: 'post',
                parameters: { login: username.value, password: password.value, json: "true" },
                onFailure: function(transport)
                    {
                        var errorLine = document.getElementById("signInError");     

                        if (errorLine)
                            errorLine.innerHTML = "Sign in failed.  Please check your username and password.";
                    },
                onSuccess: function(transport)
                    {
                        checkUserSignIn();
                    }
            });
        
    }
    
    function checkUserSignIn()
    {
        var pageURL;
    
        new Ajax.Request(getServerRoot() + "service/profile/getAuthenticatedUser.ws?json=true",
          {
            method: 'get',
            onSuccess: function(transport)
                {
                    if (transport.responseText.indexOf("credentials_not_found") == -1)
                    {
                        var userInfo = eval('(' + transport.responseText + ')');
                    
                        // Call the page's processSignIn method
                        processSignIn();
                    }    
                    else
                    {
                        var errorLine = document.getElementById("signInError");     

                        if (errorLine)
                            errorLine.innerHTML = "Sign in failed.  Please check your username and password.";
                    }
                },
            onFailure: function(transport)
                {
                    var errorLine = document.getElementById("signInError");     

                    if (errorLine)
                        errorLine.innerHTML = "There was a problem verifying the signed in user.";
                }
          });    
    }

    function processSignIn()
    {
        // Hide the dialog and reload the page 
        dialogs.signInDialog.close();
        
        location.reload(true);
    }

    function handleDialogEnter(e)
    {
        var keycode;
        
        if (window.event)
            keycode = window.event.keyCode;
        else 
        if (e) 
            keycode = e.which;

        if (keycode == 13) // enter
            validCredentials();
    }


