﻿/// <reference path="~/Scripts/IntelliSense/Intellisense.js" />

(function()
{
    BSC.UI.Ws.Broadcast = function()
    {
        /// Private End
        var maxlimit = 140;
        var SendBroadcast = function(guid)
        {


            $("#" + guid + "_send").hide();

            // Validate input
            var message = $("#" + guid + "_bcmessage").val();
            var productid = $("#" + guid + "_productid").val();
            var visibility = $("#" + guid + "_visibility").val();
            if (message === "")
            {
                BSC.UI.Alert(BSC.T.Get("JS.BSC.UI.Ws.Admin.OK"), null, "Info", "No text inserted in text field. Insert text.", null, null, "info");
                $("#" + guid + "_send").show();
                return;
            }
            if (BSC.R.TextField.test(message))
            {
                BSC.UI.Alert(BSC.T.Get("JS.BSC.UI.Ws.Admin.OK"), BSC.T.Get("JS.BSC.UI.Ws.Admin.Cancel"), BSC.T.Get("JS.BSC.UI.Ws.Admin.saveemailtemplate.HTML"), null, null, "info");
                $("#" + guid + "_send").show();
                return;
            }

            BSC.UI.Loader(guid);
            // Refresh Broadcast
            var data = {
                message: message,
                productid: productid,
                visibility: visibility,
                guid: guid
            };

//            BSC.UI.AlertConfirmPurchase(data, function(res)
//            {
//                alert("Broadcast sent");
//            });
//            return;

            $.post("/broadcast/create", data, function(r)
            {
                if (!r.success)
                {
                    if (r.error == 1)
                        BSC.UI.AlertLogin();
                    else if (r.error == 2)
                        BSC.UI.AlertCurrentlyFREE();
                    else if  (r.error == 3)
                        BSC.UI.AlertCurrentlyFREE();
                    else
                        BSC.UI.Alert(BSC.T.Get("JS.BSC.UI.Ws.BroadCast.SendCB.OK"), null, r.header, r.message, BSC.UI.W.Close(this.d));
                    return;
                }
                $("#" + guid + "_send").show();

                BSC.UI.HideLoader(data.guid);
                GO("/broadcast/confirm");
            }, "json");
        };

        return {
            Bind: function(guid)
            {
                $("#" + guid + "_content").css("overflow-y", "auto");
                $("#" + guid + "_content").css("overflow-x", "auto");
                $("#" + guid + "_bcmessage").bind("keydown", guid, BSC.UI.Ws.Broadcast.ValidateCharCount);
                $("#" + guid + "_bcmessage").bind("keyup", guid, BSC.UI.Ws.Broadcast.ValidateCharCount);
                $("#" + guid + " .box.buttons").bind("click", guid, function(e)
                {
                    var el = BSC.E.GetElement(e, "div", "a");
                    var p = (el.id).split("_");
                    var action = p[1];
                    if (action === "cancel")
                        BSC.UI.W.Close(e.data);
                    else if (action === "send")
                        SendBroadcast(e.data);
                    return false;
                });

                $("#" + guid + "_close").bind("click", guid, function(e) { BSC.UI.W.Close(e.data); });
            },
            Unbind: function(guid)
            {
                $("#" + guid + "_bcmessage").unbind();
                $("#" + guid + "_bcmessage").unbind();
                $("#" + guid + " .box.buttons").unbind();
            },
            ValidateCharCount: function(e)
            {
                if (this.value.length > maxlimit)
                {
                    this.value = this.value.substring(0, maxlimit);
                    $("#" + e.data + "_count").html("0");
                }
                else
                    $("#" + e.data + "_count").html("" + (maxlimit - this.value.length));
            }
        };
    } ();
})();
BSC.E.Subscribe("bind", BSC.UI.Ws.Broadcast.Bind, null, "broadcast.create");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Broadcast.Unbind, null, "broadcast.create");
BSC.E.Subscribe("bind", BSC.UI.Ws.Broadcast.Bind, null, "broadcast.confirm");
BSC.E.Subscribe("unbind", BSC.UI.Ws.Broadcast.Unbind, null, "broadcast.confirm");

  
