//Totalmente feito por Ian Carvalho
function init(){
criarPrompt();
criarObrigatoriedade();
criarFonteMudar();
criarLinksExternos();
criarImagemMudar();
}
function addEvent(obj, evType, fn){
if(obj.addEventListener){
obj.addEventListener(evType, fn, false);
return true;
}else if (obj.attachEvent){
var r = obj.attachEvent('on'+evType, fn);
return r;
}else{
return false;
}
}
addEvent(window, "load", init);
/*
function fonte(acao){
alert("i");
ids = new Array("av");
tamanho = new Array();
for(i=0; i<ids.length; i++){
if(document.body.getElementById(ids[i]).currentStyle){
var tamanho[i] = document.body.getElementById(ids[i]).currentStyle.fontSize;
}else{
var tamanho[i] = document.body.defaultView.getComputedStyle(document.getElementById(ids[i]),null).getPropertyValue('font-size');
}
var tamanho[i] = tamanho[i].substring(0,2);
var tamanho[i] = eval(tamanho[i]);
alert(tamanho[i]);
}
if(acao=='+'){
for(i=0; i<ids.length; i++){
if(tamanho[i]<24){
document.getElementById(ids[i]).style.fontSize = tamanho[i] + 2 +"px";
}
}
}else if(acao=='-'){
if(tamanho>11){
document.body.style.fontSize = tamanho - 2 +"px";
}
}else if(acao=='='){
document.body.style.fontSize = "11px";
}
}
*/
function mostra(id){
var objeto = document.getElementById(id);
if(objeto.style.display=="none"){
objeto.style.display = "inline";
}else{
objeto.style.display = "none";
}
}
function esconde(id){
if(document.getElementById(id).style.display=='inline'){
mostra(id); 
} 
}
function obriga(id){
input = document.getElementById(id);
if(input.getAttribute('rel')=="obrigatorio"){
input.setAttribute('rel', '');
}else{
input.setAttribute('rel', 'obrigatorio');
}
}
function criarPrompt(){
if(document.getElementsByTagName){
var inputs = document.getElementsByTagName('input');
var textareas = document.getElementsByTagName('textarea');
for(var i=0; i<inputs.length; i++){
var input = inputs[i];
if(input.getAttribute('alt')){
if(input.value==""){
input.value = input.getAttribute('alt');
cor = input.style.color;
input.setAttribute('cor', cor);
input.style.color = 'gray';
input.onfocus = function onfocus(){
if(this.value==this.getAttribute('alt')){
this.style.color = this.getAttribute('cor');
this.value="";
} 
}
input.onblur = function onblur(){
if(this.value==""){
this.style.color = 'gray';
this.value=this.getAttribute('alt');
} 
}
}
}
}
for(var t=0; t<textareas.length; t++){
var textarea = textareas[t];
if(textarea.getAttribute('alt')){
if(textarea.value==""){
textarea.value = textarea.getAttribute('alt');
cor = textarea.style.color;
textarea.setAttribute('cor', cor);
textarea.style.color = 'gray';
textarea.onfocus = function onfocus(){
if(this.innerHTML==this.getAttribute('alt')){
this.style.color = this.getAttribute('cor');
this.value="";
} 
}
textarea.onblur = function onblur(){
if(this.value==""){
this.style.color = 'gray';
this.value = this.getAttribute('alt');
} 
}
}
}
}
}
}
function trim(str, chars) {
return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
} 
function rtrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function criarObrigatoriedade(){
formularios = document.forms;
for(var f=0; f<formularios.length; f++){
form = formularios[f];
form.onsubmit = function submit(){
campo = "";
this.setAttribute('vazio', '0');
inputs = this.elements;
for(var i=0; i<inputs.length; i++){
input = inputs[i];
if(input.getAttribute("rel")){
if(input.getAttribute("rel")=="obrigatorio"){
if(input.value==input.getAttribute('alt') || input.value.trim(" ", "")==""){
this.setAttribute('vazio', 'sim');
campo += "* "+input.getAttribute('target')+"\n";
}
}
}else{
if(this.getAttribute('vazio')=='0'){
if(input.getAttribute('alt')){
if(input.value==input.getAttribute('alt')){
input.value = "";
}
}
}
}
}
if(this.getAttribute('vazio')=="sim"){
alert("Por favor, preencha o(s) seguinte(s) campo(s) obrigatório(s):\n"+campo);
return false;
}else{
return true;
}
}
}
}
function criarFonteMudar(){
if(document.getElementsByTagName){
var inputs = document.getElementsByTagName('input');
var textareas = document.getElementsByTagName('textarea');
for(var i=0; i<inputs.length; i++){
var input = inputs[i];
if(input.getAttribute('b')){
input.onkeyup = function onkeyup(){
b = this.getAttribute('b');
if(b=='+'){
this.value = this.value.toUpperCase();
}else if(b=='-'){
this.value = this.value.toLowerCase();
}
}
}
}
}
}
function Abre(url, width, height){
window.open(url,"_blank","resizable=no,toolbar=no,status=no,menubar=no,scrollbars=yes,width=" + width + ",height=" + height).focus();
}
function desabilita(id){
elem = document.getElementById(id);
if(elem.disabled==true){
elem.disabled = false;
}else{
elem.disabled = true;
}
}
numero = 0;
function cria(conteudo, id, limite, id_link, botao_remover){
var documento = document.getElementById(id);
if(numero<limite){
numero++
var span = document.createElement('span');
span.setAttribute('id', numero);
documento.appendChild(span);
span.innerHTML = "<br />Carregando<blink>...</blink>";
if(botao_remover){
conteudo += "<a href=\"javascript: void(0)\" onclick=\"javascript: remove("+numero+", '"+id+"', '"+id_link+"');\">Remover</a><br />";
}
span.innerHTML = conteudo;
}
if(numero==limite){
document.getElementById(id_link).style.display = 'none';
}
}
function remove(id, id_pai, id_link){
pai = document.getElementById(id_pai);
elemento = document.getElementById(id);
pai.removeChild(elemento);
numero--
idlink = document.getElementById(id_link);
if(idlink.style.display=='none'){
document.getElementById(id_link).style.display = 'block';
}
if(numero_campo){
numero_campo--
}
}
function criarLinksExternos(){
if(document.getElementsByTagName){
var anchors = document.getElementsByTagName('a');
for(var i=0; i<anchors.length; i++){
var anchor = anchors[i];
if(anchor.getAttribute("href") && anchor.getAttribute('rel')=='externo'){
anchor.target = '_blank';
var title = anchor.title + ' (Este link abre uma nova janela)';
anchor.title = title;
}
}
}
}
function criarImagemMudar(){
imagens = document.getElementsByTagName('img');
for(var i=0; i<imagens.length; i++){
imagem = imagens[i];
if(imagem.getAttribute('abbr')){
imagem.onmouseover = function mouseover(){
this.setAttribute('rel', this.src);
this.src = this.getAttribute('abbr');
}
imagem.onmouseout = function mouseout(){
this.src = this.getAttribute('rel');
}
}
}
}
//Fazer essa função dinâmica-------------------------------------------------------------
function txtBoxFormat(id, sMask, evtKeyPress) {
var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
if(document.all){ // Internet Explorer
nTecla = evtKeyPress.keyCode; 
}else if(document.layers){
nTecla = evtKeyPress.which;
}
sValue = document.getElementById(id).value;
// Limpa todos os caracteres de formatação que
// já estiverem no campo.
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( " ", "" );
fldLen = sValue.length;
mskLen = sMask.length;
i = 0;
nCount = 0;
sCod = "";
mskLen = fldLen;
while (i <= mskLen) {
bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
if (bolMask) {
sCod += sMask.charAt(i);
mskLen++; 
}else{
sCod += sValue.charAt(nCount);
nCount++;
}
i++;
}
document.getElementById(id).value = sCod;
if (nTecla != 8) { // backspace
if (sMask.charAt(i-1) == "9") { // apenas números...
return ((nTecla > 47) && (nTecla < 58)); 
}else{ // qualquer caracter...
return true;
} 
}else{
return true;
}
}
function ajaxInit(){
var xmlhttp;
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(E){
xmlhttp = false;
}
}
if(!xmlhttp && typeof XMLHttpRequest != 'undefined' ){
try{
xmlhttp = new XMLHttpRequest();
}catch(e){
xmlhttp = false ;
}
}
return xmlhttp;
}
// cria a fila
fila=[];
ifila=0;
ajax = ajaxInit();
// funcao que carrega uma pagina dentro de um objeto qualquer
function carrega(url, destino){
// coloca o texto "carregando" no objeto de carregamento
document.getElementById(destino).innerHTML="Carregando...";
// adiciona o item na fila
fila[fila.length]=[url,destino];
// se a fila estiver vazia, inicia a execução
if((ifila+1)==fila.length)ajaxRun();
}
//Executa a próxima conexão da fila
function ajaxRun(){
// carrega os dados da pagina da fila
url        = fila[ifila][0];
destino    = fila[ifila][1];
// carrega a pagina
ajax.open("POST", url, true);  
// headers
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
ajax.setRequestHeader("Pragma", "no-cache");
// ao completar o carregamento
ajax.onreadystatechange=function() {
if (ajax.readyState==4){
// coloca o valor no objeto requisitado
texto=unescape(ajax.responseText.replace(/\+/g," "));
document.getElementById(destino).innerHTML=texto;
// executa a proxima requisição da lista (se existir)
ifila++;
if(ifila<fila.length)setTimeout("ajaxRun()",20);
}
}
// executa
ajax.send(url);
}
// função que le todos os campos de um form e envia!
function enviaForm(frmNome, url, destino){
// Captura o form
f = document.getElementById(frmNome);
// inicializa nova URL
var query=url;
//Percorre elementos do formulario
for (i=0;i<f.elements.length;i++){
// se for a primeira variavel adiciona o "?" senao adiciona "&"
query += i==0 ? '?' : '&';
// concatena a variavel na query
query += f.elements[i].name + '=' + f.elements[i].value;
}
// envia o formulario
carrega(query, destino);
}
function confirma(texto, pag){
alerta = window.confirm(texto);
if(alerta){
window.location.href = pag;
}
}