/**
 * ajax constructor
 */
function Ajax() {
    this.http = false;
    this.method = 'GET';
    this.async = true;		//[2007-04-20] Rolandas: Kodel visada turi buti asinhroninis? :)

	//2007.07.04 Stepas: supaprastinta versija
	// IE7 & Firefox & opera & safari & ...
	if(window.XMLHttpRequest)
	{
	        this.http = new XMLHttpRequest();
    }
	else
	{ //IE < 7 & others
	    var msxmlhttp = ['Microsoft.XMLHTTP','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP'];
	    for (var i=0; i<msxmlhttp.length; i++) {
	        try {
	            this.http = new ActiveXObject(msxmlhttp[i]);
                break;
	        } catch (e) {
	            this.http = false;
	        }
	    }
	    
	    // for others
	    if ( !this.http && window.createRequest ) {
	        try {
	            this.http = window.createRequest();
	        } catch (e) {
	            this.http=false;
	        }
	    }		
	}
}

/**
 * retrieves JSON data from server via GET method
 */
Ajax.prototype.get = function (url, callback, params) {
    var http = this.http;

	//2007.07.04 Stepas: galimybe IE7 >1 karta naudoti ta pati objekta
	http.open('GET', url, this.async);

    http.onreadystatechange = function () {
        if ( http.readyState == 4 ) {
            if (http.status == 200) {
                if ( (typeof callback == 'function' ) && http.responseText.length > 1) {
                	try {
						if (params)	{
							callback(eval('('+http.responseText+')'),params);
						} else {
		                    callback(eval('('+http.responseText+')'));
						}
	                } catch(err) {
	                	if ( window.on_error_exec ) {
							var data = new Array();
								data.error = err;
								data.code = http.responseText;
		                	window.on_error_exec(data);
	                	} else {
		                	alert("AJAX Catch: " +err.description + '\n' +err );
	                	}	                	
	                }
                } else if( http.async == true ) {
                	if ( window.on_error_exec ) {
						var data = new Array();
							data.error = 'AJAX ERROR: callback function doesn\'t exists';
	                	window.on_error_exec(data);
                	} else {
                    	alert('AJAX ERROR: callback function doesn\'t exists');
					}
                }
            } else {
            	if ( window.on_error_exec ) {
					var data = new Array();					                
					data.error = 'unknown';
					data.status = http.status;
					window.on_error_exec(data);
	            } else {
		            alert('HTTP ERROR: '+http.status);
	            }
            }
        }
    }

    http.send(null);
    
	try {
    	if( this.async == false ) return eval('(' +http.responseText +')');
	}catch ( e ){
    	if ( window.on_error_exec ) {
			window.on_error_exec(e);
        } else {
            alert(e);
        }
	}
}


Ajax.prototype.call = function ( moduleName, functionName, param, callback ) {
	return this.get('/action3.php?m=' + moduleName +'&f=' +functionName +'&p=' +param.join('||'), callback);
}


/**
 * retrieves JSON data from server via POST method
 */
Ajax.prototype.post = function (url, callback) {
    var http = this.http;

    http.open('POST', url, true);

    http.onreadystatechange = function () {
        if ( http.readyState == 4 ) {
            if ( http.status == 404 ) {
                callback(eval(http.responseText));
            }
            else {
                callback(eval(http.responseText));
            } 
        }
    }

    http.send(null);
}

