﻿function OpenSpecialsDialog(propId, data, method) {
    if (typeof (pageTracker) != "undefined") {
        str = window.location.href;
        name = str.match(/\w+\.htm|html/) != null ? str.match(/\w+\.htm|html/) : "Community Homepage";            
        pageTracker._trackPageview(propId + "/" + name + "/SpecialsDialog");
    }

    if ($("#" + propId + "SpecialsDialog").length == 1) {
        $("#" + propId + "SpecialsDialog").dialog("open");
    } else {
        $.ajax({
            type: "POST",
            url: "/webservices/SpecialsService.asmx/" + method,
            data: data,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(arg) {
                var html = typeof (arg) == "string" ? arg : arg.d;
                var htmlOnly = html;
                //                var start = html.indexOf("<!--Begin JavaScript-->");
                //                if (start > 0) {
                //                    htmlOnly = html.substr(0, start) +
                //                    html.substr(html.indexOf("<!--End JavaScript-->") + "<!--End JavaScript-->".length);
                //                }
                var el = $("<div id=\"" + propId + "SpecialsDialog\" class=\"flora\" style='overflow:hidden;'>" + htmlOnly + "</div>");

                var elDlg = el.dialog({
                    modal: true,
                    width: 660,
                    height: 400,
                    overlay: {
                        opacity: 0.6,
                        background: "black"
                    },
                    close: function() {
                        $("#" + propId + "SpecialsDialog").dialog("destroy").remove();
                    }
                });
            },
            error: function(xhr, ajaxOptions, thrownError) {
                var response = eval("(" + xhr.responseText + ")");
                var el = $("<div id=\"" + propId + "SpecialsDialogError\" class=\"flora\">" +
                "<p>" + response.ExceptionType + "</p>" +
                "<p>" + response.Message + "</p>" +
                "<p>" + response.StackTrace + "</p>" +
                "</div>");
                el.dialog({
                    modal: true,
                    width: 800,
                    height: 600,
                    overlay: { opacity: 0.6, background: "black" },
                    close: function() {
                        $("#" + propId + "SpecialsDialogError").dialog("destroy").remove();
                    }
                });
            }
        });

    }
    return false;
}

