function goAction(form, paramAcao){
	form.acao.value = paramAcao;
	form.submit();
}

function abrir(url, width, height, name){
    var horizontal  = window.screen.availWidth;
    var vertical    = window.screen.availHeight;
    var comprimento = 600;
    var altura      = 280;
    var nome	    = "x";

    if(width != null && height != null) {
      comprimento = width;
      altura      = height;
    }

    if(name != null) {
      nome = name;
    }

    var x = window.open(url,nome," resizable=yes,toolbar=no,scrollbars=yes,width="+ comprimento + ",height=" + altura);

    horizontal = Math.round((horizontal - comprimento) / 2);
    vertical   = Math.round((vertical   - altura) / 2);

    x.moveTo(horizontal, vertical);
    x.focus();

}


function abrirRelatorio(url, width, height, name){
    var horizontal  = window.screen.availWidth;
    var vertical    = window.screen.availHeight;
    var comprimento = 640;
    var altura      = 480;
    var nome	    = "x";

    if(width != null && height != null) {
      comprimento = width;
      altura      = height;
    }

    if(name != null) {
      nome = name;
    }

    var x = window.open(url,nome," resizable=yes,toolbar=no,scrollbars=yes,width="+ comprimento + ",height=" + altura);

    horizontal = Math.round((horizontal - comprimento) / 2);
    vertical   = Math.round((vertical   - altura) / 2);

    x.moveTo(horizontal, vertical);
    x.focus();
}

function eDataValida(Str) {
    vlraux = trim(Str);
    if ((vlraux == "") || (vlraux.length != 10) ||
       (vlraux.charAt(2) != "/") || (vlraux.charAt(5)!= "/")){
       return false;
    }

    dia = parseInt(vlraux.substring(0,2),10);
    mes = parseInt(vlraux.substring(3,5),10);
    ano = parseInt(vlraux.substring(6,10),10);

    if (isNaN(dia) || isNaN(mes) || isNaN(ano) || (mes < 1) || (mes > 12) || (dia < 1)) {
      return false;
    }

    tabmes = "312831303130313130313031";

    if ((dia == 29) && (mes == 2)){
      if ((ano == 0) || ((ano % 4) != 0)){
        return false;
      }
      else { return true; }
    }

    k = (mes * 2 - 2);

    if (dia > tabmes.substring(k,k + 2)) {
      return false;
    }
    else { return true;}

   return false;
}

function formataData(Campo) {
  if(Campo.value != ""){
    Campo.value = retiraCaracteresSeparacao(Campo.value);
    vr = Campo.value;
    tam = vr.length;

    if ( tam > 2 && tam < 5 )
            Campo.value = vr.substr( 0, tam - 2) + "/" + vr.substr( tam - 2, tam );
    if ( tam >= 5 && tam <= 10 )
            Campo.value = vr.substr( 0, 2 ) + "/" + vr.substr( 2, 2 ) + "/" + vr.substr( 4, 4 );
    if (!eDataValida(Campo.value)) { alert("Data Inv�lida!"); Campo.value = ""; Campo.focus();}
  }
}

function retiraCaracteresSeparacao(Str) {
  var s = "";
  var espaco = "X X";
  Str = trim(Str);
  for (i = 0; i < Str.length ; i++) {
    if (Str.charAt(i) != "/" && Str.charAt(i) != "-" && Str.charAt(i) != "."  && Str.charAt(i) != "," &&
        Str.charAt(i) != ";" && Str.charAt(i) != "|" && Str.charAt(i) != espaco.charAt(1) && Str.charAt(i) != "\\" &&
        Str.charAt(i) != ":" && Str.charAt(i) != "(" && Str.charAt(i) != ")"){
      s = s + Str.charAt(i);
    }
  }
        return s;
}

function trim(Str) {
  for (l = 0; l < Str.length; l++) {
          if (Str.charAt(l) != ' ') {
                  break;
                }
        }
        if (l == Str.length) {
          return '';
        }
  fim =  Str.length - 1;
        for (i = 0; i < Str.length; i++) {
                if (Str.charAt(i) != " ") {
                        break;
                }
        }
        for(j = fim; j > 0; j--){
                if (Str.charAt(j) != " ") {
                        break;
                }
        }
        return Str.substring(i, j+1);
}

