﻿// JScript JQuery File

$(document).ready(function(){
    ChatSetup();
});

var MessageType = 'NEWPOST';
var ReplyToMessageID = 0;
var ReplyToConversationID = 0;

function jsonp_callback(result)
{
    var parsedresponse = result.split("&");
            
    if (parsedresponse[0].toString() != 'NOERROR') {
        //ERROR!!!
        document.getElementById('errormessage').innerHTML = "<p>" + parsedresponse[0] + "</p>" ; 
        }
    else {
        //SUCESSFUL POST!
        //clear out the message text box
        document.getElementById("message").value = 'Your message has been posted!';
        //PREPEND the message to the widget body
        document.getElementById('widgetbody').innerHTML = parsedresponse[1] + document.getElementById('widgetbody').innerHTML;
        //Hide the authentication
        $('#userauthentication').hide("slow");
        //clear any previous errors
        document.getElementById('errormessage').innerHTML = '';
        
        //move the new post stuff back to the bottom if it was a reply
        if (ReplyToMessageID != 0) {
            SetupNewPost();
            }
        }
        
    //re-enable the button, hide the image
    document.getElementById('submitchat').setAttribute('disabled',false);
    document.getElementById('submitchat').disabled = false;
    document.getElementById('busyindicator').style.display = 'none';
}   

function ChatSetup()
{
    $('#submitchat').click(function(){
        // format the querystring
        var poststr = "message=" + escape(encodeURI(document.getElementById("message").value )) +
                    "&username=" + escape(encodeURI( document.getElementById("username").value )) +
                    "&pword=" + escape(encodeURI( document.getElementById("pword").value )) +
                    "&emailreply=" + encodeURI( document.getElementById("emailreply").checked ) +
                    "&wallblogID=" + bfWallBlogID +
                    "&wallUserID=" + bfWallUserID + 
                    "&SendEmail=" + bfWallEmail +
                    "&rmessID=" + ReplyToMessageID +
                    "&convID=" + ReplyToConversationID;
        
        // Call our Web Service, passing in our Query params. The error function is called
        // whenever an exception is thrown by the web service (among other times)
        $.ajax({
          type: "GET",
          url: "http://theblogfrog.com/widgets/ChatHandler.ashx?" + poststr + "&callback=jsonp_callback",
          contentType: "application/json; charset=utf-8",
          dataType: "jsonp",
          error: function(xhr, msg){
            $('#errormessage').innerHTML = 'There was a problem with the request, please try again<br/>';
          }
          
        
        });
        //show the busy image, disable the button
        document.getElementById('submitchat').setAttribute('disabled',true);
        document.getElementById('busyindicator').style.display = "inline";
        return false;
    });
}
   
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
	limitField.value = limitField.value.substring(0, limitNum);
} else {
	limitCount.innerHTML = limitNum - limitField.value.length;
    }
}

function NewMessageClick() {
    $("#userauthentication").show("slow");
    
    if (document.getElementById('message').value == 'Click here to write a note to your friend') {
        document.getElementById('message').value = '';
        }
}

function StartReply(messageID, convID) {
   if(ReplyToMessageID == 0) {
        //controls are in the new post area
        $("#reply"+messageID).html( $("#messagecontrols").html() );
        $("#messagecontrols").html("");           
   }
   else {
        //controls are in another reply area
        $("#reply"+messageID).html( $("#reply"+ReplyToMessageID).html() );
        $("#reply"+ReplyToMessageID).html("");
   }
   $("#replyintrotext").show();
   $("#userauthentication").show("slow");
   $("#writenewpost").show();
   $("#message").val('');
   $("#submitchat").val('Reply');
   //it appears that after moving the button loses its event handlers
   ChatSetup();
   
   ReplyToMessageID = messageID;
   ReplyToConversationID = convID;
}

function SetupNewPost() {
   //move new message tools back to the bottom
   $("#messagecontrols").html( $("#reply"+ReplyToMessageID).html() ); 
   $("#replyintrotext").text("Enter you new post here:");
   
   $("#reply"+ReplyToMessageID).html("");          
   $("#submitchat").val('Post');
   
   //it appears that after moving the button loses its event handlers
   ChatSetup();
   
   ReplyToMessageID = 0;
   ReplyToConversationID = 0;   
}

