var ie4 = (document.all) ? true : false;
var ns4 = (document.layers) ? true : false;
var ns6 = (document.getElementById && !document.all) ? true : false;

/*=====================================================*/
/*       Muda cor do campo ao entrar nele              */
/*=====================================================*/
function tbFocus(el){
	el.style.Color = "#ffffff";
	el.style.backgroundColor = "#90CB61";
}

/*=====================================================*/
/*       Muda cor do campo ao sair dele                */
/*=====================================================*/
function tbBlur(el){
	el.style.Color = "#007937";
	el.style.backgroundColor = "#ffffff";
}

/*=====================================================*/
/*  Funcoes para mascara e validacao de hora e data    */
/*=====================================================*/
function mascara_data(data){ 
//alert(data);
    var mydata = ''; 
    mydata = mydata + data; 
    if (mydata.length == 2){ 
        mydata = mydata + '/'; 
        document.forms[0].data.value = mydata; 
    } 
    if (mydata.length == 5){ 
        mydata = mydata + '/'; 
        document.forms[0].data.value = mydata; 
    } 
    if (mydata.length == 10){ 
        verifica_data(); 
    } 
} 
 
function verifica_data () { 

  dia = (document.forms[0].data.value.substring(0,2)); 
  mes = (document.forms[0].data.value.substring(3,5)); 
  ano = (document.forms[0].data.value.substring(6,10)); 

  situacao = ""; 
  // verifica o dia valido para cada mes 
  if ((dia < 01)||(dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31) { 
      situacao = "falsa"; 
  } 

  // verifica se o mes e valido 
  if (mes < 01 || mes > 12 ) { 
      situacao = "falsa"; 
  } 

  // verifica se e ano bissexto 
  if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) { 
      situacao = "falsa"; 
  } 

  if (document.forms[0].data.value == "") { 
      situacao = "falsa"; 
  } 

  if (situacao == "falsa") { 
      alert("Data inválida!"); 
      document.forms[0].data.focus(); 
  } 
} 

function mascara_hora(hora){ 

	var hora01 = ''; 
	hora01 = hora01 + hora.value; 

	if (hora01.length == 2){ 
		hora01 = hora01 + ':'; 
		document.forms[1].hora.value = hora01; 
		alert(hora01);
	} 
	if (hora01.length == 5){ 
		Verifica_Hora(hora); 
	} 
}  
 
function verifica_hora(hora){ 
    hrs = (document.forms[0].hora.value.substring(0,2)); 
    min = (document.forms[0].hora.value.substring(3,5)); 
     
    alert('hrs '+ hrs); 
    alert('min '+ min); 
     
    situacao = ""; 
    // verifica data e hora 
    if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59)){ 
        situacao = "falsa"; 
    } 
     
    if (document.forms[0].hora.value == "") { 
        situacao = "falsa"; 
    } 

    if (situacao == "falsa") { 
        alert("Hora inválida!"); 
        document.forms[0].hora.focus(); 
    } 
} 

/*=====================================================*/
/*  Funcao p mostrar/esconder layer                    */
/*=====================================================*/
 function show_div(lay,altura,campo,outra_div){
	td  = eval('td_'+lay);
	div = 'div_'+lay;
	if (campo.checked == true) {
		td.height = altura;
		if (document.getElementById) { // DOM3 = IE5, NS6
			document.getElementById(div).style.display = 'block';
		}
		else {
			if (document.layers) { // Netscape 4
				document.div.display = 'block';
			}
			else { // IE 4
				document.all.div.style.display = 'block';
			}
		}

		}
	else if (campo.checked == false) {
		if (outra_div.checked == false){
			if (document.getElementById) { // DOM3 = IE5, NS6
				document.getElementById(div).style.display = 'none';
			}
			else 
			{
				if (document.layers) { // Netscape 4
					document.div.display = 'none';
				}
				else { // IE 4
					document.all.div.style.display = 'none';
				}
			}
			td.height = 1;
		}
	}
}

/*=====================================================*/
/*  Funcao p mostrar/esconder layer do preventivo      */
/*=====================================================*/
 function show_obs_prev(lay,altura,campo){
	td  = eval('td_'+lay);
	div = 'div_'+lay;
	var objForm = document.forms[0];	
	checado = false;
	for(i=0;i<objForm.length;i++){
		if (objForm.elements[i].type == "radio"){
			if (objForm.elements[i].checked == true && objForm.elements[i].value == 2) {
				checado = true;
			}
		}
	}	
	if (checado == true) {
		td.height = altura;
		if (document.getElementById) { // DOM3 = IE5, NS6
			document.getElementById(div).style.display = 'block';
		}
		else {
			if (document.layers) { // Netscape 4
				document.div.display = 'block';
			}
			else { // IE 4
				document.all.div.style.display = 'block';
			}
		}

		}
	else if (checado == false) 
		{
		if (document.getElementById) { // DOM3 = IE5, NS6
			document.getElementById(div).style.display = 'none';
		}
		else 
		{
			if (document.layers) { // Netscape 4
				document.div.display = 'none';
			}
			else { // IE 4
				document.all.div.style.display = 'none';
			}
		}
		td.height = 1;
	}
	
}
/*=====================================================*/
/*  Funcao para permitir digitacao de somente numeros  */
/*=====================================================*/
function chk_num(campo,teclapres){
	var tecla = teclapres.keyCode;		
	if (tecla == 13)
		{
		return false;
		}
	if (tecla < 47 || tecla > 57)
		{
		alert('Digite apenas números!'); 
		event.returnValue = false;
		campo.focus();
		return false;
		}

return true;
}

