// JavaScript Document

function mostraPopoup(){
	document.getElementById("popFlutuante").style.display = 'block';
}
function escondePopoup(){
	document.getElementById("popFlutuante").style.display = 'none';
}
function flash(movie,w,h)
{
	document.writeln("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0\" width="+w+" height="+h+">");
	document.writeln("<param name=\"movie\" value="+movie+">");
	document.writeln("<param name=\"quality\" value=\"high\">");
	document.writeln("<param name=\"wmode\" value=\"transparent\">");
	document.writeln("<embed src="+movie+" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width="+w+" height="+h+" wmode=\"transparent\"></embed></object>");
}
function flash(movie,w,h,vars)
{
	document.writeln("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0\" width="+w+" height="+h+" id="+Math.random()+" >");
	document.writeln("<param name=\"movie\" value="+movie+">");
	document.writeln("<param name=\"quality\" value=\"high\">");
	document.writeln("<param name=\"wmode\" value=\"transparent\">");
	document.writeln("<PARAM NAME=FlashVars VALUE=\""+vars+"\">");
	document.writeln("<embed src='"+movie+"?"+vars+"' quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width="+w+" height="+h+" wmode=\"transparent\"></embed></object>");
}
function isNumber(numero){
   var CaractereInvalido = false;

   for (i=0; i < numero.length; i++){
      var Caractere = numero.charAt(i);
      if(Caractere != "." && Caractere != "," && Caractere != "-"){
         if (isNaN(parseInt(Caractere))) CaractereInvalido = true;
      }
   }
   return !CaractereInvalido;
} 

function verifica_qtd(qtd) {
	if (!isNumber(qtd.value)) {
		qtd.value = '1';
      	alert('Quantidade inválida !');
		return false;
	} else {
		if (qtd.value <= 0) {
		    qtd.value = '1';
      		alert('Quantidade inválida !');
			return false;
		}
	}
	return true;
}

//onkeypress="javascript:evita_letra(event);"
function evita_letra(tecla) {
	if (tecla.keyCode < 48 || tecla.keyCode > 57)
		tecla.returnValue = false;
}

function somenteNumeros(elemento){
	if (elemento.value.match(/[^\d]/g)) {
		elemento.value = elemento.value.replace(/[^\d]/g, '');
	}
}

//Valida o cpf 
function val_cpf(numero1) {
  dig_1 = 0;
  dig_2 = 0;
  controle_1 = 10;
  controle_2 = 11;
  lsucesso = 1;
  numero = numero1.value;
  numero = numero.replace(".","");
  numero = numero.replace(".","");
  numero = numero.replace(".","");
  numero = numero.replace("-","");
  if (numero.length != 11) {
     return false;
  }
  else {
     for ( i=0 ; i < 9 ; i++) {
        dig_1 = dig_1 + parseInt(numero.substring(i, i+1) * controle_1);
          controle_1 = controle_1 - 1;
     }
    
     resto = dig_1 % 11;
     dig_1 = 11 - resto;
     if ((resto == 0) || (resto == 1)) {
          dig_1 = 0;
     }
     for ( i=0 ; i < 9 ; i++) {
          dig_2 = dig_2 + parseInt(numero.substring(i, i + 1) * controle_2);
        controle_2 = controle_2 - 1;
     }
     dig_2 = dig_2 + 2 * dig_1;
     resto = dig_2 % 11;
     dig_2 = 11 - resto;
     if ((resto == 0) || (resto == 1)) {
        dig_2 = 0;
     }
     dig_ver = (dig_1 * 10) + dig_2;
     if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {
          return false;
        }
  }
return true;
}

//Valida o cnpj
function val_cnpj(numero1) {
numero = numero1.value;
dig_1 = 0;
dig_2 = 0;
controle_1 = 5;
controle_2 = 6;

numero = numero.replace(".","");
numero = numero.replace(".","");
numero = numero.replace("/","");
numero = numero.replace("-","");

	for ( i=0 ; i < 12 ; i++) {
		dig_1 = dig_1 + parseFloat(numero.substring(i, i+1) * controle_1);
		controle_1 = controle_1 - 1;
		if (i == 3) {
		   controle_1 = 9;
		}
	}
	resto = dig_1 % 11;
	dig_1 = 11 - resto;
	if ((resto == 0) || (resto == 1)){
		dig_1 = 0;
	}
	for ( i=0 ; i < 12 ; i++) {
		dig_2 = dig_2 + parseInt(numero.substring(i, i+1) * controle_2);
		  controle_2 = controle_2 - 1;
		if (i == 4) {
		   controle_2 = 9;
		}
	}
	dig_2 = dig_2 + (2 * dig_1);
	resto = dig_2 %11;
	dig_2 = 11 - resto;
	if ((resto == 0) || (resto == 1)){
		dig_2 = 0;
	}
	dig_ver = (dig_1 * 10) + dig_2;
	if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {
		  return false;
	}
	return true;
}

