function exibeStatus(obj) {
  window.status = obj.msgStatus;
}

function redireciona(url){		
  location.href=url;
  mostraAguarde();
}

function mostraAguarde(){
  var caixa = document.all("caixaAguarde");
  var tabela = document.all("tabelaPrincipal");
  tabela.style.visibility='hidden';
  window.scrollTo(0,0);
  caixa.style.visibility = 'visible';
}

function inicio(obj) {
  obj.focus();
}

function anexar(nova) {
  nova = window.open(nova, '', 'width=600, height=520, alwaysRaised=no, alwaysLowered=yes, titlebar=no, toolbar=no, scrollbars=yes, statusbar=no, resizable=no, location=no, top=0, left=0');
  nova.focus();
  nova.location.href = 'areasAnexar.html';
}

function fMinuscula(obj) {
  obj.value = obj.value.toLowerCase();
}

function fMaiuscula(obj) {
  obj.value = obj.value.toUpperCase();
}

function fValidaLetras(obj) {
  var retorno = true;
  if (fLetrasInvalidas(obj) == false) {
    alert('Campo contem caracteres invalidos. \n \n Não utilize palavras acentuadas, símbolos ou cedilha.');
    obj.focus();
    obj.select();
    retorno = false;
  }
  return retorno;
}

function fLetrasInvalidas(obj){
  //var invalidos = "®ºª¨äëöïüÁíÉéáÍÓóÚúâÂêÊîÎôÔûÛãÃõÕçÇ~^´`'";
  var invalidos = "";
  var retorno = true;
  for (var i=0; i < obj.value.length; i++) {
    for (var j=0; j < invalidos.length; j++) {
      if (obj.value.substring(i,i+1) == invalidos.substring(j,j+1)) {
        retorno = false;
      }
    }
  }
  return retorno;
}

function fValidaUf(obj) {
  var retorno = true;
  if (obj.value != "") {
    if (fUf(obj) == false) {
      alert('Sigla da Unidade Federativa Inválida.');
      obj.focus();
      obj.select();
      retorno = false;;
    }
  }
  return retorno;
}

function fUf(obj) {
  var estados = "ACALAMAPBACEDFESGOMAMGMTMSPBPEPAPRPIRJRNRORRRSSCSESPTO";
  var retorno = false;
  for (var i=0; i < estados.length; i+=2) {
    if (obj.value == estados.substring(i,i+2)) {
      retorno = true;
    }
  }
  return retorno;
}

function fRetiraHifen(obj) {
  if (obj.value != "") {
    if (obj.value.indexOf("-") != -1) {
      var i = obj.value.indexOf("-");
      obj.value  = obj.value.substring(0,i) + "" + obj.value.substring(i+1,obj.value.length);
      obj.focus();
      obj.select();
    }
  }
}

function fRetiraFormatoCpf(obj) {
  if (obj.value != "" && obj.value.length == 14) {
    obj.value  = obj.value.substring(0,3) + "" + obj.value.substring(4,7) + "" +
                 obj.value.substring(8,11) + "" + obj.value.substring(12,14);
    obj.focus();
    obj.select();
  }
}


function fValidaTelefone(obj,max,min) {
  var retorno = true;
  if (obj.value != "") {
    if (fValidaNumeros(obj,max,min) == true) {
      if (obj.value.length == 8 && obj.value.indexOf("-") == -1){
        obj.value = obj.value.substring(0,4) + '-' + obj.value.substring(4,8);
        retorno = false;
      } else if (obj.value.length == 7 && obj.value.indexOf("-") == -1) {
        obj.value = obj.value.substring(0,3) + '-' + obj.value.substring(3,7);
        retorno = false;
      }
    }
    return retorno;
  }
}

function fValidaNumeros(obj,max,min) {
  var retorno = true;
  if (obj.value != "") {
    if (fNumeros(obj) == false) {
      alert('Campo '+ obj.idCutrale.toUpperCase() +' deve conter apenas NÚMEROS.');
      obj.focus();
      obj.select();
      retorno = false;
    }
    if (retorno == true && max != 0 && obj.value.length > max) {
      alert('Campo '+ obj.idCutrale.toUpperCase() +' deve conter no máximo '+max+' dígitos.');
      obj.focus();
      obj.select();
      retorno = false;
    }
    if (retorno == true && min != 0 && obj.value.length < min) {
      alert('Campo '+ obj.idCutrale.toUpperCase() +' deve conter no mínimo '+min+' dígitos.');
      obj.focus();
      obj.select();
      retorno = false;
    }
    return retorno;
  }
}

