//Waiting image
waitingImage="/images/rotating_arrow.gif";

//tag in the html to change
csstag="ajaxbody";
loadingcsstag="loading";

/*
---------------------------------------------------------------------------

 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program (see the file COPYING); if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA  02111-1307, USA
 
 The following code is copyright 2003 by Matthew Eernisse
 Modified by Yvonne L
 
---------------------------------------------------------------------------
*/ 
function formData2QueryString(docForm)
{
    var strSubmitContent = '';
    var formElem;
    var strLastElemName = '';

    for (i = 0; i < docForm.elements.length; i++) {

        formElem = docForm.elements[i];
        switch (formElem.type) {
            // Text fields, hidden form elements
            case 'text':
            case 'hidden':
            case 'password':
            case 'textarea':
            case 'image':
            case 'select-one':
                strSubmitContent += formElem.name + '=' + encodeURIComponent(formElem.value) + '&';
                break;
            case 'select-multiple':
                for (count = 0; count < formElem.length; count++) {
                    if ( formElem[count].selected ) {
                        strSubmitContent += formElem.name + '=' + encodeURIComponent(formElem[count].value) + '&';
                    }
                }
                break;
            // Radio buttons
            case 'radio':
                if (formElem.checked) {
                    strSubmitContent += formElem.name + '=' + encodeURIComponent(formElem.value) + '&';
                }
                break;
            // Checkboxes
            case 'checkbox':
                if (formElem.checked) {
                    // Continuing multiple, same-name checkboxes
                    if (formElem.name == strLastElemName) {
                        // Strip of end ampersand if there is one
                        if (strSubmitContent.lastIndexOf('&') == strSubmitContent.length-1) {
                            strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
                        }
                        // Append value as comma-delimited string
                        strSubmitContent += ',' + encodeURIComponent(formElem.value);
                    }
                    else {
                        strSubmitContent += formElem.name + '=' + encodeURIComponent(formElem.value);
                    }
                    strSubmitContent += '&';
                }
                break;
        }
        strLastElemName = formElem.name;
    }

    // Remove trailing separator
    strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
    return strSubmitContent;
}

//taken from http://www.sitepoint.com/article/take-command-ajax
function makeHttpRequest(url, callback_function) 
{ 
    var http_request = false; 
 
    if (window.XMLHttpRequest) { // Mozilla, Safari,... 
        http_request = new XMLHttpRequest(); 
        if (http_request.overrideMimeType) { 
        http_request.overrideMimeType('text/xml'); 
        } 
    } else if (window.ActiveXObject) { // IE 
        try { 
            http_request = new ActiveXObject("Msxml2.XMLHTTP"); 
        } catch (e) { 
            try { 
                http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
            } catch (e) {} 
        } 
    } 
 
    if (!http_request) { 
        alert('Unfortunatelly you browser doesn\'t support this feature.'); 
        return false; 
    } 
    http_request.onreadystatechange = function() { 
        if (http_request.readyState == 4) { 
        if (http_request.status == 200) { 
            eval(callback_function + '(http_request.responseText)'); 
        } else { 
            alert('There was a problem with the request.(Code: ' + http_request.status + ')'); 
        } 
        } 
    } 
    http_request.open('GET', url, true); 
    http_request.send(null); 
}

function showloading() {
    document.getElementById(loadingcsstag).innerHTML = '<img src="' + waitingImage + '" alt="Loading.....">';
}

function hideloading() {
    document.getElementById(loadingcsstag).innerHTML = '<img src="/images/spacer.jpg" width="1" height="21">';
}

function loadpage(page) {
    showloading();
    makeHttpRequest(page, 'writespan');
}
function loadform(myform, action) {
    showloading();
    request=formData2QueryString(myform);
    makeHttpRequest(action + '?' + request, 'writespan');
}
function writespan(body) {
    document.getElementById(csstag).innerHTML = body;
    hideloading();
} 

function loadjava(page) {
    showloading();
    makeHttpRequest(page, 'runjava');
}
function runjava(body) {
    eval(body);
    hideloading();
}

