// ----------------------------------
// cross-browser functions

var IE_all_cache = new Object();
function IE_getElementById(id) {
  if (IE_all_cache[id] == null) {
    IE_all_cache[id] = document.all[id];
  }
  return IE_all_cache[id];
}

if (document.all) {
  if (!document.getElementById) {
    document.getElementById = IE_getElementById;
  }
}

//-----------------------------------
// Browser detection
var agt = navigator.userAgent.toLowerCase();
var is_opera = (agt.indexOf("opera") != -1);
var is_ie = (agt.indexOf("msie") != -1) && document.all && !is_opera;
var is_ie5 = (agt.indexOf("msie 5") != -1) && document.all;
var is_ie7 = (agt.indexOf("msie 7") != -1) && document.all;
var is_firefox = (agt.indexOf("firefox") != -1);
var barraAberta = false;
var timer2 = null;


var parent_site = parent.document.getElementById('slide');
if ( parent_site ) {
	parent_site.style.display = 'none';
}

function valida() {
	var gravar = document.getElementById("btn_entrar");  gravar.disabled = true;
	var l1 = document.getElementById("usuario");
	var l2 = document.getElementById("senha");
	var msg = document.getElementById("mensagem");

	if ( msg ) {
		msg.innerHTML = "";
	}
     
    if ( l1.value.length == 0 ) {
		//alert("Informe seu login / matrícula !!");
		msg.innerHTML = "Informe seu login / matrícula !!";
		gravar.disabled = false;
		l1.focus();
		return false;
	}
     
    if ( l2.value.length == 0 ) {
		//alert("Informe sua senha ! solicite-a na secretaria !!");
		msg.innerHTML = "Informe sua senha ! solicite-a na secretaria !!";
		gravar.disabled = false;
		l2.focus();
		return false;
	}

	///gravar.disabled = false;

	return true;
}

function CreateXmlHttpReq(handler) {
	var xmlhttp;
	try{
	    xmlhttp = new XMLHttpRequest();
	}catch(ee){
	    try{
	        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	    }catch(e){
	        try{
	            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	        }catch(E){
	            xmlhttp = false;
	        }
	    }
	}
	return xmlhttp;
}


function atu_perc(){

    var xmlhttp = CreateXmlHttpReq();
    var d = new Date();		var t = d.getTime();
    xmlhttp.open("POST", "login_status.php?d="+t, true);
    xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState==4){
		
			var texto = xmlhttp.responseText;
			//texto = texto.replace(/\+/g," ");
			//texto = unescape(texto);
			var pos = texto.indexOf("#");
			
			if ( pos > 0 ) {
				var percentual = texto.substring(0,pos);
				var restante = 100 - percentual;
				//if ( percentual >= 100 ) {	
					var data_pub = texto.substring(pos+1,texto.length+pos+1);
				    var div_pub = document.getElementById('div_pub');
				    div_pub.innerHTML = data_pub;
				//}

			    //var tit_pag = document.getElementById('perc_titulo');
			    var tit_bar = document.getElementById('barra_titulo');
			    var bar_perc = document.getElementById('barra_perc');
			    var bar_rest = document.getElementById('barra_restante');

				tit_bar.innerHTML = percentual + '%';
				//document.title = percentual + '% Enviados: ';
				var pos = percentual.indexOf(",");
				if ( pos > 0 ) {
					percentual = percentual.substring(0,pos);
				}
				bar_rest.style.width = restante + "%";
				bar_perc.style.width = percentual + "%";
				//if ( percentual >= 100 ) {
				    testa_status(false);
					//clearInterval(timer2);
					//inicia_timer(60);
				//}
			}
		}
    }
    xmlhttp.send(null);
}

timer2 = setInterval('inicia_timer()', 1200);


function inicia_timer() {
	clearInterval(timer2);
	timer2 = null;
	//
	testa_status(true);
	//
	return true;
}

function testa_status(inicio) {
	var bar_perc = document.getElementById('barra_titulo'); // barra_perc
	//var perc = bar_perc.width;
	var perc = bar_perc.innerHTML;
	perc = perc.replace("%","");
	if ( perc == "") { perc = "0"; }
	if ( parseInt(perc) < 100 ) {
		mostra_barra(true, inicio);
		return true;
	} else {
		mostra_barra(false, inicio);
		return false;
	}
}

function mostra_barra(lmostra, inicio) {
	if ( lmostra != barraAberta || inicio ) {
		if ( timer2 != null ) {
			clearInterval(timer2);
		}
		var div_progresso = document.getElementById('div_progresso');
		var div_status = document.getElementById('status_atu');
		if ( lmostra ) {
			div_progresso.style.display = "";
			div_status.style.display = "none";
			timer2 = setInterval('atu_perc()', 20*1000);
		} else {
			div_progresso.style.display = "none";
			div_status.style.display = "";
			timer2 = setInterval('atu_perc()', 40*1000);
			// Limpar Msg de Status se não for Inicio
			if ( !inicio ) {
				var msg = document.getElementById("mensagem");
				if ( msg ) { msg.innerHTML = ""; }
			}
		}
		barraAberta = lmostra;
	}
	return true;	
}
