var closeMenuTimer;
var menuHoverTimer;
var main_menu_open;
var test;

function openLoginDropdown(e) {
    if (e) {
        e.preventDefault();
    }
    $("fieldset#signin_menu").toggle();
    $(".signin").toggleClass("menu-open");
    $("#username").focus();
    return false;
}

function notice_close(notice) {
    $(".notice_" + notice).slideUp();
    var d = new Date();
    var t = parseInt(d.getTime() / 1000, 10);
    createCookie("sdnotice" + notice, t, 365);
}

function menu_closesub() {
    main_menu_open = false;
    $("#main_menu_02 table").hide().find("li").hide();
    $("#main_menu_03 table").hide().find("li").hide();
    $("#main_menu_05 table").hide().find("li").hide();
}

function stopCloseMenuTimer() {
    clearTimeout(closeMenuTimer);
}

function startCloseMenuTimer() {
    clearTimeout(menuHoverTimer);
    stopCloseMenuTimer();
    closeMenuTimer = setTimeout(menu_closesub, 100);
}

function menu_showsub(ele) {
    $("#main_menu_02 table").show().find("li").slideDown("fast");
    $("#main_menu_03 table").show().find("li").slideDown("fast");
    $("#main_menu_05 table").show().find("li").slideDown("fast");
    main_menu_open = true;
}

function menuHover(e) {
    var elTarget = $(e.target);
    var menuWait = 450;
    stopCloseMenuTimer();
    clearTimeout(menuHoverTimer);
    while (!elTarget.is("#menu2")) {
        if (elTarget.is("#main_menu_05, #main_menu_03, #main_menu_02")) {
            if (!main_menu_open) {
                clearTimeout(menuHoverTimer);
                menuHoverTimer = setTimeout(function () {
                    menu_showsub(elTarget);
                }, menuWait);
            }
            break;
        }
        else if (elTarget.is("#main_menu_01, #main_menu_04")) {
            clearTimeout(menuHoverTimer);
            break;
        }
        else {
            elTarget = elTarget.parent();
        }
    }
}

function menu_bind() {
    menu_closesub();
    $("#menu2").mouseover(menuHover).mouseout(startCloseMenuTimer);
}

function sdinit() {
    setUserOptions();
    menu_bind();
}

