// ----------------------------------------------------------------------------------------------- //
function mascaraMoeda(_obj,_evnt,_func,_desc){
  if( !specialKeys(_evnt) ){
    _obj.value = formataMoeda(_obj.value,_desc);
    if(_func){ eval(_func); }
  }
}

function mascaraInt(_obj,_evnt,_func){
  if( !specialKeys(_evnt) ){
    mascaraFloat(_obj,-1,_evnt);
    if(_func){ eval(_func); }
  }
}

function mascaraFloat(_obj,_desc,_evnt,_func){
  if( !specialKeys(_evnt) ){
    _obj.value = formataFloat(_obj.value,_desc);
    if(_func){ eval(_func); }
  }
}

function mascaraData(_obj,_evnt,_func){
  if( !specialKeys(_evnt) ){
    _obj.value = formataData(_obj.value);
    if(_func){ eval(_func); }
  }
}

function mascaraHora(_obj,_evnt,_func){
  if( !specialKeys(_evnt) ){
    _obj.value = formataHora(_obj.value);
    if(_func){ eval(_func); }
  }
}

function mascaraCEP(_obj,_evnt,_func){
  if( !specialKeys(_evnt) ){
    _obj.value = formataCEP(_obj.value);
    if(_func){ eval(_func); }
  }
}

function mascaraCPF(_obj,_evnt,_func){
  if( !specialKeys(_evnt) ){
    _obj.value = formataCPF(_obj.value);
    if(_func){ eval(_func); }
  }
}

function mascaraCNPJ(_obj,_evnt,_func){
  if( !specialKeys(_evnt) ){
    _obj.value = formataCNPJ(_obj.value);
    if(_func){ eval(_func); }
  }
}

function mascaraTEL(_obj,_evnt,_func){
  if( !specialKeys(_evnt) ){
    _obj.value = formataTEL(_obj.value);
    if(_func){ eval(_func); }
  }
}

function mascaraRG(_obj,_evnt,_func){
  if( !specialKeys(_evnt) ){
    _obj.value = formataRG(_obj.value);
    if(_func){ eval(_func); }
  }
}

function mascaraMatricula(_obj,_evnt,_func){
  if( !specialKeys(_evnt) ){
    _obj.value = formataMatricula(_obj.value);
    if(_func){ eval(_func); }
  }
}
// ----------------------------------------------------------------------------------------------- //

function formataFloat(_valor,_desc){
  _valor = String(_valor);
  var i;
  var output = '0';
  if(!_desc){ _desc = 10; }
  for(i=1; i<_desc; i++){ output += '0'; }
  var tmp = _valor.replace(',','');
  output += tmp.replace(/\./g,'');
  var tam = output.length;
  var tamdes = (tam-_desc);
  var p1  = output.substr(0,tamdes);
  var p2  = output.substr(tamdes,_desc);
  p1 = p1.replace(/^0*/g,"");
  p1 = (p1=='')?'0':p1;
  if(_desc > 0){ _valor = p1+','+p2; }else{ _valor = p1; }
  return _valor;
}

function formataMoeda(_valor,_desc){
	if(!_desc){ _desc = 2; }
  _valor = formataFloat(_valor,_desc);
  var i;
  var tmp = _valor.replace(/\./g,'');
  var partes = tmp.split(',');
  var output = '';
  var tres = 0;
  for(i = partes[0].length-1; i>=0; i--){
    if(tres==3){
      output = '.'+output;
      tres = 0;
    }
    output = partes[0].substr(i,1) + output;
    tres++;
  }
  if(partes[1]){
  	output += ','+partes[1];
  }
  return output;
}

function desformataMoeda(_valor){
	return parseFloat(_valor.replace(/\./g,'').replace(/\,/g,'.')) * 100;
}

function formataData(_valor){
  var i;
  var tmp = _valor.replace(/\D/g,'');
  var output='';
  for(i = 0; i < tmp.length;i++){
    if(i==2){ output += '/'; }
    if(i==4){ output += '/'; }
    output += tmp.substr(i,1);
  }
  return output;
}

function formataHora(_valor){
  var i;
  var tmp = _valor.replace(/\D/g,'');
  var output='';
  for(i = 0; i < tmp.length;i++){
    if(i==2){ output += ':'; }
    output += tmp.substr(i,1);
  }
  return output;
}

function formataCEP(_cep){
  var i;
  var tmp = _cep.replace(/\D/g,'');
  var output='';
  for(i = 0; i < tmp.length;i++){
    if(i==5){ output += '-'; }
    output += tmp.substr(i,1);
  }
  return output;
}

function formataCPF(_cpf){
  var i;
  var tmp = _cpf.replace(/\D/g,'');
  var output='';
  for(i = 0; i < tmp.length;i++){
    if(i==3){ output += '.'; }
    if(i==6){ output += '.'; }
    if(i==9){ output += '-'; }
    output += tmp.substr(i,1);
  }
  return output;
}

