// JavaScript Document
function alerta() {
	if (!confirm('Esta operação é irreversível! Deseja continuar ?')) {
		return false; 
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_findObj(n, d) { //v4.0
var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_dmenu(objname) {
var obj = MM_findObj(objname);
if(obj && obj.style) obj.style.display = (obj.style.display=="none") ? "" : "none";
}

function MM_dmenuh() {
var a=MM_dmenuh.arguments;
for (i=0; i<a.length; i++) {var obj = MM_findObj(a[i]); if(obj && obj.style) obj.style.display = "none"; }
}

function MM_dmenus() {
var a=MM_dmenus.arguments;
for (i=0; i<a.length; i++) {var obj = MM_findObj(a[i]); if(obj && obj.style) obj.style.display = ""; }
}

<!-- Original:  Xavier R. (xav@lougaou.com) -->
<!-- Modified:  Benjamin Wright, Editor -->
<!-- Web Site:  http://www.lougaou.com/ -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function clock() {
if (!document.layers && !document.all) return;
var digital = new Date();
var hours = digital.getHours();
var minutes = digital.getMinutes();
var seconds = digital.getSeconds();
var amOrPm = "AM";
// if (hours > 11) amOrPm = "PM";
// if (hours > 12) hours = hours - 12;
// if (hours == 0) hours = 12;
if (minutes <= 9) minutes = "0" + minutes;
if (seconds <= 9) seconds = "0" + seconds;
dispTime = hours + ":" + minutes + ":" + seconds + " "; //+ amOrPm;
if (document.layers) {
document.layers.pendule.document.write(dispTime);
document.layers.pendule.document.close();
}
else
if (document.all) pendule.innerHTML = dispTime;
setTimeout("clock()", 1000);
}

function clock2(data) {
if (!document.layers && !document.all) return;
var digital = new Date(data);
var hours = digital.getHours();
var minutes = digital.getMinutes();
var seconds = digital.getSeconds();
var amOrPm = "AM";
// if (hours > 11) amOrPm = "PM";
// if (hours > 12) hours = hours - 12;
// if (hours == 0) hours = 12;
if (minutes <= 9) minutes = "0" + minutes;
if (seconds <= 9) seconds = "0" + seconds;
dispTime = hours + ":" + minutes + ":" + seconds + " "; //+ amOrPm;
if (document.layers) {
document.layers.pendule.document.write(dispTime);
document.layers.pendule.document.close();
}
else
if (document.all) pendule.innerHTML = dispTime;
datax=data+1000;
setTimeout("clock2(datax)", 1000);
}

//  End -->

var w_timeout=0,
w_tip=0,
w_id=0;
var _ie=(document.all)?1:0,_nn=(document.layers)?2:0,_w3=(document.getElementById)?3:0;
function showtip(e,n) {
var x,y,ex,ey,dw,lw,z; 
w_tip=n; 
if(_nn){
z=document.layers[n];ex=e.pageX;ey=e.pageY;
if(document.width){
	dw=document.width;
}else if(window.innerWidth){
	dw=window.innerWidth;
	} else {
		dw=800;
	}
	if(z.clip.width){
		lw=z.clip.width;
	}else{
		lw=100;
	} 
	if(ex+lw>dw){
	x=ex-lw-12;
	if(x<0){
		x=0;
	}
} 
else {
	x=ex+8;
} 
y=ey+12;
z.left=parseInt(x);
z.top=parseInt(y);
if(w_timeout>0){
	w_id=setTimeout("doshowtip()",w_timeout);
} else{
	z.visibility="show";
}
}else if(_w3){
var c=0;
z=document.getElementById(n);
if(e.pageX){	
	ex=e.pageX;
	ey=e.pageY;
} else {
	ex=e.clientX;
	ey=e.clientY;
	c=1;
}
if(document.width){
	dw=document.width;
}else if(document.body.offsetWidth){
	dw=document.body.offsetWidth;
}else if(window.innerWidth){
	dw=window.innerWidth;
}else {
	dw=800;
}
if(z.style.width){
	lw=z.style.width;
} else if(document.all){
	var _w=document.all[n];
	lw=_w.offsetWidth;
}
if(!lw){
	lw=100;
}
if(ex+lw>dw){
	x=ex-lw-12;
	if(x<0){
		x=0;
	}
} else {
	x=ex+8;
}
y=ey+12;
if(c && document.body.scrollTop){
	y+=document.body.scrollTop;
}
z.style.left=parseInt(x);
z.style.top=parseInt(y); 
if(w_timeout>0){w_id=setTimeout("doshowtip()",w_timeout);} else {z.style.visibility="visible";}
}else if(_ie){z=document.all[n];ex=e.clientX;ey=e.clientY;dw=document.body.offsetWidth;
lw=z.offsetWidth; if(!lw) {lw=100;}if(ex+lw>dw){x=ex-lw-12;if(x<0){x=0;}} else {x=ex+8;}
y=ey+12; if(document.body.scrollTop){y+=document.body.scrollTop;}
z.style.pixelLeft=parseInt(x);z.style.pixelTop=parseInt(y);
if(w_timeout>0){w_id=setTimeout("doshowtip()",w_timeout);} else{z.style.visibility="visible";}}}
function doshowtip(){ w_id=0; if(w_tip){ if(_nn){document.layers[w_tip].visibility="show";}
else if(_w3){document.getElementById(w_tip).style.visibility="visible";}
else if(_ie){document.all[w_tip].style.visibility="visible";}}}
function hidetip(n){if(w_id){ clearTimeout(w_id);w_id=0;}
if(_nn){document.layers[n].visibility="hide";}
else if(_w3){document.getElementById(n).style.visibility="hidden";}
else if(_ie){document.all[n].style.visibility="hidden";
}}

_editor_url = "/js/htmlarea/";  // URL to htmlarea files
var win_ie_ver = parseFloat(navigator.appVersion.split("MSIE")[1]);
if (navigator.userAgent.indexOf('Mac')        >= 0) { win_ie_ver = 0; }
if (navigator.userAgent.indexOf('Windows CE') >= 0) { win_ie_ver = 0; }
if (navigator.userAgent.indexOf('Opera')      >= 0) { win_ie_ver = 0; }
if (win_ie_ver >= 5.5) {
  document.write('<scr' + 'ipt src="' +_editor_url+ 'editor.js"');
  document.write(' language="Javascript1.2"></scr' + 'ipt>');  
} else { document.write('<scr'+'ipt>function editor_generate() { return false; }</scr'+'ipt>'); }

/*Ajax para calendário*/
function ajaxCalendario(mes,ano){
	if (mes!='' && ano!='') {
		document.getElementById('calendarioRes').innerHTML = "&nbsp;Carregando ...";	
		var content = "";
		doPost('calendario.php?mes='+mes+'&ano='+ano, content, 'processResult');
	}
}

/*Ajax para calendário CIs*/
function ajaxCalendarioCI(mes,ano){
	if (mes!='' && ano!='') {
		document.getElementById('calendarioRes').innerHTML = "&nbsp;Carregando ...";	
		var content = "";
		doPost('calendarioCI.php?mes='+mes+'&ano='+ano, content, 'processResult');
	}
}

function processResult(result) {
	//alert(result);
	document.getElementById('calendarioRes').innerHTML = result;	
}

function verificaLogin(_form){
	_form = document.getElementById(_form);
	if (_form.usuario.value=="" || _form.senha.value=="") {
		document.getElementById('mensagem').innerHTML = "<span class='msgErr'>Erro: Usuário e Senha devem ser preenchidos.</span>";	
		return false;
	}
	else
		return true;
}

function verificaUsuario(_form){
	var mensagem = document.getElementById('mensagem');
	_form = document.getElementById(_form);
	if (_form.usuario.value=="") {
		mensagem.innerHTML = "<span class='msgErr'>Erro: Campo <i>Usuário</i> deve ser preenchido.</span>";	
		return false;
	} else {
		_form.action = "novasenha.php";
		_form.submit();
	}
}

function classeMenu(idCateg){
	var opcaoId = document.getElementById('opcao_'+idCateg);
	var categId = document.getElementById('categ_'+idCateg);
	if (opcaoId.style.display=="none")
		opcaoId.style.display="";
	else
		opcaoId.style.display="none";
	if (categId.className=="menuOff")
		categId.className="menuOn";
	else
		categId.className="menuOff";
}

function formataData(Campo, teclapres){

	if (numbersonly(Campo, teclapres)) {
		if(window.event){
		var tecla = teclapres.keyCode;
		}else  tecla = teclapres.which;
		
		var vr = new String(Campo.value);
		vr = vr.replace("/", "");
		
		tam = vr.length + 1;
		
		
		if (tecla != 9 && tecla != 8){
		  if (tam > 2 && tam < 5)
			 Campo.value = vr.substr(0,2) + '/' + vr.substr(2, tam);
		  if (tam >= 5 && tam < 7)
			 Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,tam-4);
		}
	} else {
		return false;
	}
}

// Verifica se a data apresentada é data válida
function verificaData(campo) {
	//IsDate(29, 2, 2005)
	//IsDate(29, 2, 2004)
	if (campo.value != '') {
		var data = campo.value.split("/");
		var dateTemp = new Date();
		var blnRet = false;
		var blnDay;
		var blnMonth;
		var blnYear;
		
		dateTemp.setFullYear(data[2], data[1] -1, data[0]);
	
		blnDay   = (dateTemp.getDate()     == data[0]);
		blnMonth = (dateTemp.getMonth()    == data[1] -1);
		blnYear  = (dateTemp.getFullYear() == data[2]);
	
		if (blnDay && blnMonth && blnYear) {
			blnRet = true;
		} else {
			campo.value = '';
		}
	
		return blnRet;
	} else {
		return false;
	}
}

// Verifica se a data inicial esta preenchida
function virificaDataInicial(id, dataFinal) {
	dataInicial = window.document.getElementById(id);
	if (dataInicial.value == '') {
		alert('Data inicial deve ser preenchida primeiro.');
		dataFinal.value = '';
		dataInicial.focus();
	}
}

function numbersonly(myfield, e, dec) {
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
		(key==9) || (key==13) || (key==27) )
	   return true;
	
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;
	
	// decimal point jump
	else if (dec && (keychar == ".")) {
	   myfield.form.elements[dec].focus();
	   return false;
	} else
	   return false;
}

