/************************************************
INICIO DAS FUNÇÕES AJAX
************************************************/

// JavaScript Document 
// FUNÇÃO RESPONSÁVEL DE CONECTAR A UMA PAGINA EXTERNA NO NOSSO CASO A BUSCA_NOME.PHP 
// E RETORNAR OS RESULTADOS 

//Exemplo de utilização
//function pesquisa(valor) { 
	//FUNÇÃO QUE MONTA A URL E CHAMA A FUNÇÃO AJAX 
	//url="busca_nome.php?valor="+valor; 
	//ajax(url); 
//}

var xmlhttp = null;

function ajax(pUrl) {

    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 ;
        }
    }

    if (xmlhttp) {
        xmlhttp.onreadystatechange = processReqChange;
        xmlhttp.open("GET", pUrl);
        xmlhttp.setRequestHeader('Content-Type','text/xml');
        xmlhttp.setRequestHeader('encoding','ISO-8859-1');
        xmlhttp.send(null);
    }

}

function processReqChange() {
	// apenas quando o estado for "completado"
	if (xmlhttp.readyState == 4) {
		// apenas se o servidor retornar "OK"
		if (xmlhttp.status == 200) {
			document.getElementById('dBody').innerHTML = xmlhttp.responseText;
		} else {
			alert("Houve um problema ao obter os dados:\n" + xmlhttp.statusText);
		}
	}
}

/************************************************
FIM DAS FUNÇÕES AJAX
************************************************/

/************************************************
INICIO DAS FUNÇÕES DIVERSAS
************************************************/

var Rollar = "sim"; /*Não Altere faz parte do EFEITO*/
var tipo = "top"; /*Altere para usar as opções LEFT, RIGHT, TOP e DOWN*/
var roll = 0;  /*Não Altere faz parte do EFEITO*/
var tmp = 40; /*Defina a velocidade aqui*/

function Fmarquee(){
    var Marquee = document.getElementById("marquee");
    var Caixa = document.getElementById("caixa");
    var Altura = 0;

    if(tipo=="left" || tipo=="right"){
        Altura = Marquee.clientWidth;
    }else if(tipo=="down" || tipo=="top"){
        Altura = Marquee.clientHeight;
    }

    roll++;

    if(Altura==roll){

        if ((tipo=="top")||(tipo=="down")) {
            roll = -Caixa.clientHeight;
        } else {
            roll = -Caixa.clientWidth;
        }
    }

    if(tipo == "top"){
        Marquee.style.marginTop = (roll*-1)+"px";
    } else if (tipo == "down"){
        Marquee.style.marginTop = roll+"px";
    } else if (tipo == "left"){
        Marquee.style.marginLeft = (roll*-1)+"px";
    } else if (tipo == "right"){
        Marquee.style.marginLeft = roll+"px";
    }

    if(Rollar == "sim"){
        setTimeout("Fmarquee()",tmp);
    }

    if ((tipo!="top")&&(tipo!="down")) {
        if (Marquee.style.marginLeft==(Caixa.clientLeft-1)+"px") {
            stopM();
            setTimeout("playM()",5000);
        }
    } else {
        if (Marquee.style.marginTop==(Caixa.clientTop-1)+"px") {
            stopM();
            setTimeout("playM()",5000);
        }
    }
}
function playM(){
    Rollar = "sim";
    Fmarquee();
}

function stopM(){
    Rollar = "não";
}

function zebrar(pTable, pBackground, pColor) {
	var tabela = document.getElementById(pTable);
	var linhas = tabela.getElementsByTagName("tr");
	for (var i = 0; i < linhas.length; i++) {
		if ((i % 2) == 0) {
			linhas[i].style.background = pBackground;
			linhas[i].style.color = pColor; 
		} else {
			linhas[i].style.background = "#FFF";
			linhas[i].style.color = "#000";
		}
	}
}

var wApaga = '';

function setPosition(objO, objD) {
	var wPad = ((document.body.offsetWidth - 770) / 2);
	if (wPad < 0) {
		wPad = 0;
	}
	objD.style.display = 'block';
	objD.style.position = 'absolute';
	objD.style.top = objO.offsetTop + objO.offsetHeight + 1;
	objD.style.left = objO.offsetLeft + wPad;
}

