var powered_url      = "http://www.orgdot.com";
var powered_status   = "click to visit orgdot.com!";
var swfit_url        = "http://www.swfit.com";
var swfit_status     = "click for info on swfit!";
var client_url       = "/";
var client_status    = "click to visit scenekunstbruket.no!";
window.defaultStatus = "swfit - powered by orgdot";

var publish_root     = "/pub";
var system_root      = "/swfit2";
var site_root        = "/s2j";
var login_servlet    = system_root + "/servlet/swfit2login";
var app_servlet      = system_root + "/swapp";

// menu item
function menuItem(item_)
{
    menuItem(item_, false);
}

function menuItem(item_, validate_)
{
    if (validate_ == false || isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet;
        document.forms['swfit_form'].todo.value = item_;
        swfitSubmit();
    }
}

// site
function openSite(sid_)
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet + "?sid=" + sid_;
        document.forms['swfit_form'].todo.value = "site";
        swfitSubmit();
    }
}

// domain 

function gotoDomain()
{
    menuItem("domain");
}

function openDomain(did_)
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet + "?did=" + did_;
        document.forms['swfit_form'].todo.value = "domain";
        swfitSubmit();
    }
}

// template
function openTemplate(tid_)
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet + "?tid=" + tid_;
        document.forms['swfit_form'].todo.value = "template";
        swfitSubmit();
    }
}

// folder class
function openFolderClass(fcid_)
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet + "?fcid=" + fcid_;
        document.forms['swfit_form'].todo.value = "folderclass";
        swfitSubmit();
    }
}

// autor
function openAuthor(uid_)
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet + "?p_user__uid=" + uid_;
        document.forms['swfit_form'].todo.value = "author";
        swfitSubmit();
    }
}

// menu 

function gotoMenu()
{
    menuItem("menusel");
}

function openMenuItems(mnid_)
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet + "?mnid=" + mnid_;
        document.forms['swfit_form'].todo.value = "menuitemsel";
        swfitSubmit();
    }
}

function openMenuItem(mid_)
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet + "?mid=" + mid_;
        document.forms['swfit_form'].todo.value = "menuitem";
        swfitSubmit();
    }
}

// resource

function openImageEdit(rid_)
{
    openResourceEdit("image", rid_);
}

function openFileEdit(rid_)
{
    openResourceEdit("file", rid_);
}

function openResourceEdit(type_, rid_)
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet + "?rid=" + rid_;
        document.forms['swfit_form'].todo.value = type_;
        swfitSubmit();
    }   
}

function openArticleImageEdit(aid_, rid_)
{
    openArticleResourceEdit("artimage", aid_, rid_);
}

function openArticleFileEdit(aid_, rid_)
{
    openArticleResourceEdit("artfile", aid_, rid_);
}

function openArticleResourceEdit(type_, aid_, rid_)
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet + "?aid=" + aid_ + "&rid=" + rid_;
        document.forms['swfit_form'].todo.value = type_;
        swfitSubmit();
    }   
}

// folder
function gotoFolder()
{
    menuItem("folder");
}

function openFolderOrganizer(fid_)
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet + "?fid=" + fid_;
        document.forms['swfit_form'].todo.value = "folderartsel";
        swfitSubmit();
    }
}

// category
function gotoCategory()
{
    menuItem("category");
}

function openCategory(cid_)
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet + "?cid=" + cid_;
        document.forms['swfit_form'].todo.value = "category";
        swfitSubmit();
    }
}

function openCategoryOrganizer(fid_, cid_)
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet + "?fid=" + fid_ + "&cid=" + cid_;
        document.forms['swfit_form'].todo.value = "catartsel";
        swfitSubmit();
    }
}

// article
function gotoArticle()
{
    menuItem("article");
}

function openArticle(aid_)
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = app_servlet + "?aid=" + aid_;
        document.forms['swfit_form'].todo.value = "article";
        if (document.forms['swfit_form'].aid) 
        {
            document.forms['swfit_form'].aid.value = aid_;       
        }
        swfitSubmit();
    }
}

// helpers

function nuWin(url_)
{
    window.open(url_);
}

function open_info_url(url_)
{
    openInfoUrl(url_);
}

function openInfoUrl(url_)
{
    if (url_ != "") 
    {
        document.location.href = url_;
    }
    return false;
}

function swfitSubmit()
{
    if (self.document)
    { 
        if (self.document.swfit_form)
        {
            document.swfit_form.submit();
        } 
        else if (self.document.forms['swfit_form'])
        {
            document.forms['swfit_form'].submit();
        }
    }
}

function isValid()
{
    if (document.forms['swfit_form'].p_is_valid.value == "false" 
        && document.forms['swfit_form'].p_is_ready.value == "false")
    {
        if (hasError())
        {
            alert(document.forms['swfit_form'].p_error_msg.value);
        }
        return false;
    }
    return true;
}

function hasError()
{
    if (document.forms['swfit_form'].p_error_msg.value != "")
    {
        return true;
    }
    return false;
}

function artPreview(aid_)
{
    nuWin(site_root + "/preview?aid=" + aid_ + "&pvw=1");
}

function categoryPreview(dname_, name_, cid_)
{
    var url = publish_root + "/";
    if (dname_ != "")
    {
        url += dname_ + "/";
    }
    url += name_ + "?cid=" + cid_ + "&pvw=1";
    nuWin(url);
}

function folderPreview(dname_, name_)
{
    var url = publish_root + "/";
    if (dname_ != "")
    {
        url += dname_ + "/";
    }
    url += name_ + "?pvw=1";
    nuWin(url);
}

function doLogin()
{
    openInfoUrl("/swfit2");
}

function doLogout()
{
    if (isValid() == true)
    {
        document.forms['swfit_form'].action = login_servlet;
        document.forms['swfit_form'].mission.value = "do_logout";
        swfitSubmit();
    }
}
