// Project     : Collage Portal Server
// Module      : Remote scripting
// Created on  : March 22 2005
// CVS Id      : $Id: httprequest.js,v 1.9 2006/03/31 09:05:27 chris Exp $
// Description : Remote scripting HTTP request/reponse JavaScript mechanism based on
//              'id' property of some HTML tags.
//
// Polymita Technologies. All rights reserved.

// HTTP request connection object.
var httpConnection;

// Id of the HTML element where response will be inserted.
var htmlTagId;

// HTTP request via javascript.
// @param method GET or POST.
// @param url URl to be invoked it it contains scripts, they must use DEFER
// @params String of parameters to be added to the request at the end of the URL.
// @param tagId Id of the HTML element where response will be inserted.
function httpRequest(method, url, params, tagId) {
    if (tagId == null) {
        var separator = '?';
        if (url.indexOf(separator) != -1) separator = '&';
        document.location = url + separator + params;
        return;
    }
    htmlTagId = tagId;
    // Branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        httpConnection = new XMLHttpRequest();
        if (httpConnection) doSend(httpConnection, method, url, params);
    }
    // Branch for IE/Windows ActiveX version
    else if (window.ActiveXObject) {
        httpConnection = new ActiveXObject('Microsoft.XMLHTTP');
        if (httpConnection) doSend(httpConnection, method, url, params);
    }
    else return true;
    return false;
}

function doSend(httpConnection, method, url, params) {
    //alert("Send ("+method+") "+url+"   "+params);
    url = url.replace(/&amp;/g,'&');
    document.body.style.cursor = 'wait';
    httpConnection.onreadystatechange = httpResponse;
    if (method == 'POST') {
        if (!remoteScriptingSupported) {
            doSend(httpConnection, 'GET', url, params);
            return;
        }
        httpConnection.open(method, url, true);
        httpConnection.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        httpConnection.send(params);
    }
    else if (method == 'GET') {
        var separator = '?';
        if (url.indexOf(separator) != -1) separator = '&';
        var urlParams = url + separator + params;
        httpConnection.open(method, urlParams, true);
        httpConnection.send(null);
    }
    else {
        alert('Invalid Method ' + method);
    }
}

// HTTP response. Invoked after httpRequest completion.
function httpResponse() {
    // Only if req shows "complete"
    if (httpConnection.readyState == 4) {
        // only if "OK"
        if (httpConnection.status == 200) {
            var element = document.getElementById(htmlTagId);
            var newElement = document.createElement(element.tagName);
            newElement.id = htmlTagId;
            newElement.innerHTML = httpConnection.responseText;
            element.parentNode.replaceChild(newElement, element);
            document.body.style.cursor = 'default';
        }
        else {
            alert("XmlHttpRequest: There was a problem retrieving the data:\n" + httpConnection.status + ' ' + httpConnection.statusText);
        }
    }
}

/*function evalInsiderScripts(element) {
    var scripts = element.getElementsByTagName("script");
    var i;
    for (i = 0; i < scripts.length; i++) {
        if (scripts[i].innerHTML != "") {
            eval(scripts[i].innerHTML);
        }
        if (scripts[i].src != "") {
            var e = document.createElement("script");
            e.src = scripts[i].src;
            e.type = "text/javascript";
            scriptLoadedWatcher = false;
            document.getElementsByTagName("head")[0].appendChild(e);
        }
    }
} */

function supportsRemoteScripting() {
    if (window.XMLHttpRequest) {
        httpTmpConnection = new XMLHttpRequest();
        try {
            httpTmpConnection.open('POST', '/', true);
            httpTmpConnection.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        }
        catch(e) {
            // setRequestHeader is not implemented in Opera 8.00 thus you cannot inform
            // the server about the content type of the data you are POSTing with the
            // send method.
            return false;
        }
        return true;
    }
    // Branch for IE/Windows ActiveX version
    else if (window.ActiveXObject) {
        return true;
    }
    else {
        return false;
    }
}

var remoteScriptingSupported = supportsRemoteScripting();

// Returns a string with the specified form fields formatted in HTTP get format.
function getFormFieldsInHttpGetFormat(form) {
    var params = '';
    //alert(form.length+ " with remoteScriptingSupported = "+remoteScriptingSupported);
    for (var i = 0; i < form.length; i++) {
        field = form[i];
        if (field.type == 'hidden') {
            if (i > 0) params += '&';
            params += escape(field.name) + '=' + escape(field.value);
        }
        else if ((field.type == 'text' || field.type == 'textarea') && remoteScriptingSupported) {
            if (i > 0) params += '&';
            params += escape(field.name) + '=' + escape(field.value);
        }
        else if ((field.type == 'checkbox' || field.type == 'radio') && remoteScriptingSupported) {
            if (i > 0) params += '&';
            if (field.checked) params += escape(field.name) + '=' + escape(field.value);
        }
        else if ((field.type == 'select-one' || field.type == 'select-multiple') && remoteScriptingSupported) {
            if (i > 0) params += '&';
            for (var j = 0; j < field.length; j++) {
                if (j != 0)params += '&';
                if (field[j].selected) {
                    value = field[j].value;
                    if (value == '') value = field[j].text;
                    params += escape(field.name) + '=' + escape(value);
                }
            }
        }
        else {
            //alert(field.name+ ' is '+ field.type);
        }
        //alert(params);
    }
    return params;
}