function fNumeros(obj) {
  var retorno = true;
  if (obj.value != "") {
    for (var i=0; i < obj.value.length; i++) {
      if (obj.value.charAt(i) != "-" && obj.value.charAt(i) != " ") {
        if (obj.value.charAt(i) < "0") retorno = false;
        if (obj.value.charAt(i) > "9") retorno = false;
      } 
    }
    return retorno;
  }
}

function fValidaEmail(obj) {
  var retorno = true;
  if (obj.value != "") {
    if (fEmail(obj.value) == false) {        	
      alert('Formato de e-mail Inválido.');
      obj.focus();
      obj.select();
      retorno = false;
    }        
  }
  return retorno;
}

function fEmail(email) {

  var invalidos = "/:\,;ÁíÉéáÍÓóÚúâÂêÊîÎôÔûÛãÃõÕçÇ~^´`'";
  var retorno   = true;

  for (var i=0; i < invalidos.length; i++) {
    invalido = invalidos.charAt(i);
    if (email.indexOf(invalido,0) > -1) retorno = false;         
  }

  posicao = email.indexOf("@",1);
  if (posicao == -1) retorno = false;
  if (email.indexOf("@",posicao+1) != -1) retorno = false;
  pos = email.indexOf(".",posicao);
  if (pos == -1) retorno = false;
  if (pos+3 > email.length) retorno = false;
     
  return retorno;
}

function fValidaCpf(obj) {
  var retorno = true
  if (obj.value != "") {
    if (fValidaNumeros(obj,11,11) == true) {
      if (fCpf(obj.value) == false) {
        alert("C.P.F. INVÁLIDO.");
        obj.focus();
        //obj.value = "";
        obj.select();
        retorno = false;
      } else {
        obj.value = obj.value.substring(0,3)+ '.' + obj.value.substring(3,6) + '.' +
        obj.value.substring(6,9)+ '-' + obj.value.substring(9,obj.value.length);
	  }
	}
	return retorno;
  }
}

function fCpf(cpf) {

  var retorno = true;
  var dig1 = 0;
  var dig2 = 0;
  var som  = 0;
  var ind  = 0;
  var val  = 0;
  var pos  = 10;
  var res  = 0;
  var r       = 0;

  while (ind < 9) {
   val = cpf.substring(ind,ind+1) * pos;
   
    som += val;
    ind++;
    pos--;
  }


  res = som/11;
  //res = res.toString().substring(2,5);
  res = res.toString().substring(res.toString().indexOf('.'),res.toString().indexOf('.')+3);

  res = res * 11;
  res = Math.round(11 - res);

  if(res < 0){
    r = res * -1;
    res = r; 
  }

  if (res > 9) {
    res = 0;
  }
  dig1 = res;

  //alert("Primeiro Digito: "+dig1)

  pos = 11;
  ind = 0;
  som = 0;

  while (ind < 10) {
    val = cpf.substring(ind,ind+1) * pos;
    som += val;
    ind++;
    pos--;
  }
  
  res = som/11;
  res = res.toString().substring(2,5);
  res = res * 11;
  res = Math.round(11 - res);

  if (res > 9) {
    res = 0;
  }
  dig2 = res;

  //alert("Segundo Digito: "+dig2)

  if (cpf.substring(9,10) != dig1 || cpf.substring(10,11) != dig2) retorno = false;

  return retorno;

}


function fRetiraFormatoCpf(obj) {
  if (obj.value != "") {
    if (obj.value.length == 14) {
      obj.value  = obj.value.substring(0,3) + "" + obj.value.substring(4,7) + "" +
		  		   obj.value.substring(8,11) + "" + obj.value.substring(12,14);
      obj.focus();
      obj.select();
    }
  }
}

