function tira_acento(palavra)

{

 tam = palavra.length;

 array = new Array('à','á','â','ã','ä','è','é','ê','ë','ì','í','î','ï','ò','ó','ô','õ','ö','ù','ú','û','ü','ç','ñ','-','+','=',' ');

 novo_array = new Array('a','a','a','a','a','e','e','e','e','i','i','i','i','o','o','o','o','o','u','u','u','u','c','n','_','_','_','_');

 tam_array = array.length;

 

 for(j = 0; j < tam_array; j++)

  for(i = 0; i < tam; i++)

   palavra = palavra.replace(array[j],novo_array[j]);

  

 return palavra;

}



// Não permite digitar letras em um input type=text

function evita_letra(tecla) {

	if (tecla.keyCode < 48 || tecla.keyCode > 57) 

		tecla.returnValue = false;

}



function FormataData(campo,formname,teclapres) { // Máscara para os campos de data

	var tecla = teclapres.keyCode;

	vr = document[formname][campo].value;

	vr = vr.replace( ".", "" );

	vr = vr.replace( "/", "" );

	vr = vr.replace( "/", "" );

	tam = vr.length + 1;



	if ( tecla != 9 && tecla != 8 ){

		if ( tam > 2 && tam < 5 )

			document[formname][campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );

		if ( tam >= 5 && tam <= 10 )

			document[formname][campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 

	}

}





//Utilização: onkeypress="evita_letra(event)" onKeydown="FormataHora(this,event)" maxlength="5"

function FormataHora(campo,formname,teclapres){

	var tecla = teclapres.keyCode;

	vr = document[formname][campo].value;

	vr = vr.replace( ":", "" );

	tam = vr.length + 1;

	

	if ( tecla != 9 && tecla != 8 ){

		if ( tam > 2 && tam < 5 ){

			document[formname][campo].value = vr.substr( 0, tam - 2  ) + ':' + vr.substr( tam - 2, tam );

		}

	}

}





//formata cnpj

function FormataCNPJ(Campo, teclapres){



	var tecla = teclapres.keyCode;



	var vr = new String(Campo.value);

	vr = vr.replace(".", "");

	vr = vr.replace(".", "");

	vr = vr.replace("/", "");

	vr = vr.replace("-", "");



	tam = vr.length + 1 ;



	

	if (tecla != 9 && tecla != 8){

		if (tam > 2 && tam < 6)

			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);

		if (tam >= 6 && tam < 9)

			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);

		if (tam >= 9 && tam < 13)

			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);

		if (tam >= 13 && tam < 15)

			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);

		}

}



//Valida o cpf 

function val_cpf(numero2) {

  dig_1 = 0;

  dig_2 = 0;

  controle_1 = 10;

  controle_2 = 11;

  lsucesso = 1;

  

  var numero = numero2.substr(0,3) + numero2.substr(4,3) + numero2.substr(8,3) + numero2.substr(12,2);

  

  if (numero == "00000000000" || numero == "11111111111" || numero == "22222222222" || numero == "33333333333" || numero == "44444444444" || numero == "55555555555" || numero == "66666666666" || numero == "77777777777" || numero == "88888888888" || numero == "99999999999") {

  	return false;

  }

  

  if ((numero.length != 11) && (numero.length != 0)) {

     return false;

  }

  else {

     for ( i=0 ; i < 9 ; i++) {

        dig_1 = dig_1 + parseInt(numero.substring(i, i+1) * controle_1);

          controle_1 = controle_1 - 1;

     }

    

     resto = dig_1 % 11;

     dig_1 = 11 - resto;

     if ((resto == 0) || (resto == 1)) {

          dig_1 = 0;

     }

     for ( i=0 ; i < 9 ; i++) {

          dig_2 = dig_2 + parseInt(numero.substring(i, i + 1) * controle_2);

        controle_2 = controle_2 - 1;

     }

     dig_2 = dig_2 + 2 * dig_1;

     resto = dig_2 % 11;

     dig_2 = 11 - resto;

     if ((resto == 0) || (resto == 1)) {

        dig_2 = 0;

     }

     dig_ver = (dig_1 * 10) + dig_2;

     if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {

          return false;

        }

  }



return true;

}



function FormataCEP(Campo, teclapres){



	var tecla = teclapres.keyCode;



	var vr = new String(Campo.value);

	vr = vr.replace("-", "");

	tam = vr.length + 1 ;

    

	if (tecla != 9 && tecla != 8)

	 if(tam > 5)

	  Campo.value = vr.substr(0, 5) + '-' + vr.substr(5, tam);

}



