
/***********************************************
* Cool DHTML tooltip script II- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var offsetfromcursorX=12 //Customize x offset of tooltip
var offsetfromcursorY=10 //Customize y offset of tooltip

var offsetdivfrompointerX=10 //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=14 //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

document.write('<div id="dhtmltooltip"></div>') //write out tooltip DIV
document.write('<img id="dhtmlpointer" src="http://www.freeshop.com.br/brindes/imagens/arrow2.gif">') //write out pointer image
//document.write('<img id="dhtmlpointer" src="http://dados/site/brindes/imagens/arrow2.gif">') //write out pointer image

var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
	var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

var pointerobj=document.all? document.all["dhtmlpointer"] : document.getElementById? document.getElementById("dhtmlpointer") : ""

function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thewidth, thecolor){
	if (ns6||ie){
		if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
		if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
		tipobj.innerHTML=thetext
		enabletip=true
		return false
	}
}

function positiontip(e){
	if (enabletip){
		var nondefaultpos=false
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20
		var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20
		
		var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX
		var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY
		
		var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000

		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge < tipobj.offsetWidth){
			//move the horizontal position of the menu to the left by it's width
			tipobj.style.left=curX-tipobj.offsetWidth+"px"
			nondefaultpos=true
		}
		else if (curX<leftedge)
			tipobj.style.left="5px"
		else{
			//position the horizontal position of the menu where the mouse is positioned
			tipobj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px"
			pointerobj.style.left=curX+offsetfromcursorX+"px"
		}

		//same concept with the vertical position
		if (bottomedge<tipobj.offsetHeight){
			tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px"
			nondefaultpos=true
		}
		else{
			tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px"
			pointerobj.style.top=curY+offsetfromcursorY+"px"
		}
		tipobj.style.visibility="visible"
	if (!nondefaultpos)
		pointerobj.style.visibility="visible"
	else
		pointerobj.style.visibility="hidden"
	}
}

function hideddrivetip(){
	if (ns6||ie){
		enabletip=false
		tipobj.style.visibility="hidden"
		pointerobj.style.visibility="hidden"
		tipobj.style.left="-1000px"
		tipobj.style.backgroundColor=''
		tipobj.style.width=''
	}
}
document.onmousemove=positiontip


//==========================================================================================================================
// Função Ajax - Nova
//==========================================================================================================================
function createXMLHTTP(){
	try{ajax = new ActiveXObject("Microsoft.XMLHTTP");} 
	catch(e){
		try{
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
			alert(ajax);
		}
	catch(ex){
		try{ajax = new XMLHttpRequest();}
	catch(exc){
		alert("Esse browser não tem recursos para uso do Ajax");
		ajax = null;
		}
	}
	return ajax;
}

var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
for (var i=0; i < arrSignatures.length; i++){
	try{
		var oRequest = new ActiveXObject(arrSignatures[i]);
		return oRequest;
	} 
	catch (oError){}
	}
	throw new Error("MSXML is not installed on your system.");
}


//==========================================================================================================================
// Função valida campos obrigatórios
//==========================================================================================================================
function obriga(campo,rotulo){
	var resultado = true;
	
	if (campo.value == ""){
		alert('Você deve preencher o campo "' + rotulo + '" para prosseguir!');
		campo.focus();
		resultado = false;
	}
	
	return resultado;
}


function formatar (campo, tipo, tecla) {
	if(navigator.appName=="Microsoft Internet Explorer"){
		if (tipo == "num") {
			var intKeyCode = event.keyCode;
			if ((48 <= intKeyCode && intKeyCode <= 57) || intKeyCode < 32) return true;
			event.keyCode = 0;
			return;
		}
		if (tipo == "data") {
			var tecla = event.keyCode;
			valorSemFormato = campo.value;
			while (valorSemFormato.indexOf("/") > -1) {
				valorSemFormato = valorSemFormato.replace("/", "");
			}
			tamanho = valorSemFormato.length;
			if (tecla == 46) {
				campo.value = campo.value;
			}
			if (tamanho < 8 && tecla != 8) {
				tamanho = valorSemFormato.length + 1;
			}
			if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
				if (tecla == 8) {
					tamanho = tamanho - 1;
				}
				if ((3 < tamanho) && (tamanho <= 4)) {
					campo.value = valorSemFormato.substring(0, tamanho-2) + '/' + valorSemFormato.substring(tamanho-2);
				} else if ((5 < tamanho) && (tamanho <= 6)) {
					campo.value = valorSemFormato.substring(0, tamanho-4) + '/' + valorSemFormato.substring(tamanho-4);
				} else if ((7 < tamanho) && (tamanho <= 8)) {
					campo.value = valorSemFormato.substring(0, tamanho-6) + '/' + valorSemFormato.substring(tamanho-6, tamanho-4) + '/' + valorSemFormato.substring(tamanho-4);
				}
			}
		}
	}
	else if(navigator.appName=="Netscape"){//Firefox
		if (tipo == "num") {
			x = campo.name;
			// Por Tiago Araujo Silva (tiago@tiagoaraujo.com)
			separador="/"; // Você pode definir o separador. Ex: "/" ou "-" ou "."
			tecla = tecla.keyCode; // Identifica a tecla, caso seja backspace
			valor = document.getElementById(x).value.split(''); // Pega o valor do campo e transforma cada caractere em uma string
			formatado = ""; // Variável para carregar formatação temporariamente
			i = 0; // variável de controle
			while(i < valor.length){ // Loop para cada caractere do campo
				caractere = valor[i]; // Seleciona um caractere para ser formatado
				numeros = /^\d+$/; // Variavel contendo números positivos
					// Verifica se é número ou "barra"
				if(numeros.test(caractere)){ formatado += String(caractere);}
				i++; // Se houver, passa para o próximo caractere
			}
			document.getElementById(x).value = formatado; // Atribui o valor formatado ao campo
		}
		if (tipo == "data") {
			x = campo.name;
			// Por Tiago Araujo Silva (tiago@tiagoaraujo.com)
			separador="/"; // Você pode definir o separador. Ex: "/" ou "-" ou "."
			tecla = tecla.keyCode; // Identifica a tecla, caso seja backspace
			valor = document.getElementById(x).value.split(''); // Pega o valor do campo e transforma cada caractere em uma string
			formatado = ""; // Variável para carregar formatação temporariamente
			i = 0; // variável de controle
			while(i < valor.length){ // Loop para cada caractere do campo
				caractere = valor[i]; // Seleciona um caractere para ser formatado
				numeros = /^\d+$/; // Variavel contendo números positivos
					// Verifica se é número ou "barra"
				if(numeros.test(caractere) || caractere == separador){ formatado += String(caractere);}
					// Verifica se precisa de barra, se a tecla for backspace, então a barra não é adicionada
					// Se for para adicionar barra, aumenta ++ variável de controle "i" para pular a barra adicionada
				if((formatado.length == 2 || formatado.length == 5) && tecla != 8){formatado += separador; i++;}
				i++; // Se houver, passa para o próximo caractere
			}
			document.getElementById(x).value = formatado; // Atribui o valor formatado ao campo
		}
	}
}

//==========================================================================================================================
// Função que valida um número de CPF (Pessoa Física)
//==========================================================================================================================
function isCPF(st) {
	st = LIMP(st);
	if (st=="11111111111" || st=="22222222222" || st=="33333333333" || st=="44444444444" || st=="55555555555" || st=="66666666666" || st=="77777777777" || st=="88888888888" || st=="99999999999")
		return (false);
		l = st.length;
	// se o usuário não digitar os zeros na frente do CPF, completar sozinho
	if ((l == 9) || (l == 8))
	{for (i = l ; i < 10; i++){st = '0' + st}}
		l = st.length;
		st2 = "";
		for (i = 0; i < l; i++) {
		caracter = st.substring(i,i+1);

	if ((caracter >= '0') && (caracter <= '9'));
		st2 = st2 + caracter;
		}
	if ((st2.length > 11) || (st2.length < 10))
		return (false);
	if (st2.length==10)
		st2 = '0' + st2;
		digito1 = st2.substring(9,10);
		digito2 = st2.substring(10,11);
		digito1 = parseInt(digito1,10);
		digito2 = parseInt(digito2,10);
		sum = 0; mul = 10;
		for (i = 0; i < 9 ; i++) {
		digit = st2.substring(i,i+1);
		tproduct = parseInt(digit ,10) * mul;
		sum += tproduct;
		mul--;}
		dig1 = ( sum % 11 );
	if ( dig1==0 || dig1==1 )
		dig1=0;
	else
		dig1 = 11 - dig1;
	if (dig1!=digito1)
		return (false);
		sum = 0;
		mul = 11;
	for (i = 0; i < 10 ; i++) {
		digit = st2.substring(i,i+1);
		tproduct = parseInt(digit ,10)*mul;
		sum += tproduct;
		mul--;
		}
		dig2 = (sum % 11);
	if ( dig2==0 || dig2==1 )
		dig2=0;
	else
		dig2 = 11 - dig2;
	if (dig2 != digito2)
		return (false);
		return (true);
}
		

//==========================================================================================================================
// Função que valida um número de CNPJ (Pessoa Jurídica)
//==========================================================================================================================
function isCNPJ(CNPJ){
	CNPJ = LIMP(CNPJ);
	if(isNum(CNPJ) != 1){
		return(0);
	}
	else{
		if(CNPJ == 0){
			return(0);
		}
		else{
			g=CNPJ.length-2;
			if(RealTestaCNPJ(CNPJ,g) == 1){
				g=CNPJ.length-1;
				if(RealTestaCNPJ(CNPJ,g) == 1){
					return(1);
				}
				else{
					return(0);
				}
			}
			else{
				return(0);}
		}
	}
}

function RealTestaCNPJ(CNPJ,g){
	var VerCNPJ=0;
	var ind=2;
	var tam;
	for(f=g; f>0; f--){
		VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
		if(ind>8){
			ind=2;}
		else{
			ind++;}
	}
	VerCNPJ%=11;
	if(VerCNPJ==0 || VerCNPJ==1){
		VerCNPJ=0;}
	else{
		VerCNPJ=11-VerCNPJ;}
	if(VerCNPJ!=parseInt(CNPJ.charAt(g))){
		return(0);}
	else{
		return(1);}
}

//==========================================================================================================================
// Função Limpa Campos (Tira do campo - / , . ( ) ) e também espaços em branco
//==========================================================================================================================
function LIMP(c){
	while((cx=c.indexOf("-"))!=-1)
		{c = c.substring(0,cx)+c.substring(cx+1);}
	while((cx=c.indexOf("/"))!=-1)
		{c = c.substring(0,cx)+c.substring(cx+1);}
	while((cx=c.indexOf(","))!=-1)
		{c = c.substring(0,cx)+c.substring(cx+1);}
	while((cx=c.indexOf("."))!=-1)
		{c = c.substring(0,cx)+c.substring(cx+1);}
	while((cx=c.indexOf("("))!=-1)
		{c = c.substring(0,cx)+c.substring(cx+1);}
	while((cx=c.indexOf(")"))!=-1)
		{c = c.substring(0,cx)+c.substring(cx+1);}
	while((cx=c.indexOf(" "))!=-1)
		{c = c.substring(0,cx)+c.substring(cx+1);}
	return(c);
}


//==========================================================================================================================
// Função valida CEP
//==========================================================================================================================
function isCep(cep){

	if (cep.length < 8)
		return false;		
	if (!isNum(cep))
		return false;
	if (cep == "")
		return false;		
	return true;
}
	
//==========================================================================================================================
// Função valida E-mail
//==========================================================================================================================
function isEmail(mail) {
	var ponto = 0;
	var arroba = 0;
	var letraAntes = 0;
	var letraDepois = 0;
	var i = 0;
	var l = mail.length;
	var ch = 0;
	if (l < 10)	return (false);
	for (i = 0; i < l; i++) {
		ch = mail.charCodeAt(i);
		if (ch == 46) {
			if (arroba > 0)	ponto += 1;
		} else {
			if (ch == 64) {
				arroba += 1;
			} else {
				if (((ch >= 48) && (ch <= 57)) || ((ch >= 65) && (ch <= 90)) || ((ch >= 97) && (ch <= 122))) {
					if (arroba > 0)
						letraDepois += 1;
					else
						letraAntes += 1;
				} else {
					if ((ch != 45) && (ch != 95)) {
						if ((ch < 48) || (ch > 57)) {
							return (false);}}}}}}
	if ((arroba == 1) && (ponto > 0) && (letraAntes > 1) && (letraDepois > 5))
		{return (true);}
	else {return (false);}
}

//==========================================================================================================================
// Função valida DDD
//==========================================================================================================================
function isDDD(ddd){

	if (ddd.length != 2)
		return false;		
	if (!isNum(ddd))
		return false;	
	return true;
}
	
//==========================================================================================================================
// Função valida Telefone
//==========================================================================================================================
function istel(tel){

	if (tel.length < 7)
		return false;		
	if (!isNum(tel))
		return false;	
	return true;
}

//==========================================================================================================================
// Função valida Data
//==========================================================================================================================
function isDate(campo) {
	if (campo != ""){
		if(campo.charAt(2) != "/" || campo.charAt(5) != "/" || campo.length != 10){
			return(false);
		}
		for(i=0;i<10;i++){
			if(i != 2 && i != 5){
				if(campo.charAt(i) != 0 && campo.charAt(i) != 1 && campo.charAt(i) != 2 && campo.charAt(i) != 3 && campo.charAt(i) != 4 && campo.charAt(i) != 5 && campo.charAt(i) != 6 && campo.charAt(i) != 7 && campo.charAt(i) != 8 && campo.charAt(i) != 9 || campo.charAt(i) == " "){
					return(false);
				}
			}
		}
		var Dia = 0;
		var Mes = 0;
		var Ano = 0;
		var Resto = 0;
		Dia = campo.substring(0,2);
		Mes = campo.substring(3,5);
		Ano = campo.substring(6,10);
		if (Ano < 1900) {return(false);	}
		if ((Mes < 1) || (Mes > 12)) {return(false);}
		if ((Dia < 1) || (Dia > 31)) {return(false);}
		if ((Mes == "01") && (Dia <= 31)) {return(true);}
		if (Mes == "02") {Resto = Ano % 4;
			if ((Resto == 0) && (Dia <= 29))
				{return(true);}
			if ((Resto != 0) && (Dia <= 28))
				{return(true);}}
		if ((Mes == "03") && (Dia <= 31)) {return(true);}
		if ((Mes == "04") && (Dia <= 30)) {return(true);}
		if ((Mes == "05") && (Dia <= 31)) {return(true);}
		if ((Mes == "06") && (Dia <= 30)) {return(true);}
		if ((Mes == "07") && (Dia <= 31)) {return(true);}
		if ((Mes == "08") && (Dia <= 31)) {return(true);}
		if ((Mes == "09") && (Dia <= 30)) {return(true);}
		if ((Mes == "10") && (Dia <= 31)) {return(true);}
		if ((Mes == "11") && (Dia <= 30)) {return(true);}
		if ((Mes == "12") && (Dia <= 31)) {return(true);}
		return(false);}
}

//==========================================================================================================================
// Função que verifica se o campo é numérico
//==========================================================================================================================
function isNum(str){
	var VBlnIsNum;
	VIntTam = str.length;
	VBlnIsNum = true;
	if (VIntTam == 0){
		return false;
	}
	else{
		for (i=0; i < VIntTam; i++){
			if (str.substring(i,i+1) < '0' || str.substring(i,i+1) > '9'){
				VBlnIsNum = false;
			}
		}
		return VBlnIsNum;
	}
}

//=================================================================================== 
function limita (campo, tamanhomaximo){
	var tamanho = (campo.value.length);
	var tex = campo.value;
	if (tamanho >= tamanhomaximo) 
	{campo.value=tex.substring(0,tamanhomaximo-1);}
		return true;}
//=================================================================================== 	
function contacampo(campoIN, campoOUT, tamanhomaximo) {
	var campoIN = campoIN;
	var campoOUT = campoOUT;	
	campoOUT.value = tamanhomaximo - campoIN.value.length;}
	

//===================================================================================
function ltrim(ptexto) {
    var i = 0;
    while (i<ptexto.length && (ptexto.charAt(i)== " ")) {
        i++;
    }
    return (ptexto.substring(i,ptexto.length));}
//=================================================================================== 
function rtrim(ptexto) {
    var i = ptexto.length-1;
    while (i>0 && (ptexto.charAt(i) == " ")) {
        i--;
    }
    return (ptexto.substring(0,i+1));}
//===================================================================================    
function trim(ptexto) {return ltrim(rtrim(ptexto));}

function CharSpecial(str){
	var pos, i, ok, tamanho, numeros;
	if (!CheckAspas(str)){
		return false;
	}
	ok = true;
	numeros = '0123456789';
	str = trim(str);	
	tamanho = str.length;
	for(i=0; i < tamanho; i++){
		pos = str.substring(i, i+1);
		if (!( ( numeros.indexOf(pos) >= 0 ) || ( pos.charCodeAt(pos) >= 65 && pos.charCodeAt(pos) <= 90 ) || ( pos.charCodeAt(pos) >= 97 && pos.charCodeAt(pos) <= 122 ) || (pos.charCodeAt(pos) == 45 ) || (pos.charCodeAt(pos) == 32 ) ) ){
			ok = false;
			break;
		}	
	}	

	if (ok == true){
		return true;				
	}
	else{
		return false;
	}
	return true;
}

//===================================================================================
function CheckAspas(str){
	ok = true;
	str = trim(str);
	tamanho = str.length;
	for(i=0; i < tamanho; i++){
		pos = str.substr(i, i+1);
		if ((pos == "'") || (pos == '"')){
			ok = false;
			break;
		}
	}
	
	if (ok == true){
		return true;
	}
	else{
		return false;
	}
}