function toggle_firstpost_tab(tab) {
    var allvistabs = $("#sharethistab:visible,#publicfeedback:visible");
    var tabcontainer = $("#firstpost_tabcontainer");
    if ("#" + allvistabs.attr("id") == tab) {
        tabcontainer.slideToggle("fast");
    }
    else {
        if (allvistabs.length === 0) {
            tabcontainer.hide();
        }
        allvistabs.not(tab).hide();
        $(tab).fadeIn("fast");
        tabcontainer.slideDown();
    }
}
$(document).ready(function () {
    sdinit();
    $(".signin").click(openLoginDropdown);
    $("fieldset#signin_menu").mouseup(function () {
        return false;
    });
    $(document).mouseup(function (e) {
        if ($(e.target).parent("a.signin").length === 0) {
            $(".signin").removeClass("menu-open");
            $("fieldset#signin_menu").hide();
        }
    });
    $(function () {
        $('#forgot_username_link').tipsy({
            gravity: 'w'
        });
    });
    $('#global_login_box').dialog({
        autoOpen: false,
        position: 'top',
        width: 300
    });
    $('#global_userbar_login').click(function () {
        $('#global_login_box').dialog('open');
        return false;
    });
    $('#global_userbar_username').click(function () {
        if ($("#global_userbar_pulldown").is(":visible")) {
            $("#global_userbar_pulldown,.settings_box.sbuserbar").fadeOut('fast');
        }
        else {
            $("#global_userbar_pulldown").fadeIn('fast');
        }
        return false;
    });
    $("#searchbar_menu_link").click(function () {
        $("#searchbar_menu").slideToggle("normal");
    });
    $(".settings_button").click(function () {
        if ($(".settings_box.sbuserbar").is(":visible")) {
            $(".settings_box.sbuserbar").fadeOut('fast');
        }
        else {
            $(".settings_box.sbuserbar").fadeIn('fast');
        }
        return false;
    });
    $(".settings2_button").click(function () {
        if ($(".settings_box.mystuff").is(":visible")) {
            $(".settings_box.mystuff").fadeOut('fast');
        }
        else {
            $(".settings_box.mystuff").fadeIn('fast');
        }
        return false;
    });
    $(".sbuserbar .settings_close").click(function () {
        $(".settings_box.sbuserbar").fadeOut("fast");
        return false;
    });
    $(".mystuff .settings_close").click(function () {
        $(".settings_box.mystuff").fadeOut("fast");
        return false;
    });
    $("#search_box").focus(function () {
        if (this.value == 'Enter Keywords') {
            this.value = '';
        }
    });
    $("#search_box").blur(function () {
        if (this.value == '') {
            this.value = 'Enter Keywords';
        }
        $("#searchbar_menu").slideUp("normal");
    });
    $("#global_search_form").submit(function () {
        try {
            pageTracker._trackEvent('globalsearch', 'search', $("#search_box").val());
        }
        catch (err) {
            return true;
        }
    });
    if ($("#sharethislinkthread").length == 1) {
        $("#sharethislinkthread").click(function () {
            toggle_firstpost_tab("#sharethistab");
            return false;
        });
    }
    $("#showfeedback").click(function () {
        toggle_firstpost_tab("#publicfeedback");
        var sdfirstptab = fetch_cookie("sdfirstptab");
        sdfirstptab = (sdfirstptab === 0) ? 1 : 0;
        createCookie("sdfirstptab", sdfirstptab, 365);
        return false;
    });
    $(".voteselected").click(function () {
        $("#vote_feedback").fadeOut();
        return false;
    });
    $(".showwikipost").click(function () {
        $("#wikipost").slideToggle("normal");
        return false;
    });
    $("#editwiki").click(function () {
        $("#wikipost:hidden").show();
        return false;
    });
    $(".blackarrow img").attr("src", "http://javaiq.net/images/arrow_down.gif");
    $(".notice_tan .notice_close").click(function () {
        notice_close("tan");
        return false;
    });
    $(".notice_blue .notice_close").click(function () {
        notice_close("blue");
        return false;
    });

    function megaHoverOver() {
        $(this).find(".sub").show();
        (function ($) {
            jQuery.fn.calcSubWidth = function () {
                rowWidth = 0;
                $(this).find("ul").each(function () {
                    rowWidth += $(this).width();
                });
            };
        })(jQuery);
        if ($(this).find(".row").length > 0) {
            var biggestRow = 0;
            $(this).find(".row").each(function () {
                $(this).calcSubWidth();
                if (rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });
            $(this).find(".sub").css({
                'width': biggestRow
            });
            $(this).find(".row:last").css({
                'margin': '0'
            });
        } else {
            $(this).calcSubWidth();
            $(this).find(".sub").css({
                'width': rowWidth
            });
        }
    }

    function megaHoverOut() {
        if ($(this).find("#sub_mystuff").length > 0) {
            $(".settings_box.mystuff").hide();
        }
        $(this).find(".sub").hide();
    }
    var config = {
        sensitivity: 2,
        interval: 100,
        over: megaHoverOver,
        timeout: 50,
        out: megaHoverOut
    };
    $("ul#global_nav_v3 li").hoverIntent(config);
});

function log_out() {
    ht = document.getElementsByTagName("html");
    ht[0].style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
    if (confirm('Are you sure you want to log out?')) {
        return true;
    }
    else {
        ht[0].style.filter = "";
        return false;
    }
}

function displayExtra(layer_name, expand_name) {
    layerObject = getObject(layer_name);
    expandObject = getObject(expand_name);
    if (layerObject.style.display == "none") {
        layerObject.style.display = "";
        expandObject.innerHTML = "";
    }
    else {
        layerObject.style.display = "none";
        expandObject.innerHTML = "<span class=small><a href=\"javascript:displayExtra('" + layer_name + "','" + expand_name + "')\">Expand</a> <img src=images/expand.jpg width=7 height=7></span>";
    }
}
jQuery.fn.fadeToggle = function (speed, easing, callback) {
    return this.animate({
        opacity: 'toggle'
    }, speed, easing, callback);
};

function toggledeal(id, state) {
    var headerid = id.replace("deal_", "deal_header_");
    switch (state) {
    case 'ca':
        $("#" + id).hide();
        $("#" + headerid).removeClass("deal_header_expand");
        break;
    case 'c':
        $("#" + id).hide();
        $("#" + headerid).removeClass("deal_header_expand");
        break;
    case 'ea':
        $("#" + id).show();
        $("#" + headerid).addClass("deal_header_expand");
        break;
    case 'e':
        $("#" + id).show();
        $("#" + headerid).addClass("deal_header_expand");
        break;
    default:
        if ($("#deal_list").hasClass("grid")) {
            location.href = $("#" + id + "_permalink").attr("href");
            return false;
        }
        $("#" + id).toggle();
        $("#" + headerid).toggleClass("deal_header_expand");
        if ($('div#' + id + '_like iframe').length == 0) {
            var post_no = parseInt(id.replace('deal_', ''));
            $('div#' + id + '_like').html('<iframe src="http://www.facebook.com/plugins/like.php?href=http://slickdeals.net/permadeal/' + post_no + '&layout=button_count&show_faces=false&width=450&action=like&colorscheme=light&height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:90px; height:21px;" allowTransparency="true"></iframe>');
        }
        break;
    }
    return true;
}

function toggle(obj) {
    $(obj).toggle();
}

function toggleimg(img) {
    var img_re = new RegExp("_collapsed\\.gif$");
    if (img.src.match(img_re)) {
        img.src = img.src.replace(img_re, '.gif');
    } else {
        img_re = new RegExp("\\.gif$");
        img.src = img.src.replace(img_re, '_collapsed.gif');
    }
}

function togglewelcome(id) {
    $("#" + id).slideUp();
    createCookie(id, 1, 365);
}

function toggledeal_array() {
    var img = $("#" + arguments[0]);
    var deals = $("nonexistant");
    var dealheaders = $("nonexistant");
    var len = arguments.length;

    for (var i = 1; i < len; i++) {
        deals = deals.add("#" + arguments[i]);
        dealheaders = dealheaders.add("#" + arguments[i].replace("deal_", "deal_header_"));
    }

    if (img.attr("src").match("plus.gif")) {
        img.attr("src", "/images/minus.gif");
        dealheaders.addClass("deal_header_expand");
        deals.show();
    } else {
        img.attr("src", "/images/plus.gif");
        deals.hide();
        dealheaders.removeClass("deal_header_expand");
    }
}

function sdfetch_object(idname) {
    if (document.getElementById) {
        return document.getElementById(idname);
    }
    else if (document.all) {
        return document.all[idname];
    }
    else if (document.layers) {
        return document.layers[idname];
    }
    else {
        return null;
    }
}

function toggle_hottopic(id) {
    var canSee = 'table-row';
    if (navigator.appName.indexOf("Microsoft") > -1) {
        canSee = 'block';
    }
    obj = sdfetch_object(id);
    var newobj;
    if (obj.style.display == canSee) {
        obj.style.display = "none";
        newobj = sdfetch_object(id + "_heading");
        if (newobj.className == "alt4a") {
            newobj.className = "alt6";
        }
        else {
            newobj.className = "alt5";
        }
    }
    else {
        obj.style.display = canSee;
        newobj = sdfetch_object(id + "_heading");
        if (newobj.className == "alt5") {
            newobj.className = "alt4";
        }
        else {
            newobj.className = "alt4a";
        }
    }
}

function toggleexpandall() {
    expandall = (readCookie('expandall') == 1) ? 0 : 1;
    createCookie('expandall', expandall, 365);
    state = expandall ? 'e' : 'c';
    if (state == 'e') {
        $("#fp_style_expandall").addClass("active").blur();
        $(".deal_body").show();
        $(".dealitem").addClass("deal_header_expand");
        $(".deal_header_date img").attr("src", "/images/minus.gif");
    }
    else {
        $("#fp_style_expandall").removeClass("active").blur();
        $(".deal_body").hide();
        $(".dealitem").removeClass("deal_header_expand");
        $(".deal_header_date img").attr("src", "/images/plus.gif");
    }
}

function getElementsByClass(searchClass, node, tag) {
    var classElements = [];
    if (!node) {
        node = document;
    }
    if (!tag) {
        tag = '*';
    }
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp('(^|\\s)' + searchClass + '(\\s|$)');
    for (i = 0, j = 0; i < elsLen; i++) {
        if (pattern.test(els[i].className)) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

function addEvent(obj, type, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(type, fn, false);
    }
    else if (obj.attachEvent) {
        obj["e" + type + fn] = fn;
        obj[type + fn] = function () {
            obj["e" + type + fn](window.event);
        };
        obj.attachEvent("on" + type, obj[type + fn]);
    }
    else {
        obj["on" + type] = obj["e" + type + fn];
    }
}

function insertAfter(parent, node, referenceNode) {
    parent.insertBefore(node, referenceNode.nextSibling);
}

function showDiv(e, id) {
    var getdiv = sdfetch_object(id);
    getdiv.style.display = "";
    var x = (e.pageX ? e.pageX : (e.clientX ? e.clientX : 0));
    var y = (e.pageY ? e.pageY : (e.clientY ? e.clientY : 0));
    getdiv.style.left = x + 5 + "px";
    getdiv.style.top = y + 5 + "px";
}

function closeDiv(e, id) {
    if (!e) {
        e = window.event;
    }
    var getdiv = sdfetch_object(id);
    getdiv.style.display = "none";
}
jQuery.fn.extend({
    sdshowRow: function () {
        var animparam = {};
        animparam.opacity = 'show';
        if (!jQuery.browser.msie) {
            animparam.height = 'show';
        }
        this.find("td").not(".sdrowwrap").children().wrap("<div></div>").end().addClass("sdrowwrap");
        return this.show().find("div").not(".nosdanim").animate(animparam, "fast");
    },
    sdhideRow: function () {
        var animparam = {};
        animparam.opacity = 'hide';
        if (!jQuery.browser.msie) {
            animparam.height = 'hide';
        }
        var jqObject = this;
        this.find("td").not(".sdrowwrap").children().wrap("<div></div>").end().addClass("sdrowwrap");
        this.find("div").not(".nosdanim").slice(0).animate(animparam, "fast");
        this.find("div").not(".nosdanim").slice(0, 1).animate(animparam, "fast", function () {
            return jqObject.hide();
        });
        return jqObject;
    },
    sdtoggleRow: function () {
        if (this.css("display") == "none") {
            return this.sdshowRow();
        }
        else {
            return this.sdhideRow();
        }
    }
});

function cumulativeOffset(element) {
    var valueT = 0,
        valueL = 0;
    do {
        valueT += element.offsetTop || 0;
        valueL += element.offsetLeft || 0;
        element = element.offsetParent;
    } while (element);
    return [valueL, valueT];
}

function toggleSearchFilter() {
    var filtCol = $("#search_left");
    var $sideimg = $("#search_showhidefilter img");
    var $newsrc;
    if (filtCol.filter(":visible").length > 0) {
        filtCol.hide().parent().attr("width", "0px").siblings().attr("width", "100%");
        createCookie("hidefiltercol", 1, 365);
        $newsrc = $sideimg.attr("src").replace("hide", "show");
    }
    else {
        createCookie("hidefiltercol", 0, 365);
        filtCol.show().parent().attr("width", "150px").siblings().removeAttr("width");
        $newsrc = $sideimg.attr("src").replace("show", "hide");
    }
    $sideimg.attr("src", $newsrc);
}

function cellHover(ele) {
    ele = $(ele);
    if (ele.hasClass("alt1")) {
        ele.removeClass("alt1").addClass("alt1Active");
    }
    else if (ele.hasClass("alt2")) {
        ele.removeClass("alt2").addClass("alt2Active");
    }
    if (ele.hasClass("tdClick")) {
        ele.addClass("cursor");
    }
}

function cellHoverOut(ele) {
    ele = $(ele);
    if (ele.hasClass("alt1Active")) {
        ele.removeClass("alt1Active").addClass("alt1");
    }
    else if (ele.hasClass("alt2Active")) {
        ele.removeClass("alt2Active").addClass("alt2");
    }
    if (ele.hasClass("tdClick")) {
        ele.removeClass("cursor");
    }
}

function cellHoverClick(ele, linksel) {
    ele = $(ele);
    var loc = "";
    var tdTarget = ele.find("a.tdTarget");
    if (tdTarget.length > 0) {
        ele = tdTarget;
    }
    if (ele.is("a")) {
        loc = ele.attr("href");
    }
    else {
        loc = ele.find("a:" + linksel).attr("href");
    }
    window.location.href = loc;
}

function tdHover() {
    cellHover(this);
}

function tdHoverOut() {
    cellHoverOut(this);
}

function tdHoverClick(ev) {
    if (!(ev.shiftKey || ev.ctrlKey)) {
        cellHoverClick(this, "last");
    }
}

function tdThreadHover() {
    cellHover(this);
    cellHover($(this).siblings(".tdPostDate"));
    cellHover($(this).siblings(".tdThread"));
}

function tdThreadHoverOut() {
    cellHoverOut(this);
    cellHoverOut($(this).siblings(".tdPostDate"));
    cellHoverOut($(this).siblings(".tdThread"));
}

function tdPostDateClick(ev) {
    if (!(ev.shiftKey || ev.ctrlKey)) {
        var threadlink = $(this).attr("id");
        threadlink = threadlink.replace("td_postdate_", "thread_title_");
        cellHoverClick($("#" + threadlink));
    }
}

function akst_share_url(base, url, title) {
    base = base.replace('{url}', url);
    return base.replace('{title}', title);
}

function akst_share(id, url, title) {
    var form = sdfetch_object("akst_form");
    var post_id = sdfetch_object("akst_post_id");
    var post_message = sdfetch_object("akst_email_message");
    var post_subject = sdfetch_object("akst_email_subject");
    var post_action = sdfetch_object("akst_form_action");
    var post_link = sdfetch_object("akst_post_permalink");
    var permalink = sdfetch_object("akst_permalink_link");
    var post_hidden_url = sdfetch_object("akst_post_hidden_url");
    var post_hidden_t = sdfetch_object("akst_post_hidden_t");
    var messagelink = sdfetch_object("atsk_sendmessage_link");
    if (form.style.display == 'block' && post_id.value == id) {
        form.style.display = 'none';
        return;
    }
    var link = sdfetch_object("akst_link_" + id);
    var offset = cumulativeOffset(link);
    sdfetch_object("akst_delicious").href = akst_share_url("http://del.icio.us/post?url={url}&title={title}", url, title);
    sdfetch_object("akst_digg").href = akst_share_url("http://digg.com/submit?phase=2&url={url}&title={title}", url, title);
    sdfetch_object("akst_furl").href = akst_share_url("http://furl.net/storeIt.jsp?u={url}&t={title}", url, title);
    sdfetch_object("akst_netscape").href = akst_share_url(" http://www.netscape.com/submit/?U={url}&T={title}", url, title);
    sdfetch_object("akst_yahoo_myweb").href = akst_share_url("http://myweb2.search.yahoo.com/myresults/bookmarklet?u={url}&t={title}", url, title);
    sdfetch_object("akst_stumbleupon").href = akst_share_url("http://www.stumbleupon.com/submit?url={url}&title={title}", url, title);
    sdfetch_object("akst_google_bmarks").href = akst_share_url("  http://www.google.com/bookmarks/mark?op=edit&bkmk={url}&title={title}", url, title);
    sdfetch_object("akst_technorati").href = akst_share_url("http://www.technorati.com/faves?add={url}", url, title);
    sdfetch_object("akst_blinklist").href = akst_share_url("http://blinklist.com/index.php?Action=Blink/addblink.php&Url={url}&Title={title}", url, title);
    sdfetch_object("akst_newsvine").href = akst_share_url("http://www.newsvine.com/_wine/save?u={url}&h={title}", url, title);
    sdfetch_object("akst_reddit").href = akst_share_url("http://reddit.com/submit?url={url}&title={title}", url, title);
    sdfetch_object("akst_windows_live").href = akst_share_url("https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url={url}&title={title}&top=1", url, title);
    sdfetch_object("akst_tailrank").href = akst_share_url("http://tailrank.com/share/?link_href={url}&title={title}", url, title);
    sdfetch_object("akst_facebook").href = akst_share_url("http://www.facebook.com/share.php?&={url}", url, title);
    sdfetch_object("akst_aim").href = akst_share_url("aim:goim?message={title}{url}", url, title);
    sdfetch_object("akst_magnolia").href = akst_share_url("http://ma.gnolia.com/bookmarklet/add?url={url}&title={title}", url, title);
    messagelink.href = "http://slickdeals.net/forums/sendmessage.php?do=sendtofriend&t=" + id;
    post_action.action = "http://slickdeals.net/forums/sendmessage.php?do=dosendtofriend&t=" + id;
    post_id.value = id;
    post_hidden_url.value = url;
    post_hidden_t.value = id;
    post_message.value = "Greetings! This SlickDeal has been shared with you:\n\n'" + decodeURIComponent(title) + "'.\n\nRead more about it at " + decodeURIComponent(url) + "";
    post_subject.value = "Shared from Slickdeals: " + decodeURIComponent(title);
    post_link.value = decodeURIComponent(url);
    permalink.innerHTML = '<a href="' + decodeURIComponent(url) + '">Permalink For This Post</a>';
    form.style.left = offset[0] + 'px';
    form.style.top = (offset[1] + link.offsetHeight + 3) + 'px';
    form.style.display = 'block';
}

function akst_share_tab(tab) {
    var tab1 = sdfetch_object('akst_tab1');
    var tab2 = sdfetch_object('akst_tab2');
    var tab3 = sdfetch_object('akst_tab3');
    var body1 = sdfetch_object('akst_social');
    var body2 = sdfetch_object('akst_email');
    var body3 = sdfetch_object('akst_permalink');
    switch (tab) {
    case '1':
        tab3.className = '';
        tab2.className = '';
        tab1.className = 'selected';
        body3.style.display = 'none';
        body2.style.display = 'none';
        body1.style.display = 'block';
        break;
    case '2':
        tab3.className = '';
        tab1.className = '';
        tab2.className = 'selected';
        body3.style.display = 'none';
        body1.style.display = 'none';
        body2.style.display = 'block';
        break;
    case '3':
        tab3.className = 'selected';
        tab1.className = '';
        tab2.className = '';
        body3.style.display = 'block';
        body1.style.display = 'none';
        body2.style.display = 'none';
        break;
    }
}

function updateHtmlBits(htmlbits) {
    htmlbits = $(htmlbits);
    htmlbits.find('htmlbit').each(function () {
        var id_text = $(this).attr('id');
        var effect = $(this).attr('effect');
        var mode = $(this).attr('mode');
        var newbit = $($(this).text());
        var bit = $("#" + id_text);
        if (typeof(effect) != "undefined") {
            bit.hide();
            try {
                newbit.hide();
            }
            catch (e) {}
        }
        if (mode == "remove_append" || mode == "remove_prepend") {
            bit.remove();
            mode = mode.replace("remove_", "");
        }
        if (mode == "append") {
            var appendto = $("#" + $(this).attr('appendto'));
            if (appendto.children("tbody").length > 0) {
                appendto.children("tbody").append(newbit);
            }
            else {
                appendto.append(newbit);
            }
        }
        else if (mode == "prepend") {
            var prependto = $("#" + $(this).attr('prependto'));
            if (prependto.children("tbody").length > 0) {
                prependto.children("tbody").prepend(newbit);
            }
            else {
                prependto.prepend(newbit);
            }
        }
        else {
            bit.replaceWith(newbit);
        }
        if (effect == "slide") {
            newbit.slideDown();
        }
        else if (effect == "fade") {
            newbit.fadeIn();
        }
    });
    var redir = htmlbits.find('redirecturl');
    if (redir.length > 0) {
        if (redir.attr('delay')) {
            setTimeout("window.location = '" + redir.text() + "';", redir.attr('delay'));
        }
        else {
            window.location = redir.text();
        }
    }
}

function geturlvar(url, varname) {
    var vars = url.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == varname) {
            return pair[1];
        }
    }
    return "";
}