function formataCpf(Campo) {
	  if(Campo.value != ""){
	    var cpf = retiraCaracteresSeparacao(Campo.value);
	    if (eNumerico(cpf)) {
	      if (cpf.length == 11) {
	        cpf = imprimeInteiro(cpf.substring(0, 9)) +"-"+ cpf.substring(9, 11);
	        if(!eCpfValido(cpf)){
	          alert("CPF inválido");
	          Campo.value = "";
	          Campo.focus();
	        }
	        else{
	          Campo.value = cpf;
	        }
	      }
	      else{
	        alert("CPF inválido");
	        Campo.value = "";
	        Campo.focus();
	      }
	    }
	    else{
	      alert("Digite somente números no campo");
	      Campo.value = "";
	      Campo.focus();
	    }
	  }
	}

	function eCpfValido(Str){

	  var CPF =  retiraCaracteresSeparacao(Str);


	    if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
	            CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
	            CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
	            CPF == "88888888888" || CPF == "99999999999")
	            return false;
	    soma = 0;
	    for (i=0; i < 9; i ++)
	            soma += parseInt(CPF.charAt(i)) * (10 - i);
	    resto = 11 - (soma % 11);
	    if (resto == 10 || resto == 11)
	            resto = 0;
	    if (resto != parseInt(CPF.charAt(9)))
	            return false;
	    soma = 0;
	    for (i = 0; i < 10; i ++)
	            soma += parseInt(CPF.charAt(i)) * (11 - i);
	    resto = 11 - (soma % 11);
	    if (resto == 10 || resto == 11)
	            resto = 0;
	    if (resto != parseInt(CPF.charAt(10)))
	            return false;

	    return true;
	}

	function eCgcValido(Str) {

	  var arg   = retiraCaracteresSeparacao(Str);
	  c = trim(arg);
	  t = arg.length;
	  ax = "";
	  dv = "";
	  dv1 = 0;
	  dv2 = 0;

	  if (t < 14) {return false;}

	  while(t < 14) {
	    ax += "0";
	    t++;
	  }

	  c = ax+c;
	  sm1 = 0;
	  sm2 = 0;
	  peso = 6;

	  for (i=0;i<=11; i++){
	    if (i >= 4) {
	      i==4 ? peso=9 : peso-- ;
	    }
	    else { peso--; }

	    sm1+=c.substring(i,i+1)*peso;
	  }

	  peso = 7;

	  for (i=0;i<=12; i++){
	    if (i>=5) {
	      i==5 ? peso=9 : peso-- ;
	    }
	    else {peso--;}

	    sm2+=c.substring(i,i+1)*peso;
	  }

	  t = 11 - sm1%11;
	  t < 10 ? dv1=t : dv1=0;
	  t = 11 - sm2%11;
	  t < 10 ? dv2=t : dv2=0;

	  if ((c.substring(12,13)==dv1)&&(c.substring(13,14)==dv2)) {
	    return true;
	  } else { return false; }
	}

	function formataCNPJ(Campo) {

	  if(Campo.value != ""){
	    var cnpj = retiraCaracteresSeparacao(Campo.value);

	    if (eNumerico(cnpj)) {
	      if (cnpj.length == 14) {
	        cnpj = imprimeInteiro(cnpj.substring(0, 8)) +"/"+ cnpj.substring(8, 12) +"-"+cnpj.substring(12, 14);

	        if(!eCgcValido(cnpj)){
	          alert("CNPJ inválido");
	          Campo.value = "";
	          Campo.focus();
	        }
	        else{
	          Campo.value = cnpj;
	        }

	      }
	      else{
	        alert("CNPJ inválido");
	        Campo.value = "";
	        Campo.focus();
	      }

	    }
	    else{
	      alert("Digite somente números no campo");
	      Campo.value = "";
	      Campo.focus();
	    }
	  }
	}
	function eNumerico(Valor){
		  for(j=0; j < Valor.length; j++){
		    if(!eNumero(Valor.charAt(j))){
		      return false;
		    }
		  }
		  return true;
	}
	function eNumero(Char){
		  numeros = "0,1,2,3,4,5,6,7,8,9";
		  arrayNumeros = numeros.split(",");
		  for(i=0; i < arrayNumeros.length; i++){
		   if(Char == arrayNumeros[i]){
		     return true;
		   }
		  }
		  return false;
	}
	function imprimeInteiro(Str) {
		  var resultado = "";
		  if(Str.length > 3){
		    var count = 0;
		    for (i = (Str.length - 1); i >= 0 ; i--) {
		      if(count == 3){
		        resultado = "." + resultado;
		        count = 0;
		      }
		      resultado = Str.charAt(i) + resultado;
		      count++;
		    }
		    return resultado;
		  } else {
		    return Str;
		  }
		}

	function validaCpfCnpj(campo){
		if(campo.value != ""){
		    var cnpj = retiraCaracteresSeparacao(campo.value);

		    if (eNumerico(cnpj)) {
		      if (cnpj.length == 14) {
		    	  formataCNPJ(campo);
		      }else{
			    	formataCpf(campo);
		      }
		    }
		}
	}

