function topix_forumctx_internal(obj, jsfile, url, pcode, cobrand)
{
    var ok = true;
    var error = "";

    if (!url.match("^http://"))
    {
        ok = false;
        error = "invalid url, url must start with 'http://', no https, or raw hostnames, the url you passed in was: " + url;
    }
    pcode = "" + pcode;
    if (pcode && !pcode.match("^[0-9]+$"))
    {
        ok = false;
        error = "invalid pcode, pcode must be a number.  the pcode you passed in was: " + pcode;
    }
    
    if (!ok && obj.testing && error)
    {
        alert("topix debug error: " + error);
    }
    if (!ok)
    {
        return;
    }
    
    var baseurl = obj.host + "/forum/ctx/" + jsfile;
    var params = "url=" + escape(url);
    params += "&version=" + obj.version;
    if (obj.testing)
    {
        params += "&test=1";
    }
    if (pcode > 0)
    {
        params += "&p=" + pcode;
        params += "&s=PB";
        if (cobrand)
        {
            params += "&co=1";
        }
    }

    if (window.location.href && window.location.href.match("topix_forcerecrawl="))
    {
        if (confirm("Force topix to try to recrawl " + url))
        {
            params += "&force=1";
        }
    }

    var scripturl = baseurl + "?" + params;
    document.write("<" + "script language=\"JavaScript\" src=\"" + scripturl + "\">");
    document.write("<" + "/script>");
}

function topix_forumctx_obj(host, version, testing)
{
    this.host = host;
    this.version = version;
    this.testing = testing;
    if (document.topix_forumctx_override_host)
    {
        this.host = document.topix_forumctx_override_host;
    }
    this.create_link = function(url) { topix_forumctx_internal(this, "link.js", url, 0, 0) };
    this.create_form = function(url) { topix_forumctx_internal(this, "widget.js", url, 0, 0) };
    this.create_cobrand_link = function(url, pcode, cobrand) { topix_forumctx_internal(this, "link.js", url, pcode, cobrand) };
    this.create_cobrand_form = function(url, pcode, cobrand) { topix_forumctx_internal(this, "widget.js", url, pcode, cobrand) };
}

document.topix_forumctx = new topix_forumctx_obj("http://forumctx.topix.net", 1, 0);
document.topix_forumctx_test = new topix_forumctx_obj("http://forumctx-test.topix.net", 1, 1);