function FormataFone(Campo, teclapres){



	var tecla = teclapres.keyCode;



	var vr = new String(Campo.value);

	vr = vr.replace("-", "");

	tam = vr.length + 1 ;

    

	if (tecla != 9 && tecla != 8)

	 if(tam > 4)

	  Campo.value = vr.substr(0, 4) + '-' + vr.substr(4, tam);

}



function FormataCPF(Campo, teclapres){



	var tecla = teclapres.keyCode;



	var vr = new String(Campo.value);

	vr = vr.replace(".", "");

	vr = vr.replace(".", "");

	vr = vr.replace(".", "");

	vr = vr.replace("/", "");

	vr = vr.replace("-", "");



	tam = vr.length + 1 ;



	

	if (tecla != 9 && tecla != 8){

		if (tam >= 10 && tam < 12) {

			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);

		} else if (tam >= 7 && tam < 10) {

			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);

		} else if (tam > 3 && tam < 7) {

			Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);

		}

		

	}

		

}



function testa_data(data)

{

 tipo = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/;

 return tipo.test(data);

}



function testa_hora(hora)

{

 tipo = /^([0-1]\d|2[0-3]):[0-5]\d$/;

 return tipo.test(hora);

}



function testa_email(email)

{

 tipo = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;

 return tipo.test(email);

}



//------------------------------------------ cookie.js ------------------------------------------

//by bamboo, lindau & szilard







//     * GetCookie(<cookie-name>)

//        - Description: Returns the value of <cookie-name>

//        - Returns: value of <cookie-name>

function GetCookie(name)

{

  var arg = name + "=";

  var alen = arg.length;

  var clen = document.cookie.length;

  var i = 0;

  while (i < clen)

  {

    var offset = i + alen;

    if (document.cookie.substring(i, offset) == arg)

    {

      var endstr = document.cookie.indexOf (";", offset);

      if (endstr == -1) endstr = document.cookie.length;

      return unescape(document.cookie.substring(offset, endstr));

    }

    i = document.cookie.indexOf(" ", i) + 1;

    if (i == 0) break;

  }

  return null;

}





//     * SetCookie(<cookie-name>, <cookie-data>, [expires-date], [path], [domain], [secure])

//        - Description: Stores the cookie <cookie-name> with <cookie-data>.

//        - Returns: nothing

function SetCookie(name, value)

{

  var argv        = SetCookie.arguments;

  var argc        = SetCookie.arguments.length;

  var expires     = (argc > 2) ? argv[2] : null;

  var path        = (argc > 3) ? argv[3] : null;

  var domain      = (argc > 4) ? argv[4] : null;

  var secure      = (argc > 5) ? argv[5] : false;

  document.cookie = name + "=" + escape (value) +

    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +

    ((path == null) ? "" : ("; path=" + path)) +

    ((domain == null) ? "" : ("; domain=" + domain)) +

    ((secure == true) ? "; secure" : "");

}





//     * DeleteCookie(<cookie-name>)

//        - Description: Deletes the cookie <cookie-name>

//        - Returns: nothing

function DeleteCookie(name)

{

  var exp = new Date();

  exp.setTime (exp.getTime() - 1);

  var cval = GetCookie (name);

  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();

}



function FormataValorMonetario(num)

{

 var centsSeparator = ',';

 var thousandSeparator = '.';

 num = num.toFixed(2).replace('.', centsSeparator);

  while (thousandSeparator && num.search(/[0-9]{4}/) > -1) {

    num = num.replace(/([0-9])([0-9]{3})([^0-9])/,'$1'+thousandSeparator+'$2$3');

	}

	return num;

}



function FormataValor(num)
{
 while(num.indexOf('.') != -1) 
  num = num.replace('.','');
 num = num.replace(',', '.');
 return num;
}

function mascara(o,f){
    v_obj=o
    v_fun=f
    v_obj.value=v_fun(v_obj.value);
}

function soNumerosMascara(v){
  return v.replace(/\D/g,"");
}

function soNumerosVirgulaMascara(v){
	var pos = v.indexOf("\,");
  v = v.replace(/\D/g,"");  
	if(pos > 0)
		v = v.substring(0,pos) + ',' + v.substring(pos,v.length);
  return v;
}

function FormataTelefoneMascara(v){
    v=v.replace(/\D/g,"");                 //Remove tudo o que não é dígito
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2"); //Coloca parênteses em volta dos dois primeiros dígitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2");    //Coloca hífen entre o quarto e o quinto dígitos
    return v;
}

function FormataCPFMascara(v){
    v=v.replace(/\D/g,"");                    //Remove tudo o que não é dígito
    v=v.replace(/(\d{3})(\d)/,"$1.$2");       //Coloca um ponto entre o terceiro e o quarto dígitos
    v=v.replace(/(\d{3})(\d)/,"$1.$2");       //Coloca um ponto entre o terceiro e o quarto dígitos de novo (para o segundo bloco de números)
    v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2"); //Coloca um hífen entre o terceiro e o quarto dígitos
    return v;
}

