%D \module
%D   [       file=java-exa,
%D        version=2002.??.??,
%D          title=\CONTEXT\ JavaScript Macros,
%D       subtitle=Example Support,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% XFDF versus HTML
% localhost versus remote versus set

% filename | filename-nr => name
% fakename | fakename-nr => file upload, unless localhost
%
% we erase the list because otherwise we end up in browser remembering
% problems; also, it is not possible to set upload fields 'manually'

\startJSpreamble request_1 used now

    var example_method = "HTML" ;
    var example_host   = "" ;
    var example_port   = "" ;
    var example_url    = "" ;
    var example_file   = "" ;

    var example_log    = true ; % false

    function stripped_exa_value( name ) {
        f = this.getField(name) ;
        if (f) {
            str = f.value ;
            if (str.indexOf(name+'-')==0) {
                str = str.substr(name.length+1) ;
            }
            return str ;
        } else {
            return "" ;
        }
    }

    function identify_example_url ( ) {
        if (example_log) {
            console.clear ;
            console.println("base url: "+this.baseURL) ;
            console.println("this url: "+this.URL) ;
        }
        if (this.baseURL != "") {
            example_url = this.baseURL ;
        } else {
            if (this.URL != "") {
                example_url = this.URL
            }
        }
        if (example_url.indexOf("file://")>=0) {
            example_url = "" ;
        }
        str = stripped_exa_value("examplehost") ;
        if (str != "auto") {
            example_host = str ;
            if (str == "localhost") {
                example_port = "8061"
            }
        }
        str = stripped_exa_value("exampleport") ;
        if (str != "auto") {
            example_port = str ;
        }
        if (example_log) {
            console.println("example_url: "+example_url) ;
            console.println("example_host: "+example_host) ;
            console.println("example_port: "+example_port) ;
        }
    }

    function check_example_url ( url ) {
        if (url.indexOf("file://")>=0) {
            url = "http://localhost:8061" ;
            console.println("file url replaced by: "+url) ;
        } else {
            if (url.indexOf("://")<0) {
                url = "http://" + url ;
            }
            if (example_log) {
                console.println("url before check: "+url) ;
            }
%            url = url.replace(/(http:\\\/\\\/.+)(\\\/.*$)/, "$1") ;
            url = url.replace(/(https*:\\\/\\\/.+?)(\\\/.*$)/, "$1/exarequest") ;
            if (example_log) {
                console.println("url after check: "+url) ;
            }
        }
        return url ;
    }

    function set_example_xfdf ( method ) {
        example_method = method ;
    }

    function set_example_file ( file ) {
        example_method = file ;
    }

    function set_example_host ( host ) {
        example_host = host ;
    }

    function set_example_port ( port ) {
        example_host = host ;
    }

    function do_erase_example_file(tag) {
        f = this.getField(tag) ;
        if (f) {
            f.value = "" ;
        }
    }

    function do_erase_example_list(tag) {
        for (i=1;i<=100;i++) {
            f = this.getField(tag+"-"+i) ;
            if (f) {
                f.value = "" ;
            } else {
                return
            }
        }
    }

    function do_submit_example_url ( url ) {
        if ((example_method == "XFDF" ) || (url.indexOf("localhost")>=0)) {
            do_erase_example_file("fakename") ;
            do_erase_example_list("fakename") ;
        }
        url = check_example_url(url) ;
        if (example_log) {
            console.println("submitting form to "+url+" using method "+example_method) ;
        }
        if (example_file != "") {
            url = url + "/" + example_file ;
        }
        % we need the bFDF for acrobat 5
        this.submitForm({cURL : url, bFDF : false, cSubmitAs : example_method}) ;
    }

    function submit_example_form ( ) {
        identify_example_url() ;
        if (example_host != "") {
            if (example_port != "") {
                do_submit_example_url(example_host+":"+example_port) ;
            } else {
                do_submit_example_url(example_host) ;
            }
        } else {
            if (example_url != "") {
                do_submit_example_url(example_url) ;
            } else {
                if (example_port != "") {
                    do_submit_example_url("localhost"+":"+example_port) ;
                } else {
                    do_submit_example_url("localhost:8061") ; % local exampler
                }
            }
        }
        resetfilename () ;
    }

    function submit_form ( host, port) {
        set_example_host(host) ;
        set_example_post(post) ;
        submit_example_form ;
    }

\stopJSpreamble

\startJSpreamble request_2 used now

    var exa_command    = "" ;
    var exa_option     = "" ;
    var exa_filename   = "" ;
    var exa_filelist   = "" ;
    var exa_registered = "" ;

    function set_request (command,option) {
        exa_command = command ;
        if (exa_command == "") {
            exa_option = option ;
        } else { if (option == "") {
            exa_option = "" ;
        } else { if (option.indexOf("--")<0) {
            exa_option = "--action=" + option ;
        } else {
            exa_option = option ;
        } } }
    }

    function assemble_request ( ) {
        v = this.getField('filename') ;
        if (v) {
            exa_filename = v.value ;
        }
        v = this.getField('filelist') ;
        if (v) {
            exa_filelist = v.value ;
        }
%       exa_filename = exa_filename.replace(/\\\\/g,'/') ;
%       exa_filelist = exa_filelist.replace(/\\\\/g,'/') ;
        str = "<?xml version='1.0' ?>\\n" ;
        str = str+"<exa:request xmlns:exa='http://www.pragma-ade.com/schemas/example.rng'><exa:application>" ;
        if (exa_filelist == "") {
            exa_filelist = exa_registered ;
        } else { if (exa_registered != "") {
            exa_filelist = exa_filelist + "\\n" + exa_registered ;
        } }
        if (exa_command != "") {
            str = str+"<exa:command>"+exa_command+"</exa:command>" ;
        }
        if (exa_option != "") {
            str = str+"<exa:option>"+exa_option+"</exa:option>" ;
        }
        if (exa_filename != "") {
            str = str+"<exa:filename>"+exa_filename+"</exa:filename>" ;
        }
        var fls = "" ;
        if (exa_filelist != "") {
            % old method, soon obsolete
            lst = exa_filelist.split(/\\s/) ;
            for (i=0;i<lst.length;i++) {
                fls = fls+"<exa:entry>"+lst[i]+"</exa:entry>" ;
            }
        } else { if (exa_multiple) {
            for (i=1;i<=100;i++) {
                % console.println("file field "+i) ;
                f = this.getField("filename-"+ i) ;
%                 if (f) { if (f.value != "") {
%                     if (g) { if (g.value == "") {
%                         fls = fls+"<exa:entry>"+f.value+"</exa:entry>" ;
%                     } else {
%                         fls = fls+"<exa:entry label='" + g.value + "'>"+f.value+"</exa:entry>" ;
%                     } } else {
%                         fls = fls+"<exa:entry>"+f.value+"</exa:entry>" ;
%                     }
%                 } }
                if (f) { if (f.value != "") {
                    fls = fls + "<exa:entry" ;
                    g = this.getField("dataname-"+ i) ;
                    if (g) { if (g.value == "") {
                        % console.println("a") ;
                        fls = fls + ">" ;
                    } else {
                        % console.println("b") ;
                        fls = fls + " label='" + g.value + "'>" ;
                    } } else {
                        % console.println("c") ;
                        fls = fls + ">" ;
                    }
                    fls = fls + f.value + "</exa:entry>" ;
                } }
            }
        } }
        if (fls != "") {
            str = str + "<exa:filelist>" + fls + "</exa:filelist>" ;
        }
        str = str + "</exa:application></exa:request>" ;
        v = this.getField('exa:request') ;
        if (v) {
            v.value = str ;
        }
    }

\stopJSpreamble

\startJSpreamble request_3 used now

    var exa_multiple = false ;

    function setfilename ( suffixes ) {
        if (event.targetName) {
            var name = event.targetName ;
            var fake = name.replace(/filename/,"fakename") ;
        } else {
            var name = 'filename' ;
            var fake = 'fakename' ;
        }
        f = this.getField(fake) ;
        if (f) {
            f.browseForFileToSubmit() ;
            if ((suffixes != "") && (f.value != "")) {
                %
                s = suffixes.replace(/,/g,"|") ;
                r = new RegExp() ;
                s = "\\\\.(" + s + ")$" ;
                r.compile(s, "i") ;
                if (f.value.search(r)<=0) {
                    f.value = "" ;
                    app.alert('This filetype is not permitted.') ;
                %
                % lst = suffixes.split(/,/) ;
                % ok = false ;
                % for (i=0;i<lst.length;i++) {
                %     if (f.value == lst[i]) {
                %         ok = true ;
                %     }
                % }
                % if (! ok) {
                %     f.value = "" ;
                %     app.alert('This filetype is not permitted.') ;
                %
                }
            }
            g = this.getField(name) ;
            if (g) {
                g.value = f.value ;
            }
% we need to set the value of the selector to empty else
% we get some funny preloading of multimeg files in spite
% of the specs that say that xml fields will not get sent
%             if (example_method == "XFDF" ) {
%                 f.value = "" ;
%             } else {
%                 identify_example_url ;
%                 if (example_url.indexOf("localhost")>=0) {
%                     f.value = "" ;
%                 }
%             }
        }
        this.dirty = false ;
    }

    function addfilename () {
        if (exa_multiple) {
            h = this.getField("filelist") ;
            g = this.getField("filename") ;
            if ((g) && (h)) {
                str = g.value ;
                if (h.value == "") {
                    h.value = str ;
                } else {
                    h.value = h.value + "\\n" + str ;
                }
                g.value = "" ;
                this.value = "" ;
            }
        }
        this.dirty = false ;
    }

    % this only works with client that assembles request

    function registerfilename (str) {
        if (str!="") {
            h = this.getField("filelist") ;
            if (h) {
                if (h.value != "") {
                    h.value = h.value + "\\n" ;
                }
                h.value = h.value + str ;
            } else {
                if (exa_registered != "") {
                    exa_registered = exa_registered + "\\n" ;
                }
                exa_registered = exa_registered + str ;
            }
        }
        console.show ;
        console.println('registered files') ;
        console.println("file: "+str) ;
        console.println("list: "+exa_registered) ;
        this.dirty = false ;
    }

    function checkfilename () {
    }

    function getfilename ( suffixes ) {
        setfilename(suffixes) ;
        checkfilename() ;
        addfilename() ;
    }

    function resetfilename () {
        do_erase_example_file("fakename") ;
        do_erase_example_file("filename") ;
        do_erase_example_file("filelist") ;
        do_erase_example_list("filename") ;
        do_erase_example_list("fakename") ;
    }

\stopJSpreamble

\endinput
