// JavaScript Document

function teste()
{
alert('testando script')
}

/*
function toUpper()
{
		for (a = 0; a < document.form1.elements.length; a++)
		{
			if (document.form1.elements[a].type == "text" || document.form1.elements[a].type == "textarea")
				document.form1.elements[a].value = document.form1.elements[a].value.toUpperCase();
		}
	
}  */

function campo(vlr1, vlr2)
{
	this.nomeCampo = vlr1;
	this.descCampo = vlr2;
}

listCampo = new campo();

//Cadastro de Ocorrência
listCampo[1] = new campo("numero_uc","Numero da UC");
listCampo[2] = new campo("nome","Nome");
listCampo[3] = new campo("rg_reclamante","RG");
listCampo[4] = new campo("cpf_reclamante","CPF");
listCampo[5] = new campo("endereco","Endereco");
listCampo[6] = new campo("email","E-mail");
listCampo[7] = new campo("telefone111","Telefone");
listCampo[8] = new campo("dt_ocorrencia","Data da ocorrencia");
listCampo[9] = new campo("hora_ocorrencia","Hora da ocorrencia");
listCampo[10] = new campo("int_fornecimento111","int_fornecimento");
listCampo[11] = new campo("comunicacao_empresa111","comunicacao_empresa");
listCampo[12] = new campo("problema_apresentado","Problema apresentado");
listCampo[13] = new campo("equipamento_danificado","Equipamento(s) danificado(s)");

totalCampos = 13;

function valida()
{	


	cont = 1;
	qtdErro = 0;
	qtdErroLocal = 0;
	msgError = "O(s) campo(s):\n";

		qtdCampo = document.form1.elements.length;
		for (x = 0; x < qtdCampo; x++)
		{

			campo = document.form1.elements[x];

			if ((campo.value == "" && (campo.type == "text" || campo.type == "textarea" || campo.type == "password" || campo.type == "file")) || (campo.type == "select-one" && campo.value == 0) || (campo.type == "select-multiple" && campo.value == 0))
			{
			   for (i = 1; i <= totalCampos; i++)
				{

					if (campo.name == listCampo[i].nomeCampo)
					{
						msgError += "- " + listCampo[i].descCampo + "\n";
						qtdErro = 1;
					}
				}
			}
		}

	local()	;

	if (qtdErroLocal == 1)
	{
		alert (msgError2);
		
	}
	if (qtdErro == 1)
	{
		msgError += "deve(m) ser preenchido(s)";
		alert (msgError);
	}
	if (qtdErro == 0 && qtdErroLocal == 0)
	{
		//toUpper();
  		document.form1.submit();
	}
}



////////////////////////////////////////////////////////////////////////

function mask(_mask, val) {
	var i, mki;
	var aux="";
	
	for(i=mki=0; i<val.length; i++, mki++) {
		if(_mask.charAt(mki)=='' || _mask.charAt(mki)=='#' || _mask.charAt(i)==val.charAt(i)) {
			aux+=val.charAt(i);
		} else {
			aux+=_mask.charAt(mki)+val.charAt(i);
			mki++;
		}
	}
	return aux;
}

/**
 * function maskEvent(field, _mask, event)
 *
 * field = Objeto que esta enviando o evendo onKeyPress()
 * _mask = Mascara Exemplo: ##/##/#### ou ###.###.###-##
 * event = Evento a ser observado.
 *
 * Formata um valor para a mascara definida conforma o valor vai sendo digitado.
 *
 * pedro.leao@ig.com.br 2003;08/16
 */
function maskEvent(field, _mask, event) {
	var key ='';
	var aux='';
	var len=0;
	var i=0;
	var strCheck = '0123456789';
	var rcode = (window.Event) ? event.which : event.keyCode;
	
	if(rcode == 13) {
		//Enter
		return true;
	}
	
	//Get key value from key code
	key=String.fromCharCode(rcode);
	
	if(strCheck.indexOf(key)==-1) {
		//Not a valid key
		return false;
	}
	
	aux=field.value+key;
	//window.alert(aux);
	aux=mask(_mask,aux);
	//window.alert(aux);
	field.value=aux;	
	return false;
}

