/**
 * by Florin Vasilache <florin.vasilache@work.ro> on 2 Oct 2006
 * v 1.0 - 02102006
 *
 * dwi-ajax encapsulates the transaction request and callback logic.
 *
 */

//DWI.namespace("ajax");

DWI.ajax = {
    /**
        will dynamicly load js recived from ajax response
    **/
    load_js: function (file) {
        if (typeof file == 'string') {
            var ins = document.body.firstChild;
            var script = document.createElement('script');
                script.src = file;
                script.type = 'text/javascript';
                script.language = 'JavaScript';
                ins.parentNode.insertBefore(script,ins);          
        }
        
    },

	handleSuccess: function (o) {
        this.stopAnimation();
        this.response = {};
        try {
            this.response = YAHOO.lang.JSON.parse(o.responseText); // ptr yui 260
        } catch (e) {
            if (o.argument.supressFail) {
                return false;
            }
            this.reponse = {
                fail: true,
                error: e
            }

        }
        if (! this.response) {
            if (o.argument.supressFail) {
                return false;
            }
            alert('Eroare nedefinita. Dati refresh la pagina !');
        } else if (this.response.fail) {
            if (this.response.e) {
                alert(this.response.e);
            } else {
                alert('Eroare nedefinita. Dati refresh la pagina');
            }
        } else if (typeof o.argument.success == 'function') {
            o.argument.success(this.response);
        }
        return true;
	},

	handleFailure: function (o) {
        this.stopAnimation();
        this.response = {};
        if (o.argument.supressFail) {
            return false;
        }
            
        this.response = o.responseText;
        if(typeof o.argument.fail == 'function') {
            o.argument.fail(this.response);
        } else {
            //alert(this.debug(o));
            alert('Eroare nedefinita. Dati refresh la pagina ('+o.status+'-'+o.statusText+')');
        }
        return true;
	},
    
	startRequest: function (method, url, callback, param, loader) {
       this.startAnimation(loader);
	   YAHOO.util.Connect.asyncRequest(method, url, callback, param);
	},
	
    ReqTest: function () {
        var failCB = function () { };
        var succCB = function () { };
        callback.unsetArg();       
        callback.setSuccessCB(succCB);
        callback.setFailCB(failCB);

	    this.startRequest('POST', 'ajax.php', callback, "module=test");
	},
    animate: true,
    animation: null,
    to_stop: null,

    startAnimation: function (loader)
    {
        if (! this.animate) {
            return false;
        }
        if (loader) {
            this.to_stop = loader;
            YAHOO.util.Dom.removeClass(loader,'hide');
        } else {
            if (! this.animation) {
                this.animation = DWI.util.get_cache('ajax-loader');
                if (! this.animation) {
                    return false;
                }
            }
            this.to_stop = this.animation;        
            YAHOO.util.Dom.removeClass(this.animation,'hide');
        }
        
                                                                                                                                
        //window.onmousemove = function (e) { 
        /*
        document.onmousemove = function (e) { 
                                        if (! e) { var e = window.event; }
                                        var xy = YAHOO.util.Event.getXY(e);
                                        xy[0] += 20;
                                        xy[1] += 20;
                                        YAHOO.util.Dom.setXY(DWI.ajax.animation,xy);
                                        };
        */
        
    },

    stopAnimation: function ()
    {
        if (! this.animate) {
            return false;
        }
        //window.onmousemove = null;
        //document.onmousemove = null;
        if (this.to_stop) {
            YAHOO.util.Dom.addClass(this.to_stop,'hide');        
        } else if (this.animation) {
            YAHOO.util.Dom.addClass(this.animation,'hide');        
        }
    },

/****************************
        DEBUG
****************************/

    parseHeaders:function(headerStr) { 
        var allHeaders = headerStr.split("\n");
        var headers; 
        
        for(var i=0; i < headers.length; i++) {
            var delimitPos = header[i].indexOf(':');
            if(delimitPos != -1) {
                headers[i] = "<p>" +
                headers[i].substring(0,delimitPos) + ":"+
                headers[i].substring(delimitPos+1) + "</p>";
            }
        }
            
        return headers;
    },
    
    debug:function(o) {
        var output;
             output = "\nTransaction id: " + o.tId;
             output += "\nHTTP status: " + o.status;
             output += "\nStatus code message: " + o.statusText;
             output += "\nHTTP headers: \n " + o.getAllResponseHeaders + "\n";
             output += "\nPHP response: " + o.responseText;
             output += "\nXML response: " + o.responseXML;
             output += "\nArgument object: " + o.argument;
        return output;
    },

    noFailCB: function() {}


};

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
DWI.ajax.callback = 
{
	success:DWI.ajax.handleSuccess,
	failure:DWI.ajax.handleFailure,
    timeout: 5000,
	scope: DWI.ajax,
    argument: {},

    setSuccessCB: function(arg) {
        this.argument.success = arg;
    },
    setFailCB: function(arg) {
        this.argument.fail = arg;
    },
    unsetArg: function() {
        this.argument = { };
        this.timeout = 5000;
    },
    setArg: function (f,v)
    {
        this.argument[f] = v;
    }
};



