/*
 * $Id: ajax.js $
 *
 * Copyright (c) 2007 Logica. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of Logica
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with Logica.
 *
 * LOGICA MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-
 * INFRINGEMENT. LOGICA SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

/**
 * This object can be used to execute an action and to call the sended methods to be executed.
 */
function AjaxObject() {
    
    var ready = true;
    
    var request = null;
    var xmlProcessMethod = null;
    var textProcessMethod = null;
    
    this.executeAction = function(url, textMethod, xmlMethod) {
    
        ready = false;
        
        textProcessMethod = textMethod;
        xmlProcessMethod = xmlMethod;
        
        if (window.XMLHttpRequest) { // Non-IE browsers

            request = new XMLHttpRequest();
            request.onreadystatechange = processStateChange;
            
            try {
                request.open("GET", url, true);
                request.setRequestHeader("Content-Type", "text/plain;charset=ISO-8859-1");
            } catch (e) {
                alert(e);
            }
            
            request.send(null);
        } else if (window.ActiveXObject) { // IE

            request = new ActiveXObject("Microsoft.XMLHTTP");
        
            if (request) {
                request.onreadystatechange = processStateChange;
                request.open("GET", url, true);
                request.setRequestHeader("Content-Type", "text/plain;charset=ISO-8859-1");
                request.send();
            }
        }
    }

     var processStateChange = function() {

        if (request.readyState == 4) { // Complete
        
            if (request.status == 200) { // OK response
                
                if(textProcessMethod != null) {
                    textProcessMethod(request.responseText);
                }
                
                if(xmlProcessMethod != null) {
                    xmlProcessMethod(request.responseXml);
                }
                
                ready = true;
                
            } else {
                
                alert("The action could not be executed. \nStatus code : " + request.statusText);
            }
        }
    }
}

var ajaxObj = new AjaxObject();