// 63.028.476/0001-50
function formataCNPJ(_cnpj){
  var i;
  var tmp = _cnpj.replace(/\D/g,'');
  var output='';
  for(i = 0; i < tmp.length;i++){
    if(i==2){  output += '.'; }
    if(i==5){  output += '.'; }
    if(i==8){ output += '/'; }
    if(i==12){ output += '-'; }
    output += tmp.substr(i,1);
  }
  return output;
}

function formataTEL(_tel){
  var i;
  var tmp = _tel.replace(/\D/g,'');
  var output='';
  /*
  COM DDD ...
  if(tmp.length > 2){ output = '('; }
  for(i = 0; i < tmp.length;i++){
    if(i==2){  output += ')'; }
    if(i==6){  output += '-'; }
    output += tmp.substr(i,1);
  }
  */
  for(i = 0; i < tmp.length;i++){
    if(i==4){  output += '-'; }
    output += tmp.substr(i,1);
  }
  return output;
}

function formataRG(_rg){
	/*
  var i;
  var tmp = _rg.replace(/\W/g,'');
  var output = '';
  var tres = 0;
  for(i=(tmp.length-1); i>=0; i--){
    if(i==(tmp.length-2)){ output = '-'+output; tres=0; }
    if(tres%3==0 && tres>0){ output = '.'+output; }
    output = tmp.substr(i,1)+output;
    tres++;
  }
  return output;
  */
	return _rg;
}

function formataMatricula(_matr){
  var i;
  var tmp = _matr.replace(/\W/g,'');
  var output = '';
  var tres = 0;
  for(i=(tmp.length-1); i>=0; i--){
    if(i==(tmp.length-2)){ output = '-'+output; tres=0; }
    if(tres%3==0 && tres>0){ output = '.'+output; }
    output = tmp.substr(i,1)+output;
    tres++;
  }
  return output;
}

// ----------------------------------------------------------------------------------------------- //

function teclasInt(_obj,_evnt){
    if (document.all){ var evnt = window.event; x=evnt.keyCode; }
    else{ x=_evnt.keyCode; }

    if( (x >= 48 && x <= 57) || (x >= 96 && x <= 105) ){
      //   0 a 9
      return true;
    }else if(x == 8 || x == 9 || x == 46 || x == 13){
      //   BACKSPACE ou TAB ou DELETE ou ENTER
      return true;
    }else if(x == 35 || x == 36){
      //   HOME ou END
      return true;
    }else if(x >= 37 && x <= 40){
      //   setas
      return true;
    }
    return false;
}

function teclasFloat(_obj,_evnt){
    if (document.all){ var evnt = window.event; x=evnt.keyCode; }
    else{ x=_evnt.keyCode; }
    
    var statusMascara = 0;
    if( _obj.value.match(',') ){ statusMascara = 1; }
    if( (x >= 48 && x <= 57) || (x >= 96 && x <= 105) ){
      //   0 a 9
      return true;
    //}else if(x == 110 || x == 188 || x == 194 || x == 190){
    }else if(x == 110 || x == 188){
      //   , ou .
      if(_obj.value.length > 0){
        if(statusMascara==0){ return true; }
      }
    }else if(x == 8 || x == 9 || x == 46 || x == 13){
      //   BACKSPACE ou TAB ou DELETE ou ENTER
      return true;
    }else if(x == 35 || x == 36){
      //   HOME ou END
      return true;
    }else if(x >= 37 && x <= 40){
      //   setas
      return true;
    }
    return false;
}

function maxLength(_obj,_evnt,_lenght){
    if (document.all){ var evnt = window.event; x=evnt.keyCode; }
    else{ x=_evnt.keyCode; }
    
	  if(_obj.value.length >= _lenght ){
	    if(x >= 37 && x <= 40){
	      //   setas
	      return true;
	    }else if(x == 35 || x == 36){
	      //   HOME ou END
	      return true;
	    }else if( x == 8 || x == 46 ){
	      //   BACKSPACE ou DELETE
	      return true;
			}
			return false;
	  }
		return true;
}

function specialKeys(_evnt){
  if (document.all){ var evnt = window.event; x=evnt.keyCode; }
  else{ x=_evnt.keyCode; }

  if(x==9 || x == 35 || x == 36){
      return true;
  }else if(x >= 37 && x <= 40){
      return true;
  }
  return false;
}

function teclasRG(_obj,_evnt){
    if (document.all){ var evnt = window.event; x=evnt.keyCode; }
    else{ x=_evnt.keyCode; }

    if( (x >= 48 && x <= 57) || (x >= 96 && x <= 105) ){
      //   0 a 9
    	return true;
    }else if( x >= 65 && x <= 90 ){
    	//   A a Z
    	return true;
    }else if( x == 194 || x == 190 || x == 109 ){
    	//   . ou -
    	return true;
    }else if(x == 8 || x == 9 || x == 46 || x == 13){
      //   BACKSPACE ou TAB ou DELETE ou ENTER
      return true;
    }else if(x == 35 || x == 36){
      //   HOME ou END
      return true;
    }else if(x >= 37 && x <= 40){
      //   setas
      return true;
    }
    return false;
}