function seleciona_itens(comboorigem,combodestino){
	comboorigem = document.getElementById(comboorigem);
	combodestino = document.getElementById(combodestino);
	if (retorna_num_selecionados(comboorigem)>0){
	   for(i=comboorigem.length-1;i>=0;i--){
			if (comboorigem[i].selected && comboorigem[i].value!=""){
				combodestino.length = combodestino.length + 1;
				combodestino[combodestino.length-1].value = comboorigem[i].value;
				combodestino[combodestino.length-1].text = comboorigem[i].text;
				comboorigem.options[i]=null;
				sortSelect(combodestino);
			}
		}
	}
}

function selecionaDestino (combodestino){
	combodestino = document.getElementById(combodestino);
	for(i=0;i<combodestino.length;i++){
		combodestino.options[i].selected=true;
	}
	return true;
}

function sortSelect(obj){
    var o = new Array();
    for (var i=0; i<obj.options.length; i++){
        o[o.length] = new Option(obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected);
    }
    o = o.sort(
        function(a,b){ 
            if ((a.text+"") < (b.text+"")) { return -1; }
            if ((a.text+"") > (b.text+"")) { return 1; }
            return 0;
        } 
    );

    for (var i=0; i<o.length; i++){
        obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
    }
}


function limpaCombo(comboorigem,combodestino){
	comboorigem = document.getElementById(comboorigem);
	combodestino = document.getElementById(combodestino);
	for(i=combodestino.length-1;i<0;i--){
	    for(l=comboorigem.length-1;l>=0;l--){
			if (combodestino[i].value = comboorigem[l].value){
				comboorigem.options[l]=null;
				break;
			}
		}
   	}
}

function retorna_num_selecionados(obj){
	var cont = 0;
	for(i=0;i<obj.length;i++){
			if (obj[i].selected){
				cont++;
			}
	}
	return cont;
}

function ocultaMostraDescricao(divId, botaoMaisId, botaoMenosId) {
	divDesc = window.document.getElementById(divId);
	botaoMais = window.document.getElementById(botaoMaisId);
	botaoMenos = window.document.getElementById(botaoMenosId);
	if (divDesc.style.display == 'none') {
		divDesc.style.display = '';
		botaoMais.style.display = 'none';
		botaoMenos.style.display = '';
	} else {
		divDesc.style.display = 'none';
		botaoMais.style.display = '';
		botaoMenos.style.display = 'none';
	}
}
