/**
 * Sajax.php-hoz tartozó JS objektum
 * @version $Id: nxSajax.js,v 1.1 2010-11-05 14:45:54 horfer Exp $
 */
nxSajax = function( request_type, uri, obj ){
	this.counter= 0;
	this.eval   = false;
	this.refObj = null;
	this._debug = !false;
	if( typeof obj != 'boolean' && obj !== null ){
		this.nxCalendar = obj;
		this._debug = this.nxCalendar.debug;
	}else if( obj!= null ){
		this._debug = obj;
	}
	this.uri = uri;
	this.request_type = new String( request_type ).toLocaleUpperCase().toString();
	this.target_id = "";
	this.failure_redirect;
	this.requests = new Array();
	this.init();
	
}
/**
 * Referencia objektum beállítása
 * @param {Object} refObj
 */
nxSajax.prototype.setRefObj = function( refObj ){
	this.refObj = refObj;
}
/**
 * Debug
 * @param {Object} text
 */
nxSajax.prototype.debug = function( text ){
	if ( this._debug ){
		alert( text );
	}
}
/**
 * Inicializáció
 */
nxSajax.prototype.init = function() {
	this.debug("sajax_init_object() called..");
	var A;
	if (window.XMLHttpRequest) {
		// If IE7, Mozilla, Safari, etc: Use native object
		var xmlHttp = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject) {
			var msxmlhttp = new Array(
				'Msxml2.XMLHTTP.5.0',
				'Msxml2.XMLHTTP.4.0',
				'Msxml2.XMLHTTP.3.0',
				'Msxml2.XMLHTTP',
				'Microsoft.XMLHTTP');
		
			// ...otherwise, use the ActiveX control for IE5.x and IE6
			for (var i = 0; i < msxmlhttp.length; i++) {
				try {
					xmlHttp = new ActiveXObject(msxmlhttp[i]);
					break;
				} catch (e) {
					xmlHttp = null;
				}
			}
		}
	}
	if (!xmlHttp) {
		this.debug("Could not create connection object.");
	}
	return xmlHttp;
}
		
/**
 * lekérdezések leállítása
 */
nxSajax.prototype.cancel = function() {
	for (var i = 0; i < this.requests.length; i++) 
		this.requests[i].abort();
}
		
nxSajax.prototype.doCall = function(func_name, args) {
	var i, x, n;
	var post_data;
	var target_id;
	
	this.debug( "in this.doCall().." + this.request_type + "/" + this.target_id );
	target_id = this.target_id;
	if ( typeof( this.request_type ) == "undefined" || this.request_type == "" ){
		this.request_type = "GET";
	}
	uri = this.uri;
	if (this.request_type == "GET") {
		if (uri.indexOf("?") == -1) 
			uri += "?rs=" + escape(func_name);
		else
			uri += "&rs=" + escape(func_name);
		uri += "&rst=" + escape(this.target_id);
		uri += "&rsrnd=" + new Date().getTime();
		
		for ( i = 0; i < args.length - 1; i++ ){
			uri += "&rsargs[]=" + escape(args[i]);
		}
		post_data = null;
	} 
	else if (this.request_type == "POST") {
		post_data = "rs=" + escape( func_name );
		post_data += "&rst=" + escape( this.target_id );
		post_data += "&rsrnd=" + new Date().getTime();
		
		for (i = 0; i < args.length-1; i++) 
			post_data = post_data + "&rsargs[]=" + escape(args[i]);
	}
	else {
		alert("Illegal request type: " + this.request_type);
	}
	
	x = this.init();
	if (x == null) {
		if (this.failure_redirect != "") {
			location.href = this.failure_redirect;
			return false;
		} else {
			this.debug("NULL sajax object for user agent:\n" + navigator.userAgent);
			return false;
		}
	} else {
		x.open(this.request_type, uri, true);
		// window.open(uri);
		
		this.requests[this.requests.length] = x;
		
		if (this.request_type == "POST") {
			x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
			x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		var obj = this;
		var counter = ++this.counter;
		x.onreadystatechange = function() {
			if ( obj.counter != counter ) {
				return;
			}
			if ( x.readyState != 4 ) {
				return;
			}
			obj.debug("received " + x.responseText);
			var status;
			var data;
			var txt = x.responseText.replace(/^\s*|\s*$/g,"");
			status = txt.charAt(0);
			data = txt.substring(2);
			if (status == "") {
				// let\'s just assume this is a pre-response bailout and let it slide for now
			} else if (status == "-") 
				alert("Error: " + data);
			else {
				if (target_id != "") 
					document.getElementById(target_id).innerHTML = eval(data);
				else {
					try {
						var callback;
						var extra_data = false;
						if (typeof args[args.length-1] == "object") {
							callback = args[args.length-1].callback;
							extra_data = args[args.length-1].extra_data;
						}else if( typeof args[args.length-1] == "array" ){
							
						}
						else {
							callback = args[args.length-1];
						}
						if(typeof callback == 'function') {
							data = Url.decode( data );
							if ( obj.eval ) {
								callback(eval(data), extra_data);
							}else{
								callback( data, extra_data);
							}
						}
					} catch (e) {
						obj.debug("Caught error " + e + ": Could not eval " + data );
					}
				}
			}
		}
	}
	
	this.debug(func_name + " uri = " + uri + "/post = " + post_data);
	x.send(post_data);
	this.debug(func_name + " waiting..");
	delete x;
	return true;
}

