var campos = new Array();
var corCampoAtual = '';
function define(nome,tipo,label,min,max,req,d){
	var p;
	var i;
	var x;
	if(!d) d=document;
	if((p=nome.indexOf("?"))>0&&parent.frames.length){
		d=parent.frames[nome.substring(p+1)].document;
		nome=nome.substring(0,p);
	}
	if(!(x=d[nome])&&d.all) x=d.all[nome];

	for (i=0;!x&&i<d.forms.length;i++){
		x=d.forms[i][nome];
	}
	for(i=0;!x&&d.layers&&i<d.layers.length;i++){
		x=define(nome,tipo,label,min,max,req,d.layers[i].document);
		return x;
	}

	eval("V_"+nome+" = new formResult(x,tipo,label,min,max,req);");
	campos[eval(campos.length)] = eval("V_"+nome);
}

function formResult(form,tipo,label,min,max,req){
	this.form = form;
	this.tipo = tipo.toUpperCase();
	this.label = label;
	this.min  = min;
	this.max  = max;
	this.req = req;
}



function validacao(campo,tipo,label,min,max,req){
	var i = 0;
	var aux = '';
	var msg = '';
	var msgCab = 'O seguinte erro foi encontrado no formulário:\n\n';
	var valor = campo.value;
	if (campo.style.color == "rgb(255,0,0)") campo.style.color = corCampoAtual;
	tipo = tipo.toUpperCase();
	if (tipo == 'TIT') {
		aux = validatit(valor,req);
		if (aux != '') {
			msg = 'O campo '+label+' '+aux+'\n';
		}else {
			campo.value = mascara(campo,'###.###.###-##');
		}
	}else if (tipo == 'CPF') {
		aux = validacpf(valor,req);
		if (aux != '') {
			msg = '* O campo '+label+' é '+aux+'\n';
		}else {
			campo.value = mascara(campo,'###.###.###-##');
		}
	}else if (tipo == 'CNPJ') {
		aux = validacnpj(valor,req);
		if (aux != '') {
			msg += '* O campo '+label+' é '+aux+'\n';
		}else {
			thisshit = tiraSimb(campo.value);
			if (thisshit.length == 14)
			campo.value = mascara(campo,'##.###.###/####-##')
			else
			campo.value = mascara(campo,'###.###.###/####-##');
		}
	}else if (tipo == 'DATA') {
		aux = formataData(campo,req);
		if (aux != '') {
			msg += '* O campo '+label+' '+aux+'\n';
		}
	}else if (tipo == 'NUMERO') {
		aux = validaNumero(campo,req);
		if (aux != '') {
			msg += '* O campo '+label+' '+aux+'\n';
		}
	}else if (tipo == 'HORA') {
		aux = formataHora(campo,req);
		if (aux != '') {
			msg += '* O campo '+label+' '+aux+'\n';
		}
	}else if (tipo == 'EMAIL') {
		aux = validaemail(valor,req);
		if (aux != '') {
			msg += '* O campo '+label+' é '+aux+'\n';
		}
	}else if (tipo == 'CEP') {
		aux = validacep(valor,min,max,req);
		if (aux != '') {
			if (aux == max) {
				msg += '* '+aux+'\n';
			}else {
				msg += '* O campo '+label+' é '+aux+'\n';
			}
		}else {
			campo.value = mascara(campo,'#####-###');
		}
	}else if (tipo == 'TELEFONE') {
		aux = tiraSimb(campo.value);
			if (aux != '') {
				if(aux.length == 10)
				campo.value = mascara(campo,'(##)####-####');
				else if(aux.length == 8)
				campo.value = mascara(campo,'####-####');
				else{
					msg += '* O campo '+label+' é inválido;\n';
				}
			}

	}
	if (msg != '') {
		corCampoAtual = campo.style.color;
		alert(msgCab+msg+"\n");
		campo.style.color = "rgb(255,0,0)";
		return false;
	}
}