function fRetiraFormatoCnpj(obj) {
  if (obj.value != "") {
    if (obj.value.length == 18) {
      obj.value  = obj.value.substring(0,2) + "" + obj.value.substring(3,6) + "" +
      obj.value.substring(7,10) +  "" + obj.value.substring(11,15) + "" +
      obj.value.substring(16,18);
      obj.focus();
      obj.select();
    }
  }
}

function fValidaCnpj(obj) {
  var retorno = true
  if (obj.value != "") {
    if (fValidaNumeros(obj,14,14) == true) {
      if (fCnpj(obj.value) == false) {
        alert('C.N.P.J. INVÁLIDO.');
        obj.focus();
        //obj.value = "";
        obj.select();
        retorno = false;
      } else {
        obj.value = obj.value.substring(0,2)+ '.' + obj.value.substring(2,5)  + '.' +
        obj.value.substring(5,8)+ '/' + obj.value.substring(8,12) + '-' +
        obj.value.substring(12,14);
      }
    }	
    return retorno;
  }
}
function fValidaCampo(obj) {
  if (obj.value != "") {
	 if (obj.value.length == 11) {
		fValidaCpf(obj);
	 } else if (obj.value.length == 14) {
		fValidaCnpj(obj);
	 } else {
	alert('CPF / CNPJ Inválido');
		obj.focus();
		obj.select();
	 }
  }
 }

function fRetiraFormato(obj) {
  if (obj.value.indexOf(".") != -1) {
    if (obj.value.length == 14) {
	  fRetiraFormatoCpf(obj);
	} else if (obj.value.length == 18){
	  fRetiraFormatoCnpj(obj);
    }
  }  
}

function mostraComunicado() {
  window.open('comunicado.html', 'comunicado', 'toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=400,height=320, left=0, top=0'); 
}


function fCnpj(w_cnpj) {

  var retorno = true;
  var pes = 2;
  var som = 0;
  var ind = 0;
  var val = 0;
  var dg8 = 0;
  var dv1 = 0;
  var dv2 = 0;
  var res = 0;

  while (ind < 7) {
	val = w_cnpj.substring(ind,ind+1) * pes;
	//alert(val)
	if (val > 9) {
	  som += val.toString().substring(0,1) * 1;
	  som += val.toString().substring(1,2) * 1;
	} else {
	  som += val;
	}
	if (pes == 1) {
	  pes = 2;
	} else {
	  pes = 1;
	}
	ind++;
  }


  dg8 = som.toString().substring(1,2) * 1;
  if (dg8 > 0)
	  dg8 = 10 - dg8;

//  alert('Oitavo Digito: '+dg8)

  ind = 0;
  pes = 5;
  som = 0;
  val = 0;

  while (ind < 12) {
	val = w_cnpj.substring(ind,ind+1) * pes;
	//alert(val)
	som += val;

	if (pes == 2) {
		pes = 9;
	} else {
		pes--;
	}
	ind++;
  }

  res = som/11;

  if (res.toString().indexOf(".") == -1) {
    res = 0
  } else {
    res = res.toString().substring(res.toString().indexOf("."),5);
    res *= 11;
  }

  res = Math.round(11 - res);

  if (res > 9)
	  res = 0;

  dv1 = res;

//  alert('Primeiro Dig. de Controle: '+dv1)

  ind = 0;
  pes = 6;
  som = 0;
  val = 0;
  res = 0;

  while (ind < 13) {
	val = w_cnpj.substring(ind,ind+1) * pes;
	//alert(val)
	som += val;
	if (pes == 2) {
		pes = 9;
	} else {
		pes--;
	}
	ind++;
  }

  res = som/11;

  if (res.toString().indexOf(".") == -1) {
    res = 0
  } else {
    res = res.toString().substring(res.toString().indexOf("."),5);
    res *= 11;
  }

  res = Math.round(11 - res);

  if (res > 9)
      res = 0;

  dv2 = res;

//  alert('Segundo Dig. de Controle: '+dv2)

  if (w_cnpj.substring(12,13) != dv1 || w_cnpj.substring(13,14) != dv2) {
	retorno = false;
  }

  return retorno;
}

