﻿function initShoutBox() {

    $("#txtmsg").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "Message"
        }).removeClass("watermark").val("");
    });
    $("#txtname").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "Name"
        }).removeClass("watermark").val("");
    });
    $("#txtlink").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "Email/Url"
        }).removeClass("watermark").val("");
    });
  
    $("#txtmsg").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).addClass("watermark").val("Message");
    });

    $("#txtname").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).addClass("watermark").val("Name");
    });
    $("#txtlink").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).addClass("watermark").val("Email/Url");
    });

    $("#ceeboxsubmit").click(function() {
        //Check valid form
        //0,10
        
        if ($("#txtname").val().length == 0 || $("#txtname").val() == 'Name' || $("#txtname").val().length >= 20) {
            alert("Please enter name (no more than 20 letters).");
            return false;
        }
        if ($("#txtmsg").val().length == 0 || $("#txtmsg").val() == 'Message' || $("#txtmsg").val().length >= 140) {
            alert("Please enter message (no more than 140 letters).");
            return false;
        }

        var data = $("#txtname").serialize() + "&";
        data += $("#txtmsg").serialize() + "&";
        data += $("#txtlink").serialize() + "&";
        data += $("input[name='__RequestVerificationToken']").serialize();

        $(this).attr("disabled", "disabled");
       
        $.ajax({ type: "POST", url: "/Home/PostShout/", data: data, dataType: "json"
                           , success: function(obj) {
                               if (obj) {
                                   if (obj.IsCompleted) {
                                       $("#shout").html(obj.HtmlString);
                                       $("#txtname").val('Name'); $("#txtlink").val('Email/Url'); $("#txtmsg").val('Message');
                                   } else {
                                       alert(obj.ErrorMessage);
                                   }
                               }
                               $(this).removeAttr("disabled");
                           }, error: function() {
                               alert("Ooop! Can't process this time.");
                               $(this).removeAttr("disabled");
                           }
        }); //End Ajax

    });
}
function disposeShout(theValue) {
    if (confirm('Do you delete this?')) {
        if (theValue != null) {
            var data = "shoutid=" + theValue + "&";
            data += $("input[name='__RequestVerificationToken']").serialize();
            $.ajax({ type: "POST", url: "/Home/DisposeShout/", data: data, dataType: "json"
                           , success: function(obj) {
                               if (obj) {
                                   if (obj.IsCompleted) {
                                       $("#shout").html(obj.HtmlString);
                                       $("#txtname").val('Name'); $("#txtlink").val('Email/Url'); $("#txtmsg").val('Message');
                                   } else {
                                       alert(obj.ErrorMessage);
                                   }
                               }
                           }, error: function() {
                               alert("Ooop! Can't process this time.");
                           }
            }); //End Ajax
        }
    }
}
