/* -----------------------------------------------------------------------------------------------------------*/

/*
 * IREDS_URL: object for AJAX and common URL calls
 */

IREDS_URL=Class.create();

IREDS_URL.prototype = {
	target:'',
	method:'GET',
	vars: '',
	callback: true, //true: use standard function, false: no callback; 
	onStart: false, //ajax onStart callback
	onSuccess: false, //ajax onSuccess callback
	onError: false, //ajax onError callback
	evalScripts: false,
	ajax_request: null,
	encoding: 'UTF-8',
	
	initialize: function(target) {
		var options = Object.extend({
		 }, arguments[1] || {});
		this.target=target;
		this.method=options.method;

		if(typeof options.vars=='string') {
			this.vars=options.vars;
		}
		else {
			this.vars='';
		}

		if(typeof options.standard_action=='boolean' && !options.standard_action) {
			//should standard_ajax_back be called? replaces the divs and exec html code if the XML is a valid IREDS-xml
			this.callback=null
		}
		else {
			this.callback=this.standard_ajax_back
		}
		if(typeof options.onStart == 'function') {
			this.onStart=options.onStart;
		}
		else {
			this.onStart=function() {};
		}
		
		if(typeof options.onError == 'function') {
			this.onError=options.onError;
		}
		else {
			this.onError=function() {};
		}
		if(typeof options.onSuccess == 'function' || this.callback) {
			this.onSuccess=function (ajax_request) {	
				if(this.callback) this.callback(ajax_request);
				if(typeof options.onSuccess == 'function') options.onSuccess(ajax_request);
			}.bind(this);
		}
		else {
			this.onSuccess=function() {};
		}		
	},
	call: function() { 
		$A(document.getElementsByTagName("meta")).each(function (i) { m = i.content.match(/.*charset=(.*)/); if(m) this.encoding = m[1].toUpperCase()}.bind(this))
		
		if(this.method.toUpperCase()=='POST') { //POST request
			ajax_options={
				method: 'POST', 
				postBody: this.vars, //use postBody instead of parameters for sending correct data to server
				onSuccess: this.onSuccess,
				onFailure: this.onError,
				onLoading: this.onStart,
				encoding: this.encoding
			};
		}
		else {
			ajax_options={
				method: this.method, 
				parameters: this.vars, 
				onSuccess: this.onSuccess,
				onFailure: this.onError,
				onLoading: this.onStart,
				encoding: this.encoding
			};
		}
		
		this.ajax_request= new Ajax.Request(
			this.target, ajax_options
		);
	},
	standard_ajax_back: function(ajax_request) {
		var html_teil = ajax_request.responseXML.getElementsByTagName('html');
		var js_teil = ajax_request.responseXML.getElementsByTagName('js');

		for(i=0; i < html_teil.length; i++) {
			var node = html_teil.item(i); 
			var node_id = node.getAttribute('html_id'); 
			var append_node = node.getAttribute('append');

			if($(node_id)) {	
				if(append_node==1) {
					$(node_id).innerHTML += node.firstChild.nodeValue
				}
				else {
					$(node_id).innerHTML = node.firstChild.nodeValue
				}
			}
		}

		for(i=0; i < js_teil.length; i++) {
			var node = js_teil.item(i); 
			var node_id = node.getAttribute('call'); 							
			jscode = node.firstChild.nodeValue.replace(/\n/g,';'); 
			jscode = jscode.replace(/\r/,';'); 
			jscode = jscode.replace(/"/g,'\\"'); 
			eval(node_id+" = new Function('"+jscode+"');"); 
			eval(node_id+"()");
		}
	}
}