function teclasMatricula(_obj,_evnt){
    if (document.all){ var evnt = window.event; x=evnt.keyCode; }
    else{ x=_evnt.keyCode; }
		var myRE = new RegExp("x", "i");

    if( (x >= 48 && x <= 57) || (x >= 96 && x <= 105) ){
      //   0 a 9
			if( !_obj.value.match(myRE) ){
	    	return true;
			}
    //}else if( x >= 65 && x <= 90 ){ //   a-z
    }else if( x == 88 ){ //   x
			if( !_obj.value.match(myRE) ){
	    	return true;
			}
    }else if(x == 8 || x == 9 || x == 46 || x == 13){
      //   BACKSPACE ou TAB ou DELETE ou ENTER
      return true;
    }else if(x == 35 || x == 36){
      //   HOME ou END
      return true;
    }else if(x >= 37 && x <= 40){
      //   setas
      return true;
    }
    return false;
}

// ----------------------------------------------------------------------------------------------- //

function vVazio(_id){
  if(!existObj(_id)) return false;
  if(trim(document.getElementById(_id).value)==''){
  	return true;
  }
  return false;
}

function vTexto(_id,_titulo,_hidemsg,_nofocus){
  if(!existObj(_id)) return false;
  if(vVazio(_id)){
  	if(!_hidemsg){
			if(!_titulo) alert('Por favor, preencha o campo!');
			else alert('Por favor, informe '+_titulo+'!');
			if(!_nofocus) foco(_id);
  	}
    return false;
  }
  return true;
}

function vNumerico(_id,_titulo){
  if(!vTexto(_id,_titulo)) return false;
	var valor = document.getElementById(_id).value
	valor = valor.replace(/\./g,'');
  if( isNaN(valor) ){
		if(!_titulo) _titulo = 'O Campo';
    alert(_titulo+' deve ser numérico!');
    foco(_id);
    return false;
  }
  return true;
}

function vFNumerico(_id,_titulo){ // float (1.000.000,00)
  if(!vTexto(_id,_titulo)) return false;
	var valor = document.getElementById(_id).value
	valor = valor.replace(/\./g,'');
	valor = valor.replace(/\,/g,'.');
  if( isNaN(valor) ){
		if(!_titulo) _titulo = 'O Campo';
    alert(_titulo+' deve ser numérico!');
    foco(_id);
    return false;
  }
  return true;
}

function vPositivo(_id,_titulo){
  if(!vNumerico(_id,_titulo)) return false;
	var valor = document.getElementById(_id).value
	valor = valor.replace(/\./g,'');
  if( parseInt(valor) <= 0 ){
  	if(!_titulo) _titulo = 'O Campo';
  	alert(_titulo+' deve ser maior que zero!');
    foco(_id);
    return false;
  }
  return true;
}

function vFPositivo(_id,_titulo,_hidemsg){ // float (1.000.000,00)
  if(!vFNumerico(_id,_titulo)) return false;
	var valor = document.getElementById(_id).value
	valor = valor.replace(/\./g,'');
	valor = valor.replace(/\,/g,'.');
  if( parseFloat(valor) <= 0 ){
  	if(!_titulo) _titulo = 'O Campo';
  	if(!_hidemsg){
  		alert(_titulo+' deve ser maior que zero!');
    	foco(_id);
  	}
    return false;
  }
  return true;
}

function vMoeda(_id,_titulo){
	if(!vTexto(_id)) return false;
	var valor = document.getElementById(_id).value
	valor = valor.replace(/\./g,'');
	valor = valor.replace(/\,/g,'.');
  if( parseFloat(valor) <= 0 ){
  	if(!_titulo) _titulo = 'O Campo';
    alert(_titulo+' deve ser maior que zero!');
    foco(_id);
    return false;
  }
  return true;
}

function vQtde(_id,_msg){
	var qtde = parseInt(document.getElementById(_id).value);
  if( qtde <= 0 ){
    alert(_msg);
    return false;
  }
  return true;
}

function vSelect(_id,_crit,_titulo){
  if(!existObj(_id)) return false;
  var objSel = document.getElementById(_id);
  if(objSel.options[objSel.selectedIndex].value == _crit){
  	if(!_titulo) alert('Selecione uma opção!');
		else alert('Por favor, informe '+_titulo+'!');
    foco(_id);
    return false;
  }
  return true;
}

function vCheck(_name,_titulo,_altMsg){
  var i,optID;
  var checkeds = false;
  var opts = document.getElementsByName(_name);
  for(i=0; i<opts.length; i++){
    if(opts[i].checked){ checkeds = true; }
  }
  if(!checkeds){
  	if(_altMsg){
  		alert(_altMsg);
  	}else{
	  	if(!_titulo) alert('Por favor, escolha uma das opções!');
			else alert('Por favor, informe '+_titulo+'!');
  	}
  	opts[0].focus();
    return false;
  }
  return true;
}

