/* versao 2.3 - 10-04-2006 - compatível com Firefox e IE */

var erro = 'Os seguintes campos estão incompletos ou com erro:\n\n';
var temErro = 0;
var campo_erro = '';
/*************************************************************
// CRIACAO DOS ARRAYS
//*************************************************************/
var campo = new Array();
var apelido = new Array();
var requerido = new Array();
var formato = new Array();
var minimo = new Array();
var maximo = new Array();
var numMin = new Array();
var numMax = new Array();
var igual = new Array();
var mensagem = new Array();

/************************************************************ 
funcoes necessarias
*************************************************************/
function limpa_string(S){
 // Deixa so' os digitos no numero
 var Digitos = "0123456789";
 var temp = "";
 var digito = "";

 for (var i=0; i < S.length; i++) {
  digito = S.charAt(i);
  if (Digitos.indexOf(digito)>=0) {
   temp=temp+digito }
 } //for

 return temp
}

function valida_CPF(s) {
 var i;
 s = limpa_string(s);
 var 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) return false;
 d1 = 11 - (d1 % 11);
 if (d1 > 9) d1 = 0;
 if (dv.charAt(0) != d1)
 {
  return false;
 }

 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)
 {
  return false;
 }
        return true;
}

function valida_CNPJ(s) {
 var i;
 s = limpa_string(s);
 var 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) return false;
        d1 = 11 - (d1 % 11);
 if (d1 > 9) d1 = 0;
 if (dv.charAt(0) != d1)
 {
  return false;
 }

 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)
 {
  return false;
 }
 return true;
}

function validaForm(parametro)
{
var contador=0;
while (contador < campo.length)
{// do while
NomefObj = document.getElementById(campo[contador]).name;


/************************************************************ 
Definir o campo a ser mostrado na mensagem de erro.
*************************************************************/
if ((typeof (apelido[contador]) == 'undefined') || (apelido[contador] == ''))
{
campo_erro = campo[contador];
}
else
{
campo_erro = apelido[contador];
}

/*######################################################################
// testar se é requerido
//#####################################################################*/

if (requerido[contador] == 'Sim')
{
// é requerido, ver se o campo esta vazio
if (document.getElementById(NomefObj).value == '')
{
temErro = 1;
if (mensagem[contador] == '')
{
erro = erro + '* ' + campo_erro + ' - O Campo não pode ser vazio.\n';
}
else
{
erro = erro + '* ' + campo_erro +  ' - ' + mensagem[contador] + '\n';
}
}
}

/*######################################################################
// testar se tem mínimo
//######################################################################*/

if ((minimo[contador] == 'Sim') && (document.getElementById(NomefObj).value != ''))
{
var caracteres = document.getElementById(NomefObj).value;
if (caracteres.length <  numMin[contador])
{
temErro = 1;
erro = erro + '* ' + campo_erro + ' - O Campo deve ter no mínimo '+ numMin[contador] + ' Caracteres.\n';
} 
}


/*######################################################################
// testar se tem maximo
//######################################################################*/

if ((maximo[contador] == 'Sim') && (document.getElementById(NomefObj).value != ''))
{
var caracteres = document.getElementById(NomefObj).value;
if (caracteres.length >  numMax[contador])
{
temErro =1;
erro = erro + '* ' + campo_erro + ' - O Campo deve ter no máximo '+ numMax[contador] + ' Caracteres.\n';
}
}

/* ######################################################################
// testar se é email
//####################################################################### */
temErroEmail = 0;
var erroemail = '';
var pos;
if ((formato[contador]=='Email') && (document.getElementById(NomefObj).value != ''))
{
emailStr= document.getElementById(NomefObj).value;

var caracteres_invalidos = '\/\'\\ ";:?!()[]\{\}^|';
for (i=0; i<caracteres_invalidos.lenght; i++)
{
if (emailStr.indexOf(caracteres_invalidos.charAt(i), 0) > -1)
{
temErroEmail = 1;
erroemail = 'O email não pode conter os seguintes caracteres: ' + caracteres_invalidos;
}
}
pos = emailStr.indexOf('@', 0);
if (pos == -1)
{
temErroEmail = 1;
erroemail = 'O email precisa conter uma @';
}
if (pos == 0)
{
temErroEmail = 1;
erroemail = 'O email não pode começar com uma @';
}
if (emailStr.indexOf('@', pos+1) > -1)
{
temErroEmail = 1;
erroemail = 'O email só pode conter uma @';
}

if (emailStr.indexOf('.', pos) == -1)
{
temErroEmail = 1;
erroemail = 'O email precisa ter um "." no nome do domínio.';
}
if (emailStr.indexOf('@.', pos) != -1)
{
temErroEmail = 1;
erroemail = 'O "." não pode ser imediameatamente após a @.';
}
if (emailStr.indexOf('.@', pos) != -1)
{
temErroEmail = 1;
erroemail = 'O "." não pode ser imediatamente antes da @.';
}
if (emailStr.indexOf('..', pos) != -1)
{
temErroEmail = 1;
erroemail = 'Dois "." não podem ser adjacentes em um endereço de email.';
}
var suffix = emailStr.substring(emailStr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') 
{
temErroEmail = 1;
erroemail = 'Domínio primário inválido.';
}
if (temErroEmail == 1) {
temErro =1;
erro = erro + '* ' + campo_erro + ' - ' + erroemail + '\n';
}
}

/* ######################################################################
// testar se é letras e numeros
//####################################################################### */
if ((formato[contador]=='LetrasNumeros') && (document.getElementById(NomefObj).value != '')) {
var oRegExp = /\W|[ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöùúûüýÿ]/; 

if (oRegExp.test(document.getElementById(NomefObj).value)) {
temErro =1;
erro = erro + '* ' + campo_erro +' - '+ mensagem[contador] +'.\n';
} }

/* ######################################################################
// testar se é Igual
//#######################################################################*/

if ((formato[contador]=='Igual') && (document.getElementById(NomefObj).value != ''))
{
NomefObj2 = document.getElementById(igual[contador]).name; 
if (document.getElementById(NomefObj).value != document.getElementById(NomefObj2).value) {
temErro = 1;
erro = erro + '* ' + campo_erro +' - '+ mensagem[contador] +'.\n';
}
}


/* ######################################################################
// testar se a primeira opção do select está marcada
//#######################################################################*/

if (formato[contador]=='Select')
{
if (document.getElementById(NomefObj).selectedIndex == 0)
{
temErro = 1;
erro = erro + '* ' + campo_erro +' - '+ mensagem[contador] +'.\n';
}
}

/* ######################################################################
// testar se é CPF
//#######################################################################*/
if ((formato[contador]=='CPF') && (document.getElementById(NomefObj).value != '')) {

if (!valida_CPF(document.getElementById(NomefObj).value)) {
temErro =1;
erro = erro + '* ' + campo_erro +' - '+ mensagem[contador] +'.\n';
}
}
// ######################################################################
// testar se é CNPJ
//#######################################################################

if ((formato[contador]=='CNPJ') && (document.getElementById(NomefObj).value != '')) {
if (!valida_CNPJ(document.getElementById(NomefObj).value)) {
temErro =1;
erro = erro + '* ' + campo_erro +' - '+ mensagem[contador] +'.\n';
} }




















contador++;
}// do while

/* ######################################################################
// Mostrar os erros
//#######################################################################*/

if (parametro != 'silencioso')
{
if (temErro > 0) {
alert (erro);
erro = 'Os seguintes campos estão incompletos ou com erro:\n\n';
temErro = 0;
return false;
}
}


}/* da função validaForm()*/