function successdialog(msg, okfunc) {
    var sdialog = $("#success_dialog");
    if (sdialog.length === 0) {
        $("body").append("<div id='success_dialog' title='Success'></div>");
        sdialog = $("#success_dialog");
        sdialog.dialog({
            autoOpen: false,
            modal: true,
            buttons: {
                Ok: function () {
                    if (okfunc) {
                        okfunc(this);
                    }
                    $(this).dialog('close');
                }
            }
        });
    }
    sdialog.html(msg);
    sdialog.dialog("open");
}

function errordialog(error, nobuttons, w) {
    var errdialog = $("#errordialog");
    if (errdialog.length === 0) {
        $("body").append("<div id='errordialog' title='Error'></div>");
        errdialog = $("#errordialog");
        errdialog.dialog({
            bgiframe: true,
            autoOpen: false,
            modal: true,
            width: (w ? w : ''),
            buttons: (nobuttons ? {} : {
                Ok: function () {
                    $(this).dialog('close');
                }
            })
        });
    }
    errdialog.html(error);
    errdialog.dialog('open');
}

function confirmdelete(postid) {
    var messagebox = confirm('You have chosen to delete Post ' + postid + '.\r\rClick OK to delete it, or Cancel to hide this prompt.');
    var divobj, myform, sessinput, doinput, pinput, dinput, sinput, bodyobj;
    if (messagebox) {
        divobj = document.createElement("div");
        divobj.display = "none";
        myform = document.createElement("form");
        myform.action = "editpost.php";
        myform.method = "post";
        sessinput = document.createElement("input");
        sessinput.name = "s";
        sessinput.type = "hidden";
        sessinput.value = fetch_sessionhash();
        doinput = document.createElement("input");
        doinput.name = "do";
        doinput.type = "hidden";
        doinput.value = "deletepost";
        pinput = document.createElement("input");
        pinput.name = "p";
        pinput.type = "hidden";
        pinput.value = postid;
        dinput = document.createElement("input");
        dinput.name = "deletepost";
        dinput.type = "hidden";
        dinput.value = "delete";
        sinput = document.createElement("input");
        sinput.name = "securitytoken";
        sinput.type = "hidden";
        sinput.value = SECURITYTOKEN;
        myform.appendChild(sessinput);
        myform.appendChild(doinput);
        myform.appendChild(pinput);
        myform.appendChild(dinput);
        myform.appendChild(sinput);
        divobj.appendChild(myform);
        bodyobj = document.getElementById("contentbody");
        bodyobj.appendChild(divobj);
        myform.submit();
        return false;
    }
    return true;
}