function vData(_id,_titulo,_hidemsg){
  if(!vTexto(_id,_titulo,_hidemsg)) return false;
  if(!vDataStr(document.getElementById(_id).value)){
  	if(!_hidemsg){
	  	if(!_titulo) alert('Informe uma data válida!');
	  	else alert(_titulo+' não é uma data válida!');
	    foco(_id);
  	}
    return false;
  }
  return true;
}

function vIdade(_dtNasc,_min,_titulo,_dtCalc){
	var titulo = (_titulo)?_titulo:'';
	var dNas = (_dtNasc)?document.getElementById(_dtNasc).value:DATA_ATUAL;
	var vAtu = (_dtCalc)?document.getElementById(_dtCalc).value:DATA_ATUAL;
	if(getIdade(dNas,vAtu)<_min){
		alert(titulo+' não pode ser menor que '+_min+' anos');
		if(_dtCalc) foco(_dtCalc); return false;
		if(_dtNasc) foco(_dtNasc); return false;
	}
	return true;
}

function comparaDatasId(_d1,_d2){
	var vD1 = (_d1)?document.getElementById(_d1).value:DATA_ATUAL;
	var vD2 = (_d2)?document.getElementById(_d2).value:DATA_ATUAL;
	return comparaDatas(vD1,vD2);
}

function vCPF(_id,_titulo) {
  if(!vTexto(_id,_titulo)) return false;
  var cpfText = document.getElementById(_id).value;
  if(!Verify(cpfText, 'CPF')){
  	if(!_titulo) alert('Informe um CPF válido!');
  	else alert(_titulo+' não é válido!');
    foco(_id);
    return false;
  }
  return true;
}

function vCNPJ(_id,_titulo) {
  if(!vTexto(_id,_titulo)) return false;
  var cpfText = document.getElementById(_id).value;
  if(!Verify(cpfText, 'CNPJ')){
  	if(!_titulo) alert('Informe um CNPJ válido!');
  	else alert(_titulo+' não é válido!');
    foco(_id);
    return false;
  }
  return true;
}

function vCEP(_id,_titulo){
  if(!vTexto(_id,_titulo)) return false;
  if(document.getElementById(_id).value.length < 9){
  	if(!_titulo) alert('Informe um CEP válido!');
  	else alert(_titulo+' não é válido!');
  	foco(_id);
  	return false;
  }
  return true;
}

function vEmail(_id,_titulo){
  if(!vTexto(_id,_titulo)) return false;
	var reg1 = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/i;
	var reg2 = /\.\./i;
	var endereco = document.getElementById(_id).value;
	if (!endereco.match(reg1) || endereco.match(reg2)) {
  	if(!_titulo) alert('Informe um Email válido!');
  	else alert(_titulo+' não é válido!');
    foco(_id);
    return false;
  }
  return true;
}

function vTelefone(_id,_titulo){
  if(!vTexto(_id,_titulo)) return false;
	var reg1 = /^\(\d{2}\)\d{4}\-\d{4}$/i;
	var telefone = document.getElementById(_id).value;
	if( !telefone.match(reg1) ) {
  	if(!_titulo) alert('Informe um Telefone válido!');
  	else alert(_titulo+' não é válido!');
    foco(_id);
    return false;
  }
  return true;
}

function vSenhas(_id1,_id2) {
  if(!vTexto(_id1,'a senha')) return false;
  if(!vTexto(_id2,'a senha novamente')) return false;
  var senha1 = trim(document.getElementById(_id1).value);
  var senha2 = trim(document.getElementById(_id2).value);
  if(senha1!=senha2){
    alert('As senhas estão diferentes!');
    foco(_id1);
    return false;
  }
  return true;
}

function vRG(_id,_titulo){
  if(!vTexto(_id,_titulo)) return false;
  return true;
}

function vMatricula(_id){
  if(!vTexto(_id)) return false;
	var reg1 = /^\d{2}\.\d{3}\.\d{3}\-[0-9x]$/i;
	var matricula = document.getElementById(_id).value;
	if( !matricula.match(reg1) ) {
    alert('Matricula inválida!');
    foco(_id);
    return false;
  }
  return true;
}

function findChecked(_prefixo){
	var rgexck = new RegExp("\^"+_prefixo, "g");
	var inputs = document.getElementsByTagName('INPUT');
	for(i=0; i<inputs.length; i++){
		if(inputs[i].type=='checkbox'){
			if(inputs[i].id.match(rgexck)){
				if(inputs[i].checked==true) return true;
			}
		}
	}
	return false;
}

// ----------------------------------------------------------------------------------------------- //


