/**
 * Copyright (c) 2006 IBM. All Rights Reserved.
 */
if (typeof wpf_io == "undefined")
{
var wpf_io = {
xhpool: [],
// Simplified data access method using same interface as dojo.io.bind.
bind: function(opts) {
    var method = (opts.formNode && opts.formNode.method) || "GET";
    var isPost = method.toLowerCase() == "post";    
    var url = opts.url || opts.formNode.action;    
    var qs;
    if (opts.formNode)
    {
        qs = this.serializeForm(opts.formNode);    
        if (qs && !isPost)
        {
            var sep = "?";
            if (url.indexOf("?") >= 0)
                sep = "&";
            url += sep + qs;
        }
    }
    var xh = this.getXMLHTTP();
    xh.xmlhttp.onreadystatechange = function() {
        if (xh.xmlhttp.readyState == 4) {
            if (xh.xmlhttp.status == 200 && xh.xmlhttp.responseText) {            
                var data = xh.xmlhttp.responseText;
                if (xh.xmlhttp.responseXML && /application.xml/.test(xh.xmlhttp.getResponseHeader("Content-Type"))) {
                    var feed = wpf_io.findElement(xh.xmlhttp.responseXML, "feed"); 
                    if (feed) {
                        var entry = wpf_io.findElement(feed, "entry");
                        if (entry) {
                            var c = wpf_io.findElement(entry, "content");
                            if (c && c.firstChild)
                                data = c.firstChild.nodeValue;
                        }
                    }
                }
                opts.load(null, data, null);
            }
            delete xh.xmlhttp['onreadystatechange'];
            xh.active = false;             
        }   
    };

    xh.xmlhttp.open(isPost ? "POST" : "GET", url, true);  
    xh.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");  
    xh.xmlhttp.send(isPost ? qs : null);
},
findElement: function(el, p) {
    for (var i = 0; i < el.childNodes.length; i++) {
        var n = el.childNodes[i];
        if (n.nodeType != 1) 
            continue;
        if (n.localName == p || n.baseName == p)
            return n;
    }
},
    
PROGIDS: ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'],

getXMLHTTP: function() {
    // See if there's an available xhr in the cache.
    var xh = null, xmlhttp = null;
    for (var x = 0; x < this.xhpool.length; x++) {
        if (!this.xhpool[x].active) {
            xh = this.xhpool[x];
            xh.active = true;     
            xh.xmlhttp.abort();
            return xh;
        }
    }

    var err = null; 
    // First try native JS XMLHttpRequest - if found, don't add to pool.
    try { xmlhttp = new XMLHttpRequest(); if (xmlhttp) { return { xmlhttp: xmlhttp, active: true }; } } catch(e) { } 
    // No native JS impl - try ActiveX.
    for (var i = 0; !xmlhttp && i < this.PROGIDS.length; ++i) {
        var progid = this.PROGIDS[i];
        try { xmlhttp = new ActiveXObject(progid); } catch(e) { err = e; }
    }
    if (xmlhttp) this.PROGIDS = [progid];
    else throw(err);
    // Add this to the pool, so we can reuse instances later.
    return this.cacheXMLHTTP(xmlhttp);
},
cacheXMLHTTP: function(xmlhttp) {
    var xh = { active: true, xmlhttp: xmlhttp };
    this.xhpool.push(xh);
    return xh;
},
serializeForm: function(form) {
    var q = [];
    for (var i = 0; i < form.elements.length; i++) {
        var vals = this.serializeElement(form.elements[i]);
        if (vals && vals.length > 0)
            q.push(vals.join("&"));
    }
    return q.join("&");
},

serializeElement: function(el) {
    var vals = [];
    if (!el.type) return vals;
    switch (el.type.toLowerCase()) {
    case 'text':    
    case 'textarea':
    case 'hidden':
    case 'password':    
    case 'submit':
        vals.push(encodeURIComponent(el.name) + "=" + encodeURIComponent(el.value));
        break;        
    case 'checkbox':
    case 'radio':
        if (el.checked)
            vals.push(encodeURIComponent(el.name) + "=" + encodeURIComponent(el.value));
        break;
    case 'select-one':
    case 'select-multiple':
        for (var i = 0; i < el.options.length; i++) {
            if (el.options[i].selected)
                vals.push(encodeURIComponent(el.name) + "=" + encodeURIComponent(el.options[i].value));
        }
        break;    
    }
    return vals;  
}
}
}