function tiraSimb (valorFormatado){
	var numero = '0123456789';
	var tam = valorFormatado.length;
	var aux = '';
	var ret = '';
	for (var i = 0; i < tam; i++){
		aux = valorFormatado.charAt(i);
		if (numero.indexOf(aux) != -1){
			ret += aux;
		}
	}
	return (ret);
}

function validacaoReq()
{
	var i      = 0;
	var aux    = 0;
	var msg    = '';
	var msgCab = 'O formulário tem os seguintes erros:\n\n';
//alert(campos);  
	for( i = 0; i < campos.length; i++ )
  {
		label = campos[i].label;
		tipo  = campos[i].tipo;
		req   = campos[i].req;
		campo	= campos[i].form;
		valor = campo.value;
		min   = campos[i].min;
		max   = campos[i].max;
    
		if( ( req ) && ( valor == "" ) ) 
    {
			msg += "- O campo " + label + " é de preenchimento obrigatório;\n";
			if (aux == 0) 
      {
        aux = i;
      }// End if
		}else
    {
		  if (tipo == 'TIT') 
      {
			  aux = validatit(valor,req);
			  if (aux != '') 
        {
				  msg += '- O campo '+label+' é inválido;\n';
			  }else 
        {
				  campo.value = mascara(campo,'###.###.###-##');
			  }
		  }else if (tipo == 'CPF') 
      {
			  aux = validacpf(valor,req);
			  if (aux != '') 
        {
				  msg += '- O campo '+label+' é inválido;\n';
			  }else 
        {
				  campo.value = mascara(campo,'###.###.###-##');
			  }
		  }else if (tipo == 'CNPJ') 
      {
			  aux = validacnpj(valor,req);
			  if (aux != '') 
        {
				  msg += '- O campo '+label+' é inválido;\n';
			  }else 
        {
				  thisshit = tiraSimb(campo.value);
				  if (thisshit.length == 14)
          {
				    campo.value = mascara(campo,'##.###.###/####-##')
          }
				else
				campo.value = mascara(campo,'###.###.###/####-##');
			  }
		  }else if (tipo == 'DATA') 
      {
			  aux = formataData(campo,req);
			  if (aux != '') 
        {
				  msg += '- O campo '+label+' é inválido;\n';
			  }
		  }else if (tipo == 'TELEFONE') 
      {
			  aux = tiraSimb(campo.value);
			  if (aux != '') 
        {
				  if(aux.length == 10)
          {
				    campo.value = mascara(campo,'(##)####-####');
				  }else if(aux.length == 8)
          {
				    campo.value = mascara(campo,'####-####');
				  }else
          {
					  msg += '* O campo '+label+' é inválido;\n';
				  }
			  }
		  }else if (tipo == 'NUMERO') 
      {
			  /*aux = validaNumero(campo,req);
			  if (aux != '') {
			  msg += '- O campo '+label+' é inválido;\n';
			  }*/
		  }else if (tipo == 'HORA') 
      {
			  aux = formataHora(campo,req);
			  if (aux != '') 
        {
				  msg += '- O campo '+label+' é inválido\n';
			  }
		  }else if (tipo == 'EMAIL') 
      {
			  aux = validaemail(valor,req);
			  if (aux != '') 
        {
				  msg += '- O campo '+label+' é inválido\n';
			  }
		  }else if (tipo == 'CEP') 
      {
			  aux = validacep(valor,min,max,req);
			  if (aux != '') 
        {
				  if (aux == max) 
          {
					  msg += '- O campo ' + label + ' é inválido\n';
				  }else 
          {
					  msg += '- O campo ' + label + ' é inválido\n';
				  }
			  }else 
        {
				  campo.value = mascara(campo,'#####-###');
			  }
		  }else 
      {
			  if ((req) && (valor=="")) 
        {
				  msg = '* O campo '+label+' é obrigatório\n';
			  }
		  }
    }// End if()
	}// End for()
  
	if (msg != '') 
  {
		alert( msgCab + msg + '\n\nPreencha os dados para continuar !' );
		return false;
	}// End if()
	return true;
}