function remarcaChecksFilhos(_prefixo){
	var obj = document.getElementById(_prefixo);
	var fim = obj.src.substr(obj.src.length-5,1);
	if(fim=='y'){
		remarcaChecks(_prefixo,'N');
		remarcaPai(_prefixo,'N');
	}else{
		remarcaChecks(_prefixo,'Y');
		remarcaPai(_prefixo,'Y');
	}
}

function remarcaCheckPai(_prefixo){
	var cky = 0;
	var ckn = 0;
	var len = _prefixo.length;
	var checks = document.getElementsByTagName('input');
	for(i=0; i<checks.length; i++){
		if(checks[i].id.substr(0,len)==_prefixo){
			if( checks[i].checked ){
				cky++;
			}else{
				ckn++;
			}
		}
	}
	if( cky > 0 && ckn > 0){
		remarcaPai(_prefixo,'P');
	}else if(cky > 0){
		remarcaPai(_prefixo,'Y');
	}else{
		remarcaPai(_prefixo,'N');
	}
}

function remarcaPai(_prefixo,_tipo){
	var obj = document.getElementById(_prefixo);
	switch(_tipo){
		case 'Y': obj.src = 'images/layout/ck_y.gif'; break;
		case 'N': obj.src = 'images/layout/ck_n.gif'; break;
		case 'P': obj.src = 'images/layout/ck_p.gif'; break;
	}
}

function remarcaChecks(_prefixo,_tipo){
	var len = _prefixo.length;
	var checks = document.getElementsByTagName('input');
	for(i=0; i<checks.length; i++){
		if(checks[i].id != _prefixo){
			if(checks[i].id.substr(0,len)==_prefixo){
				if( _tipo == 'Y' ){
					checks[i].checked = true;
				}else{
					checks[i].checked = false;
				}
			}
		}
	}
}

// ----------------------------------------------------------------------------------------------- //

function trim(_text){
  return _text.replace(/^\s*|\s*$/g,"");
}

function existObj(_id){
  if( document.getElementById(_id) ){
    return true;
  }else{
    alert('objeto "'+_id+'" nao existe');
    return false;
  }
}

function foco(_id){ document.getElementById(_id).focus(); }

// ----------------------------------------------------------------------------------------------- //

function ClearStr(str, char) {
  while((cx=str.indexOf(char))!=-1) {		
    str = str.substring(0,cx)+str.substring(cx+1);
  }
  return(str);
}

function ParseNumb(c) {
  c=ClearStr(c,"-");
  c=ClearStr(c,"/");
  c=ClearStr(c,",");
  c=ClearStr(c,".");
  c=ClearStr(c,"(");
  c=ClearStr(c,")");
  c=ClearStr(c," ");
  if((parseFloat(c) / c != 1)) {
    if(parseFloat(c) * c == 0) { return(c); }else{ return(0); }
  }else{ return(c); }
  return(c);
}

function Verify(CNUMB,CTYPE) {
  CNUMB=ParseNumb(CNUMB)
  if(CNUMB == 0) {
    return(false);
  }else{
    g=CNUMB.length-2;
    if(TestDigit(CNUMB,CTYPE,g)){
      g=CNUMB.length-1;
      if(TestDigit(CNUMB,CTYPE,g)){ return(true); }
      else{ return(false); }
    }else{
      return(false);
    }
  }
}

function TestDigit(CNUMB,CTYPE,g) {
  var dig=0;
  var ind=2;
  for(f=g;f>0;f--){
    dig+=parseInt(CNUMB.charAt(f-1))*ind;
    if (CTYPE=='CNPJ') { if(ind>8) {ind=2} else {ind++} }
    else { ind++ }
  }
  dig%=11;
  if(dig<2){ dig=0; }else{ dig=11-dig; }
  if(dig!=parseInt(CNUMB.charAt(g))) { return(false); }
  else { return(true); }
}

// ----------------------------------------------------------------------------------------------- //

function valorRadio(_name){
	var valor='';
  var i,optID;
  var checkeds = false;
  var opts = document.getElementsByTagName('input');
  for(i=0; i<opts.length; i++){
    if(opts[i].name==_name){
      optID=opts[i].id;
      if(opts[i].checked){ valor = opts[i].value; }
    }
  }
  return valor;
}

function valorSelect(_id){
  if(!existObj(_id)) return false;
  var objSel = document.getElementById(_id);
  return objSel.options[objSel.selectedIndex].value;
}

function limpaText(_id){
  if(!existObj(_id)) return false;
  document.getElementById(_id).value='';
  return true;
}

// ----------------------------------------------------------------------------------------------- //

function comparaDatas(_d1,_d2){
	// formato: dd/mm/yyyy
	if(!vDataStr(_d1)) return 0;
	if(!vDataStr(_d2)) return 0;
	
	var p = Array();
	p = _d1.split('/');
	var t1 = parseInt(p[2]+''+p[1]+''+p[0]);
	p = _d2.split('/');
	var t2 = parseInt(p[2]+''+p[1]+''+p[0]);
	
	//alert(t1+' - '+t2);
	if(t1 > t2) return 1;
	else if(t1 < t2) return 3;
	else return 2;
}

