/**
 * This is the XTmotion's core Ajax library. It contains implementation of reusable
 * connector class which can be used accross different web applications 
 * to load remote data.
 *
 * @version 0.5 26/March/2006
 *
 * Copyright (c) XTmotion
 * United Kingdom, Europe
 * All Rights Reserved.
 */

/**
 * These are XTmotion's unique namespace objects 
 */
if (typeof(xtmotion) == "undefined") {
    xtmotion = new Object();
}
xtmotion.ajax = new Object();

/** 
 * Constants which define different states of an Ajax request.
 */
xtmotion.ajax.READY_STATE_UNINITIALIZED = 0;
xtmotion.ajax.READY_STATE_LOADING       = 1;
xtmotion.ajax.READY_STATE_LOADED        = 2;
xtmotion.ajax.READY_STATE_INTERACTIVE   = 3;
xtmotion.ajax.READY_STATE_COMPLETE      = 4;

/**
 * This class provides inerface necessary for on-demand Ajax requests. 
 * After an instance of this class has been created, clients can use 
 * the <code>loadRemoteData()</code> method to retrieve remote data.
 *
 * @param onfinish - reference to custom callback function that will be invoked upon a successful completition of the <code>loadRemoteData()</code> method  
 * @param onerror  - [optiona] reference to a custom callback function that will be invoked in case of a request error
 */
xtmotion.ajax.AjaxContentLoader = function(onfinish, onerror){
    this.url = null;
    this.req = null;
    this.onfinish = onfinish;
    this.onerror = (onerror) ? onerror : this.defaultError;
    this.error = null;
    this.defaultMethod = "POST";
}

/**
 * Definition of methods of the AjaxContentLoader class
 */
xtmotion.ajax.AjaxContentLoader.prototype = {
    
    /**
     *
     */
    loadRemoteData:function(url, params, method) {
        if (window.XMLHttpRequest) {
            this.req = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            this.req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (this.req) {
            try {
                var loader = this;
                var httParams  = (params) ? params : null;
                var httpMethod = (method) ? method : this.defaultMethod;
                this.req.onreadystatechange = function() {
                    loader.onReadyState.call(loader);
                }
                this.req.open(httpMethod, url, true);
                if (httpMethod.toUpperCase() == "POST") {
                    this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                }
                this.req.send(params);
            } catch (exception) {
                this.error = exception;
                this.onerror.call(this);
            }
        }
    },

    /**
     *
     */   
    onReadyState:function() {
        var req = this.req;
        var ready = req.readyState;
        if (ready == xtmotion.ajax.READY_STATE_COMPLETE) {
            var httpStatus = req.status;
            if (httpStatus == 200 || httpStatus == 0) {
                this.onfinish.call(this);
                req = null;
                this.req = null;
            } else {
                this.onerror.call(this);
            }
        }
    },

    /**
     *
     */   
    defaultError:function() {
        alert(
            "error fetching data!"
            + "\nreadyState:"+this.req.readyState
            + "\nexception: "+this.error.toString()
        );
//            + "\nstatus: "+this.req.status
//            + "\nheaders: "+this.req.getAllResponseHeaders()
   }
}
