function valida_estado(estado){
	var estados = new Array('AC','AL','AP','AM','BA','DF','CE','ES','GO','MA','MT','MS','MG','PB','PR','PA','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO');
	for (var i = 0; i < estados.length; i++){
		if (estados[i] == estado.toUpperCase()){
			return true;
		}
	}
	return false;
}

function validaCPF(cpf){
	erro = new String;
	
	cpf = cpf.replace('.', '');
	cpf = cpf.replace('.', '');
	cpf = cpf.replace('-', '');

	if (cpf.length == 11){    
		var nonNumbers = /\D/;
		if (cpf == "00000000000" || 
				cpf == "11111111111" || 
				cpf == "22222222222" || 
				cpf == "33333333333" || 
				cpf == "44444444444" || 
				cpf == "55555555555" || 
				cpf == "66666666666" || 
				cpf == "77777777777" || 
				cpf == "88888888888" || 
				cpf == "99999999999") {
				
				return false;
		}
	
		var a = [];
		var b = new Number;
		var c = 11;
	
		for (i=0; i<11; i++){
				a[i] = cpf.charAt(i);
				if (i < 9) b += (a[i] * --c);
		}
	
		if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
		b = 0;
		c = 11;
	
		for (y=0; y<10; y++) b += (a[y] * c--); 
	
		if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	
		if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])) {
			return false;
		}
	} else 
		return false;
	
    return true;    
}

function validaCNPJ(cnpj){
	var cnpj = cnpj.replace(".","").replace(".","").replace("-","").replace("/","");
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
	digitos_iguais = 1;
	
	if (cnpj == '11111111111111')
		return true;
	
	if (cnpj.length < 14 && cnpj.length < 15)
		return false;
		
	for (i = 0; i < cnpj.length - 1; i++)
		if (cnpj.charAt(i) != cnpj.charAt(i + 1)){
			  digitos_iguais = 0;
			  break;
		}
		if (!digitos_iguais){
			tamanho = cnpj.length - 2
			numeros = cnpj.substring(0,tamanho);
			digitos = cnpj.substring(tamanho);
			soma = 0;
			pos = tamanho - 7;
			for (i = tamanho; i >= 1; i--){
				  soma += numeros.charAt(tamanho - i) * pos--;
				  if (pos < 2)
						pos = 9;
			}
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
			if (resultado != digitos.charAt(0))
				  return false;
			tamanho = tamanho + 1;
			numeros = cnpj.substring(0,tamanho);
			soma = 0;
			pos = tamanho - 7;
			for (i = tamanho; i >= 1; i--){
				soma += numeros.charAt(tamanho - i) * pos--;
				if (pos < 2)
					pos = 9;
			}
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
			if (resultado != digitos.charAt(1))
				return false;
		
		return true;
	} else
		return false;
} 

function validar_email(email){
   if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) || !email) 
     return false;
   else 
     return true;
}


function validar_data(campo){
	var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
	if ((campo.match(expReg)) && (campo!='')){
		var dia = campo.substring(0,2);
		var mes = campo.substring(3,5);
		var ano = campo.substring(6,10);

		if((mes==4 || mes==6 || mes==9 || mes==11) && dia > 30){
			//alert("Dia incorreto !!! O mês especificado contém no máximo 30 dias.");
			return false;
		} else{
			if(ano%4!=0 && mes==2 && dia>28){
	//			alert("Data incorreta!! O mês especificado contém no máximo 28 dias.");
				return false;
			} else{
				if(ano%4==0 && mes==2 && dia>29){
	//				alert("Data incorreta!! O mês especificado contém no máximo 29 dias.");
					return false;
				} else{	
	//				alert ("Data correta!");
					return true;
				}
			}
		}
	} else {
	//	alert(msgErro);
	//	campo.focus();
		return false;
	}
}

function $_get( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) 
{
    var keyCode = (isNN) ? e.which : e.keyCode; 
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    if(input.value.length >= len && !containsElement(filter,keyCode)) 
	{
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
	}
}