function getIdade(_dNas,_dAtu){
	// formato: dd/mm/yyyy
	if(!vDataStr(_dNas)) return 0;
	if(!vDataStr(_dAtu)) return 0;
	
	var p = Array();
	p = _dNas.split('/');
	var d1 = parseInt(p[2]+''+p[1]+''+p[0]);
	p = _dAtu.split('/');
	var d2 = parseInt(p[2]+''+p[1]+''+p[0]);

	var idade = parseInt((d2 - d1)/10000);
	return idade;
}

function vDataStr(_str){
  if(vVazioStr(_str)) return false;
  var reDate = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)\d{2}$/;
  textData = _str.replace(/\-/g,'/');
  if(!textData.match(reDate) ){
    return false;
  }
  return true;
}

function vVazioStr(_str){
  return (trim(_str)=='');
}

function atualizaIdade(_data,_span){
	var dt = document.getElementById(_data).value;
	if(vDataStr(dt)){
		var idade = getIdade(dt,DATA_ATUAL);
		document.getElementById(_span).innerHTML = '('+idade+' anos)';
	}else{
		document.getElementById(_span).innerHTML = '';
	}
}

function resetFormDivId(_id){
	var obj = document.getElementById(_id);
	var inputs = obj.getElementsByTagName('input');
	for(i=0; i<inputs.length; i++){
		if(inputs[i].type=="text"){ inputs[i].value = ''; }
		if(inputs[i].type=="radio"){ inputs[i].checked = false; }
		if(inputs[i].type=="checkbox"){ inputs[i].checked = false; }
		if(inputs[i].type=="hidden"){ inputs[i].value = ''; }
	}
	var select = obj.getElementsByTagName('select');
	for(i=0; i<select.length; i++){ select[i].options[0].selected = true; }
	var txarea = obj.getElementsByTagName('textarea');
	for(i=0; i<txarea.length; i++){ txarea[i].value = ''; }
}

function switchDisplay(_id){
  var obj = document.getElementById(_id);
  obj.style.display = (obj.style.display=='none')?'':'none';
}


function hideTR(_id){
	if(document.getElementById(_id)){
		document.getElementById(_id).style.display = 'none';
	}
}

function showTR(_id){
	if(document.getElementById(_id)){
		if(navigator.appName=='Netscape'){
			document.getElementById(_id).style.display = 'table-row';
		}else{
			document.getElementById(_id).style.display = 'block';
		}
	}
}

function hideInLine(_id){
	document.getElementById(_id).style.display = 'none';
}

function showInLine(_id){
	document.getElementById(_id).style.display = 'inline';
}

function addOptSel(selObj,v,t){
	var optNew = document.createElement('option');
  optNew.text = t;
  optNew.value = v;
	try {
		selObj.add(optNew, null); // standards compliant; doesn't work in IE
	}catch(ex) {
		selObj.add(optNew); // IE only
	}
}

// ----------------------------------------------------------------------------------------------- //

function getListaGestores(i_cont_pai_cod){
	if(i_cont_pai_cod <= 0) {
		return false;
	}

	ajax = ajaxInit();	
	ajax_param = "&mode=getListaGestores&cod_cont=" + i_cont_pai_cod;		
	ajax.open("POST", "pages/conteudos.ajax.php?" + new Date().getTime() + ajax_param, true);						
	ajax.onreadystatechange = function() {	
		if(ajax.readyState == 4) {
			var result = new Object();
			eval("result = " + ajax.responseText);
			if(result) {
				bloquear = (result.data.editavel=='N')?true:false;
				bloquearCheckBoxes('GESTOR_',false,bloquear);
				bloquearAltPai(false); //bloquearAltPai(bloquear);

				for(x in result.data.aprovadores) {
					var gestor = result.data.aprovadores[x];
					if(gestor.COD_GCMS){
						document.getElementById('GESTOR_A_'+gestor.COD_GCMS).checked = true;
					}
				}

				for(x in result.data.publicadores) {
					var gestor = result.data.publicadores[x];
					if(gestor.COD_GCMS){
						document.getElementById('GESTOR_P_'+gestor.COD_GCMS).checked = true;
					}
				}
				
			}				
		}
	};			
	ajax.send(null);
}

