// PrestoConn.js

var maxcons=30;
var httpcons = new Array();

function conn(nome, div, url){
	this.conn = false;
	this.nome = nome;
	this.div = div;
	this.url = url;
	this.onload = undefined;
	
	this.clean = function() {
		this.conn = false;
		this.nome = '';
		this.div = '';
		this.url = '';
		this.onload = undefined;
		}
}

for (i=0;i<maxcons;i++)
{
	httpcons[i]=new conn('','','');
}

function getPrestoHttp() {
	var xmlHttp=false;
	/* v v v RM (7/Jul/2010) v v v */
	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest()
		if(xmlHttp.overrideMimeType) {
			xmlHttp.overrideMimeType('text/html; charset=iso-8859-1');
		}
	} else if (window.ActiveXObject) { 
		try {
			xmlHttp = new ActiveXObject("Msxml2.xmlHttp");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.xmlHttp");
			} catch (E) {
				xmlHttp = false;
			}
		}
	}
	/* ^ ^ ^ RM ^ ^ ^ */
	return xmlHttp;
}



function createPresto(nome, url, div){
	res = false;
 	url = limpaAmp(url);
 	/* v v v RM (7/Jul/2010) v v v */
	var indice = findPresto( '' );
	
	if(indice>=0 && indice < maxcons ){
		httpcons[indice].nome = nome;
		httpcons[indice].div = div;
		httpcons[indice].url = url;
		if( !httpcons[indice].conn ) {
			httpcons[indice].conn = getPrestoHttp();
		}
		res=true;
	}else{
		res=false;
	}
	/* ^ ^ ^ RM ^ ^ ^ */
	return res;
}

function findPresto( nome ) {
	var res = -1;
	for (i=0;i<maxcons&&(res==-1);i++){
		/* v v v RM (7/Jul/2010) v v v */
		if( nome == '' && httpcons[i].nome == '' && httpcons[i].conn == false ){
			res = i;
		} else if( httpcons[i].nome == nome && (httpcons[i].conn && httpcons[i].conn.readyState == 0) ){
		/* ^ ^ ^ RM ^ ^ ^ */
			res = i;
		}
	}
	return res;
}

/* v v v RM (7/Jul/2010) v v v */
processUpdateResponse = function( myPresto, i ){
	if(myPresto.conn.readyState == 4) {
		var content = document.getElementById(myPresto.div);
		if( content != undefined ) {
			var response;
			var shortName = getShortName( myPresto.div );
			if( myPresto.conn.status == 200 ) {
				response = myPresto.conn.responseText;
			
				/* Adiciona o conteúdo HTML recebido */
				content.innerHTML = response;
				/*
				debugPresto = document.getElementById(shortName+'_debugPresto');
				if( debugPresto != undefined ) {
					debugPresto.innerHTML = debugPresto.innerHTML +" <br/><span>"+ i +", " + myPresto.div +", "+ myPresto.onload +"<span>";
				} else {
					content.innerHTML ="<a href=\"#\" id='"+ shortName +"_debugPresto' class='debugPresto'><span>"
								+ i +", " + myPresto.div +"</span></a>" + content.innerHTML;
				}
				*/
				
			
				if( window.parent.resizeCaller ){
					window.parent.resizeCaller();
				}
				
				/* Adiciona o conteúdo JS recebido ao cabeçalho da página, executando-o */
				var head = document.getElementsByTagName('head').item(0);
				
				var scripts = content.getElementsByTagName('script');
				if( scripts != undefined ) {
					for( j = 0 ; j < scripts.length ; j++ ){
						s = scripts[j].innerHTML;
						if( s.length >0 ){
							script = document.createElement('script');
							script.text = s;  
							script.type = 'text/javascript';
							script.defer = true;
							void(head.appendChild(script));
						}
					}
				}
				
				if(myPresto.onload != undefined) {
					try {
						eval(myPresto.onload)
					} catch(e){}
				}
			} else {
				
				response = "<div id=\""+ shortName +"_infoPresto\" class=\"infoPresto\" onclick=\"javascript: this.parentNode.removeChild(this);\">"
						+ "<span class=\"error\">Ocorreu uma falha de comunicação com o servidor (" + myPresto.conn.status + ": " + myPresto.conn.statusText + ", " + myPresto.nome + ").</span>"
						+ "</div>";
						
				var infoPresto = document.getElementById( shortName+'_infoPresto');
				if( infoPresto != undefined ) {
					infoPresto.parentNode.removeChild(infoPresto);
				}
				content.innerHTML = response + content.innerHTML;
			}
		}
		
		myPresto.clean();
	}
}
/* ^ ^ ^ RM ^ ^ ^ */

function setPrestoOnload(nome, onload) {
	var i = findPresto( nome );
	//alert(i+"-"+onload+" - "+ onload.substr(0,4));
	if( i >= 0 && onload.substr(0,4) != 'aaa(' ){
		httpcons[i].onload = onload;
	}
}

function callServer( nome, id ) {
	var i = findPresto( nome );
	if( i >= 0 ){
		try{
			var shortName = getShortName( httpcons[i].div );
			document.getElementById( httpcons[i].div ).innerHTML = "<div id=\""+ shortName +"_infoPresto\" class=\"infoPresto\" onclick=\"javascript: this.parentNode.removeChild(this);\">"
						+ "<div class=\"container mynetLinhaSelBold\"><span class=\"loading\">A obter dados...</span></div></div>" + document.getElementById( httpcons[i].div ).innerHTML;
						
		}catch(e){}
		
		if( typeof id != 'undefined' ){
			httpcons[i].url += id;
		}
		var url = httpcons[i].url;
		/* v v v RM (7/Jul/2010) v v v */
		url += "&token=" + (new Date().getTime());
		/* ^ ^ ^ RM ^ ^ ^ */
		httpcons[i].conn.open("GET", url, true);
		
		/* v v v RM (7/Jul/2010) v v v */
		httpcons[i].conn.onreadystatechange =  function(){ processUpdateResponse( httpcons[i], i ) };
		/* ^ ^ ^ RM ^ ^ ^ */
		httpcons[i].conn.send(null);
	}
}

getShortName = function( name ) {
	var shortName = name;
	if( name.lastIndexOf('_') > 0 ) {
		shortName = name.substr( 0, name.lastIndexOf('_') );
	}
	return shortName
}
