/**
 * Copyright (c) 2006 IBM. All Rights Reserved.
 */
if (typeof wpf_ppr == "undefined")
{

// Object used to do partial page refresh.
var wpf_ppr = {
    // Submit the form, then update the indicated areas based on what returns.
    submit: function(form, idlist, hdlr) {
       var myself = this;
       myself.startLoad();
       var options = {
            url: form.action,
            load: function(type, data, evt) { 
                myself.endLoad();
                (hdlr || wpf_ppr.explicitHandler).update(data, idlist);  
            },
            error: function(type, error) { myself.endLoad(); myself.error(type, error); }
       };
       if (form.elements)
           options["formNode"] = form;
       this.bind(options);
    },
    
    // Invoke URL, then update the indicated areas based on what returns.
    load: function(url, idlist, hdlr) {   
        var myself = this;
        myself.startLoad();
        this.bind({
            url: url,
            load: function(type, data, evt) { 
                myself.endLoad();            
                (hdlr || wpf_ppr.explicitHandler).update(data, idlist); 
            },
            error: function(type, error) { myself.endLoad(); myself.error(type, error); }
        });
    },
    error: function(type, error) { debugError(error); },    
    // Can overload these to, e.g., show a progress indicator for long operations.
    startLoad: function() { },
    endLoad: function() {  },
    
    bind: function(opts) {
        if (typeof wpf_io == "undefined")
            dojo.io.bind(opts);
        else
            wpf_io.bind(opts);
    },
    
    debug: function(msg) {
        if (typeof dojo == "undefined")
            alert(msg);
        else
            dojo.debug(msg);
    },
    
    debugError: function(err) {
        if (typeof dojo == "undefined")
            alert(err);
        else
            dojo.debug(dojo.errorToString(err));
    },
    
    buildWidgets: function() {
        if (typeof(dojo) != 'undefined' && dojo.hostenv) {
            if (typeof(dojo.hostenv.modulesLoaded) == 'function')
                dojo.hostenv.modulesLoaded();
            if (typeof(dojo.hostenv.makeWidgets) == 'function')
                dojo.hostenv.makeWidgets();
        }    
    },
    
    // Handler which evaluates returned data as JavaScript. If idlist is also specified,
    // replace matching document elements with the returned HTML.
    scriptHandler: {
        update: function(data, idlist) {    
            if (data) {
                // See if this data has a direct-evaluation-prevention wrapper - if so, remove it.
                for (var i = 0; i < wpf_ppr.wrapperPatterns.length; i++) {
                    var regex = wpf_ppr.wrapperPatterns[i];
                    var result = data.match(regex);
                    if (result != null) {
                        data = result[1];
                        break;
                    }
                }               
                eval(data);
            }
        }
    },
    
    // Take HTML from data and use it to replace contents of elements whose IDs are in idlist.
    directHandler: {
        update: function(data, idlist) {
            for (var i = 0; idlist && i < idlist.length; i++)
            {
                var id = idlist[i];
                var dst = document.getElementById(id);
                if (dst)
                    dst.innerHTML = data;
            }
            setTimeout(wpf_ppr.buildWidgets, 1);            
        }
    },

    // This handler parses the data as HTML, and walks over it replacing areas in the
    // document DOM with matching IDs.
    explicitHandler: {
        // Given some HTML as text, replace the current DOM items
        // as specified in idlist.
        update: function(data, idlist)
        {      
            var tmp = document.createElement("span");
            tmp.innerHTML = data;
            var nchildren = tmp.childNodes.length;
            for (var n = 0; n < nchildren; n++)
            {
                var node = tmp.childNodes[n];
                wpf_ppr.explicitHandler.updateContent(node, idlist);
            }
            setTimeout(wpf_ppr.buildWidgets, 1);
        },

        // Recursive function that walks a tree of DOM Nodes,
        // fetches content from any whose id is contained in idlist
        // and replaces the corresponding element in the main document.
        updateContent: function(node, idlist)
        {
            var id = node.getAttribute ? node.getAttribute("id") : null;
            if (id)
            {
                var found = false;
                // If no idlist specified, then find replace contents of
                // all document DOM items whose IDs are the same as IDs in
                // incoming content.
                if (!idlist || idlist.length == 0)
                {
                    var dst = document.getElementById(id);
                    if (dst /* && dst.innerHTML != node.innerHTML */) // would checking be a win?
                    {
                        dst.innerHTML = node.innerHTML;
                        return;
                    }
                }
                else
                {
                    for (var i = 0; i < idlist.length; i++)
                    {
                        if (id == idlist[i])
                        {
                            var dst = document.getElementById(id);
                            if (dst)
                                dst.innerHTML = node.innerHTML;
                            return;
                        }
                    }
                }
            }
            for (var c = node.firstChild; c != null; c = c.nextSibling)
            {
                wpf_ppr.explicitHandler.updateContent(c, idlist);
            }
        }
    }, // end of explicitHandler implementation
        
    debugHandler: {
        update: function(data, idlist) { debug(data); }
    },
    
    // Script data may be wrapped to prevent direct execution - by default, we handle these three ways:
    wrapperPatterns: [
        /^\/\*(.*)\s*\*\/\s*$/m, // Simply commented-out
        /^\/\*-secure-\s*(.*)\s*\*\/\s*$/m, // Prototype-style commenting
        /^\s*while\s*\(\s*1\s*\)\s*;(.*)$/m // Google-style infinite loop prefix
    ]    

} // end wpf_ppr definition
} // end include guard
