//DEFINICOES
var LINKMOD = "http://www.decisaorh.com.br/cadastro.php?form=new";
var config = new Array();
config['imagens'] = '';
config['loadingDiv'] = "loadingDiv"; //o id da div loadingDiv

//array que determina qual distancia do topo da pagina serao posicionadas as divs
var divPositions = new Array(0, 350, 350, 850, 1100, 1100, 1500); 

//frame é o frame acessado
//se for uma string vazia, ignora
function setValue(id, value, frame) {
	
	//se frame for diferente de string significa que estamos trabalhando num frame, senao
	//estamos no topo da window
	if (typeof(frame)!=undefined&&frame!="") {b=1;a = frame;}
	else {b=0;a = window;}
	return a.document.getElementById(id).value = value;
}

//frame é o frame acessado
//se for uma string vazia, ignora
function setInnerHTML(id, value, frame) {
	
	//se frame for diferente de string significa que estamos trabalhando num frame, senao
	//estamos no topo da window
	if (typeof(frame)!=undefined&&frame!="") {b=1;a = frame;}
	else {b=0;a = window;}
//alert("b: "+b);
//alert("frame: "+frame);
//alert("document.forms.length: "+document.forms.length);
//alert("window.frames.length: "+window.frames.length);
//alert("a.frames.length: "+a.frames.length);
	return a.document.getElementById(id).innerHTML = value;
}

function ajaxRequest(){
	var activexmodes=['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'] //activeX versions to check for in IE
	if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
		for (var i=0; i<activexmodes.length; i++){
			try{ return new ActiveXObject(activexmodes[i]); }
			catch(e){}
		}
	} else if (window.XMLHttpRequest) // if Mozilla, Safari etc
		return new XMLHttpRequest();
	else return false
}

function XML() {
	this.teste = 5;
	this.ajaxRequest = new ajaxRequest();
	this.lastSent = null; //ultimo xml enviado (Date)
	this.currentSent = null; //xml atual (Date)

	//funciona: Firefox, Chrome
	//nao funciona: IE
	try { this.ajaxRequest.overrideMimeType('text/xml'); } catch (e) {}
}

XML.prototype.abort = function () {
	this.ajaxRequest.abort();
}

XML.prototype.getTeste = function() {
   return this.teste;
}

//event e: é o evento ocorrido
//string frame: é uma string que indica o frame acessado, por exemplo: window.frames[0] é o primeiro frame
//se for uma string vazia, define que os objetos estao no topo (window)
//table: tabela onde sera procurado no banco de dados
//divId: div onde serao colocados os resultados
//divContent: div que aparece na frente de tudo, ao clicar em um resultado esta devera ser escondida
//elementId: o id do input onde sera colocado o resultado quando clicado no link
//obs: o valor do resultado sera colocado no span de id = 'span_'+elementId
XML.prototype.showHint = function (str, table, divId, divContent, elementId, frame, e) {

	var window_event = window.event; //assume que a window do evento é a window (para o IE)
	
	//se frame for uma string e de tamanho > 0 significa que estamos trabalhando num frame, senao
	//estamos no topo da window
	try {
		b = (frame!=""&&frame.length>0) ? 1 : 0;
		window_event = window.top.event; //se estamos num frame, assessamos a window top
	} catch (e) {
		b = 0;
	}
	a = b ? frame : "window"; //a é uma string
	
	//window.event	-->IE
	//e				-->Firefox, Chrome
	evento = typeof e == "undefined" ? window_event : e;
	var tecla = evento.keyCode;
	
	//           <-          espaco          a              z                0              9                0              9
	if (!(tecla==8 || tecla==32 || (tecla >= 65 && tecla <= 90) || (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105))) {
		//esconde a imagem carregando
		document.getElementById(divId).innerHTML = '';
		return;
	}
	
	html = '';
	if (str.length==0) {
		document.getElementById(divId).innerHTML = html;
		return;
	}
	
	var url= LINKMOD+'&in=xml&table='+table+'&SearchTbAux='+str;

	//apenas para firefox
	var readyState = this.ajaxRequest.readyState;
	//  UNINITIALIZED || COMPLETED
	if (readyState!=0 && readyState!=4) {
		this.ajaxRequest.abort();
//		alert("abortou");
	}
	
	this.ajaxRequest.open("GET",url,false);
	this.ajaxRequest.send(null);
	
	var xmldata=this.ajaxRequest.responseXML; //retrieve result as an XML object
	var x=xmldata.getElementsByTagName(table);
//alert(x.length);
	check = 0;
	html = '<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#EBEBEB" align="center"><tr><td><table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#EFEFEF"><tr height="25" class="arial_1133"><td align="center" background="'+config['imagens']+'iface_content_fd_top.jpg"><strong>campo</strong></a></td></tr>';
	for (i=0;i<x.length;i++) {
		title = x[i].getElementsByTagName('nome')[0].childNodes[0].nodeValue;
		id = x[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;
		if(check == 1) { bg = '#F6F6F6'; check--; }
		else { bg = '#FFFFFF'; check++; }
		bg = ' bgcolor='+bg+' id="aL'+i+'" name="aL'+i+'" onmouseover="javascript:changeLine(\'aL'+i+'\',\'#FFF2CC\')" onmouseout="javascript:changeLine(\'aL'+i+'\',\''+bg+'\')"';

		html += '<tr><td height="22" '+bg+' class="arial_1133" style="cursor: pointer;" onclick="setInnerHTML(\'span_'+elementId+'\', \''+title+'\','+a+'); setValue(\''+elementId+'TDesc\', \''+title+'\','+a+'); setValue(\''+elementId+'\', \''+id+'\','+a+'); hideDiv(\''+divContent+'\','+a+');">'+title+'</td></tr>';
	}
	html += '</table></td></tr></table>';
//alert(html);
	if (x.length==0) html = '<center><br>sem resultados</center>';
	window.top.document.getElementById(divId).innerHTML = html;
}

//mostra a div e posiciona no lugar correto da pagina
//se divNumber for uma string vazia ele é ignorado
function showDiv(divId, divNumber) {
	if (theDiv = window.top.document.getElementById(divId)) {
		if (divNumber!='') theDiv.style.top = divPositions[divNumber];
		theDiv.style.display = 'block';
	}
}

//hides the div
function hideDiv(divId) {
	if (theDiv = window.top.document.getElementById(divId)) {
		theDiv.style.display = 'none';
	}
}
