
//inicialização da página
window.onload=function(){
	
	var url = document.location+"";
	if (url.indexOf("?licitacoes")!=-1){
		showLicitacoes(1);
	} else {
		removeNotaLicitacao();
	}
}

//Ancora para exibir licitação em separado
function ancora(cod){
	if (document.getElementById(cod)){
		var tb = document.getElementById(cod);
		var width = tb.offsetWidth;
		var height = tb.offsetHeight;
		var top = findPos(tb)[1];
		var left = findPos(tb)[0];
		var div = document.createElement('div');
		div.className='moldura';
		div.id='moldura';
		div.style.visibility = "hidden";
		
		if (navigator.appName.indexOf('Microsoft')!=-1){
			div.style.top = (top-20)+"px";
			div.style.left = (left-20)+"px";
			div.style.width = (width+40)+"px";
			div.style.height = (height+40)+"px";
		} else {
			div.style.top = (top-20)+"px";
			div.style.left = (left-20)+"px";
			div.style.width = (width+20)+"px";
			div.style.height = (height)+"px";
		}
		document.body.appendChild(div);
		window.scroll(0,top-140);
		myImage = new fx.Opacity('moldura',{duration:600});
		myImage.hide();
		setTimeout('myImage.custom(0,1)',200);
		setTimeout('myImage.custom(1,0)',3800);
	}
}


//Pbjeto HTTPRequest
function criaObjetoXml(){
	var req;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		req = new XMLHttpRequest();
	}
	return req; 
}
var request = criaObjetoXml(); 

//posição de elementos
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function allImages(element_id){
	div = document.getElementById(element_id);
	imgs = div.getElementsByTagName('img');
	for (i=0; i<imgs.length; i++){
		//alert(imgs[i].style.display);
		//if(imgs[i].style.display==''){
			imgs[i].focus();
		//}
	}
}


function showLicitacoes(tipo){
	
	var funcao = function(str){
		document.getElementById('conteudo').innerHTML=str;
		
		//nota de esclarecimento
		if (!document.getElementById('nota')){
			request.open("GET","requests/licitacao_nota.htm" ,false);
			request.send(null);
			resultset=request.responseText;
			document.getElementById('menu').innerHTML+=resultset;
		}
		
		var url = document.location+"";
		if (url.indexOf("&codlic_efetiva=")!=-1){
			var pos = url.indexOf("&codlic_efetiva=");
			ancora(url.substring(pos+16));
		}
		
	}
	var url_ = "requests/consulta_licitacoes.php?tipo="+tipo;
	var ajax = new Request();
	ajax.doGet(url_, funcao, 'text');

}

//mostra/oculta a lista de documentos para download
var a=0;
function showManuais(){
	var img = document.getElementById('manuais').getElementsByTagName('img')[0];
	var man = document.getElementById('manuais_download');
	
	if(a%2==0){
		img.src='images/licitacao_manuais_down.gif';
		img.focus();//ie bug: não mostra a imagem de pronto.
		man.style.display='block';
		a++;
		img.parentNode.blur();
	
	}else{
		img.src='images/licitacao_manuais.gif';
		img.focus();
		man.style.display='none';
		a++;
		img.parentNode.blur();
	}
}

/*DOWNLOAD DE EDITAIS
Exibe o ofrmulário de download
argumento 'cod': código da licitação a ser 
passada para a função de download*/
function showForm(a, cod, url){
	
	if(!document.getElementById('screen')){
		var conteudo = document.getElementById('conteudo');
		var div1 = document.createElement('div');
		div1.id='screen';
		div1.style.height=(conteudo.offsetHeight+250)+"px";
		document.body.appendChild(div1);
	}
	
	if(!document.getElementById('form_container')){
		var div2 = document.createElement('div');
		div2.id='form_container';
		div2.style.top=(findPos(a)[1]-180)+"px";
		document.body.appendChild(div2);
	}
	
	request.open("GET","requests/licitacao_form_download.php" ,false);
	request.send(null);
	resultset=request.responseText;
	document.getElementById('form_container').innerHTML=resultset;
	
	/*adiciona a ação 'onclick' no sumbit por aqui, 
	para passar o código e a url do documento*/
	var bt = document.getElementById('submit_form');
	bt.onclick=function(){
		donwloadDoc(cod, url);
	}
	
	//conveniência
	div2.getElementsByTagName('input')[0].focus();
	
}

//Remove o ofrmulário de download
function removeForm(){
	if(document.getElementById('form_container')){
		var form = document.getElementById('form_container');
		form.parentNode.removeChild(form);
	}
	
	if(document.getElementById('screen')){
		var div = document.getElementById('screen');
		div.parentNode.removeChild(div);
	}
}

/*DOWNLOAD DO DOCUMENTO
Argumentos: cod=código da licitação; doc=url do documento*/
function donwloadDoc(cod, doc){
	lic = document.getElementById('form_container').getElementsByTagName('input');
	
	for (i=0;i<lic.length;i++){
		f = document.forms[0];
		if (lic[i].value==''){
			alert('O campo \"'+lic[i].name+'\" deve ser preenchido');
			lic[i].focus();
			return;
		}
	}
	
	//for dos campos
	for (j=0;j<lic.length;j++){
		if (j==0){
			insert = "cod="+cod+"&"+f.elements[j].name+"="+f.elements[j].value;
		} else {
			if (f.elements[j].type!='radio'){
				insert += "&"+f.elements[j].name+"="+f.elements[j].value;
			} else {
				if(f.elements[j].checked){
					insert += "&"+f.elements[j].name+"="+f.elements[j].value;
				}
			}
		}
	}
	
	request.open("POST","requests/download_edital.php",false);
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.send(insert);
	resultset=request.responseText;

	if (resultset==''){
		alert('Dados enviados. A seguir será feito o   \ndownload do arquivo.');
		window.location.href = "."+doc;
		removeForm();
	} else {
		alert('Número de CNPJ ou CPF inválido. \nRetire os pontos e traços e tente novamente');
	}
}

//relatório de gestão
function removeNotaLicitacao(){

	//retira a nota sobre licitações
	if (document.getElementById('nota')){
		var nota=document.getElementById('nota');
		nota.parentNode.removeChild(nota);
	}
}