/*=====================================================*/
/*          Funcao para validar boletim                */
/*=====================================================*/
function chk_boletim() {

    var ok = true;
	var campo = "";
	var objForm = document.form_boletim;	
	for(i=0;i<objForm.length;i++){
		if (objForm.elements[i].type == "file" && objForm.elements[i].value != ""){
			campo = campo + objForm.elements[i].value;
		}
		objForm.arquivos.value = campo;
	}
	if (document.forms[0].cpo_nome.value == 0)      {ok = false;}
/*	if (document.forms[0].cpo_data.value == 0)      {ok = false;}
	if (document.forms[0].cpo_site_uf.value == 0)   {ok = false;}
	if (document.forms[0].cpo_site_cida.value == 0) {ok = false;}
	if (document.forms[0].cpo_site_end.value == 0)  {ok = false;}
	if (document.forms[0].cpo_ocorr.value == 0)      {ok = false;}
	if (document.forms[0].cpo_proc.value == 0)      {ok = false;}
	if (document.forms[0].cpo_conc.value == 0)      {ok = false;}
	
	if (document.forms[0].chk_ocorrencia.checked == true) {
		if (document.forms[0].cpo_ocorrencia.value == 0) {ok = false;};
	}
	*/
	if (!ok){ 
		alert("O formulario não está preenchido corretamente. Verifique.");
		MostraStatus('none');
	} 
return ok;
}

/*=====================================================*/
/*          Funcao para validar preventiva             */
/*=====================================================*/
function chk_prev() {

    var ok = true;
	var campo = 0;
	var objForm = document.forms[0];	
	// Verifica campo de arquivos para upload
	for(i=0;i<objForm.length;i++){
		if (objForm.elements[i].type == "file" && objForm.elements[i].value != ""){
			campo = campo + 1;
		}
		objForm.arquivos.value = campo;
	}

	// Verifica se todos os botoes de radio stao marcados
	var cnt=0;
	for (var zxc0=1;zxc0<15;zxc0++){
		var rads=objForm['rd_item'+zxc0];
		for (var zxc1=0;zxc1<rads.length;zxc1++){
			if (rads[zxc1].checked){ cnt++; }
		}
	}
	if (cnt==14){ ok = true; } else {ok = false};

	if (document.forms[0].cpo_nome.value == 0)      {ok = false;}
	if (document.forms[0].cpo_data.value == 0)      {ok = false;}
	if (document.forms[0].cpo_site_uf.value == 0)   {ok = false;}
	if (document.forms[0].cpo_site_cida.value == 0) {ok = false;}
	if (document.forms[0].cpo_site_end.value == 0)  {ok = false;}
	
	if (!ok){ 
		alert("O formulario não está preenchido corretamente. Verifique.");
		MostraStatus('none');
	} 
return ok;
}

/*=======================================================*/
function chk_alt_senha(theform)   {
/*=======================================================*/
	 if (theform.oldsenha.value == "")
		{
        alert ("Por favor, a senha precisa ser informada.");
        theform.oldsenha.focus();
        return false;
	 }
	 if (theform.newsenha1.value == "")
		{
        alert ("Por favor, a nova senha precisa ser informada.");
        theform.newsenha1.focus();
        return false;
	 }
	 if (theform.newsenha2.value == "")
		{
        alert ("Por favor, a nova senha de conferência precisa ser informada.");
        theform.newsenha2.focus();
        return false;
	 }
	 if (theform.newsenha1.value != document.alt_senha.newsenha2.value)
		{
        alert ("Por favor digite sua nova senha novamente.");
        theform.newsenha1.focus();
        return false;
	 }

return true;
}

/*=====================================================*/
function showIt(lay) {
/*=====================================================*/
	if (ie4) {document.all[lay].style.visibility = "visible";}
	if (ns4) {document.layers[lay].visibility = "show";}
	if (ns6) {document.getElementById([lay]).style.display = "block";}
}

/*=====================================================*/
function hideIt(lay) {
/*=====================================================*/
	if (ie4) {document.all[lay].style.visibility = "hidden";}
	if (ns4) {document.layers[lay].visibility = "hide";}
	if (ns6) {document.getElementById([lay]).style.display = "none";}
}

/*=====================================================*/
function pop_win(win_url,win_largura,win_altura) {
/*=====================================================*/
	var telaW = screen.width, telaH = screen.height;
	var winW = 800, winH = 600;
	var largura = win_largura,  altura = win_altura;

	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth/2;
			winH = window.innerHeight/2;
		}
	}
	if (navigator.appName.indexOf("Microsoft")!=-1) {
		winW = (document.body.offsetWidth-largura)/2;
		winH = (document.body.offsetHeight-altura)/2;
		if (telaW - winW > 0){
			winW = window.screenLeft + winW;
		}

		if (telaH - winH > 0){
			winH = winH + window.screenTop;
		}
	}
	
	window.open(win_url,'anexos','top='+winH+',left='+winW+',width='+largura+',height='+altura+',location=no,menubar=0,resizable=0,scrollbars=no,status=0,toolbar=0');
}