function containsElement(arr, ele) 
{
    var found = false, index = 0;
    while(!found && index < arr.length)
	{
        if(arr[index] == ele)
            found = true;
        else
            index++;
	}
	return found;
}

function getIndex(input) 
{
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
	{
        if (input.form[i] == input)index = i;
        else i++;
        return index;
    }
	return true;
}

function vai (url){
  location.href = url;
}

function trocaponto(valor){
	return (valor ? valor.replace(".", ",").replace(",",".") : 0);
}

function number_format (number, decimals, dec_point, thousands_sep){
  var exponent = "";
  var numberstr = (number ? number.toString () : 0);
  var eindex = numberstr.indexOf ("e");
  
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}

function bloquearCaracterAlfa(evnt){
	evnt = (evnt.keyCode ? evnt.keyCode : evnt.which)  // Internet Explorer
	if (evnt != 13) {
		if ((evnt >= 40 && evnt <= 47) || (evnt >= 96 && evnt <= 122) || (evnt >= 65 && evnt <= 90)){
			return false;
		}
	}
}

// Retorno o valor selecionado do Radio Button 
function valRadio(nomeCampo){ 
  var i;
  for (i=0; i < nomeCampo.length; i++){ 
    if (nomeCampo[i].checked) 
      return nomeCampo[i].value; 
  } 	
  return 0;
}

function valida_camposArray(campo,nome){
  var d = document;
  for (var i = 0;i<campo.length; i++){
  	 if (d.getElementById(campo[i]).value == ""){
    	alert ("Campo "+nome[i]+" esta em branco!");
	 	d.getElementById(campo[i]).focus();
	 	return false;
     }
	 if (campo[i] == "email" || campo[i] == "emailamigo"){
	 	if (!checkEmail2(d.getElementById(campo[i]).value)){
    		alert ("Campo "+nome[i]+" esta incorreto!");
	 		d.getElementById(campo[i]).focus();	 
			return false;
	 	}
	 }
  }
  return true;
}

function janelaCenter(url, nome, parms, width, height,msg_anti_popup) 
{
	var msg_anti_popup = "Lembrete: Favor desbloquear o bloqueador de Pop Up para este site.";
 	var str = "height=" + height + ",innerHeight=" + height;
	str += ",width=" + width + ",innerWidth=" + width;
	var hwnd = null;
	if (parms != "") {
		str += ", " + parms;
	}
	if (window.screen) 
	{
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;

	} else {
		var xc = 0, xy = 0;
	}
	str += ",left=" + xc + ",screenX=" + xc;
	str += ",top=" + yc + ",screenY=" + yc;
	hwnd = window.open(url, nome, str);
	if (hwnd != null)
	{
		if(hwnd.window.focus){hwnd.window.focus();}
	} 
	else 
	{
		alert (msg_anti_popup)
	}
//	if(text != "") hwnd.document.write(text);
//	hwnd.document.close();
	return hwnd;
}


// Descodificar String 
function url_decode(str) {  
	var n, strCode, strDecode = "";  

	for (n = 0; n < str.length; n++) {  
		if (str.charAt(n) == "%") {  
			strCode = str.charAt(n + 1) + str.charAt(n + 2);  
			strDecode += String.fromCharCode(parseInt(strCode, 16));  
			n += 2;  
		} else {  
			strDecode += str.charAt(n);  
		}  
	}  

	return strDecode;  
} 

function rawurldecode( str ) {
	var histogram = {};
	var ret = str.toString();
	var replacer = function(search, replace, str) {
		var tmp_arr = [];
		tmp_arr = str.split(search);
		return tmp_arr.join(replace);
	};
	
	histogram["'"]   = '%27';
	histogram['(']   = '%28';
	histogram[')']   = '%29';
	histogram['*']   = '%2A';
	histogram['~']   = '%7E';
	histogram['!']   = '%21';
	histogram[' ']   = '%20';
	
	for (replace in histogram) {
		search = histogram[replace]; // Switch order when decoding
		ret = replacer(search, replace, ret) // Custom replace. No regexing
	}	
	ret = decodeURIComponent(ret);
	return ret;
}