/**
 *
 * function currencyFormat(fld, milSep, decSep, e)
 *
 * fld    = Objeto a ser verficado.
 * milSep = Separador para milhar.
 * decSep = Separador para decimal.
 * e      = Evento.
 *
 * Formata um valor decimal conforme for digitado no box.
 * Criação: Anonima (coletada em http://http://www.scriptbrasil.com/?class=2&secao=javascript&categoria=Formulários&menu=javascript&ini=1
 * Revisao: pedro.leao@ig.com.br	2003/08/16
 */
	function FormataData(objCampo,teclapres) {
	  var campo = "";
	  var tecla = teclapres.keyCode;
	  vr = objCampo.value;
	  if ("0123456789".search(vr.substr(vr.length-1,1)) == -1) {
		  vr = vr.substr(0, vr.length-1);
		  objCampo.value = vr;
	  }
	  else {   
		vr = vr.replace( ".", "" );
		vr = vr.replace( "/", "" );
		vr = vr.replace( "/", "" );
		tam = vr.length + 1;
		if ( tecla != 9 && tecla != 8 ) {
		  if ( tam > 2 && tam < 5 ) {
			objCampo.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		  }
		  if ( tam >= 5 && tam <= 10 ) {
			objCampo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
	      }
		}
	  }
	  validaData();
	}
	
function currencyFormat(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;

	if (whichCode == 13) {
		return true;  // Enter
	}
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) {
		return false;  // Not a valid key
	}
	len = fld.value.length;
	for(i = 0; i < len; i++) {
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)){
			 break;
		}
	}
	
	aux = '';
	for(; i < len; i++) {
		if (strCheck.indexOf(fld.value.charAt(i))!=-1){
			aux += fld.value.charAt(i);
		}
	}
	aux += key;
			
	len = aux.length;
	if (len == 0) {
		fld.value = '';
	} else if (len == 1) {
		fld.value = '0'+ decSep + '0' + aux;
	} else if (len == 2) {
		fld.value = '0'+ decSep + aux;
	} else if (len > 2) {
		aux2 = '';

		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--) {
			fld.value += aux2.charAt(i);
		}
		
		fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}

function SomenteNumeros(input)
	{
	//
	if ((event.keyCode<48)||(event.keyCode>57))
		event.returnValue = false;
	}
