//==========================================================================================================================
// Função Ajax 
//==========================================================================================================================
function ajaxInit()  //Inicia a biblioteca para funcionar o AJAX
{
	var req;
	try
    {
	    req = new ActiveXObject("Microsoft.XMLHTTP");
	}

        catch(e)
        {
  	        try
            {
	            req = new ActiveXObject("Msxml2.XMLHTTP");
	        }

            catch(ex)
            {
	            try
                {
		            req = new XMLHttpRequest();
		        }

                catch(exc)
		        {
		            alert("Esse Navegador não tem recursos para uso do Ajax! Atualize seu Navegador!");
		            req = null;
		        }
            }
        }

	return req;
}
//===================================================================================


//==========================================================================================================================
// Função que abre a janela popup
//==========================================================================================================================
function popup(pagina,nome,altura,largura,top,left,scroll,toolbar,location,menu,resize) 
{

	var janela;
	var top = (screen.height - altura) / 2;
	var left = (screen.width  - largura) / 2; 
	
	if (altura == 0){
		top = 0;
		altura = screen.height;
	}
	if (largura == 0){
		left = 0; 
		largura = screen.width;
	}
	
	janela = window.open(pagina,nome,'width='+largura+',height='+altura+',top='+top+',left='+left+',scrollbars='+scroll+',toolbar='+toolbar+',location='+location+',menubar='+menu+',resizable='+resize);
	janela.focus();
}

theUrl="../bp/indique/indique.asp";
function popdown()
{
	reWin = window.open(theUrl,'determined','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=295,height=255,top=250,left=250');
}

//===================================================================================
function isNumeric(ptexto) {
    var numeric = true;
    for (i=0; i < ptexto.length; i++) {
        if (!isDigit(ptexto.charAt(i)) ) {
            numeric = false;
            break;
        }
    }
    return numeric;
}

//==========================================================================================================================
// Função que verifica se o valor é númerico
//==========================================================================================================================

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;
			}
	}

//==========================================================================================================================
// Função que valida um número de CPF (Pessoa Física)
//==========================================================================================================================

function isCPF(st) {
	st = LIMP(st);
	if (st == "")
		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 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 CEP
//==========================================================================================================================
function isCep(cep){

	if (cep.length < 8)
		return false;		
	if (!isNum(cep))
		return false;
	if (cep == "")
		return false;		
	
	return true;						
		
}

//==========================================================================================================================
// Função limpa Campos
//==========================================================================================================================

function reset_form(form_name){

	confirma_remocao = confirm('ATENÇÃO: Você está prestes a apagar os dados deste formulário.\n\nConfirma esta ação ?');
	if (confirma_remocao == true) 
	{

		for (var i=0;i<document.forms(form_name).elements.length;i++)
		{
			if (document.forms(form_name).elements[i].type == "select-one"){
				document.forms(form_name).elements[i].value = "";
			}
			if (document.forms(form_name).elements[i].type == "text"){
				document.forms(form_name).elements[i].value = "";	
			}
			if (document.forms(form_name).elements[i].type == "password"){
				document.forms(form_name).elements[i].value = "";	
			}	
			if (document.forms(form_name).elements[i].type == "checkbox"){
				document.forms(form_name).elements[i].checked = false;	
			}
			if (document.forms(form_name).elements[i].type == "textarea"){
				document.forms(form_name).elements[i].value = "";		
			}			
		}
		
		for (var i=0;i<document.forms(form_name).elements.length;i++)
		{
			if (document.forms(form_name).elements[i].type == "text"){
				document.forms(form_name).elements[i].focus();
				break;
			}
		}				
	}

}
//===================================================================================

//=================================================================================== 	
function abre_pagina(larg,altu,pagi){
  
  largura = screen.width;
  altura = screen.height;
  poshor = (largura - larg) / 2;
  posver = (altura - altu) / 2;
  comando = "window.open('" + pagi + "','nova','top=" + posver + ",scrollbars=yes,resizable=no,left=" + poshor + ",width=" + larg + ",height=" + altu + "');";
  eval(comando);
} 

//===================================================================================    
  
function  isMoney(vTexto){

	var vCampo = vTexto;
 	var vTexto = vTexto.value;
	var sepDec = ",";
	var csDec = inStr(vTexto,sepDec);

	if ((csDec == 0) && (vTexto != ''))
	{
		 vTexto = vTexto+sepDec+"00";
		 csDec = inStr(vTexto,sepDec);
	}
	
 
	if (!csDec>0 || csDec+2!=vTexto.length) return false;
	if (inStr(vTexto,"+")>0 || inStr(vTexto,"-")>0 || inStr(vTexto,".")>0) return false;
	if (isNaN(vTexto.substr(0,csDec-1)) || isNaN(vTexto.substr(csDec,2))) return false;

	vCampo.value = vTexto;	
	return true;
}

