function trimL(str) {
  for (var i=0; str.charAt(i)==" "; i++);
  return str.substring(i,str.length);
}

function trimR(str) {
  for (var i=str.length-1; str.charAt(i)==" "; i--);
  return str.substring(0,i+1);
}

function trim(str) {
  return trimL(trimR(str));
}


function extrairMascaraCPF(cpf) {
  var tam = cpf.length;
  var cpfNumerico = "";
  for (var idx = 0; idx < tam; idx++) {
    ch = cpf.substring(idx, idx+1);
    if (ch != '.' && ch != '-')
      cpfNumerico += ch;
  }

  return cpfNumerico;
}

function verificarCPF(CPFVal) {
    var i;
    var dig1 = 0;
    var dig2 = 0;
    
    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = CPFVal.toString().replace( "-", "" );
    sValue = CPFVal.toString().replace( "-", "" );
    sValue = CPFVal.toString().replace( ".", "" );
    sValue = CPFVal.toString().replace( ".", "" );
    sValue = CPFVal.toString().replace( " ", "" );
    sValue = CPFVal.toString().replace( " ", "" );
    
    CPFValue = extrairMascaraCPF(sValue);

    if (trim(CPFValue) == null || trim(CPFValue).length > 11 || 
        trim(CPFValue).length < 11){
       return false;

   }

    for (i=1; i<=9; i++){
      dig1= dig1 + parseInt(CPFValue.substring(i-1,i),10) *i;
    }

    dig1= dig1%11;
    
    if (dig1==10) dig1=0;

    if (dig1 != CPFValue.substring(9,10)) return false;

    for (i=2; i<=10; i++){
       dig2= dig2 + parseInt(CPFValue.substring(i-1,i),10)*(i-1); 
    }

    dig2= dig2%11;

    if (dig2==10) dig2=0;

    if(dig2 != CPFValue.substring(10,11)){
      return false;
    }
    else{
      return true;
    }
}

function extrairMascaraCGC(cgc) {
  var tam = cgc.length;
  var cgcNumerico = "";
  for (var idx = 0; idx < tam; idx++) {
    ch = cgc.substring(idx, idx+1);
    if (ch != '.' && ch != '-' && ch != '/')
      cgcNumerico += ch;
  }

  return cgcNumerico;
}

function validarMascaraCGC(cgc) {
  var posDot1 = cgc.indexOf('.', 0);
  var posDot2 = cgc.indexOf('.', posDot1+1);
  var posSlash = cgc.indexOf('/');
  var posBar = cgc.indexOf('-');

  if (posDot1 > -1 || posDot2 > -1 || posSlash > -1 || posBar > -1) {
    if (posDot1 != 2 || posDot2 != 6 || posSlash != 10 || posBar != 15) {
      return false;
    }
  }
  return true;
}

function verificarCGC(CGCValue) {
  var dig1 = 0;
  var dig2 = 0;
  var i;
  var fator;
  
  CGCValue = extrairMascaraCGC(CGCValue);

  if (CGCValue ==null || trim(CGCValue).length > 14 ||
      trim(CGCValue).length < 14 ){
       return false;
  }

  // primeiro digito
  fator=14;
  for (i=12; i>=1; i--){ 
	if (i==4) 
	  fator= 6;
    
      dig1= dig1 + parseInt(CGCValue.substring(i-1,i),10)*(fator-i);
     }

  dig1= dig1%11;
  if (dig1==0 || dig1==1) dig1=0;
  else dig1= 11-dig1;

  if (!parseInt(dig1,10) == CGCValue.substring(12,13) ) return false;

  // segundo digito
  fator=15;
  for (i=13; i>=1; i--){
	  if (i==5) fator= 7;
	  
      dig2= dig2 + parseInt(CGCValue.substring(i-1,i),10)*(fator-i);
     }

  dig2= dig2%11;
  if (dig2==0 || dig2==1) dig2=0;
  else dig2= 11-dig2;

  if( parseInt(dig2,10) != (CGCValue.substring(13,14)) ){
    return false;
  }
  else{
    return true;
  }
  
}

function VerificaCep(cep){
	cep = cep.replace(" ","");
	cep = cep.replace("-","");
	cep = cep.replace(".","");
	if(cep.length != 5 && cep.length != 8){
		alert ("O comprimeto do CEP não é válido.");
		return(true);
	}
	else if(isNaN(cep)){
		alert ("O CEP não pode conter valores não numéricos.");
		return(true);
	}
	return(false);
}

function VerificaEMail(email)
{
	if (email.indexOf('\@') == -1){
		alert("Atenção:\n' "+email+" ' não é um e-mail válido!\nPor favor, verifique.");
		return true;
	}
	provedor = email.substr(email.indexOf('\@') + 1).toLowerCase();
	usuario = email.substr(email.indexOf('\@') + 1);

	if ((usuario == "") || (provedor == "") || (provedor.indexOf('.') < 0)){
		alert("Atenção:\n' "+email+" ' não é um e-mail válido!\nPor favor, verifique.");
		return true;
	}
	return false;
}

function TelefoneInvalido(telefone){
	telefone = telefone.replace(" ","");
	telefone = telefone.replace("-","");
	telefone = telefone.replace(".","");
	if(telefone.length == 0){
		alert("O campo telefone deve ser preenchido.\nTente novamente.");
		return true;
	}	
	if(telefone.length != 7 && telefone.length != 8){
		alert("O comprimento do telefone é inválido.\nTente novamente.");
		return true;
	}
	if(isNaN(telefone)){
		alert("Formato de Telefone inválido! Letras não são aceitas.");
		return true;
	}
	return false;
}

//MÁSCARA DE VALORES

function txtBoxFormat(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;


if(document.all) { // Internet Explorer
    nTecla = evtKeyPress.keyCode;
} else if(document.layers) { // Nestcape
    nTecla = evtKeyPress.which;
} else {
    nTecla = evtKeyPress.which;
    if (nTecla == 8) {
        return true;
    }
}

    sValue = objeto.value;

    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;

    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
      bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
      bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

      if (bolMask) {
        sCod += sMask.charAt(i);
        mskLen++; }
      else {
        sCod += sValue.charAt(nCount);
        nCount++;
      }

      i++;
    }

    objeto.value = sCod;

    if (nTecla != 8) { // backspace
      if (sMask.charAt(i-1) == "9") { // apenas números...
        return ((nTecla > 47) && (nTecla < 58)); } 
      else { // qualquer caracter...
        return true;
      } 
    }
    else {
      return true;
    }
  }