//-------------------------------
function FormataValor(campo,tammax,teclapres) {

	var tecla = teclapres.keyCode;
	var vr = campo.value;
	vr = vr.replace( "-", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 13){ tam = vr.length + 1 ; }

	if (tecla == 13 ){	tam = tam - 1 ; }
		
	if ( tecla == 13 || (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105) ){
		if ( tam <= 2 ){ 
	 		campo.value = vr ; }
	 	tam = tam - 1;
	 	if ( (tam > 2) && (tam <= 5) ){
	 		campo.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		campo.value = vr.substr( 0, tam - 6 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		campo.value = vr.substr( 0, tam - 9 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 12) && (tam <= 14) ){
	 		campo.value = vr.substr( 0, tam - 12 ) + '.' + vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	
	}
}



function mascara_data(dt_ini){ 
              var mydata = ''; 
              mydata = mydata + dt_ini; 
              if (mydata.length == 2){ 
                  mydata = mydata + '/'; 
                  document.form1.dt_ini.value = mydata; 
              } 
              if (mydata.length == 5){ 
                  mydata = mydata + '/'; 
                  document.form1.dt_ini.value = mydata; 
              } 
              if (mydata.length == 10){ 
                  verifica_data(); 
              } 
          } 
           
          function verifica_data () { 

            dia = (document.form1.dt_ini.value.substring(0,2)); 
            mes = (document.form1.dt_ini.value.substring(3,5)); 
            ano = (document.form1.dt_ini.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.form1.dt_ini.value == "") { 
                situacao = "falsa"; 
            } 
    
            if (situacao == "falsa") { 
                alert("Data inválida!"); 
                document.form1.dt_ini.focus(); 
            } 
          } 


function mascara_data2(dt_fim){ 
              var mydata = ''; 
              mydata = mydata + dt_fim; 
              if (mydata.length == 2){ 
                  mydata = mydata + '/'; 
                  document.form1.dt_fim.value = mydata; 
              } 
              if (mydata.length == 5){ 
                  mydata = mydata + '/'; 
                  document.form1.dt_fim.value = mydata; 
              } 
              if (mydata.length == 10){ 
                  verifica_data2(); 
              } 
          } 
           
          function verifica_data2() { 

            dia = (document.form1.dt_fim.value.substring(0,2)); 
            mes = (document.form1.dt_fim.value.substring(3,5)); 
            ano = (document.form1.dt_fim.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.form1.dt_fim.value == "") { 
                situacao = "falsa"; 
            } 
    
            if (situacao == "falsa") { 
                alert("Data inválida!"); 
                document.form1.dt_fim.focus(); 
            } 
          } 
   
//Verifica se a data1 é maior ou igual que a data 2
function datamaiorigual(dt1,dt2)
{
data1 = dt1.value;
dia = data1.substring(0,2);
mes = data1.substring(3,5);
ano = data1.substring(6,12);

data2 = dt2.value;
dia2 = data2.substring(0,2);
mes2 = data2.substring(3,5);
ano2 = data2.substring(6,12);

var data_t = new Date(ano,mes,dia);
var data_t2 = new Date(ano2,mes2,dia2);
var data_atual = new Date();

dia_atual = data_atual.getDate();
mes_atual = data_atual.getMonth();
mes_atual2 = mes_atual + 1;
mes_atual3 = '0' + mes_atual2;
ano_atual = data_atual.getFullYear();

mes_acum = mes_atual2 + 1;

var data_acum = new Date(ano_atual,mes_acum,dia_atual)

var data_atual2 = new Date(ano_atual,mes_atual3,dia_atual)

qtdE=0;

if(data_t < data_atual2)
    {
     qtdE = 1;
    }
if(data_t > data_acum)
    {
     qtdE = 3;
    }
if(dt2.value != "")
   {
      if(data_t > data_t2)
	    {
		 qtdE=2;
		}
	   if(qtdE==2)
	   {
		document.form1.dt_ini.value = "";
		alert("Data Inicial menor que Data Atual!");
	   }
   }

  if(qtdE==1)
   {
	document.form1.dt_ini.value = "";
	alert("Data Inicial maior que Data Final!");
   }

   if(qtd==3){
		alert("Data Inicial excede 30 dias da data atual");
   }
   if(qtdE==0)
   document.form1.submit();
}
//valida ano
/*
function anomenor(ano_doc)
{
ano = ano_doc.value;
qtdE = 0;

if(ano < 1998)
{
alert("Erro no ano");
qtdE=1;
}
if(qtdE==0){
	document.form1.submit();
}


}
*/

 function mascara_cpf (campo,documento,f){
         var mydata = '';
         mydata = mydata + documento;
/*
         if (mydata.length == 3){
            mydata   = mydata + '.';

            ct_campo = eval("document."+f+"."+campo+".value = mydata");
            ct_campo;
         }

         if (mydata.length == 7){
                  mydata   = mydata + '.';

                  ct_campo = eval("document."+f+"."+campo+".value = mydata");
                  ct_campo;
         }

         if (mydata.length == 11){
            mydata      = mydata + '-';

            ct_campo1 = eval("document."+f+"."+campo+".value = mydata");
            ct_campo1;
         } */

        if (mydata.length < 11){

            alert("CPF incorreto!");
    		aux1 = eval("document."+f+"."+campo+".focus");
            aux2 = eval("document."+f+"."+campo+".value = ''");		 
     
	     }
		 
		 if ((mydata.length == 12)||(mydata.length == 13)){

            alert("CPF incorreto!");
    		aux1 = eval("document."+f+"."+campo+".focus");
            aux2 = eval("document."+f+"."+campo+".value = ''");		 
     
	     }
		 
		 else if (mydata.length == 11){

          valida_cpf(f,campo);
         }
   }

   function valida_cnpj(f,campo){
         pri = eval("document."+f+"."+campo+".value.substring(0,2)");
         seg = eval("document."+f+"."+campo+".value.substring(2,6)");
         ter = eval("document."+f+"."+campo+".value.substring(6,8)");
         qua = eval("document."+f+"."+campo+".value.substring(8,10)");
         qui = eval("document."+f+"."+campo+".value.substring(10,14)");

         var i;
         var numero;
         var situacao = '';

         numero = (pri+seg+ter+qua+qui);

         s = numero;

         c = s.substr(0,12);
         var dv = s.substr(12,2);
         var d1 = 0;


         for (i = 0; i < 12; i++){
            d1 += c.charAt(11-i)*(2+(i % 8));
         }

         if (d1 == 0){
            var result = "falso";
         }
            d1 = 11 - (d1 % 11);

         if (d1 > 9) d1 = 0;

            if (dv.charAt(0) != d1){
               var result = "falso";
            }

         d1 *= 2;
         for (i = 0; i < 12; i++){
            d1 += c.charAt(11-i)*(2+((i+1) % 8));
         }

         d1 = 11 - (d1 % 11);
         if (d1 > 9) d1 = 0;

            if (dv.charAt(1) != d1){
               var result = "falso";
            }


         if (result == "falso") {
            alert("CNPJ inválido!");
            aux1 = eval("document."+f+"."+campo+".focus");
            aux2 = eval("document."+f+"."+campo+".value = ''");

         }
   }

   function valida_cpf(f,campo){
         pri = eval("document."+f+"."+campo+".value.substring(0,3)");
         seg = eval("document."+f+"."+campo+".value.substring(3,5)");
         ter = eval("document."+f+"."+campo+".value.substring(5,9)");
         qua = eval("document."+f+"."+campo+".value.substring(9,11)");

         var i;
         var numero;

         numero = (pri+seg+ter+qua);
		 //numero = campo.value;

           //alert("document."+f+"."+campo+".value.substring(9,11)");

         s = numero;
         c = s.substr(0,9);
         var dv = s.substr(9,2);
         var d1 = 0;

         for (i = 0; i < 9; i++){
            d1 += c.charAt(i)*(10-i);
         }

         if (d1 == 0){
            var result = "falso";
         }

         d1 = 11 - (d1 % 11);
         if (d1 > 9) d1 = 0;

         if (dv.charAt(0) != d1){
            var result = "falso";
         }

         d1 *= 2;
         for (i = 0; i < 9; i++){
            d1 += c.charAt(i)*(11-i);
         }

         d1 = 11 - (d1 % 11);
         if (d1 > 9) d1 = 0;

         if (dv.charAt(1) != d1){
            var result = "falso";
         }

         if (result == "falso") {
            alert("CPF incorreto!");
            aux1 = eval("document."+f+"."+campo+".focus");
            aux2 = eval("document."+f+"."+campo+".value = ''");

         }
   }

// FUNÇãO HORA
function Mascara_Hora2(evento, objeto){ 

var keypress=(window.event)?event.keyCode:evento.which;
campo = eval (objeto);

var hora01 = ''; 
hora01 = hora01 + campo.value; 

if (campo.value == '00:00')
	{
		campo.value=""
	}

if (campo.value.length == 2){ 
hora01 = hora01 + ':'; 
campo.value = hora01;
Verifica_Hora();
} 
if (campo.value.length == 5){ 
Verifica_Hora(); 
} 
} 
           
function Verifica_Hora(){ 
//hrs = (document.form1.Hora.value.substring(0,2)); 
//min = (document.form1.Hora.value.substring(3,5)); 
hrs = (campo.value.substring(0,2)); 
min = (campo.value.substring(3,5)); 

               
estado = ""; 
if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59)){ 
estado = "errada"; 
} 
               
if (campo.value == "") { 
estado = "errada"; 
} 

if (estado == "errada") { 
alert("Hora incorreta!"); 
campo.value="00:00"; 
} 
} 