function confirmfilter(postid) {
    var messagebox = confirm('You have chosen to filter Post ' + postid + '.\r\rClick OK to filter it, or Cancel to hide this prompt.');
    var divobj, myform, sessinput, doinput, pinput, sinput, bodyobj;
    if (messagebox) {
        divobj = document.createElement("div");
        divobj.display = "none";
        myform = document.createElement("form");
        myform.action = "editpost.php";
        myform.method = "post";
        sessinput = document.createElement("input");
        sessinput.name = "s";
        sessinput.type = "hidden";
        sessinput.value = fetch_sessionhash();
        doinput = document.createElement("input");
        doinput.name = "do";
        doinput.type = "hidden";
        doinput.value = "filterpost";
        pinput = document.createElement("input");
        pinput.name = "p";
        pinput.type = "hidden";
        pinput.value = postid;
        sinput = document.createElement("input");
        sinput.name = "securitytoken";
        sinput.type = "hidden";
        sinput.value = SECURITYTOKEN;
        myform.appendChild(sessinput);
        myform.appendChild(doinput);
        myform.appendChild(pinput);
        myform.appendChild(sinput);
        divobj.appendChild(myform);
        bodyobj = document.getElementById("contentbody");
        bodyobj.appendChild(divobj);
        myform.submit();
        return false;
    }
    return true;
}

