source: trunk/workflow/inc/report/includes/js/validacoes.js @ 5307

Revision 5307, 25.5 KB checked in by pereira.jair, 12 years ago (diff)

Ticket #2416 - Inclusao da nova ferramenta de relatorios do workflow.

Line 
1/**
2 * Função:     submeter(acao, prefixo)
3 * Data:       12/12/2008
4 * Descrição:  Função utilizada para validar os campos obrigatórios de um formulário
5                        Foi criada no intuito de agiizar o desenvolvimento de formulários de pesquisa
6                        Exige que os campos possuam um prefixo passado por parametro e o prefixo deve possuir 4 caracteres
7                        Exige que o formulario possua o nome frm
8                        Trabalha validando o preenchimento dos campos onde há o prefixo passado
9                        e submete o frm em caso de sucesso, caso contrario alerta e muda a cor de fundo dos campos
10 * Chamada:     onClick="submeter('pesquisar','pesq');"
11 *     
12 */
13 
14 
15 /**
16 * Criado em: 04/03/2009
17 *
18 *     
19 *      Função. valida(form,acao,submete,obrigatorio);
20 *
21 *      @param form = objeto forumlario que deseja validar;
22 *      @param acao = valor que deseja que seja setado no input de ação
23 *      @param submete = valor (true ou false) para dizer se o script vai submeter o formulario
24 *      @param obrigatorio = campo opcional que altera o default de 'nao obrigatorios' com o valor (_not)
25 *                                              para 'obrigatorios' com o valor passado
26 *
27 *
28 * Script tem como funcionalidade validar todos os inputs do formulario
29 *
30 *  - Compativel com: Firefox 3, Internet Explorer 7, Konqueror 3.5, Chrome 1.0, Opera 9
31 *
32 *      NOTAS IMPORTANTES
33 *
34 *      - para o script identificar a acao do form, o input tipo hidden 'acao' deve possuir o um id com o prefixo do form
35 *    em que ele está, da seguinte maneira 'id_form'+'_acao'. Ex: <input type="hidden" id="frm1_acao" name="frm1_acao"> ;
36 *
37 *  - para o script identificar os blocos de checkboxes, os checkboxes devem estar dentro de um <div> identificado da seguinte forma:
38 *        o id do div deve ser composto de 'id_form'+_div_ckbox+'id_qualquer'. Ex: <div id="frm1_div_ckbox_10">
39 *        os checkboxes devem estar dentro do div da seguinte maneira:
40 *        Ex: <div id="frm1_div_ckbox_10">
41 *                      <input type="checkbox" id="id_qualquer" value="1">
42 *                      <input type="checkbox" id="id_qualquer2" value="2">
43 *                      <input type="checkbox" id="id_qualquer3" value="3">
44 *                </div>
45 *
46 *   - para o script identificar os blocos de radio, os radios devem estar dentro de um <div> identificado da seguinte forma:
47 *        o id do div deve ser composto de 'id_form'+_div_radio+'id_qualquer'. Ex: <div id="frm1_div_radio_11">
48 *        os checkboxes devem estar dentro do div da seguinte maneira:
49 *        Ex: <div id="frm1_div_radio_11">
50 *                      <input type="radio" name="rad1" id="id_qualquer1" value="1" >
51 *                      <input type="radio" name="rad1" id="id_qualquer2" value="2">
52 *                      <input type="radio" name="rad1" id="id_qualquer3" value="3">
53 *                </div>
54 *
55 *
56 *
57 *
58 */
59
60
61function submeter(acao, prefixo)
62{
63    var pode_enviar = true;
64    var achou = false;
65    for ( var i = 0; i < document.frm.elements.length; i++ ) {
66        obj = document.frm.elements[i];
67        if (obj.name.substring(0,4) == prefixo && obj.getAttribute('obrigatorio') == 1) {
68            if (obj.type == 'text' || obj.type == 'textarea' || obj.type == 'select') {
69                obj.style.background = "#FFFFFF";
70                if (obj.value == '') {
71                    obj.style.background = "#FDF3D9";
72                   
73                    pode_enviar = false;
74                }
75            }
76        }
77    }
78    if (pode_enviar == true) {
79        $id('acao').value = acao;
80        $id('frm').submit();
81    } else {
82        cria_msg_html('div_msg','Existem campos obrigatórios não preenchidos.');
83        return;
84    }
85}
86
87/* funcao de validar */
88function valida(form,acao,submete,obrigatorio){
89   
90    /* declaração de variaveis */
91    var erro = 0;
92    var x = 0 ;
93    var debug = 1;
94   
95    ckbox_a_lista = new Array ;
96    ckbox_a_div = new Array;
97    ckbox_a_error = new Array;
98   
99    /* variavel que seta o sufixo para nao obrigatorio */
100    var n_obri = 'not_';
101   
102    limpaCores();
103   
104    / * alerta de erro se não tiver form informado */
105      if(form.length == 0){
106        alert('ERRO: form inválido informado');
107        return false;
108    }
109   
110    for (i=0; i<form.elements.length ;i++){
111   
112        /* todos os checkboxes de listagem */
113        if(form.elements[i].type == 'checkbox' || form.elements[i].type == 'radio'){
114         
115            if( form.elements[i].className.substr(0,11) == 'obrigatorio'){
116             
117                if(!ckbox_a_lista[form.elements[i].name.substr(0,form.elements[i].name.length-2)]){
118                   
119                    ckbox_a_lista[form.elements[i].name.substr(0,form.elements[i].name.length-2)] = 0;
120                       
121                    }
122               
123                if(document.getElementById( form.elements[i].id ).checked == true){
124               
125                    ckbox_a_lista[form.elements[i].name.substr(0,form.elements[i].name.length-2)] = 1;
126                   
127                }
128
129            }
130             
131        }
132           
133        /* todos os inputs text */
134        if(form.elements[i].type == 'text'){
135         
136            if(debug == 1){
137                / * alerta de erro: se o elemento nao possuir um id ele grita * /
138                if(!form.elements[i].id){
139                    alert('Para validação input '+form.elements[i].name+' precisa possuir um id');
140                }
141            }
142             
143            /*so valida se tiver id*/
144            if(form.elements[i].id){
145                /* verificando se o parametro de string obrigatorio foi passado */
146                if(obrigatorio){
147                    /* verificando se é obrigatorio a partir da string de obrigatorio setada*/             
148                    if( (form.elements[i].id.substr(0,obrigatorio.length)) == obrigatorio){
149                         
150                        if(form.elements[i].value.length < 1){
151                            muda_cor(form.elements[i].id);
152                            erro = 1;
153                        }else{
154                            remove_cor(form.elements[i].id);
155                        }   
156                    }
157                               
158                }else{
159
160                    /* verificando se é obrigatorio */
161                    if( (form.elements[i].id.substr(0,n_obri.length)) != n_obri){
162                                             
163                        if(form.elements[i].value.length < 1){
164                            muda_cor(form.elements[i].id);
165                            erro = 1;
166                        }else{
167                            remove_cor(form.elements[i].id);
168                        }
169                    }
170                }
171            }       
172        }
173         
174
175        /* todos os selects */
176        if(form.elements[i].type == 'select-one'){
177           
178            if(debug == 1){
179                / * alerta de erro: se o elemento nao possuir um id ele grita * /
180                if(!form.elements[i].id){
181                    alert('Para validação input '+form.elements[i].name+' precisa possuir um id');
182                }
183            }
184           
185            /*so valida se tiver id*/
186            if(form.elements[i].id){
187                 
188                /* verificando se o parametro de string obrigatorio foi passado */
189                if(obrigatorio){
190                   
191                    /* verificando se é obrigatorio a partir da string de obrigatorio setada*/     
192                    if( (form.elements[i].id.substr(0,obrigatorio.length)) == obrigatorio){
193                         
194                        if(form.elements[i].value == 0 || form.elements[i].value == ""){
195                            muda_cor(form.elements[i].id);
196                            erro = 1;
197                        }else{
198                            remove_cor(form.elements[i].id);
199                        }   
200                    }
201                               
202                }else{
203                   
204                    /* verificando se é obrigatorio */
205                    if( (form.elements[i].id.substr(0,n_obri.length)) != n_obri){
206                        if(form.elements[i].value == 0 || form.elements[i].value == ""){
207                            muda_cor(form.elements[i].id);
208                            erro = 1;
209                        }else{
210                            remove_cor(form.elements[i].id);
211                        }               
212                    }
213                }
214            }
215        }
216         
217        /* todos os inputs textarea */
218        if(form.elements[i].type == 'textarea'){
219            if(debug == 1){
220                / * alerta de erro: se o elemento nao possuir um id ele grita * /
221                if(!form.elements[i].id){
222                    alert('Para validação input '+form.elements[i].name+' precisa possuir um id');
223                }
224            }
225           
226            /*so valida se tiver id*/
227            if(form.elements[i].id){
228                 
229                if(obrigatorio){
230                   
231                    /* verificando se é obrigatorio a partir da string de obrigatorio setada*/             
232                    if( (form.elements[i].id.substr(0,obrigatorio.length)) == obrigatorio){
233                       
234                        if(form.elements[i].value.length < 1){
235                            muda_cor(form.elements[i].id);
236                            erro = 1;
237                        }else{
238                            remove_cor(form.elements[i].id);
239                        }   
240                    }
241                               
242                }else{
243                   
244                    /* verificando se é obrigatorio */
245                    if( (form.elements[i].id.substr(0,n_obri.length)) != n_obri){
246                        if(form.elements[i].value.length < 1){
247                            muda_cor(form.elements[i].id);
248                            erro = 1;
249                        }else{
250                            remove_cor(form.elements[i].id);
251                        }
252                    }
253                }
254            }               
255        }
256    }
257     
258     // olhá só isso
259    for(i_a in ckbox_a_lista){
260     
261        //alert('KEY' + i_a + ' - VALOR ' + ckbox_a_lista[i_a]);
262     
263        if(ckbox_a_lista[i_a] == 0){
264           
265            g=0;
266            while(document.getElementById('td_'+i_a+'_'+g)){
267                muda_cor('td_'+i_a+'_'+g)
268                g++;
269            }
270            erro = 1;
271       
272        }else{
273       
274            if(ckbox_a_lista[i_a] == 1){       
275           
276                        g=0;
277                    while(document.getElementById('td_'+i_a+'_'+g)){
278                        remove_cor('td_'+i_a+'_'+g)
279                        g++;
280                    }
281           
282            }
283               
284        }
285    }
286     
287
288    / * checkboxes e radios * /
289     
290    // capturando todos os divs
291    ckbox_a_div = document.getElementsByTagName('div');
292     
293    for(i=0;i<ckbox_a_div.length;i++){
294   
295        / * checkboxes e radios* /
296       
297        /* verificando se são obrigatórios e se tem parametro de obrigatorio */
298        if(obrigatorio){
299   
300            // verificando se é o div a ser checado
301            if( ckbox_a_div[i].id.substr(0, (11 + form.id.length + obrigatorio.length )) == obrigatorio+"_"+form.id+"_div_ckbox" || ckbox_a_div[i].id.substr(0, (11 + form.id.length + obrigatorio.length )) == obrigatorio+"_"+form.id+"_div_radio") {
302               
303                if((ckbox_a_div[i].id.substr(0,obrigatorio.length)) == obrigatorio){
304                           
305                    /* verificando se os checkboxes são do mesmo form */   
306                    if( ckbox_a_div[i].id.substr((obrigatorio.length+1),form.id.length) == form.id){
307                           
308                        ckbox_a_child = document.getElementById(ckbox_a_div[i].id).childNodes;
309                        ckbox_a_error[ckbox_a_div[i].id] = 0;
310                             
311                            for(j=0;j<ckbox_a_child.length;j++){
312                                 
313                                if(ckbox_a_child[j].nodeName == "INPUT"){
314                                 
315                                    //CHECKBOX
316                                    if(ckbox_a_child[j].getAttribute("type") == "checkbox"){
317                                        if(document.getElementById(ckbox_a_child[j].id).checked == true){
318                                            ckbox_a_error[ckbox_a_div[i].id] = 1;
319                                            break;           
320                                        }   
321                                    }
322                                   
323                                    //RADIO BUTTON
324                                    if(ckbox_a_child[j].getAttribute("type") == "radio"){
325                                        if(document.getElementById(ckbox_a_child[j].id).checked == true){
326                                            ckbox_a_error[ckbox_a_div[i].id] = 1;
327                                            break;           
328                                        }   
329                                    }
330                                     
331                                }else{
332                                 
333                                    /* Se o CHILD não for do tipo INPUT, ele busca RECURSIVAMENTE ate achar o INPUT */
334                                    if(ckbox_a_child[j].nodeName == "TABLE"){
335                                           
336                                        if(ckbox_a_child[j].hasChildNodes()){
337                                               
338                                            for(t=0;t<ckbox_a_child[j].childNodes.length;t++){
339                                                   
340                                                if(ckbox_a_child[j].childNodes[t].nodeName == "TBODY"){
341                                                   
342                                                    for(q=0;q<ckbox_a_child[j].childNodes[t].childNodes.length;q++){
343                                                       
344                                                        if(ckbox_a_child[j].childNodes[t].childNodes[q].nodeName == "TR"){
345                                                                   
346                                                            for(w=0;w<ckbox_a_child[j].childNodes[t].childNodes[q].childNodes.length;w++){
347                                                                   
348                                                                if(ckbox_a_child[j].childNodes[t].childNodes[q].childNodes[w].nodeName == "TD"){
349                                                                       
350                                                                    for(s=0;s<ckbox_a_child[j].childNodes[t].childNodes[q].childNodes[w].childNodes.length;s++){
351                                                                           
352                                                                        if(ckbox_a_child[j].childNodes[t].childNodes[q].childNodes[w].childNodes[s].nodeName == "INPUT"){
353                                                                           
354                                                                            a_tmp = ckbox_a_child[j].childNodes[t].childNodes[q].childNodes[w].childNodes[s]; 
355                                                                               
356                                                                            //CHECKBOX E RADIO
357                                                                            if(a_tmp.getAttribute("type") == "checkbox" || a_tmp.getAttribute("type") == "radio"){
358                                                                                   
359                                                                                if(document.getElementById(a_tmp.id).checked == true){
360                                                                                    ckbox_a_error[ckbox_a_div[i].id] = 1;
361                                                                                }   
362                                                                            }
363                                                                        }
364                                                                    }
365                                                                }
366                                                            }
367                                                        }
368                                                    }
369                                                }
370                                            }
371                                        }                         
372                                    }
373                                }
374                            }
375                             
376                            if(ckbox_a_error[ckbox_a_div[i].id] == 0){
377                                muda_cor(ckbox_a_div[i].id)
378                                erro = 1;
379                            }else{
380                                remove_cor(ckbox_a_div[i].id);
381                            }
382                           
383                            x ++ ;
384                         
385                        }
386                    }
387                }
388               
389            }else{
390               
391                if( ckbox_a_div[i].id.substr(0, (10 + form.id.length )) == form.name+"_div_ckbox" || ckbox_a_div[i].id.substr(0, (10 + form.id.length  )) == form.name+"_div_radio") {
392               
393                    if((ckbox_a_div[i].id.substr(0,n_obri.length)) != n_obri){
394                       
395                        /* verificando se os checkboxes são do mesmo form */   
396                        if( ckbox_a_div[i].id.substr(0,form.id.length) == form.id){
397                           
398                            ckbox_a_child = document.getElementById(ckbox_a_div[i].id).childNodes;
399                            ckbox_a_error[ckbox_a_div[i].id] = 0;
400                           
401                            for(j=0;j<ckbox_a_child.length;j++){
402                             
403                                if(ckbox_a_child[j].nodeName == "INPUT"){
404                                   
405                                    //CHECKBOX
406                                    if(ckbox_a_child[j].getAttribute("type") == "checkbox" || ckbox_a_child[j].getAttribute("type") == "radio"){
407                                       
408                                        if(document.getElementById(ckbox_a_child[j].id).checked == true){
409                                            ckbox_a_error[ckbox_a_div[i].id] = 1;
410                                            break;         
411                                        }   
412                                    }
413                                    /*  //RADIO BUTTON
414                                    if(ckbox_a_child[j].getAttribute("type") == "radio"){
415                                        if(document.getElementById(ckbox_a_child[j].id).checked == true){
416                                            ckbox_a_error[ckbox_a_div[i].id] = 1;
417                                            break;         
418                                        }   
419                                    }*/
420                                   
421                                }else{
422                                 
423                                    if(ckbox_a_child[j].nodeName == "TABLE"){
424                                           
425                                        if(ckbox_a_child[j].hasChildNodes()){
426                                           
427                                            for(t=0;t<ckbox_a_child[j].childNodes.length;t++){
428                                               
429                                                if(ckbox_a_child[j].childNodes[t].nodeName == "TBODY"){
430                                               
431                                                    for(q=0;q<ckbox_a_child[j].childNodes[t].childNodes.length;q++){
432                                                   
433                                                        if(ckbox_a_child[j].childNodes[t].childNodes[q].nodeName == "TR"){
434                                                               
435                                                            for(w=0;w<ckbox_a_child[j].childNodes[t].childNodes[q].childNodes.length;w++){
436                                                               
437                                                                if(ckbox_a_child[j].childNodes[t].childNodes[q].childNodes[w].nodeName == "TD"){
438                                                                   
439                                                                    for(s=0;s<ckbox_a_child[j].childNodes[t].childNodes[q].childNodes[w].childNodes.length;s++){
440                                                                       
441                                                                        if(ckbox_a_child[j].childNodes[t].childNodes[q].childNodes[w].childNodes[s].nodeName == "INPUT"){
442                                                                       
443                                                                            a_tmp = ckbox_a_child[j].childNodes[t].childNodes[q].childNodes[w].childNodes[s]; 
444                                                                           
445                                                                            //CHECKBOX E RADIO
446                                                                            if(a_tmp.getAttribute("type") == "checkbox" || a_tmp.getAttribute("type") == "radio"){
447                                                                               
448                                                                                if(document.getElementById(a_tmp.id).checked == true){
449                                                                                    ckbox_a_error[ckbox_a_div[i].id] = 1;
450                                                                                }   
451                                                                            }
452                                                                        }
453                                                                    }
454                                                                }
455                                                            }
456                                                        }
457                                                    }
458                                                }
459                                            }
460                                        }                       
461                                    }
462                                }
463                            }
464
465                            if(ckbox_a_error[ckbox_a_div[i].id] == 0){
466                                muda_cor(ckbox_a_div[i].id)
467                                erro = 1;
468                            }else{
469                                remove_cor(ckbox_a_div[i].id);
470                            }
471                           
472                            x ++ ;
473                           
474                        }
475                    }
476                }
477            }
478        }
479     
480        if(erro == 1){
481         
482            if(!document.getElementById("mess")){
483                criarDiv('mess', '<table height=\"32\" width=\"100%\" valign=\"middle\"><tr onclick=\"removeDiv(\'mess\');\"><td class="\msg\" width=\"96%\" heigth=\"100%\">&nbsp;<font color=\"#CD0000\">Existem campos obrigatórios não preenchidos!</font></td><td width=\"4%\"><img width=\"15\" height=\"15\" src=\"images/X.jpg\"></img></td></tr></table>', '100%', '34', '0', '0');
484                alinhaDiv('mess');
485            }
486         
487            id_interval = setInterval("alinhaDiv('mess')",500);
488                   
489            fade(0,'mess',80);
490           
491            return false;
492         
493        }else{
494         
495            if(document.getElementById("mess")){
496                removeDiv('mess');
497            }
498         
499            / * alerta de erro se nao encontrar input de acao * /
500            if(document.getElementById(form.id+'_acao').length == 0){
501                alert('ERRO: erro ao definir ação. Input '+form.id+'_acao não existe');
502            }
503         
504            document.getElementById(form.id+'_acao').value = acao;
505         
506            /* se o parametro for true é para submeter o form */
507            if(submete == true || submete == 1){
508                form.submit();
509            }else{
510                return true;
511            }
512        }
513}
514
515function diferencaEntreDatas(dataMaior,dataMenor) {
516    Data = dataMaior.replace( " ", "" );
517    dia1 = Data.substring( 0, 2 );
518    dia1 = parseInt(dia1,10)+0;
519    mes1 = Data.substring(3,5);
520    mes1 = parseInt(mes1,10)-1;
521    ano1 = Data.substring(6,10);
522    ano1 = parseInt(ano1,10)+0;
523
524    Data = dataMenor.replace( " ", "" );
525    dia2 = Data.substring( 0, 2 );
526    dia2 = parseInt(dia2,10)+0;
527    mes2 = Data.substring(3,5);
528    mes2 = parseInt(mes2,10)-1;
529    ano2 = Data.substring(6,10);
530    ano2 = parseInt(ano2,10)+0;
531
532    return ( (Date.UTC(ano1,mes1,dia1,0,0,0)-Date.UTC(ano2,mes2,dia2,0,0,0)) / 86400000);
533}
534
535
536function validarPeriodo(dataInicial, dataFinal) {
537    diferenca = diferencaEntreDatas(dataFinal.value,dataInicial.value);
538    var array_tmp = dataInicial.value.split('/');
539    var dia_ini = array_tmp[0];
540    var mes_ini = array_tmp[1];
541    var ano_ini = parseInt(array_tmp[2]);
542   
543    if (dataFinal.value.length == 10) {
544        array_tmp = dataFinal.value.split('/');
545        var dia_fim = array_tmp[0];
546        var mes_fim = array_tmp[1];
547        var ano_fim = parseInt(array_tmp[2]);
548        // diferença de um ano
549        var ano = diferencaEntreDatas(dataFinal.value,dia_fim+'/'+mes_fim+'/'+(ano_fim-1));
550   
551        if((ano_ini!=ano_fim && ano_fim<ano_ini) || (ano_ini==ano_fim && mes_ini>mes_fim) || (ano_ini==ano_fim && mes_ini==mes_fim && dia_ini>dia_fim)){
552            dataFinal.value="";
553            return false;
554        }
555    }
556    return true;
557}
Note: See TracBrowser for help on using the repository browser.