function getListaSetores(oDepto,idSetor,base_url) {
  if(!base_url) base_url = '';
	var oSetores = document.getElementById(idSetor);
	oSetores.options.length = 0;
	if (oDepto.selectedIndex == 0) {
		oSetores.options[0] = new Option('-- nenhum setor --', '0');
	} else {
		oSetores.options[0] = new Option('Aguarde... carregando lista', '0');
		var oXml = new xml();
		oXml.file = base_url+'ws/setores.ws.php';
		oXml.xml  = 'coddept='+oDepto.options[oDepto.selectedIndex].value+'&ac=getSetores';
		oXml.ret  = function () {
			var oData = oXml.xmlDoc;
			var aListaSetores = oData.getElementsByTagName('SETOR');
			if (aListaSetores.length > 0) {
				var sCodSetor = '';
				var sNomeSetor = '';
				oSetores.options.length = 0;
				oSetores.options[0] = new Option('-- selecione --', '0');
				for (var i = 0; i < aListaSetores.length; i++) {
					sCodSetor = nValue(aListaSetores[i], 'COD_SETR');
					sNomeSetor = nValue(aListaSetores[i], 'TXSETOR_SETR');
					oSetores.options[(i+1)] = new Option(sNomeSetor, sCodSetor);
				}
			} else {
				oSetores.options.length = 0;
				oSetores.options[0] = new Option('-- nenhum setor --', '0');
			}
		};
		oXml.load();
	}
}

function getListaTiposDeSolicitacao(oOrigem,idDestino,codPai,codSetor) {
  //alert('getListaTiposDeSolicitacao');
	if(codPai== 'undefined') codPai = 0;
	if(codSetor== 'undefined') codSetor = 0;
	
	if(idDestino){
	  var oDestino = document.getElementById(idDestino);
	  oDestino.options.length = 0;
	}
	
	if (oOrigem.selectedIndex == 0) {
	  //alert('x0');
	  if(idDestino) oDestino.options[0] = new Option('-- nenhum item --', '0');
	} else {
	  //alert('x1');
		if(idDestino) oDestino.options[0] = new Option('Aguarde... carregando lista', '0');
		
		var oXml = new xml();
		oXml.file = 'ws/tiposDeSolicitacao.ws.php';
		oXml.xml  = 'pai='+codPai+'&setor='+codSetor+'&ac=getTiposDeSolicitacao';
		oXml.ret  = function () {
		  //alert(oXml.xml);
			var oData = oXml.xmlDoc;
			var aListaItens = oData.getElementsByTagName('TIPO');
			var aCampos = oData.getElementsByTagName('CAMPO');
			
			if (aListaItens.length) {
				var sCodItem = '';
				var sNomeItem = '';
				if(idDestino){
				  oDestino.options.length = 0;
				  oDestino.options[0] = new Option('-- selecione --', '0');
				}
				for (var i = 0; i < aListaItens.length; i++)
				{
					sCodItem = nValue(aListaItens[i], 'COD_TPSL');
					sNomeItem = nValue(aListaItens[i], 'TXTIPO_TPSL');
					if(idDestino) oDestino.options[(i+1)] = new Option(sNomeItem, sCodItem);
				}
			} else {
			  if(idDestino){
				  oDestino.options.length = 0;
				  oDestino.options[0] = new Option('-- nenhum item --', '0');
			  }
			}
			
			if(aCampos.length)
			{
				var sJSON = '';
				for (var i = 0; i < aCampos.length; i++)
				{	
					var cod_cpst = nValue(aCampos[i],'COD_CPST');
					var fgexibe_cpst = nValue(aCampos[i],'FGEXIBE_CPST')=='S'? 1:0;
					var fgobrig_cpst = nValue(aCampos[i],'FGOBRIG_CPST')=='S'? 1:0;
					var fgencer_cpst = nValue(aCampos[i],'FGENCER_CPST')=='S'? 1:0;
					sJSON += '{';
					sJSON += '"cod_cpst":'		+ cod_cpst		+ ',';
					sJSON += '"fgexibe_cpst":'	+ fgexibe_cpst	+ ',';
					sJSON += '"fgobrig_cpst":'	+ fgobrig_cpst	+ ',';
					sJSON += '"fgencer_cpst":'	+ fgencer_cpst;
					sJSON += '}';
					
					if(i < (aCampos.length - 1)) sJSON += ', ';
					else CamposJson('[' + sJSON + ']');
				}
			}
		};
		oXml.load();
	}
}


function getListaCategorias(oOrigem,idDestino,codPai,base_url) {
  if(!base_url) base_url = '';
	var oDestino = document.getElementById(idDestino);
	oDestino.options.length = 0;
	if (oOrigem.selectedIndex == 0) {
		oDestino.options[0] = new Option('-- nenhuma categoria --', '0');
	} else {
		oDestino.options[0] = new Option('Aguarde... carregando lista', '0');
		var oXml = new xml();
		oXml.file = base_url+'ws/tiposDeSolicitacao.ws.php';
		oXml.xml  = 'codsetr='+oOrigem.options[oOrigem.selectedIndex].value+'&codpai='+codPai+'&ac=getCategorias';
		oXml.ret  = function () {
			var oData = oXml.xmlDoc;
			var aListaCategorias = oData.getElementsByTagName('TIPO');
			if (aListaCategorias.length > 0) {
				var sCodTpsl = '';
				var sNomeTpsl = '';
				oDestino.options.length = 0;
				oDestino.options[0] = new Option('-- selecione --', '0');
				for (var i = 0; i < aListaCategorias.length; i++) {
					sCodTpsl = nValue(aListaCategorias[i], 'COD_TPSL');
					sNomeTpsl = nValue(aListaCategorias[i], 'TXTIPO_TPSL');
					oDestino.options[(i+1)] = new Option(sNomeTpsl, sCodTpsl);
				}
			} else {
				oDestino.options.length = 0;
				oDestino.options[0] = new Option('-- nenhuma categoria --', '0');
			}
		};
		oXml.load();
	}
}

