// JavaScript Document
var msgerro = '';
function VAL(form)
{
	 this.form = form;
	 this.elementos = eval("document." + form + ".elements");
}
VAL.prototype = {
   inicio:        function(){
   					 //iniciando variaveis
                     this.vez = "";
                     this.erro = false;
					 this.busca();
	              },

    f_blur:       function(){
                     this.erro = false;
					 
					 this.texto = '';
					 
                     //procurando pelo tipo do campo
                     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;
					 }
					 if(this.texto == "") {
					 	$('err_' + this.vez.id).innerHTML = '';
						$('err_' + this.vez.id).style.display = 'none';
					 } else {
						$('err_' + this.vez.id).innerHTML = this.texto;
						$('err_' + this.vez.id).style.display = 'block';
						this.erro = true;
					 }
                  },

	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"))
                            {
								 this.texto = "";
								 
								 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':
											 this.f_checkbox();
											 break;
										 case 'select':
											 this.f_select();
											 break;
										 case 'listbox':
											 this.f_listbox();
											 break;
								 }
								 
								 if(this.texto == '') {
								 	$('err_' + this.vez.id).innerHTML = '';
									$('err_' + this.vez.id).style.display = 'none';
								 } else {
									$('err_' + this.vez.id).innerHTML = this.texto;
									$('err_' + this.vez.id).style.display = 'block';
									this.erro = true;
								 }
								 
							}else if(this.vez.getAttribute("funcao")){
								this.f_funcao();
							}
						  }

						},
	f_text: 			function(){
							 switch(this.vez.getAttribute("val"))
							 {
								  case '0':
								    if(this.vez.value.length == 0){
									    this.texto = "Campo de preenchimento obrigatório.";
                            }
							    break;
								  case '1':
								    if(this.vez.value.length != this.vez.getAttribute('cond'))
									    this.texto = "Preencha com " + this.vez.getAttribute('cond') + " caracteres.";
								    break;
								  case '2':
								    if(this.vez.value.length < this.vez.getAttribute('cond'))
									   this.texto = "Preencha com no mínimo " + this.vez.getAttribute('cond') + " caracteres";
								    break;
								  case '3':
									 if(this.vez.value.search(this.vez.getAttribute('expReg')) == -1)
									    this.texto = "Valor inválido.";
								    break;
								  case '4':
									 if(this.vez.value != "" && this.vez.value.search(/^(((0[1-9]|[12][0-8]|19)\/(0[1-9]|1[012])|(29|30)\/(0[13-9]|1[012])|(31\/(0[13578]|10|12)))\/\d{4}|29\/02\/\d{2}([13579][26]|[02468][048]))$/) == -1)
									    this.texto = "Data inválida.";
								    break;
								  case '5':
									 if(this.vez.value.search(/^[\w\.-]+@\w+\.\w+(\.\w+)?$/) == -1)
									    this.texto = "Email inválido.";
								    break;
								  case '6':
								    if(this.vez.value.search(/^\w{3,18}$/) == -1)
								    	this.texto = "Login inválido. Use apenas letras, números ou underline, com no mínimo 3 e no máximo 18 caracteres."
								    break;
								  case '7':
								    if(this.vez.value.search(/^[\w-]{3,18}$/) == -1)
								    	this.texto = "Senha inválida. Use apenas letras, números, underline ou hífen, com no mínimo 3 e no máximo 18 caracteres."
								    break;
								 case '8':
								    if(this.vez.value != $(this.vez.getAttribute('cond')).value)
								    	this.texto = "A senha de confirmação não é igual a senha informada. Digite a mesma senha nos dois campos."
								    break;
							 }
						},
	 f_textarea:   function(){
							 switch(this.vez.getAttribute("val"))
							 {
								  case '0':
								    if(this.vez.value.length == 0){
									    this.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'))){
										 this.texto = "Preencha com no mínimo " + this.vez.getAttribute('cond') + " e no máximo "
										       + this.vez.getAttribute('cond2') + " caracteres.";
									 }
							 }
						},
	f_select:	   function(){
							 switch(this.vez.getAttribute("val"))
							 {
								  case '0':
								    if(this.vez.value.length == 0){
									    this.texto = "Selecione uma opção.";
									 }
								    break;
							 }
						},
	f_listbox:	   function(){
							 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')){
									 	  this.texto = "Selecione no mínimo " + this.vez.getAttribute('cond') + " campo(s).";
									 }
									 break;
								 case '1':
									 if(this.vez.options.length == 0){
									 	  this.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')){
									 	  this.texto = "Selecione " + this.vez.getAttribute('cond') + " campo(s).";
									 }
									 break;
							 }
						},
   f_radio:       function(){
							 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)
									   this.texto = "Selecione algum item.";
							 }
						},
	f_checkbox:		function(){
							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'))
									{
										 this.texto = "Você deve selecionar ao menos " + this.vez.getAttribute('cond') + " itens.";
									}
								break;
								case '1':
									if(!this.vez.checked) {
										this.texto = "Você deve aceitar os termos de compromisso para poder se cadastrar."
									}
								break;
							}
						}

}
//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)
		{
         abrirJanela("Há erros no formulário!");
      }
      return !obj.erro;
}
