function trim(texto) { if (texto == null) return ''; var temp = texto.charAt(0); while ((temp == ' ') || (temp == '\t') || (temp == '\n') || (temp == '\r')) { texto = texto.substr(1, texto.length - 1); temp = texto.charAt(0); } var temp = texto.charAt(texto.length-1); while ((temp == ' ') || (temp == '\t') || (temp == '\n') || (temp == '\r')) { texto = texto.substr(0, texto.length - 1); temp = texto.charAt(texto.length-1); } return texto; } function str_replace(antigo, novo, texto) { if ((texto != null) && (texto != '')) { while (texto.indexOf(antigo) != -1) { texto = texto.replace(antigo, novo); } return texto; } else { return ''; } } function explode(separador, valor) { var temp = ''; var retorno = new Array(); for (i = 0; i < valor.length; i++) { caracter = valor.substr(i, 1); if (caracter != separador) { temp += caracter; } else { retorno[retorno.length] = temp; temp = ''; } } if (temp != '') retorno[retorno.length] = temp; return retorno; } function FiltraTexto(texto, dominio) { var i, j, c; var temp = ''; for (i = 0; i < texto.length; i++) { c = texto.substr(i, 1); for (j = 0; j < dominio.length; j++) { if (c == dominio.substr(j, 1)) { break; } } if (j < dominio.length) { temp = temp + c; } } return temp; } function SomenteNumeros(texto) { return FiltraTexto(texto, '0123456789'); } function StringReplace(string, old_char, new_char) { var temp = string; while (temp.indexOf(old_char) != -1) { temp = temp.replace(old_char, new_char); } return temp; } function MaxLength(field, maxlimit) { if (field.value.length > maxlimit) { field.value = field.value.substring(0, maxlimit); } } function EmailValido(email) { if (email.length > 0) { if (!/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(email)) { return false; } } return true; } function DataValida(data) { data = SomenteNumeros(data); if (data == '') { return true; } var dia = data.substr(0, 2); var mes = data.substr(2, 2); var ano = data.substr(4, 4); if (mes > 12) { return false; } var dias = 31; switch (mes) { case '02': if ((ano % 4) == 0) { dias = 29; } else { dias = 28; } break; case '04': case '06': case '09': case '11': dias = 30; break; } if (dia > dias) { return false; } return true; } function DataMaior(data1, data2) { data1 = SomenteNumeros(data1); data1 = parseInt(SomenteNumeros(data1.substr(4, 4)+' '+data1.substr(2, 2)+' '+data1.substr(0, 2)), 10); data2 = SomenteNumeros(data2); data2 = parseInt(SomenteNumeros(data2.substr(4, 4)+' '+data2.substr(2, 2)+' '+data2.substr(0, 2)), 10); return (data1 > data2); } function HoraValida(hora, completa) { hora = SomenteNumeros(hora); if (hora == '') { return true; } if ((hora.substr(0, 2) > 23) || (hora.substr(2, 2) > 59)) { return false; } if (completa) { if (hora.substr(4, 2) > 59) { return false; } } return true; } function CPFValido(cpf) { cpf = SomenteNumeros(cpf); if (cpf == '') { return true; } var i; var c = cpf.substr(0, 9); var dv = cpf.substr(9, 2); var d1 = 0; for (i = 0; i < 9; i++) { d1 += c.charAt(i) * (10 - i); } if (d1 == 0) { return false; } d1 = 11 - (d1 % 11); if (d1 > 9) { d1 = 0; } if (dv.charAt(0) != d1) { return false; } d1 *= 2; for (i = 0; i < 9; i++) { d1 += c.charAt(i) * (11 - i); } d1 = 11 - (d1 % 11); if (d1 > 9) { d1 = 0; } if (dv.charAt(1) != d1) { return false; } return true; } function CNPJValido(cnpj) { cnpj = SomenteNumeros(cnpj); if (cnpj == '') { return true; } var i; if (cnpj.length > 14) { if (cnpj.substr(0,1) == 0) { var c = cnpj.substr(1, 12); var dv = cnpj.substr(13, 2); } else{ var c = cnpj.substr(0, 12); var dv = cnpj.substr(12, 2); } } else{ var c = cnpj.substr(0, 12); var dv = cnpj.substr(12, 2); } var d1 = 0; if (cnpj.length < 14) { return false; } for (i = 0; i < 12; i++) { d1 += c.charAt(11 - i) * (2 + (i % 8)); } if (d1 == 0) { return false; } d1 = 11 - (d1 % 11); if (d1 > 9) { d1 = 0; } if (dv.charAt(0) != d1) { return false; } d1 *= 2; for (i = 0; i < 12; i++) { d1 += c.charAt(11 - i) * (2 + ((i + 1) % 8)); } d1 = 11 - (d1 % 11); if (d1 > 9) { d1 = 0; } if (dv.charAt(1) != d1) { return false; } return true; } function urlencode(str) { if (str == null) { str = ''; } return str_replace('@', '%40', str_replace('/', '%2F', str_replace('*', '%2A', str_replace('%20', '+', str_replace('+', '%2B', encodeURIComponent(str)))))); } function urldecode(str) { return unescape(str_replace('+', ' ', str)); } function PostHTTP(url, params) { var retorno = ''; var XMLHTTP = NewXMLHttpRequest(); if (XMLHTTP != null) { if (params != null) { XMLHTTP.open('POST', url, false); XMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); XMLHTTP.setRequestHeader("Content-Length", params.length); XMLHTTP.setRequestHeader("Connection", "close"); } else { XMLHTTP.open('GET', url, false); } if (window.XMLHttpRequest && XMLHTTP.overrideMimeType) { XMLHTTP.overrideMimeType("text/plain"); } XMLHTTP.send(params); if ((XMLHTTP.readyState == 4) && (XMLHTTP.status == 200)) { var retorno = urldecode(XMLHTTP.responseText); } } return retorno; } function RequisicaoHTTPSincrono(url, resposta) { if (window.XMLHttpRequest) { XMLHTTP = new XMLHttpRequest(); } else if (window.ActiveXObject) { XMLHTTP = new ActiveXObject("Msxml2.XMLHTTP"); if (!XMLHTTP) { XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP"); } } if (XMLHTTP) { XMLHTTP.open('GET', url, false); if (window.XMLHttpRequest && XMLHTTP.overrideMimeType) { XMLHTTP.overrideMimeType("text/plain"); } XMLHTTP.send(null); if ((resposta != null) && resposta) { if ((XMLHTTP.readyState == 4) && (XMLHTTP.status == 200)) { return XMLHTTP.responseText; } else { return ''; } } } } function NewXMLHttpRequest() { var XMLHTTP = null; if (window.ActiveXObject) { for (var i = 5; i > 2; i--) { try { XMLHTTP = new ActiveXObject('MSXML2.XMLHTTP.'+i+'.0'); } catch (E) { XMLHTTP = null; } } if (XMLHTTP == null) { XMLHTTP = new ActiveXObject('MSXML2.XMLHTTP') if (XMLHTTP == null) { XMLHTTP = new ActiveXObject('Microsoft.XMLHTTP'); } } } if ((XMLHTTP == null) && window.XMLHttpRequest) { XMLHTTP = new XMLHttpRequest(); } return XMLHTTP; } function RequisicaoHTTP(url, funcaoRetorno, celula, i, funcaoData) { new Ajax.Request(url, {method:"get",contentType:'text/plain', onLoaded: function(){ if (funcaoData != null) { funcaoData(); } }, onSuccess: function (transport) { if (funcaoRetorno != null) { if(transport.readyState == 4) { if (celula != null){ funcaoRetorno(transport.responseText, celula, i); }else{ funcaoRetorno(transport.responseText); } } } }}); } function consulta_cep(valor) { if ((valor.length == 9)&&(valor != '_____-___')) { var CEP = valor; dados = RequisicaoHTTPSincrono('./consulta_cep.php?CEP='+encodeURIComponent(CEP), true); if (dados != 'NAO ENCONTRADO') { while (dados.indexOf('§') > -1) { linha = dados.substr(0, dados.indexOf('§')); dados = dados.substr(dados.indexOf('§') + 1); temp = linha.substr(0, linha.indexOf('=')); linha = linha.substr(linha.indexOf('=') + 1); eval('document.frm_cadastro.'+temp+'.value=linha'); } } else { document.frm_cadastro.NME_ENDERECO.value = ''; document.frm_cadastro.NME_BAIRRO.value = ''; document.frm_cadastro.NME_CIDADE.value = ''; document.frm_cadastro.SIG_ESTADO.value = '0'; } } } function get_radio_value(campo) { var retorno = 0; if (campo != null) { if (campo.length == null) retorno = campo.value; for (var i=0; i < campo.length; i++) { if (campo[i].checked) { retorno = campo[i].value; break; } } } return retorno; } function validaEmail(obj){ email = obj.value; new Ajax.Request('validaEmail.php', { method: 'get', parameters: 'email='+email, onSuccess: function(transport){ retorno = transport.responseText; if(retorno == 'invalido'){ alert('O domínio do e-mail é inválido!'); obj.value = ''; obj.focus(); } } }) } function atualizaHistoricoNew(){ new Ajax.Request('produtos_historico_cliques.php', { method: 'get', parameters: 'cod_produto='+_cod_produto, onSuccess: function(transport){ retorno = transport.responseText; alert(_cod_produto); document.getElementById('frm_historico').innerHTML = retorno; } }) }