function val_email(Field) {
	if (!Field.value) {
  		return false;
  	}
	if ((Field.value.indexOf("@") == -1) || (Field.value.indexOf(".") == -1) || (Field.value.indexOf(" ") != -1) || (Field.value.length < 6)) {
   		return false;
   	} else {
   		return true;
   }
}   

function verifica_branco(parametro)   // FUNCAO PARA VERIFICAÇÃO DE CAMPOS NÃO PREENCHIDOS
									// OU PREENCHIDOS APENAS COM ESPAÇOS EM BRANCO
{
	teste_parametro = "false"; //variavel para teste de espacos em branco
	tamanho_parametro = parametro.length;
	if (tamanho_parametro != 0)
	{
		for (i = 0; i < tamanho_parametro; i++)
			{if (parametro.charAt(i) != " ")
				{
					teste_parametro = "true"; /*existe caracter diferente de branco*/
				}
			}
		if (teste_parametro == "false")  //todos os caracteres digitados são brancos
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
		
}


function valida_inteiro(parametro)  //FUNCAO PARA VALIDACAO DE NÚMEROS INTEIROS, E ESPAÇOS EM BRANCO
{
	if (parametro.length != 0)
	{
		if (!verifica_branco(parametro))
		{
			return true;
		}
		
		teste_ponto = "false";
		tamanho_parametro = parametro.length;
		
		if (isNaN(parametro)) //valor digitado não é numérico
		{
			return false;
		}
		else //valor digitado é um numérico válido
		{	
			
			for (k = 0; k < tamanho_parametro; k++)
			{if ((parametro.charAt(k) == '.') || (parametro.charAt(k) == '-') || (parametro.charAt(k) == '+'))
				{
					teste_ponto = "true"; /*existe caracter ponto*/
				}
			}
			
			if (teste_ponto == "true") //encontrou caracter ponto(numero real)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}
	else
	{
		return true;
	}
	
}

function valida_float(parametro)  //FUNCAO PARA VALIDACAO DE NÚMEROS INTEIROS, E ESPAÇOS EM BRANCO
{
	teste_ponto = "false";
	teste_parametro = "false"; //variavel para teste de espacos em branco
	tamanho_parametro = parametro.length;
	parametro_conv = "";	//armazena o parâmetro trocando vírgulas por ponto e vice-versa

	if (tamanho_parametro != 0)
	{
		//procedimento p/ trocar vírgulas por ponto e vice-versa, armazenando noutra variavel
		for (i = 0; i < tamanho_parametro; i++)
		{			
			if (parametro.charAt(i) == ",")
			{
				parametro_conv = parametro_conv + ".";
			}
			else
			{
				if (parametro.charAt(i) == ".")
				{
					parametro_conv = parametro_conv + ",";
				}
				else
				{
					parametro_conv = parametro_conv + parametro.charAt(i);
				}
			}
			
			if ((parametro.charAt(i) == "+") || (parametro.charAt(i) == "-"))
			{
				return false;
			}
		}

		if (isNaN(parametro_conv)) //valor digitado não é numérico
		{
			return false;
		}
		else
		{
			return true;
		}
/*		else valor digitado é um numérico válido
		{
			for (k = 0; k < tamanho_parametro; k++)
			{
				encontrou caracter ponto na primeira ou última posição (inválido)
				if (parametro_conv.charAt(k) == '.' && (k == tamanho_parametro - 1 || k == 0))
					{
						teste_ponto = "true"; existe caracter ponto em local inválido
					}
			}
			
			if (teste_ponto == "true") encontrou caracter ponto(numero real)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
*/		
	}
	else
	{
		return true;
	}
	
}



function ValidDate(data) {
	if (!data) {
		return false;
	}
	var v_dia;
	var v_mes;
	var v_ano;
	v_dia = data.substr(0,2);
	v_mes = data.substr(3,2);
	v_ano = data.substr(6,4);

	if (!valida_inteiro(v_dia) || !valida_inteiro(v_mes) || !valida_inteiro(v_ano)){
		return false;
	}
	if (v_dia.length < 1) {
		return false;
	}
	if (v_mes.length < 1) {
		return false;
	}
	if (v_ano.length < 4) {
		return false;
	}
	if (((v_ano < 1900) || (v_ano > 2079)) && (v_ano.length != 0)) {
		return false;
	}
	if (v_dia > 31) { 
		return false;
	}
	if (v_mes > 12) { 
		return false;
	}
	if (v_dia == "31") {
		if ((v_mes == "04") || (v_mes == "06") || (v_mes == "09") || (v_mes == "11")) {
			return false;
		}
	}
	if (v_mes == "02") {
		if (!(v_ano%4)) {
			if (v_dia > 29) {
				return false;
			}
		} else if (v_dia > 28) {
			return false;
		}
	}
	//o -if- abaixo testa se algum campo foi preenchido e outro deixado em branco deixando a data incompleta
	if (((v_dia != "") || (v_mes != "") || (v_ano != "")) && ((v_dia == "") || (v_mes == "") || (v_ano == ""))) {
			return false;
	}
	return true;
}

function strzero(txt,digitos) {
	var txt_new = '';
	if (txt.length > Number(digitos)) {
		txt_new = txt.substr(0,digitos);
	} else {
		txt_new = txt.toString();
		while (txt_new.length < digitos) {
			txt_new = '0'+txt_new;
		}
	}
	return txt_new;
}

function centro(p,n,l,a,v1,v2,v3,v4,v5,v6,v7,v8,v9){var l = l; var a = a; var t = (screen.height/2)-(a/2); var d = (screen.width/2)-(l/2);window.open(p,n,'width='+l+',height='+a+',top='+t+',left='+d+',scrollbars='+v1+',menubar='+v2+',directories='+v3+',location='+v4+',copyhistory='+v5+',status='+v6+',toolbar='+v7+',maximized='+v8+',resizable='+v9+'');}

var corFoco = '#FFFF99';
var corBlur = 'white';
function mudacor(ref,cor){
  ref.style.backgroundColor=cor;
}

function mostraFlash(){
	document.getElementById("direitaBanners").style.display = '';
}

function Mascara(tipo, campo, teclaPress) {
	if (window.event)
	{
		var tecla = teclaPress.keyCode;
	} else {
		tecla = teclaPress.which;
	}

    //campo.value = campo.value.replace(/[^A-Za-z]/g, "");
	//return;
    var s = new String(campo.value);
	// Remove todos os caracteres à seguir: ( ) / - . e espaço, para tratar a string denovo.

    s = s.replace(/(\.|\(|\)|\/|\-| )+/g,'');

	tam = s.length + 1;
    campo.value=campo.value.replace(/[a-zA-Z]+/g,"");

	if ( tecla != 9 && tecla != 8 ) {
		switch (tipo)
		{
		case 'CPF' :
			if (tam > 3 && tam < 7)
				campo.value = s.substr(0,3) + '.' + s.substr(3, tam);
			if (tam >= 7 && tam < 10)
				campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,tam-6);
			if (tam >= 10 && tam < 12)
				campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,3) + '-' + s.substr(9,tam-9);
		break;

		case 'CNPJ' :

			if (tam > 2 && tam < 6)
				campo.value = s.substr(0,2) + '.' + s.substr(2, tam);
			if (tam >= 6 && tam < 9)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,tam-5);
			if (tam >= 9 && tam < 13)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,tam-8);
			if (tam >= 13 && tam < 15)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,4)+ '-' + s.substr(12,tam-12);
		break;

		case 'TEL' :
            if(s == '0')
                campo.value = '';
			if (tam > 2 && tam < 4)
				campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,tam);
			if (tam >= 7 && tam < 11)
				campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,4) + '-' + s.substr(6,tam-6);
		break;

		case 'CEP' :
			campo.value=campo.value.replace(/^(\d{5})(\d)/,"$1-$2")
		break;

		case 'DATA' :
            if (tam > 2 && tam < 4)
				campo.value = s.substr(0,2) + '/' + s.substr(2, tam);
			if (tam > 4 && tam < 11)
				campo.value = s.substr(0,2) + '/' + s.substr(2,2) + '/' + s.substr(4,tam-4);
		break;
		}
	}
}

function formataMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if(!whichCode)
       whichCode = e.keyCode;
    // 13=enter, 8=backspace as demais retornam 0(zero)
    // whichCode==0 faz com que seja possivel usar todas as teclas como delete, setas, etc    
    if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
    	return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave


    if (strCheck.indexOf(key) == -1)
    	return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal))
        	break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) 
        	aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) 
    	objTextBox.value = '';
    if (len == 1) 
    	objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) 
    	objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        	objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function formataPeso(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if(!whichCode)
       whichCode = e.keyCode;
    // 13=enter, 8=backspace as demais retornam 0(zero)
    // whichCode==0 faz com que seja possivel usar todas as teclas como delete, setas, etc    
    if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
    	return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave


    if (strCheck.indexOf(key) == -1)
    	return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal))
        	break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) 
        	aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) 
    	objTextBox.value = '';
    if (len == 1) 
    	objTextBox.value = '0'+ SeparadorDecimal + '0' + '0' +aux;
    if (len == 2) 
    	objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
	if (len == 3) 
    	objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 3) {
        aux2 = '';
        for (j = 0, i = len - 4; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        	objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 3, len);
    }
    return false;
}

var req = createXMLHttpRequest();

function createXMLHttpRequest() {
 var ua;
 if(window.XMLHttpRequest) {
 try {
  ua = new XMLHttpRequest();
 } catch(e) {
  ua = false;
 }
 } else if(window.ActiveXObject) {
  try {
	ua = new ActiveXObject('Microsoft.XMLHTTP');
  } catch(e) {
	ua = false;
  }
 }
return ua;
}

function sendRequest(file, opc, retorno) {
 var rnd982g = Math.random();
 req.open('GET', file+'?'+opc+'&rnd982g='+rnd982g);
 req.onreadystatechange = retorno;
 req.send(null);	
 return false;
}