function setPositionL(objO, objD) {
	var wPad = ((document.body.offsetWidth - 770) / 2);
	if (wPad < 0) {
		wPad = 0;
	}
	objD.style.display = 'block';
	objD.style.position = 'absolute';
	objD.style.left = objO.offsetLeft + objO.offsetWidth + wPad;
	objD.style.top = objO.offsetTop + 5;
}

function hideMenu(pMenu) {
	str = 'apagaMenu('+pMenu+')';
	wApaga = setTimeout(str, 500);
}

function apagaMenu(pMenu) {
	pMenu.style.display = 'none';
}

function noHide() {
	clearTimeout(wApaga);
}

function getPagina(pPagina, pFrame) {
	if (pFrame != '') {
		pFrame.location.href = pPagina;
	} else {
		location.href= pPagina;
	}
}

/************************************************
FIM DAS FUNÇÕES DIVERSAS
************************************************/

/************************************************
INICIO DAS FUNÇÕES DE MENU
************************************************/

/* ----------------------- 
Para executar essas funções precisa definir 
<body onload="vertical();horizontal();">
----------------------- */

function vertical() {
	var navItems = document.getElementById("nav").getElementsByTagName("li");
	for (var i=0; i< navItems.length; i++) {
		if(navItems[i].className == "submenu") {
			navItems[i].onmouseover=function() {this.getElementsByTagName('ul')[0].style.display="block";this.style.backgroundColor = "#f9f9f9";}
			navItems[i].onmouseout=function() {this.getElementsByTagName('ul')[0].style.display="none";this.style.backgroundColor = "#FFFFFF";}
		}
	}
}

function horizontal() {
	var navItems = document.getElementById("barra").getElementsByTagName("li");
	for (var i=0; i< navItems.length; i++) {
		if((navItems[i].className == "menuvertical") || (navItems[i].className == "submenu")) {
			if(navItems[i].getElementsByTagName('ul')[0] != null) {
				navItems[i].onmouseover=function() {this.getElementsByTagName('ul')[0].style.display="block";this.style.backgroundColor = "#f9f9f9";}
				navItems[i].onmouseout=function() {this.getElementsByTagName('ul')[0].style.display="none";this.style.backgroundColor = "#FFFFFF";}
			}
		}
	}
}

/************************************************
FIM DAS FUNÇÕES DE MENU
************************************************/

/************************************************
INICIO DAS FUNÇÕES DE DATA
************************************************/

dias = new Array(7);
dias[0] = "Domingo";
dias[1] = "Segunda-Feira";
dias[2] = "Terça-Feira";
dias[3] = "Quarta-Feira";
dias[4] = "Quinta-Feira";
dias[5] = "Sexta-Feira";
dias[6] = "Sábado";

meses = new Array(12);
meses[0] = "Janeiro";
meses[1] = "Fevereiro";
meses[2] = "Março";
meses[3] = "Abril";
meses[4] = "Maio";
meses[5] = "Junho";
meses[6] = "Julho";
meses[7] = "Agosto";
meses[8] = "Setembro";
meses[9] = "Outubro";
meses[10] = "Novembro";
meses[11] = "Dezembro";

function getHora(sender) {
	setTimeout("getHora(tRelogio)",1000);
	wTexto = '';
	wHorario = '';
	wCum = '';
	dat = new Date();
		dia = dat.getDay();
		diaM = dat.getDate();
		diaM < 10 ? diaM = "0" + diaM: diaM = diaM;
		mes = dat.getMonth();
		ano = dat.getYear();
		hora = dat.getHours();
		hora < 10 ? hora = "0" + hora: hora = hora;
		minut = dat.getMinutes();
		minut < 10 ? minut = "0" + minut: minut = minut;
		seg = dat.getSeconds();
		seg < 10 ? seg = "0" + seg: seg = seg;
	wTexto = dias[dia] + ", " + diaM + " de " + meses[mes] + " de " + ano;
	wHorario = hora + ":" + minut + ":" + seg;
	if (hora < 12) {
		wCum = "Bom-dia";
	} else if (hora < 18) {
		wCum = "Boa-tarde";
	} else {
		wCum = "Boa-noite";
	}
	sender.value = wCum + ", " + wTexto + ". " + wHorario;
	window.status = ".:Reason Soft:. - " + wCum + ", " + wTexto + ". " + wHorario;
}

