function rootName(path) {
    var segments = path.split("/");
    if (segments.length > 0) {
        var baseName = segments[segments.length-1];
        if (typeof baseName == "undefined") {
            return "";
        } else {
            var dot = baseName.lastIndexOf(".");
            if (dot < 0) {
                return baseName;
            } else {
                return baseName.substring(0, dot);
            }
        }
    } else {
       return "";
    }
}

function convertPage(format) {
    var re = new RegExp("^http://([^/]*)/(.*)$");
    var matched = re.exec(window.location);
    if (matched != null && matched.length >= 2) {
        var server = matched[1];
        var path = ""; 
        if (matched.length >= 3) {
            path = matched[2];
        }

        var outName;
        var url;
        if (server == "www.xmlmind.com") {
            if (path.length == 0 || path.charAt(path.length-1) == "/") {
                path += "index.html";
            }

            outName = rootName(path);
            url = "file:/usr/local/httpd/xmlmind/" + path;
        } else {
            var map = ["xmlmind/", "xmlmind_site",
                       "docrep/", "docrep_site",
                       "ditac/", "ditac_site",
                       "qizx/", "qizx_site",
                       "qizxopen/", "qizxopen_site",
                       "foconverter/", "xfc_site",
                       "xmleditor/", "xmleditor_site"];

            for (var i = 0; i < map.length; i += 2) {
                var pathPrefix = map[i];
                var siteName = map[i+1];

                if (path.indexOf(pathPrefix) == 0) {
                    path = path.substring(pathPrefix.length, path.length);
                    if (path.length == 0 || path.charAt(path.length-1) == "/") {
                        path += "index.html";
                    }

                    outName = rootName(path);
                    url = "file:/home/hussein/" + siteName + "/site/" + path;
                    break;
                }
            }
        }

        var href = "http://" + server + ":8080/xslsrv/exec";
        if (outName.length > 0) {
            // Allows the Web browser to suggest a usable save file name.
            // Such PATH_INFO is ignored by the Servlet.
            outName = escape(outName) + "." + format.toLowerCase();
            href += "/" + outName;
        }
        // FOP 0.95 crashes from time to time with hyphenate=true.
        // May be this bug is fixed in FOP 1.0.
        href += "?mode=sync&param1=hyphenate&value1=false";
        href += "&param0=root-id&value0=contents-box&conv=xhtmlTo" + format;
        href += "&html=true&url=" + escape(url);

        window.location = href;
    }
}