function FormataCEPMascara(v){
    v=v.replace(/\D/g,"");                //Remove tudo o que não é dígito
    v=v.replace(/^(\d{5})(\d)/,"$1-$2"); //Esse é tão fácil que não merece explicações
    return v;
}

function FormataCNPJMascara(v){
    v=v.replace(/\D/g,"");                           //Remove tudo o que não é dígito
    v=v.replace(/^(\d{2})(\d)/,"$1.$2");             //Coloca ponto entre o segundo e o terceiro dígitos
    v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3"); //Coloca ponto entre o quinto e o sexto dígitos
    v=v.replace(/\.(\d{3})(\d)/,".$1/$2");           //Coloca uma barra entre o oitavo e o nono dígitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2");              //Coloca um hífen depois do bloco de quatro dígitos
    return v;
}

function FormataDataMascara(v){
    v=v.replace(/\D/g,"");                 //Remove tudo o que não é dígito
    v=v.replace(/(\d{2})(\d)/,"$1/$2");    //Coloca hífen entre o quarto e o quinto dígitos
    v=v.replace(/(\d{2})(\d)/,"$1/$2");    //Coloca hífen entre o quarto e o quinto dígitos		
    return v;
}

function FormataHoraMascara(v){
    v=v.replace(/\D/g,"");                 //Remove tudo o que não é dígito
    v=v.replace(/(\d{2})(\d)/,"$1:$2");    //Coloca hífen entre o quarto e o quinto dígitos
    return v;
}

function FormataValorMascara(v){
 v=v.replace(/\D/g,"");                 //Remove tudo o que não é dígito
var ponto = '.';
  v=v.replace(/(\d{1,})(\d{2})/,"$1,$2");
  while (ponto && v.search(/[0-9]{4}/) > -1) {
    v = v.replace(/([0-9])([0-9]{3})([^0-9])/,'$1'+ponto+'$2$3');
	}
	return v;
}

function ValidaDataMascara(val)
{
 var er = /(^(0[1-9]|[12][0-9]|3[01])\/(0[13578]|1[02])\/(19[0-9]{2}|2[0-9]{3}))|(^(0[1-9]|[12][0-9]|30)\/(0[469]|11)\/(19[0-9]{2}|2[0-9]{3}))|(^(0[1-9]|1[0-9]|2[0-9])\/02\/(19[0-9]{2}|2[0-9]{3}))$/;
 
 var dia = parseInt(val.substring(0,3));
 var mes = parseInt(val.substring(3,5));
 var ano = parseInt(val.substring(6,10));

 return (er.test(val) && (mes==2 && dia == 29 ? (ano%4 == 0 ? true : false) : true)); 
}


function FormataURLMascara(v){
    //Esse sem comentarios para que você entenda sozinho ;-)
    v=v.replace(/^http:\/\/?/,"")
    dominio=v
    caminho=""
    if(v.indexOf("/")>-1)
        dominio=v.split("/")[0]
        caminho=v.replace(/[^\/]*/,"")
    dominio=dominio.replace(/[^\w\.\+-:@]/g,"")
    caminho=caminho.replace(/[^\w\d\+-@:\?&=%\(\)\.]/g,"")
    caminho=caminho.replace(/([\?&])=/,"$1")
    if(caminho!="")dominio=dominio.replace(/\.+$/,"")
    v="http://"+dominio+caminho
    return v
}

function FormataDataDMMascara(v){
    v=v.replace(/\D/g,"");                 //Remove tudo o que não é dígito
    v=v.replace(/(\d{2})(\d)/,"$1/$2");    //Coloca hífen entre o quarto e o quinto dígitos
    v=v.replace(/(\d{2})(\d)/,"$1/$2");    //Coloca hífen entre o quarto e o quinto dígitos		
    return v;
}

function ValorKgMascara(linha)
{
	var peso = 0, valor = 0, res = 0;
	for(i = 0; i < document.cadastro.length; i++){

	  if(document.cadastro.elements[i].name == 'res_peso['+linha+']')	
		  peso = document.cadastro.elements[i].value;

		if(document.cadastro.elements[i].name == 'res_valor['+linha+']')	
		  valor = document.cadastro.elements[i].value;

     if(peso && valor && document.cadastro.elements[i].name == 'res_valkg['+linha+']'){
			 valor = valor.replace("\.","");
			 valor = valor.replace("\,","\.");			 
			 xajax_FormataMoeda(parseFloat(valor / peso),i);
			 peso = 0;
			 valor = 0;			 
		 }		 
	}
}