function confirmphdelete(postid, phid) {
    var messagebox = confirm('You have chosen to delete Post History ' + phid + ' for Post ' + postid + '.\r\rClick OK to delete it, or Cancel to hide this prompt.');
    var divobj, myform, sessinput, doinput, pinput, phinput, sinput, bodyobj;
    if (messagebox) {
        divobj = document.createElement("div");
        divobj.display = "none";
        myform = document.createElement("form");
        myform.action = "posthistory.php";
        myform.method = "post";
        sessinput = document.createElement("input");
        sessinput.name = "s";
        sessinput.type = "hidden";
        sessinput.value = fetch_sessionhash();
        doinput = document.createElement("input");
        doinput.name = "do";
        doinput.type = "hidden";
        doinput.value = "delete";
        pinput = document.createElement("input");
        pinput.name = "p";
        pinput.type = "hidden";
        pinput.value = postid;
        phinput = document.createElement("input");
        phinput.name = "phid";
        phinput.type = "hidden";
        phinput.value = phid;
        sinput = document.createElement("input");
        sinput.name = "securitytoken";
        sinput.type = "hidden";
        sinput.value = SECURITYTOKEN;
        myform.appendChild(sessinput);
        myform.appendChild(doinput);
        myform.appendChild(pinput);
        myform.appendChild(phinput);
        myform.appendChild(sinput);
        divobj.appendChild(myform);
        bodyobj = document.getElementById("contentbody");
        bodyobj.appendChild(divobj);
        myform.submit();
        return false;
    }
    return true;
}

function toggle_postcollapse(objid) {
    if (!is_regexp) {
        return false;
    }
    obj = fetch_object("collapseobj_" + objid);
    div = fetch_object("collapsediv_" + objid);
    divh = fetch_object("collapsedivh_" + objid);
    if (!obj) {
        return false;
    }
    if (obj.style.display == "none") {
        obj.style.display = "";
        if (div) {
            div.style.display = "none";
            divh.style.display = "";
        }
    }
    else {
        obj.style.display = "none";
        if (div) {
            div.style.display = "";
            divh.style.display = "none";
        }
    }
    return false;
}

function hideGlobalFeature() {
    var feature = $("#global_feature");
    feature.slideToggle("fast");
    createCookie("sdfeaturebar", feature.attr("version"), 365);
    return false;
}
