//Trobler class
function Trobbler(div, active, inactive) {
	var me = this;
	
	me.activate = function() {
		var aux = document.getElementById(div);
		aux.setAttribute("class", active);
		aux.setAttribute("className", active); //IE sucks
	}
	
	me.close = function() {
		var aux = document.getElementById(div);
		aux.setAttribute("class", inactive);
		aux.setAttribute("className", inactive); //IE sucks
	}
	
}

// Read the answer of an URL and write the contents into a 'div'

function minimax(url, div) {	
	var me = this;

	var semaphore = false;
	var haveSemaphore = false;
	
	var trobbler=false;
	var haveTrobbler=false;
	
	me.xhr = false;
	
	if (window.XMLHttpRequest) { // Si es Mozilla, Safari etc
		me.xhr = new XMLHttpRequest()
	} else { 
		if (window.ActiveXObject) { // pero si es IE
			try {
				me.xhr = new ActiveXObject('Microsoft.XMLHTTP')
			} catch (e) { // en caso que sea una versión antigua
				try {
					me.xhr = new ActiveXObject('Msxml2.XMLHTTP')
				} catch (e){}
			}
		} else {
			return false
		}
	}
	
	me.setDiv = function(div_x) {
		div=div_x;
	}
	
	me.setSemaphore = function(sname) {
		semaphore=document.getElementById(sname);
		haveSemaphore = true;
		if(semaphore.value=='') {
			semaphore.value='1';
		}
	}
		
	me.setTrobbler = function(tdiv,on,off) {
		trobbler=new Trobbler(tdiv,on,off);
		haveTrobbler = true;
	}
	
	me.closeTrobbler = function() {
		if(haveTrobbler) trobbler.close();
	}
	
	me.activateTrobbler = function() {
	  if(haveTrobbler) trobbler.activate();
	}
	
	me.xhr.onreadystatechange = function() {
		if (me.xhr.readyState == 4 && me.xhr.status==200 ) {
// 		var doc=me.xhr.responseXML;
// 		var body = doc.getElementsByTagName('html')[0];
// 		alert(body.childNodes[3].textContent);
// 		document.getElementById(div).innerHTML=body.childNodes[3].textContent;
			var text=me.xhr.responseText;
			if(haveSemaphore) semaphore.value='1';
			if(haveTrobbler) trobbler.close();
			if(div) document.getElementById(div).innerHTML=text;
		}
	}
	
	me.get = function () {
		if(haveTrobbler) trobbler.activate();
		//If the semaphore is in 0, request another get in 1 second,
		//for we are doing something and the result from the other
		//process would be older than ours
		if(!haveSemaphore || semaphore.value=='1') {
			/* Esto es insultante, pero Explorer solo funciona en POST */
			if(haveSemaphore) semaphore.value='0';
			me.xhr.open('POST', url, true);
			//me.xhr.overrideMimeType('text/xml');
			me.xhr.send(null);
		} else { 
			setTimeout(function (){ me.get(); }, 300);
		}
	}
	
	me.post = function (post_x) {
		if(haveTrobbler) trobbler.activate();		
		//If the semaphore is in 0, request another post in 1 second,
		//for we are sending something and the result from the other
		//process would be older than ours
		post_x=post_x.replace(/\&amp;/g, '&');
		if(!haveSemaphore || semaphore.value=='1') {
			if(haveSemaphore) semaphore.value='0';
			me.xhr.open('POST', url);
			//me.xhr.overrideMimeType('text/xml');
			me.xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			me.xhr.send(post_x);
		} else { 
				setTimeout(function (){ me.post(post_x); }, 300);
		}
	}
	
	//return me.xhr;
	
}