function getListaAtendentes(oDepto,idAtend) {
	var oAtend = document.getElementById(idAtend);
	oAtend.options.length = 0;
	if (oDepto.selectedIndex == 0) {
		oAtend.options[0] = new Option('-- nenhum atendente --', '0');
	} else {
		oAtend.options[0] = new Option('Aguarde... carregando lista', '0');
		var oXml = new xml();
		oXml.file = 'ws/setores.ws.php';
		oXml.xml  = 'coddept='+oDepto.options[oDepto.selectedIndex].value+'&ac=getAtendentes';
		oXml.ret  = function () {
			var oData = oXml.xmlDoc;
			var aListaAtend = oData.getElementsByTagName('ATENDENTE');
			if (aListaAtend.length > 0) {
				var sCodAtend = '';
				var sNomeAtend = '';
				oAtend.options.length = 0;
				oAtend.options[0] = new Option('-- selecione --', '0');
				for (var i = 0; i < aListaAtend.length; i++) {
					sCodAtend = nValue(aListaAtend[i], 'COD_USUA');
					sNomeAtend = nValue(aListaAtend[i], 'TXNOME_USUA');
					oAtend.options[(i+1)] = new Option(sNomeAtend, sCodAtend);
				}
			} else {
				oAtend.options.length = 0;
				oAtend.options[0] = new Option('-- nenhum atendente --', '0');
			}
		};
		oXml.load();
	}
}

//----------------------------------------------------------------------------------------------- //

function trocaContTreeView(tipo, inputId, strID, path){
	var _cod = document.getElementById(inputId).value;
	var _url = path+'conteudorelacionado.php?tiporel='+tipo+'&codcont='+_cod;
	var pl = '20';
	var pt = '20';
	var th = '500';
	var tw = '300';
	var opcao = null;
	if (window.showModalDialog && (navigator.userAgent.indexOf('Chrome') == -1)) {
		j_param = window.showModalDialog(_url,"conteudocms","dialogWidth:"+tw+"px;dialogHeight:"+th+"px");
	} else {
		j_param = window.open(_url,'conteudocms','height='+th+',width='+tw+',toolbar=no,directories=no,status=no,continued from previous linemenubar=no,scrollbars=no,resizable=no ,modal=yes');			
	}
	trocaContReturn(j_param, tipo, inputId, strID, path);
}


function trocaContReturn(j_param, tipo, inputId, strID, path){
	if(j_param) {
		document.getElementById(inputId).value = j_param['cont'];
		setContBreadcrumb(j_param['cont'],strID, path);
	}
}


function setContBreadcrumb(codcont, objID, path) {
	ajax_1 = ajaxInit();
	ajax_param = "&mode=get_conteudo_breadcrumb&cod_cont=" + codcont;
	ajax_1.open("POST", path+"pages/conteudos.ajax.php?" + new Date().getTime() + ajax_param, true);						
	ajax_1.onreadystatechange = function() {	
		if(ajax_1.readyState == 4) {
			var result = new Object();
			eval("result = " + ajax_1.responseText);
			if(result) {
				document.getElementById(objID).innerHTML = result.breadcrumb;
				}				
			}
		};
	ajax_1.send(null);
}


/*funco basica para iniciar o uso do Ajax no site*/
function ajaxInit() {
	var instancia;
	try{
		instancia = new XMLHttpRequest();
	}
	catch(ee){
		try{
			instancia = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try{
				instancia = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(E){
				instancia = false;
		 	}
		}
	}
	return instancia;		
}


function pingSession(pageurl) {
	ajax_obj = ajaxInit();
	ajax_param = "";
	ajax_obj.open("POST", pageurl + '?' + new Date().getTime() + ajax_param, true);						
	ajax_obj.onreadystatechange = function() {			
		if(ajax_obj.readyState == 4) {
			var result = new Object();
			// por enquanto nao faz nada
			alert('ok');
		}
	};
	ajax_obj.send(null);
}


function AtribuiAutoSuggest(id, url, Callback) {
	var options = {
		script: url,
		json: true,
		maxresults: 0,
		noresults: 'Sem sugestões',
		//shownoresults:false,
		//minchars:2,
		offsety:0,
		cache:false,
		callback:Callback,
		timeout:30000
	};
	as_json[id] = new bsn.AutoSuggest(id, options);
}


function fechaTodosAutoSuggest(){
	for(id in as_json){
		document.getElementById(id).value = '';
		as_json[id].getSuggestions('');
	}
}


var as_json = {};

