// JavaScript Document
var msgerro = '';
function VAL(form)
{
	 this.form = form;
	 this.elementos = eval("document." + form + ".elements");
}
VAL.prototype = {
   inicio:        function(){
                     this.vez = "";
                     this.erro = false;
					 		this.busca();
	              },
	              
    f_blur:       function(){
                     this.erro = false;
                     switch(this.vez.getAttribute("type"))
							 {
								case 'text':
									this.f_text();
									break;
								case 'password':
									this.f_text();
									break;
							    case 'textarea':
									this.f_textarea();
									break;
								case 'radio':
									 this.f_radio();
									 break;
								case 'checkbox':
										 this.f_checkbox();
									break;
								case 'select':
									 this.f_select();
									 break;
								case 'listbox':
									 this.f_listbox();
									 break;
								case 'file':
									 this.f_file();
									 break;									 
							 }
                  },
	
	f_funcao:	  function(){
							if(this.vez.getAttribute("funcao")){
								if(!eval(this.vez.getAttribute("funcao") + '("' + this.vez.value + '","' + this.vez.id + '")')){
									this.erro = true;
								}
							}		
						},
    
	busca:        function(){
		                 
                              for(cont = 0;cont < this.elementos.length;cont++){
                                  this.vez = this.elementos[cont];
											 if(this.vez.getAttribute("val"))
                                  {
                                     switch(this.vez.getAttribute("type"))
												  {
														case 'text':
															 this.f_text();
															 break;
														case 'password':
															 this.f_text();
															 break;
														case 'textarea':
															 this.f_textarea();
															 break;
														case 'radio':
															 if(this.vez.getAttribute("primeiro") == "1")
																 this.f_radio();
															 break;
														 case 'checkbox':
															 if(this.vez.getAttribute("primeiro") == "1")
																this.f_checkbox();
															 break;
														 case 'select':
															 this.f_select();
															 break;
														 case 'listbox':
															 this.f_listbox();
															 break;
														case 'file':
															 this.f_file();
															 break;									 
															 
												 }
										}else if(this.vez.getAttribute("funcao")){
											this.f_funcao();
										}
							  }
							  
						},
	f_text: 			function(){
		             	     texto = "";
							 switch(this.vez.getAttribute("val"))
							 {
								  case '0':
								    if(this.vez.value.length == 0){
									    texto = "Campo de preenchimento obrigatório.";
                            }
							    break;
								  case '1':
								    if(this.vez.value.length != this.vez.getAttribute('cond')){
									    texto = "Preencha com " + this.vez.getAttribute('cond') + " caracteres.";
                                     }
								    break;
								  case '2':
								    if(this.vez.value.length < this.vez.getAttribute('cond')){
									   texto = "Preencha com no mínimo " + this.vez.getAttribute('cond') + " caracteres";
                                     }
								    break;
								  case '3':
									 if(this.vez.value.search(this.vez.getAttribute('expReg')) == -1){
									    texto = "Valor inválido.";
									 }
								    break;
								  case '4':
									 if(this.vez.value.search(/^([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/) == -1){
									    texto = "Horário inválido.";
									 }
								    break;
								  case '5':
									 if(this.vez.value.search(/^([a-z]|[A-Z]|[0-9]|[.]|[_])+[@]([a-z]|[A-Z]|[0-9])+[.]((([a-z]|[A-Z]){2,})|(([a-z]|[A-Z]){2,}[.]([a-z]|[A-Z]){2,}))$/) == -1){
									    texto = "Email inválido.";
									 }
								    break;
							 }
							 if((this.vez.getAttribute("cp") != '')&&(texto != '')){
								 msgerro += this.vez.getAttribute("cp")+": "+texto+"<br>";
							 }
							document.getElementById('e' + this.vez.id).innerHTML = texto;
							this.erro = (texto == "") ? this.erro : true;
						},
	 f_textarea:   function(){
	 					    texto = "";
							 switch(this.vez.getAttribute("val"))
							 {
								  case '0':
								    if(this.vez.value.length == 0){
									    texto = "Campo de preenchimento obrigatório.";   
									 }
									 break;
								  case '1':
								    if(!(this.vez.value.length > this.vez.getAttribute('cond') && this.vez.value.length < this.vez.getAttribute('cond2'))){
										 texto = "Preencha com no mínimo " + this.vez.getAttribute('cond') + " e no máximo " 
										       + this.vez.getAttribute('cond2') + " caracteres.";
									 }
									 break;
							 }
							 if((this.vez.getAttribute("cp") != '')&&(texto != '')){
								 msgerro += this.vez.getAttribute("cp")+": "+texto+"<br>";
							 }							
							document.getElementById('e' + this.vez.id).innerHTML = texto;
							this.erro = (texto == "") ? this.erro : true;
						},
	f_select:	   function(){
							 texto = "";
							 switch(this.vez.getAttribute("val"))
							 {
								  case '0':
								    if(this.vez.value.length == 0){
									    texto = "Selecione uma opção.";
									 }
								    break;
							 }
							 if((this.vez.getAttribute("cp") != '')&&(texto != '')){
								 msgerro += this.vez.getAttribute("cp")+": "+texto+"<br>";
							 }							
							document.getElementById('e' + this.vez.id).innerHTML = texto;
							this.erro = (texto == "") ? this.erro : true;
						},
	f_listbox:	   function(){
		 					 texto = "";
							 switch(this.vez.getAttribute("val"))
							 {
								 case '0':
							         quant = 0;
   								     for(cont2 = 0;cont2 < this.vez.options.length;cont2++)
	   							     {

										 if(this.vez.options[cont2].selected)
										     quant++;
		   						     }
									 if(quant < this.vez.getAttribute('cond')){
									 	  texto = "Selecione no mínimo " + this.vez.getAttribute('cond') + " campo(s).";
									 }
									 break;
								 case '1':
									 if(this.vez.options.length == 0){
									 	  texto = "O campo não possui nenhum item.";
									 }
									 break;
								 case '2':
							         quant = 0;
   								     for(cont2 = 0;cont2 < this.vez.options.length;cont2++)
	   							     {

										 if(this.vez.options[cont2].selected)
										     quant++;
		   						     }
									 if(quant != this.vez.getAttribute('cond')){
									 	  texto = "Selecione " + this.vez.getAttribute('cond') + " campo(s).";
									 }
									 break;
							 }
							 if((this.vez.getAttribute("cp") != '')&&(texto != '')){
								 msgerro += this.vez.getAttribute("cp")+": "+texto+"<br>";
							 }							
							document.getElementById('e' + this.vez.id).innerHTML = texto;
							this.erro = (texto == "") ? this.erro : true;
						},
   f_radio:       function(){
							 texto = "";
							 switch(this.vez.getAttribute("val"))
							 {
								 case '0':
								 	sel = false;
									for(cont2 = 0;cont2 < this.elementos.length;cont2++)
									{
                                         if(this.elementos[cont2].getAttribute("type") == "radio" &&
                                            this.vez.getAttribute("nome") == this.elementos[cont2].getAttribute("nome") &&
                                            this.elementos[cont2].checked)
                                         {
                                             sel = true;
                                             break;
                                         }
									}
									if(!sel)
									   texto = "Selecione algum item.";
							 }
							 if((this.vez.getAttribute("cp") != '')&&(texto != '')){
								 msgerro += this.vez.getAttribute("cp")+": "+texto+"<br>";
							 }							
							document.getElementById('e' + this.vez.id).innerHTML = texto;
							this.erro = (texto == "") ? this.erro : true;
						},
	f_hidden: 		function(){
		             	 texto = "";
							 switch(this.vez.getAttribute("val"))
							 {
								  case '0':
								    if(this.vez.value.length == 0){
									    texto = "Campo de preenchimento obrigatório.";
                                     }
								    break;
								  case '1':
								    if(this.vez.value.length != this.vez.getAttribute('cond')){
									    texto = "Preencha com " + this.vez.getAttribute('cond') + " caracteres.";
                                     }
								    break;
								  case '2':
								    if(this.vez.value.length < this.vez.getAttribute('cond')){
									   texto = "Preencha com no mínimo " + this.vez.getAttribute('cond') + " caracteres";
                                     }
								    break;
								  case '3':
									 if(this.vez.value.search(this.vez.getAttribute('expReg')) == -1){
									    texto = "Valor inválido.";
									 }
								    break;
							 }
							 if((this.vez.getAttribute("cp") != '')&&(texto != '')){
								 msgerro += this.vez.getAttribute("cp")+": "+texto+"<br>";
							 }							
							document.getElementById('e' + this.vez.id).innerHTML = texto;
							this.erro = (texto == "") ? this.erro : true;
						},
						
	f_checkbox:		function(){
							texto = "";
							switch(this.vez.getAttribute("val"))
							{
								case '0':
								tam   = this.elementos.length;
								quant = 0;
								for(cont2 = 0; cont2 < tam; cont2++){
								   if(this.elementos[cont2].getAttribute('grupo') == this.vez.getAttribute('grupo')){
										 if(this.elementos[cont2].type == "checkbox" && this.elementos[cont2].checked)
			 						       quant++;
								   }
								}
								if(quant < this.vez.getAttribute('cond'))
								{
									 texto = "Você deve selecionar ao menos " + this.vez.getAttribute('cond') + " itens.";
								}
								break;
							}
							 if((this.vez.getAttribute("cp") != '')&&(texto != '')){
								 msgerro += this.vez.getAttribute("cp")+": "+texto+"<br>";
							 }							
							document.getElementById('e' + this.vez.id).innerHTML = texto;
							this.erro = (texto == "") ? this.erro : true;
						},
	f_file: 			function(){
		             	     texto = "";
							 switch(this.vez.getAttribute("val"))
							 {
								  case '0':
								    if(this.vez.value.length == 0){
									    texto = "Selecione um arquivo.";
                                     }
								    break;
							 }
							document.getElementById('e' + this.vez.id).innerHTML = texto;
							this.erro = (texto == "") ? this.erro : true;
						}						
	
}



//Sem alert
function validacaonoalert(form)
{
	  var obj = new VAL(form);
	  obj.inicio();
     return !obj.erro;
}
function validacao_blur(form, elem)
{
     var obj = new VAL(form);
     obj.vez = elem;
     obj.f_blur();
     return !obj.erro;
}
function validacao_funcao(form, elem){
     var obj = new VAL(form);
     obj.vez = elem;
     obj.f_funcao();
     return !obj.erro;
}

function validacao(form)
{
	  var obj = new VAL(form);
	  obj.inicio();
	  
		if(obj.erro)
		{
         alert("Há erros no formulário!");
      }
      return !obj.erro;
}


