//Trobler class
//The constructor requires the div with the trobler, the css classes used
//by div when active and when inactive.  
function Trobbler(div, active, inactive) {
	var me = this; //Just to use me instead of this
	
	//Function to activate the trobler
	me.activate = function() {
		var aux = document.getElementById(div);
		aux.setAttribute("class", active);
		aux.setAttribute("className", active); //IE sucks
	}
	
	//Function to inactivate the trobler
	me.close = function() {
		var aux = document.getElementById(div);
		aux.setAttribute("class", inactive);
		aux.setAttribute("className", inactive); //IE sucks
	}
	
}

//Minimax Class
//Read the answer of an URL and write the contents into a 'div'
function minimax(url, div) {	
	var me = this; //Just to use me instead of this if a recursived function is used

	//The semaphore to wait if other ajax call from the same instance have been called
	var semaphore = false;
	var haveSemaphore = false;
	
	var trobbler=false;
	var haveTrobbler=false;
	
	var errorTimeCount = 0;
	
	me.xhr = false;
	
	if (window.XMLHttpRequest) { // It it's Mozilla, Safari etc
		me.xhr = new XMLHttpRequest()
	} else { 
		if (window.ActiveXObject) { // if it's IE
			try {
				me.xhr = new ActiveXObject('Microsoft.XMLHTTP')
			} catch (e) { // if it's and old version
				try {
					me.xhr = new ActiveXObject('Msxml2.XMLHTTP')
				} catch (e){}
			}
		} else {
			return false
		}
	}
	
	//Functopn to set another div
	me.setDiv = function(div_x) {
		div=div_x;
	}
	
	//Function to declare the semaphore
	me.setSemaphore = function(sname) {
		semaphore=document.getElementById(sname);
		haveSemaphore = true;
		if(semaphore.value=='') {
			semaphore.value='1';
		}
	}
	
	//Function to declare the trobler
	me.setTrobbler = function(tdiv,on,off) {
		trobbler=new Trobbler(tdiv,on,off);
		haveTrobbler = true;
	}
	
	//Function to inactivate the trobbler
	me.closeTrobbler = function() {
		if(haveTrobbler) trobbler.close();
	}
	
	//Function to activate the trobbler
	me.activateTrobbler = function() {
		if(haveTrobbler) trobbler.activate();
	}
	
	//Event handler when get the answer
	me.xhr.onreadystatechange = function() {
		if (me.xhr.readyState == 4 && me.xhr.status==200 ) {
			var text=me.xhr.responseText;
			if(haveSemaphore) semaphore.value='1';
			if(haveTrobbler) trobbler.close();
			if(div) document.getElementById(div).innerHTML=text;
			errorTimeCount = 0;
		}
	}
	
	var errorTime = function() {
		answer = false;
		if(errorTimeCount > 5000 / 300) {
			alert('Ooops! Something is going wrong.\nRecovering.');
			answer = true;
		}
		return answer;
	}
	
	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(errorTime() || !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.send(null);
		} else { 
			errorTimeCount ++;
			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
		if(errorTime() ||  !haveSemaphore || semaphore.value=='1') {
			post_x=post_x.replace(/\&amp;/g, '&');
			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 { 
				errorTimeCount ++;
				setTimeout(function (){ me.post(post_x); }, 300);
		}
	}
	
}