/************************************************
FIM DAS FUNÇÕES DE DATA
************************************************/

/************************************************
INICIO DAS FUNÇÕES MATEMÁTICAS
************************************************/

function numPrimos(n) {
	if (n == 2) {
		return '2' + ' ';
	}
	if ((n % 2 == 0) && (n != 2)) {
		return numPrimos(n - 1);
	}
	cont = 0;
	for (x = 3; x <= Math.round(Math.sqrt(i)); x++) {
		if (i % x == 0) {
			cont++;
		}
		if (cont > 2) {
			cont = 0;
			break;
		}
	}
	if (cont == 2) {
		return n + ' ' + numPrimos(n - 1);
	}
}

function Logica(v1, v2, op) {
v1 == 1 ? v1 = true: v1 = false;
v2 == 1 ? v2 = true: v2 = false;
	if (op == "E") {
		if (v1 && v2) {
			return "Verdadeiro";
		} else {
			return "Falso";
		}
	} else {
		if (v1 || v2) {
			return "Verdadeiro";
		} else {
			return "Falso";
		}
	}
}

function isNum(n) {
	if (isNaN(n)) {
		alert(n + ' não é um nº válido!');
		return false;
	} else {
		return true;
	}
}

function fatorial(n) {
	if (n < 1) {
		return 0;
	} else if (n == 1) {
		return 1;
	} else {
		return n * fatorial(n - 1);
	}
}

function soma(n1, n2) {
	return parseFloat(n1) + parseFloat(n2);
}

function subtrai(n1, n2) {
	return parseFloat(n1) - parseFloat(n2);
}

function multiplica(n1, n2) {
	return parseFloat(n1) * parseFloat(n2);
}

function divide(n1, n2) {
	if (n2 == 0) {
		return "Não é possível dividir por 0";
	} else {
		return parseFloat(n1) / parseFloat(n2);
	}
}

function raiz(n) {
	if (n >= 0) {
		return Math.sqrt(n);
	} else {
		return "Não existe raiz real de nº negativo";
	}
}

/************************************************
FIM DAS FUNÇÕES MATEMÁTICAS
************************************************/

/************************************************
INICIO DAS FUNÇÕES DE STRINGS
************************************************/

function setMasc(src, masc) {
	var campo = src.value.length;
	var saida = mascara.substring(1,0);
	var texto = mascara.substring(campo);
	if (texto.substring(0,1) != saida) {
		src.value += texto.substring(0,1);
	}
}
//Exemplo de uso
// onkeypress="setMasc(this, '#####-###')"


function Mascara (formato, keypress, objeto){
	campo = eval (objeto);

	// cep
	if (formato=='cep'){
		separador = '-';
		conjunto1 = 5;
		if (campo.value.length == conjunto1){
			campo.value = campo.value + separador;
		}
	}
	
	// cpf
	if (formato=='cpf'){
		separador1 = '.'; 
		separador2 = '-'; 
		conjunto1 = 3;
		conjunto2 = 7;
		conjunto3 = 11;
		if (campo.value.length == conjunto1) {
  			campo.value = campo.value + separador1;
		}
		if (campo.value.length == conjunto2) {
			campo.value = campo.value + separador1;
	  	}
		if (campo.value.length == conjunto3) {
	  		campo.value = campo.value + separador2;
	  	}
	}

	// nascimento
	if (formato=='nascimento'){
		separador = '/'; 
		conjunto1 = 2;
		conjunto2 = 5;
		if (campo.value.length == conjunto1) {
			campo.value = campo.value + separador;
		}
		if (campo.value.length == conjunto2) {
			campo.value = campo.value + separador;
		}
	}

	// telefone
	if (formato=='telefone'){
		separador1 = '(';
		separador2 = ')';
		separador3 = '-';
		conjunto1 = 0;
		conjunto2 = 3;
		conjunto3 = 8;
		if (campo.value.length == conjunto1) {
			campo.value = campo.value + separador1;
		}
		if (campo.value.length == conjunto2) {
			campo.value = campo.value + separador2;
		}
		if (campo.value.length == conjunto3) {
			campo.value = campo.value + separador3;
		}
	}
	
	//Exemplo
	//onkeypress="Mascara('cep', window.event.keyCode, 'document.fBusca.tCEP');"
}

