//Função para inicialização do AJAX
function openAjax() {
	var ajax;
	try{
	    ajax = new XMLHttpRequest(); // XMLHttpRequest para browsers Firefox, Safari, dentre outros.
	}catch(ee){
	    try{
	        ajax = new ActiveXObject("Msxml2.XMLHTTP"); // Para o IE da MS
	    }catch(e){
	        try{
	            ajax = new ActiveXObject("Microsoft.XMLHTTP"); // Para o IE da MS
	        }catch(E){
	            ajax = false;
	        }
	    }
	}
	return ajax;
}

//Função para atualizar a página
function Atualiza() {
	//history.go(0);
	//window.location.href = window.location.href;
	window.location.reload(true);
}

function TextAreaCount(campo,divtexto,maxCaract) {
  field = document.getElementById(campo);
  txt_field = document.getElementById(divtexto);
  txt_field.innerHTML = (field.value.length);
  if(field.value.length >= maxCaract)
  {
	txt_field.innerHTML = maxCaract;
	field.value = field.value.substring(0, maxCaract);
  }
}

function Foco(campo){
	if(document.getElementById) { // Para os browsers complacentes com o DOM W3C.
		document.getElementById(campo).focus();
	}
}

//Encodar textos para o envio ao BD
function url_encode(str) {  
    var hex_chars = "0123456789ABCDEF";  
    var noEncode = /^([a-zA-Z0-9\_\-\.])$/;  
    var n, strCode, hex1, hex2, strEncode = "";  

    for(n = 0; n < str.length; n++) {  
        if (noEncode.test(str.charAt(n))) {  
            strEncode += str.charAt(n);  
        } else {  
            strCode = str.charCodeAt(n);  
            hex1 = hex_chars.charAt(Math.floor(strCode / 16));  
            hex2 = hex_chars.charAt(strCode % 16);  
            strEncode += "%" + (hex1 + hex2);  
        }  
    }  
    return strEncode;  
}  


function CadastraNews() {
	if(document.getElementById) { // Para os browsers complacentes com o DOM W3C.
		if (document.getElementById('newsname').value.length == 0)
		{
			alert("O campo \"Nome\" é necessário!");
			document.getElementById('newsname').focus();
			return false;
		}
		if (document.getElementById('newsmail').value.length == 0)
		{
			alert("O campo \"E-Mail\" é necessário!");
			document.getElementById('newsmail').focus();
			return false;
		}
		var mailmask = /^[a-z0-9\._\-]+\@[a-z0-9\._\-]+\.[a-z]{2,3}$/i;
		if (!mailmask.test(document.getElementById('newsmail').value)){
			alert("\"E-Mail\" inválido!");
			document.getElementById('newsmail').focus();
			return false;
		}

		var acao;
		if (document.getElementById('newssim').checked) {
			acao = "sim";
		} else {
			acao = "nao";
		}
		document.getElementById('envian').value = "Aguarde...";
		document.getElementById('envian').disabled = true;
		var ajax = openAjax(); // Inicia o Ajax.
		var resultado;
		var d = new Date();
		ajax.open("GET", "comunic_news.asp?acao="+acao+"&nome="+document.getElementById('newsname').value+"&email="+document.getElementById('newsmail').value+"&extra="+d.getTime(), true);
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) { // Quando estiver tudo pronto.
				if(ajax.status == 200) {
					resultado = ajax.responseText; // Coloca o resultado (da busca) retornado pelo Ajax nessa variável (var resultado).
					resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos 
					resultado = unescape(resultado); // Resolve o problema dos acentos
					document.getElementById('divnews').innerHTML = resultado;
				} else {
					alert("Ocorreu um erro no processamento...");
				}
			}
		}
		ajax.send(null); // submete
	}
}


//-----------------------------------------------------
//Funcao: MascaraMoeda
//Sinopse: Mascara de preenchimento de moeda
//Parametro:
//   objTextBox : Objeto (TextBox)
//   SeparadorMilesimo : Caracter separador de milésimos
//   SeparadorDecimal : Caracter separador de decimais
//   e : Evento
//Retorno: Booleano
//Autor: Gabriel Fróes - www.codigofonte.com.br
//-----------------------------------------------------
function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}
