//based on prototype's ajax class
//to be used with prototype.lite, moofx.mad4milk.net.

ajax = Class.create();
ajax.prototype = {
	initialize: function(url, options){
		this.transport = this.getTransport();
		this.postBody = options.postBody || '';
		this.method = options.method || 'post';
		this.onComplete = options.onComplete || null;
		this.update = $(options.update) || null;
		this.request(url);
	},

	request: function(url){
		this.transport.open(this.method, url, true);
		this.transport.onreadystatechange = this.onStateChange.bind(this);
		if (this.method == 'post') {
			this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
		}
		this.transport.send(this.postBody);
	},

	onStateChange: function(){
		if (this.transport.readyState == 4 && this.transport.status == 200) {
			if (this.onComplete) 
				setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
			if (this.update)
				//setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.bind(this), 10);
				if(mi_accion == 'showPaginadoFotos' || mi_accion == 'showPaginadoVideos'){
					//recargo la publicidad
					var xiframes = document.getElementById('ban_sup'); 
					var xiframes1 = document.getElementById('ban_inf');
					
					if(xiframes != null ){
						if(typeof xiframes != 'undefined'){
							xiframes.src=xiframes.src;		
						}
					}
					
					if(xiframes1 != null ){
						if(typeof xiframes1 != 'undefined'){
							xiframes1.src=xiframes1.src;		
						}
					}					
				}
				var extra_cert = ''; 
				if(mi_accion == 'showPaginadoFotos' || mi_accion == 'showPaginadoVideos'){
					var extra_cert = '<div style="display:none"><img src=http://hits.e.cl/cert/hit.dll?sitio_id=18873&path='+cert_p +' width="1" height="1" alt="Certifica.com" /></div>';					
				}else{
					var extra_cert = ''; 
				}
				setTimeout(function(){
					if(this.transport.responseText.substr(0,4)=='[js]'){
						var fin_msj = this.transport.responseText.indexOf('[/js]');
						eval(this.transport.responseText.substr(4,fin_msj-4));
						if(this.transport.responseText.length != eval(this.transport.responseText.indexOf('[/js]')+5)){
							this.update.innerHTML = this.transport.responseText.substr(fin_msj+5);	
						}
					}else{				
						this.update.innerHTML = this.transport.responseText+extra_cert;
					}				
				}.bind(this), 10);
			this.transport.onreadystatechange = function(){};
		}
	},

	getTransport: function() {
		if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false;
	}
};