/************************************************
FIM DAS FUNÇÕES DE STRINGS
************************************************/

/************************************************
INICIO DAS FUNÇÕES DE CPF/CNPJ
************************************************/

function geraCPF(pPonto) {

	n1 = Math.round(Math.random() * 9);
	n2 = Math.round(Math.random() * 9);
	n3 = Math.round(Math.random() * 9);
	n4 = Math.round(Math.random() * 9);
	n5 = Math.round(Math.random() * 9);
	n6 = Math.round(Math.random() * 9);
	n7 = Math.round(Math.random() * 9);
	n8 = Math.round(Math.random() * 9);
	n9 = Math.round(Math.random() * 9);

	d1 = (n9*2)+(n8*3)+(n7*4)+(n6*5)+(n5*6)+(n4*7)+(n3*8)+(n2*9)+(n1*10);
	d1 = 11 - (d1 % 11);
	if (d1 > 9) {
		d1 = 0;
	}

	d2 = (d1*2)+(n9*3)+(n8*4)+(n7*5)+(n6*6)+(n5*7)+(n4*8)+(n3*9)+(n2*10)+(n1*11);
	d2 = 11 - (d2 % 11);
	if (d2 > 9) {
		d2 = 0;
	}

	if (!pPonto) {
		cpf = ''+n1+''+n2+''+n3+''+n4+''+n5+''+n6+''+n7+''+n8+''+n9+''+d1+''+d2+'';
	} else {
		cpf = ''+n1+''+n2+''+n3+'.'+n4+''+n5+''+n6+'.'+n7+''+n8+''+n9+'-'+d1+''+d2+'';
	}
	return cpf;
	
}

function geraCNPJ(pPonto) {
	
	//Primeiro Bloco
	n1 = Math.round(Math.random() * 9);
	n2 = Math.round(Math.random() * 9);
	n3 = Math.round(Math.random() * 9);
	n4 = Math.round(Math.random() * 9);
	n5 = Math.round(Math.random() * 9);
	n6 = Math.round(Math.random() * 9);
	n7 = Math.round(Math.random() * 9);
	n8 = Math.round(Math.random() * 9);
	
	//Segundo Bloco
	 n9 = 0;
	n10 = 0;
	n11 = 0;
	n12 = 1;
	
	//Digitos Verificadores
	
	//1º Digito
	d1 = (n12*2)+(n11*3)+(n10*4)+(n9*5)+(n8*6)+(n7*7)+(n6*8)+(n5*9)+(n4*2)+(n3*3)+(n2*4)+(n1*5);
	d1 = d1 % 11;
	if (d1 <= 2) {
		d1 = 0;
	} else {
		d1 = 11 - d1;
	}
	
	//2º Digito
	d2 = (d1*2)+(n12*3)+(n11*4)+(n10*5)+(n9*6)+(n8*7)+(n7*8)+(n6*9)+(n5*2)+(n4*3)+(n3*4)+(n2*5)+(n1*6);
	d2 = d2 % 11;
	if (d2 <= 2) {
		d2 = 0;
	} else {
		d2 = 11 - d2;
	}
	
	if (!pPonto) {
		cnpj = ''+n1+''+n2+''+n3+''+n4+''+n5+''+n6+''+n7+''+n8+''+n9+''+n10+''+n11+''+n12+''+d1+''+d2+'';
	} else {
		cnpj = ''+n1+''+n2+'.'+n3+''+n4+''+n5+'.'+n6+''+n7+''+n8+'/'+n9+''+n10+''+n11+''+n12+'-'+d1+''+d2+'';
	}
	return cnpj;
	
}

/************************************************
FIM DAS FUNÇÕES DE CPF/CNPJ
************************************************/