/*=====================================================*/
function IsParent(){
/*=====================================================*/
	resultado=""
	try
	{
		if(typeof(window.opener)!="undefined")
		{
				var x_win = window.self;
				while(x_win!="undefined")
				{
					x_win = x_win.opener;
					if(typeof(x_win.opener)=="undefined")
					{
						break;
					}
				}
		}
		else
		{
//nome_win
parent.window.name.close();
//self.close();

//			window.location.href="";
		}
	}
	catch(e)
	{
		//alert("Oops");
	}
	return resultado;
}

//Exemplo de uso no campo do formulário
//onkeyDown="FormataCampo(this,event,'(##)####-####')"
//...............................................................................................
/*=====================================================*/
/*  Funcao para mascara e validacao de data    */
/*=====================================================*/
function validDate(obj){
	date=obj.value
	if (/[^\d/]|(\/\/)/g.test(date))  {obj.value=obj.value.replace(/[^\d/]/g,'');obj.value=obj.value.replace(/\/{2}/g,'/'); return }
	if (/^\d{2}$/.test(date)){obj.value=obj.value+'/'; return }
	if (/^\d{2}\/\d{2}$/.test(date)){obj.value=obj.value+'/'; return }
	if (!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(date)) return
	
	 test1=(/^\d{1,2}\/?\d{1,2}\/\d{4}$/.test(date))
	 date=date.split('/')
	 d=new Date(date[2],date[1]-1,date[0])
	 test2=(1*date[0]==d.getDate() && 1*date[1]==(d.getMonth()+1) && 1*date[2]==d.getFullYear())
	 if (test1 && test2) return true
	 alert("Data inválida !!!")
	 obj.select();
	 obj.focus()
	 return false
}

function CheckEmail(obj) {
	email = obj;
	AtPos = email.indexOf("@");
	StopPos = email.lastIndexOf(".");
	volta = true;
	
	if (email == "") {
		volta = false;
	}
	
	if (AtPos == -1 || StopPos == -1) {
		volta = false;
	}
	
	if (StopPos < AtPos) {
		volta = false;
	}
	
	if (StopPos - AtPos == 1) {
		volta = false;
	} 
	if (!volta) {
		alert("Endereço de Email inválido" + "\n" + obj);
	}
	return volta
}

function ValidarCNPJ(ObjCnpj){
    var cnpj = ObjCnpj.value;
    var valida = new Array(6,5,4,3,2,9,8,7,6,5,4,3,2);
    var dig1= new Number;
    var dig2= new Number;
    
    exp = /\.|\-|\//g
    cnpj = cnpj.toString().replace( exp, "" ); 
    var digito = new Number(eval(cnpj.charAt(12)+cnpj.charAt(13)));
        
    for(i = 0; i<valida.length; i++){
        dig1 += (i>0? (cnpj.charAt(i-1)*valida[i]):0);    
        dig2 += cnpj.charAt(i)*valida[i];    
    }
    dig1 = (((dig1%11)<2)? 0:(11-(dig1%11)));
    dig2 = (((dig2%11)<2)? 0:(11-(dig2%11)));
    if(((dig1*10)+dig2) != digito)   { 
        alert('CNPJ Inválido!');
		ObjCnpj.focus();
		}
}

/*=======================================================*/
/*
	Data:<input type="text" size="20" onkeypress="return txtBoxFormat(this, '99/99/9999', event);">
	RG:<input type="text" size="20" onkeypress="return txtBoxFormat(this, '99.999.999-9', event);">
	Telefone:<input type="text" size="20" onkeypress="return txtBoxFormat(this, '(99)9999-9999', event);">
	Código:<input type="text" size="20" onkeypress="return txtBoxFormat(this, '99-999', event);">
*/
function txtBoxFormat(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
/*=======================================================*/

if(document.all) { // Internet Explorer
    nTecla = evtKeyPress.keyCode;
} else if(document.layers) { // Nestcape
    nTecla = evtKeyPress.which;
} else {
    nTecla = evtKeyPress.which;
    if (nTecla == 8) {
        return true;
    }
}

    sValue = objeto.value;

    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;

    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
      bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
      bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

      if (bolMask) {
        sCod += sMask.charAt(i);
        mskLen++; }
      else {
        sCod += sValue.charAt(nCount);
        nCount++;
      }

      i++;
    }

    objeto.value = sCod;

    if (nTecla != 8) { // backspace
      if (sMask.charAt(i-1) == "9") { // apenas números...
        return ((nTecla > 47) && (nTecla < 58)); } 
      else { // qualquer caracter...
        return true;
      } 
    }
    else {
      return true;
    }
  } 


/*=====================================================*/
/*                    End of program                   */
/*=====================================================*/

