﻿// -------------------------------------------------------------------------| $(document).ready
$(document).ready(function()
{

    with($.theknot.uw.view.guestbookposter)
    {
        setupButtonClickEvents();
    }

});

// -------------------------------------------------------------------------| Namespace definitions.
$.theknot.uw.view.guestbookposter = function(){};

// -------------------------------------------------------------------------| Body
$.extend($.theknot.uw.view.guestbookposter,
{
    // -----------------------------------------------------| Properties
    fieldSelectors : 
    {
        inputGuestName : 'input[id$=_txtGuestName]',
        inputMessage : 'textarea[id$=_txtMessage]',
        
        submitButton : 'input#ibtnSubmitPost',
        btnRemove : 'a[id$=_lbtnRemoveGuestbookEntry]',
        PostContainer : 'div#divGuestbookMsg'
    },
    PageElements :
    {
        ErrorLabel : 'div.error',
        LoadingImage : 'div#divGuestbookPosterLoadingImg'

    },
    constants :
    {
       GuestBookService : '/view/webservices/GuestbookPoster.ashx',
       ServiceParmsFormat : 'sid={0}&cmd={1}'
        
    },
    
     WebServiceCommands : 
    {
        AddPost : 1,
        HidePost : 2
    },
    // -------------------------------------------------------------------------| setupButtonClickEvents()
    setupButtonClickEvents : function()
    {
        with($.theknot.uw.view.guestbookposter.fieldSelectors)
        {
            // Submit Button
            $(submitButton).click(function()
            {
                with($.theknot.uw.view.guestbookposter)
                {
                    var guestName = $.trim($(fieldSelectors.inputGuestName).val());
                    //if(guestName == '?'){ guestName=''; }
                    if(guestName.length == 0)
                    {
                        alert('The "Name" field cannot be blank.');
                        $(fieldSelectors.inputGuestName).val('');
                        $(fieldSelectors.inputGuestName).focus();
                        return false;
                    }
                    
                    var guestMessage = $.trim($(fieldSelectors.inputMessage).val());
                    //if(guestMessage == '?'){ guestMessage=''; }
                    if(guestMessage.length == 0)
                    {
                        alert('The "Message" field cannot be blank.');
                        $(fieldSelectors.inputMessage).val('');
                        $(fieldSelectors.inputMessage).focus();
                        return false;
                    }
                    
                    $(this).css('visibility', 'hidden');
                    $(PageElements.LoadingImage).show();
                    //AddGuestBookPost(guestName, guestMessage,
                    AddGuestBookPost(escape(guestName), escape(guestMessage), 
                    
                    function()
                    {
                        with($.theknot.uw.view.guestbookposter.fieldSelectors)
                        {
                            // Clear the input fields b/c otherwise the values will still be there after the self.location.reload() below.
                            $(inputGuestName).val('');
                            $(inputMessage).val('');
                        }
                        self.location.reload();
                    },
                    function(){
                        with($.theknot.uw.view.guestbookposter)
                        {
                            $(fieldSelectors.submitButton).css('visibility', 'visible');
                            $(PageElements.LoadingImage).hide();
                        }
                    });
                }
                return false;
            });
        }
    },
    
    // -------------------------------------------------------------------------| AddGuestBookPost()
    AddGuestBookPost : function(guestName, guestMessage, successCallback, completeCallback)
    {
        $.ajax(
		{
			type: 'POST',
			url: this.constants.GuestBookService,
			data: $.theknot.formatStr('cmd={0}&uid={1}&sid={2}&gn={3}&cpy={4}', 
			        this.WebServiceCommands.AddPost, 
			        $.theknot.uw.view.commonGuestviewElements.Site.UserId, 
			        $.theknot.uw.view.commonGuestviewElements.Site.Id,
			        guestName, 
			        guestMessage
			        ),
            dataType: 'json',   
            contentType: 'application/x-www-form-urlencoded; charset=ISO-8859-1',
            cache: false,
            async: true,
            success: function(data, textStatus)
            {
                if(!$.theknot.isNullOrUndef(data) && !$.theknot.isNullOrUndef(data.error)) // error msg.indexOf('Error:', 0) >= 0 )
                {
                    var msg = unescape(data.Message);
                    if(msg.toLowerCase().indexOf("a potentially dangerous request.") >= 0)
                    {
                        if(msg.toLowerCase().indexOf("(cpy=") >= 0)
                        {
                            alert('The "Message" field contains invalid characters.');
                            $($.theknot.uw.view.guestbookposter.fieldSelectors.inputMessage).focus();
                        }
                        else if(msg.toLowerCase().indexOf("(gn=") >= 0)
                        {
                            alert('The "Name" field contains invalid characters.');
                            $($.theknot.uw.view.guestbookposter.fieldSelectors.inputGuestName).focus();
                        }
                        else
                        {
                            alert(msg);
                        }
                    }
                    else
                    {
                        alert(msg);
                    }
                    
                    if($.isFunction(completeCallback))
			        {
			            // In this case, the complete callback works best here b/c it is the only time that the button will be re-shown.
			            completeCallback();
			        }
                }
                else
                {
                    // only call the "successCallback" if it was "really" a success (no "error" object in returned data). 
                    if($.isFunction(successCallback))
			        {
			            successCallback();
			        }
                }
            },
			error: function(XMLHttpRequest, data, textStatus, errorThrown)
			{
			    
			     //alert(data.Message);
			     location.reload();
			},
			
			complete: function(XMLHttpRequest, textStatus)
			{
			    if($.isFunction(successCallback))
		        {
		            successCallback();
		        }
			}
			
		});

    },
    
    HideGuestBookPost : function(gbid)
    {
        $(this.PageElements.LoadingImage).show();
        $.ajax(
		{
			type: 'POST',
			url: this.constants.GuestBookService,
			data: 
			    $.theknot.formatStr(
			        'cmd={0}&uid={1}&sid={2}&gbid={3}', 
			        this.WebServiceCommands.HidePost, 
			       $.theknot.uw.view.commonGuestviewElements.Site.UserId, 
			       $.theknot.uw.view.commonGuestviewElements.Site.Id,
			      gbid
			      
			    ),
			dataType: 'json',
			cache: false,
			
			success: function(data, textStatus)
			{
			    /*
			    if($.isFunction(callback))
                {
                    callback(obj, data);
                }
                */
			}, 
			
			error: function(XMLHttpRequest, textStatus, errorThrown)
			{
			     //$($.theknot.uw.view.guestbookposter.PageElements.LoadingImage).hide();
			     location.reload();
			},
			
			complete: function(XMLHttpRequest, textStatus)
			{
			     //$($.theknot.uw.view.guestbookposter.PageElements.LoadingImage).hide();
                location.reload();
			}
			
		});

    }
    
});
