/* * Funções JavaScrip utilizadas por todo o sistema * * @category includes * @package scripts * @author Christian Paul Pach * @copyright 1991-2008 ClickPronto Soluções Inteligentes * @license http://www.clickpronto.com/license/ae/aesimulador/1.0.txt ClickPronto-AE Simulador License 1.0 * @version CVS: :$ * @link includes/js/ * @since Arquivo disponível desde a versão 1.0.0 */ //Procura um CEP na tabela de CEPs e caso encontre, devolve os valores nos campos passados por parametro //onblur="if(this.value.length==9) getCep(this.value,'endereco,bairro,cidade,estado');" //resultado = seta o value dos campos endereco bairro cidade e estado com os valores vindos da tabela //compatibilidade IE6+ Firefox function getCep(cep,campos){ var ajax = new AJAX(); ajax.url='../../modules/ger/getCep.php?cep='+cep+'&campos='+campos; ajax.modo = 'T'; ajax.metodo = 'GET'; ajax.processaresultado = function(res) { extraiScript(res,ajax.url); } ajax.conectar(); } function setCep(campo,valor){ var obj=document.getElementById(campo); if(obj && obj.type){ if(obj.type=='select-one' || obj.type=='select-one'){ selsel(obj,valor); }else if(obj.type == 'password' || obj.type == 'text' || obj.type == 'textarea' || obj.type == 'hidden'){ obj.value=valor; } } } //Formata uma string com base em uma mascara de entrada //onkeyup="mascara(this, '###-####', event);" //resultado = ABC-1234 formatando a string para o formato de placa automotiva //na mascara # representam caracteres alfanuméricos, e os caracteres //[-] - menos //[.] - ponto //[ ] - espaço //[(] - parentese //[)] - parentese //são considerados como separadores //compatibilidade IE6+ Firefox function mascara(campo, Mascara, evento){ var boleanoMascara; var Digitato = evento.keyCode; exp = /\-|\.|\/|\(|\)| /g campoSoNumeros = campo.value.toString().replace( exp, "" ); var posicaoCampo = 0; var NovoValorCampo=""; var TamanhoMascara = campoSoNumeros.length; if (Digitato != 8 && Digitato != 9) { // backspace for(i=0; i<= TamanhoMascara; i++) { boleanoMascara = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".") || (Mascara.charAt(i) == "/")) boleanoMascara = boleanoMascara || ((Mascara.charAt(i) == "(") || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " ")) if (boleanoMascara) { NovoValorCampo += Mascara.charAt(i); TamanhoMascara++; }else { NovoValorCampo += campoSoNumeros.charAt(posicaoCampo); posicaoCampo++; } } campo.value = NovoValorCampo.substr(0,Mascara.length); return true; }else { return true; } } //Submete um formulário em um objeto //submete(form1,div1); //resultado = o conteúdo html retornado ao submeter o formulário form1 será devolvido no objeto com id div1 //dependencia: enviaForm, extraiScript, ajax.js //compatibilidade IE6+ Firefox function submete(cad,div){ div = (div) ? div : 'direito'; var ajax = new AJAX(); ajax.url=cad.action; ajax.modo = 'T'; ajax.metodo = cad.method.toUpperCase(); if(ajax.metodo=='GET'){ ajax.url=ajax.url+enviaForm(cad); }else{ ajax.params = enviaForm(cad); } ajax.processaresultado= function(conteudo){ document.getElementById(div).innerHTML = conteudo; extraiScript(conteudo,ajax.url); } ajax.conectar(); } function enviaForm(cad){ var objeto; if(cad.method.toUpperCase() == "GET"){ var query = '?'; }else if(cad.method.toUpperCase() == "POST"){ var query = ''; } for (i=0;i0){ for(j=0;j", ini) + 1; //Verifica se este e um bloco de script ou include para um arquivo de scripts if(pos_src < ini && pos_src >=0){//Se encontrou um "src" dentro da tag script, esta e um include de um arquivo script //Marca como sendo o inicio do nome do arquivo para depois do src ini = pos_src + 4; //Procura pelo ponto do nome da extencao do arquivo e marca para depois dele fim = texto.indexOf(".", ini)+4; //Pega o nome do arquivo codigo = texto.substring(ini,fim); //Elimina do nome do arquivo os caracteres que possam ter sido pegos por engano codigo = codigo.replace("=","").replace(" ","").replace("\"","").replace("\"","").replace("\'","").replace("\'","").replace(">",""); // Adiciona o arquivo de script ao objeto que sera adicionado ao documento objScript.src = url+codigo; }else{//Se nao encontrou um "src" dentro da tag script, esta e um bloco de codigo script // Procura o final do script fim = texto.indexOf("','popAjuda','','318','350','',''); //abre('all_help.php','help','width=318,height=350,scrollbars=1'); if(e.preventDefault){ e.preventDefault(); }else{ e.returnValue=false; } return false; } } document.onhelp = new Function("return false;"); document.onkeydown=cppAjuda; function numero2moeda(num) { num = num.toString().replace(/$|\,/g,''); if(isNaN(num)) num = "0"; sign = (num == (num = Math.abs(num))); num = Math.floor(num*100+0.50000000001); cents = num%100; num = Math.floor(num/100).toString(); if(cents<10) cents = "0" + cents; for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) num = num.substring(0,num.length-(4*i+3))+'.'+ num.substring(num.length-(4*i+3)); //return (((sign)?'':'-') + 'R$' + num + ',' + cents); return (((sign)?'':'-') + '' + num + ',' + cents); } // getElementById Special to handle quirky browsers function getElementById(id){ var obj = null; if(document.getElementById){ /* Prefer the widely supported W3C DOM method, if available:- */ obj = document.getElementById(id); }else if(document.all){ /* Branch to use document.all on document.all only browsers. Requires that IDs are unique to the page and do not coincide with NAME attributes on other elements:- */ obj = document.all[id]; } /* If no appropriate element retrieval mechanism exists on this browser this function always returns null:- */ return obj; } //verifica_data_maior('15/01/2007','16/01/2007'); true se data1 maior que data2 seno false function verifica_data_maior(data1,data2,igual){ if(igual=='1' && data1==data2) return true; var ano1,ano2,mes1,mes2,dia1,dia2; dia1=parseInt(data1.substr(0,2),10); mes1=parseInt(data1.substr(3,2),10); ano1=parseInt(data1.substr(6,4),10); dia2=parseInt(data2.substr(0,2),10); mes2=parseInt(data2.substr(3,2),10); ano2=parseInt(data2.substr(6,4),10); data1=new Date(ano1,mes1-1,dia1); data2=new Date(ano2,mes2-1,dia2); if(data1.getTime()>data2.getTime()){ return true; }else{ return false; } } function verifica_data(sdata){ day2 = sdata.substr(0,2); month2 = sdata.substr(3,2); year2 = sdata.substr(6,4); if ((sdata.substr(2,1) != "/") || (sdata.substr(5,1) != "/")) { day2 = ""; month2 = ""; year2 = ""; } var DayArray = new Array(31,28,31,30,31,30,31,31,30,31,30,31); var MonthArray = new Array("01","02","03","04","05","06","07","08","09","10","11","12"); var inpDate = day2 + month2 + year2; var filter=/^[0-9]{2}[0-9]{2}[0-9]{4}$/; //Check ddmmyyyy date supplied if (! filter.test(inpDate)) { return false; } /* Check Valid Month */ filter=/01|02|03|04|05|06|07|08|09|10|11|12/; if (! filter.test(month2)) { return false; } /* Check For Leap Year */ var N = Number(year2); if ( ( N%4==0 && N%100 !=0 ) || ( N%400==0 ) ) { DayArray[1]=29; } /* Check for valid days for month */ for(var ctr=0; ctr<=11; ctr++) { if (MonthArray[ctr]==month2) { if (day2<= DayArray[ctr] && day2 >0 ) { inpDate = day2 + '/' + month2 + '/' + year2; return true; } else { return false; } } } } function verifica_email(imeiu){ erro=0; if(imeiu.indexOf('@')<=0){ erro=1; }else if(imeiu.indexOf('.')== -1 || imeiu.indexOf('.')==imeiu.indexOf('@')+1){ erro=1; }else if(imeiu.length -1 == imeiu.indexOf('.')){ erro=1; }else{ erro=1; for(i=imeiu.indexOf('@')+1;ip.value.indexOf('');i--){ if(p.value.charAt(i)=='0'){ p.value=p.value.substring(0,i); }else{ break; } } if(p.value.indexOf('')==p.value.length-1) p.value=p.value.substring(0,p.value.length-1); } p.select(); } function format_number2(pnumber,decimals){ var strNumber = new String(pnumber); var arrParts = strNumber.split('.'); var intWholePart = parseInt(arrParts[0],10); var strResult = ''; if (isNaN(intWholePart)) intWholePart = '0'; if(arrParts.length > 1) { var decDecimalPart = new String(arrParts[1]); var i = 0; var intZeroCount = 0; while ( i < String(arrParts[1]).length ) { if( parseInt(String(arrParts[1]).charAt(i),10) == 0 ) { intZeroCount += 1; i += 1; } else break; } decDecimalPart = parseInt(decDecimalPart,10)/Math.pow(10,parseInt(decDecimalPart.length-decimals-1)); Math.round(decDecimalPart); decDecimalPart = parseInt(decDecimalPart)/10; decDecimalPart = Math.round(decDecimalPart); if(decDecimalPart==Math.pow(10, parseInt(decimals))) { intWholePart+=1; decDecimalPart="0"; } var stringOfZeros = new String(''); i=0; if( decDecimalPart > 0 ) { while( i < intZeroCount) { stringOfZeros += '0'; i += 1; } } decDecimalPart = String(intWholePart) + "." + stringOfZeros + String(decDecimalPart); var dot = decDecimalPart.indexOf('.'); if(dot == -1) { decDecimalPart += '.'; dot = decDecimalPart.indexOf('.'); } var l=parseInt(dot)+parseInt(decimals); while(decDecimalPart.length <= l) { decDecimalPart += '0'; } strResult = decDecimalPart; } else { var dot; var decDecimalPart = new String(intWholePart); decDecimalPart += '.'; dot = decDecimalPart.indexOf('.'); var l=parseInt(dot)+parseInt(decimals); while(decDecimalPart.length <= l) { decDecimalPart += '0'; } strResult = decDecimalPart; } return strResult; } function formata_numero_cpp(p,d){ var r; p.value=valor2js(p.value); if(p.value<0){ p.value=-p.value; r=p.value; r="-"+r; }else{ r=p.value; } p.value=js2valor(format_number2(r,d)); p.value=formataMoedaSimple(p.value,d); } function formataMoedaSimple(val,casas){ var i, uBound; var negativo = false; if(!casas) casas=2; invertedSTR = new String(""); //verifica se o valor que esta vindo e negativo if(val.charAt(0) == "-") negativo = true; for(i=0;i="0" && val.charAt(i)<="9") invertedSTR = invertedSTR + val.charAt(i); } if(val.charAt(val.length-casas)=='') invertedSTR = invertedSTR + '0'; invertedSTR = inverteString ( invertedSTR ); if ( invertedSTR.length < 15+casas ){ UBound = invertedSTR.length; }else{ UBound = 15+casas; } for(j=0;j<=casas;j++){ if ( invertedSTR.length == j ){ UBound = casas+1; invertedSTR = invertedSTR + criazeros(casas-j+1); } } STR = new String(""); for ( i=0; i0){ for(i=0;i0){ for(i=0;i=0){ for(i=0;i0;f--){ dig+=parseInt(CNUMB.charAt(f-1))*ind; if (CTYPE=='CNPJ'){ if(ind>8){ ind=2 }else{ ind++ } }else{ ind++ } } dig%=11; if(dig<2){ dig=0; }else{ dig=11-dig; } if(dig!=parseInt(CNUMB.charAt(g))){ return(false); }else{ return(true); } } //verifica_cpfcnpj(numero,tipo); tipo=CPF ou CNPJ function verifica_cpfcnpj(CNUMB,CTYPE){ CNUMB = CNUMB.replace('/',''); CNUMB = CNUMB.replace('-',''); CNUMB = CNUMB.replace('.',''); CNUMB = CNUMB.replace('.',''); CNUMB = CNUMB.replace('.',''); // alert(CNUMB); if((CTYPE=='CPF' && CNUMB.length!=11) || (CTYPE=='CNPJ' && CNUMB.length!=14) || (CTYPE=='CPF' && CNUMB==77777777777)) return false; CNUMB=ParseNumb(CNUMB) if(CNUMB == 0){ return(false); }else{ g=CNUMB.length-2; if(TestDigit(CNUMB,CTYPE,g)){ g=CNUMB.length-1; if(TestDigit(CNUMB,CTYPE,g)){ return(true); }else{ return(false); } }else{ return(false); } } } //desabilita todos os campos de um form uso = desabilita(nomedoformulario); function desabilita(form){ for(i=0;i // //document.getElementById('objetoQueQuandoClicadoAtivaODragDrop').onmousedown= function(ev){ mouseOffset = getMouseOffset(document.getElementById('objetoQueSeraArrastado'), ev); makeDraggable(document.getElementById('objetoQueSeraArrastado')); } document.onmousemove = mouseMove; document.onmouseup = mouseUp; var dragObject = null; var mouseOffset = null; function mouseCoords(ev){ if(ev.pageX || ev.pageY){ return {x:ev.pageX, y:ev.pageY}; } if(!document.body) return {x:0, y:0}; return { x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, y:ev.clientY + document.body.scrollTop - document.body.clientTop }; } function getMouseOffset(target, ev){ ev = ev || window.event; var docPos = getPosition(target); var mousePos = mouseCoords(ev); return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y}; } function getPosition(e){ var left = 0; var top = 0; while (e.offsetParent){ left += e.offsetLeft; top += e.offsetTop; e = e.offsetParent; } left += e.offsetLeft; top += e.offsetTop; return {x:left, y:top}; } function mouseMove(ev){ ev = ev || window.event; var mousePos = mouseCoords(ev) if(dragObject){ dragObject.style.position = 'absolute'; if(parseInt(dragObject.style.marginTop)<0){ dragObject.style.top = mousePos.y - mouseOffset.y + parseInt(dragObject.style.height)/2+'px'; }else{ dragObject.style.top = mousePos.y - mouseOffset.y+'px'; } if(parseInt(dragObject.style.marginLeft)<0){ dragObject.style.left = mousePos.x - mouseOffset.x + parseInt(dragObject.style.width)/2+'px'; }else{ dragObject.style.left = mousePos.x - mouseOffset.x+'px'; } return false; } } function mouseUp(){ dragObject = null; } function makeDraggable(item){ if(!item) return; dragObject = item; return false; } // // function tochar(texto,tamanho){ //transforma varchar em char p/ comparacoes adicionando espaos em branco if(texto.length0){ for(i=0;i0){ for(i=0;i