function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// JavaScript Document
try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}


function envia_form(tp_part) {
	//document.form1.tp_inscricao.value = tp_part;
	document.form1.submit();
}


function pula_campo(campo) {
	  if (campo.name == 'dd' && campo.value.length == 2) {
		 document.form1.mm.focus();
	  }
	  if (campo.name == 'mm' && campo.value.length == 2) {
		 document.form1.aaaa.focus();
	  }
	  if (campo.name == 'aaaa' && campo.value.length == 4) {
		 document.form1.cpf.focus();
	  }
	  if (campo.name == 'cpf' && campo.value.length == 11) {
		 document.form1.ddd.focus();
	  }
	  if (campo.name == 'ddd' && campo.value.length == 2) {
		 document.form1.telefone.focus();
	  }
	  if (campo.name == 'telefone' && campo.value.length == 8) {
		 document.form1.ddd2.focus();
	  }
	  if (campo.name == 'ddd2' && campo.value.length == 2) {
		 document.form1.telefone_celular.focus();
	  }
	  if (campo.name == 'telefone_celular' && campo.value.length == 8) {
		 document.form1.cep.focus();
	  }
}

function muda_texto(valor)  {
	switch (valor)  {
		case 1:
			document.getElementById("cpf_cnpj").innerHTML = "CNPJ";
		break;
		case 2:
			document.getElementById("cpf_cnpj").innerHTML = "CPF";
		break;
		case 3:
			document.getElementById("cpf_cnpj").innerHTML = "CPF";
		break;
	}
}

function checar_form() {
  if (!document.form_ja_cad.categoria.value)       {
     alert("Por favor, informe a categoria");
	 document.form_ja_cad.categoria.focus();
	 return false;
  }
  else if (!document.form_ja_cad.email.value)  {
     alert("Por favor, informe o email");
	 document.form_ja_cad.email.focus();
 	 return false;
  }
  else if ((document.form_ja_cad.email.value.indexOf("@",0))== -1 || 
  (document.form_ja_cad.email.value.indexOf("@",0)) < 1 || 
  (document.form_ja_cad.email.value.indexOf(".",0))== -1 ||
  (document.form_ja_cad.email.value.indexOf("@",0))== document.form_ja_cad.email.value.length - 1 ||
  (document.form_ja_cad.email.value.indexOf(".",0))== document.form_ja_cad.email.value.length - 1) {
    alert("Formato de e-mail incorreto");
	document.form_ja_cad.email.focus();
    document.form_ja_cad.email.select();
    return false;
  }
  else if (!document.form_ja_cad.cpf_cnpj.value)  {
     alert("Por favor, informe o CPF/CNPJ");
	 document.form_ja_cad.cpf_cnpj.focus();
 	 return false;
  }
  else {
    document.form_ja_cad.submit();
  }
}


function bloqueia_copia(valor,nome_campo)  {
    valor1 = valor.substr(2,valor.length -2);
    for(i=0;i<5;i++) {
		valor1 = valor1.replace(".","");
		valor1 = valor1.replace(",","");
	}
	if (isNaN(valor1))  {
		alert("Digite apenas números neste campo");
		nome_campo.value = "";
		nome_campo.focus();
	}
}		
		
function OnlyNumbers(e) { 

    if (window.event) //IE 
    { 
    tecla = e.keyCode; 
    } 

   else if (e.which) //FF 
   { 
    tecla = e.which; 
   } 
    //teclas dos numemros(0 - 9) de 48 a 57
   //techa==8 é para permitir o backspace funcionar para apagar
    
       if ( (tecla >= 48 && tecla <= 57)||(tecla == 8 ) ) {
               return true;
          }
          else {
               return false;
          }
}


  function add_projeto(origem,valor_tp_part)  {
	  //Origem: se veio da tecla Enter ou do botão adicionar
	  if (origem == 'enter') {
			check_add = true;
	  }
	  else {
			check_add = false;
	  }

      var add_projetos = document.getElementById("projetos");
	  var nome_projetos = document.getElementById("projetos_add");
	  var nom_projeto = nome_projetos.value.toLowerCase();

	  if (nom_projeto != '')  {

		  add_projetos.options.length = add_projetos.options.length + 1; 
		  tam = add_projetos.options.length;
		  add_projetos.options[tam - 1].value = nom_projeto.replace("http://", ""); 
		  add_projetos.options[tam - 1].text = "Projeto "+ tam +": http://" + nom_projeto.replace("http://", "");
		  nome_projetos.value = "";
		  
		  document.getElementById("valor").innerHTML = add_projetos.options.length * valor_tp_part;
		  document.form1.valor_total.value = add_projetos.options.length * valor_tp_part;
//		  add_projetos.focus();
		  nome_projetos.focus();
	  }
	  else  {
	 	  alert("Informe um projeto para adicionar!");
		  nome_projetos.focus();
	  }

  }

	var check_add = false;
	function press_enter(e,valor_tp_partic) {
		if (window.event)  { //IE
			tecla = e.keyCode;
		}
		else if (e.which) { //FF
			tecla = e.which; 
		}
		//Se der Enter, adiciona o endereço à lista
		if (tecla == 13) {
			add_projeto('enter',valor_tp_partic);
		}
		//Proibir digitar a vírgula
		if (tecla == 44)  {
			return false;
		}
		else {
			return true;
		}
	}


	function excluir_projeto(caixa)   {
	  var projects = document.getElementById(caixa);

	  for(i=0;i<projects.length;i++){
		if (projects.options[i].selected){
  		    document.getElementById("valor").innerHTML = (projects.length -1) * 40;
		    document.form1.valor_total.value = (projects.length -1) * 40;
			projects.options[i--] = null;
		}
	  }
	  document.form1.projetos_add.focus();
	   
	}
	
	function select_all(caixa,marcar)  {
		/*função que seleciona, a partir de um outro elemento,
		todas as opções restantes.
		PARAMETROS PASSADOS:
		- caixa = id da caixa de seleção
		- limite = numero maximo de itens
		*/
		
		var campo = document.getElementById(caixa);
		for(i=0;i<=campo.length-1;i++){
		   if (marcar == "sim")  {
			  campo.options[i].selected = true;
		   }
		   else  {
			  campo.options[i].selected = false;
		   }
		}
	}

function valida_data()  {
	 if (document.form1.dd.value && document.form1.mm.value && document.form1.aaaa.value)   {
         hoje = new Date();
         anoAtual = hoje.getFullYear();
		 data_ = document.form1.dd.value + "/" + document.form1.mm.value + "/" + document.form1.aaaa.value;
         barras = data_.split("/");
         if (barras.length == 3){
                   dia = barras[0];
                   mes = barras[1];
                   ano = barras[2];
                   resultado = (!isNaN(dia) && (dia > 0) && (dia < 32)) && (!isNaN(mes) && (mes > 0) && (mes < 13)) && (!isNaN(ano) && (ano.length == 4) && (ano <= anoAtual && ano >= 1900));
                   if (!resultado) {
                             alert("Formato de data invalido!");
		 				     document.form1.dd.value = '';
		 				     document.form1.mm.value = '';
		 				     document.form1.aaaa.value = '';
                             document.form1.dd.focus();
                             return false;
                   }
         }
		 if (document.form1.dd.value.length == 1) {
			 document.form1.dd.value = "0" + document.form1.dd.value; 
		 }
		 if (document.form1.mm.value.length == 1) {
			 document.form1.mm.value = "0" + document.form1.mm.value; 
		 }
	  }
}


function valida_cnpj(f,campo)  {

//         pri = eval("document."+f+"."+campo+".value.substring(0,2)");
//         seg = eval("document."+f+"."+campo+".value.substring(3,6)");
//         ter = eval("document."+f+"."+campo+".value.substring(7,10)");
//         qua = eval("document."+f+"."+campo+".value.substring(11,15)");
//         qui = eval("document."+f+"."+campo+".value.substring(16,18)");

         var i;
         var numero;
         var situacao = '';

  //       numero = (pri+seg+ter+qua+qui);
  		// numero = f;

         s = f;


         c = s.substr(0,12);
         var dv = s.substr(12,2);
         var d1 = 0;

         for (i = 0; i < 12; i++){
            d1 += c.charAt(11-i)*(2+(i % 8));
         }

         if (d1 == 0){
            var result = "falso";
         }
            d1 = 11 - (d1 % 11);

         if (d1 > 9) d1 = 0;

            if (dv.charAt(0) != d1){
               var result = "falso";
            }

         d1 *= 2;
         for (i = 0; i < 12; i++){
            d1 += c.charAt(11-i)*(2+((i+1) % 8));
         }

         d1 = 11 - (d1 % 11);
         if (d1 > 9) d1 = 0;

            if (dv.charAt(1) != d1){
               var result = "falso";
            }


         if (result == "falso") {
            alert("CNPJ inválido!");
            campo.value = '';
//		    campo.focus();

         }
   }
   
   
   function pula_campo(campo) {
	  if (campo.name == 'cnpj' && campo.value.length == 14) {
		 document.form1.cargo.focus();
	  }
	  if (campo.name == 'ddd1' && campo.value.length == 2) {
		 document.form1.telefone1.focus();
	  }
	  if (campo.name == 'telefone1' && campo.value.length == 8) {
		 document.form1.ddd2.focus();
	  }
	  if (campo.name == 'ddd2' && campo.value.length == 2) {
		 document.form1.telefone2.focus();
	  }
	  if (campo.name == 'telefone2' && campo.value.length == 8) {
		 document.form1.cep.focus();
	  }
}


function pula_campo_freela(campo) {
	  if (campo.name == 'dd' && campo.value.length == 2) {
		 document.form1.mm.focus();
	  }
	  if (campo.name == 'mm' && campo.value.length == 2) {
		 document.form1.aaaa.focus();
	  }
	  if (campo.name == 'aaaa' && campo.value.length == 4) {
		 document.form1.cpf.focus();
	  }
	  if (campo.name == 'cpf' && campo.value.length == 11) {
		 document.form1.ddd1.focus();
	  }
	  if (campo.name == 'ddd1' && campo.value.length == 2) {
		 document.form1.telefone1.focus();
	  }
	  if (campo.name == 'telefone1' && campo.value.length == 8) {
		 document.form1.ddd2.focus();
	  }
	  if (campo.name == 'ddd2' && campo.value.length == 2) {
		 document.form1.telefone2.focus();
	  }
	  if (campo.name == 'telefone2' && campo.value.length == 8) {
		 document.form1.cep.focus();
	  }
}

function Verifica_campo_CPF(campo) {
var CPF = campo.value; // Recebe o valor digitado no campo

// Aqui começa a checagem do CPF
var POSICAO, I, SOMA, DV, DV_INFORMADO;
var DIGITO = new Array(10);
DV_INFORMADO = CPF.substr(9, 2); // Retira os dois últimos dígitos do número informado

// Desemembra o número do CPF na array DIGITO
for (I=0; I<=8; I++) {
  DIGITO[I] = CPF.substr( I, 1);
}

// Calcula o valor do 10º dígito da verificação
POSICAO = 10;
SOMA = 0;
   for (I=0; I<=8; I++) {
      SOMA = SOMA + DIGITO[I] * POSICAO;
      POSICAO = POSICAO - 1;
   }
DIGITO[9] = SOMA % 11;
   if (DIGITO[9] < 2) {
        DIGITO[9] = 0;
}
   else{
       DIGITO[9] = 11 - DIGITO[9];
}

// Calcula o valor do 11º dígito da verificação
POSICAO = 11;
SOMA = 0;
   for (I=0; I<=9; I++) {
      SOMA = SOMA + DIGITO[I] * POSICAO;
      POSICAO = POSICAO - 1;
   }
DIGITO[10] = SOMA % 11;
   if (DIGITO[10] < 2) {
        DIGITO[10] = 0;
   }
   else {
        DIGITO[10] = 11 - DIGITO[10];
   }

// Verifica se os valores dos dígitos verificadores conferem
DV = DIGITO[9] * 10 + DIGITO[10];
   if (DV != DV_INFORMADO || CPF=='00000000000') {
      alert('Seu CPF parece estar incorreto.');
      campo.value = '';
      campo.focus();
      return false;

   } 
}

   
   function formataCep() {
	cepL = document.form1.cep.value.length;
	if (cepL == 8)  {
		if (cepL) {
			cep = document.form1.cep.value.replace("-","");
			for(i=0; i<=cepL; i++){
				cep = cep.replace("-","");
			}
			query(cep);
		}		
	}
}


function TrimJS() {
	String = document.form1.endereco.value
	Resultado = String
	//Retira os espaços do inicio//Enquanto o primeiro caracter for igual à "Espaço" //1 caracter do inicio é removido
	var i
	i = 0
//	alert(Resultado)

	if (Resultado.charCodeAt(2-1) == '32'){
		
	}
		while (Resultado.charCodeAt(0) == '32'){  
			Resultado = String.substring(i,String.length);
			i++;
		}
			//Pega a string já formatada e agora retira os espaços do final//mesmo esquema, enquanto o ultimo caracter for um espaço, //ele retira 1 caracter do final...
		while (Resultado.charCodeAt(Resultado.length-1) == "32") {
			Resultado = Resultado.substring(0,Resultado.length-1);
		}
		document.form1.endereco.value = Resultado
		String = ""
}

function query(cep) {
	document.getElementById("buscando_cep").style.display = '';
	document.form1.endereco.disabled = true;
	document.form1.bairro.disabled = true;
	document.form1.estado.disabled = true;
	
	guarda_cep = document.form1.cep.value;
	document.getElementById("cep").disabled = true;

  	url = "cep.php?cep="+cep;
	
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange=function() {
	    if (xmlhttp.readyState==4)  {
			document.getElementById("buscando_cep").style.display = 'none';
			document.form1.endereco.disabled = false;
			document.form1.bairro.disabled = false;
			document.form1.estado.disabled = false;
			document.form1.cep.value = guarda_cep;

			quebra = xmlhttp.responseText.split("|");
			document.getElementById("cep").disabled = false;
			document.getElementById("endereco").value = quebra[0] + ' ' + quebra[1];
			TrimJS(); //Tira os espaços iniciais e finais do "endereco
			document.getElementById("bairro").value = quebra[2];
			cont = document.form1.estado.options.length;
			for (i=0;i<cont;i++) {
				if (document.form1.estado.options[i].value == quebra[4].substring(0,2)) {
					document.form1.estado.options[i].selected = true;
					show_mun(quebra[3].toUpperCase());
				}
			}
	    }
	}
	 xmlhttp.send(null)
}




		  function show_mun(selec_mun)  {

			//document.form1.municipio.value = "Carregando...aguarde";
			document.form1.municipio.disabled = true;
			document.form1.municipio.options[0] = new Option('Carregando...aguarde','',true,true);
			url = "carrega_mun.asp?uf=" + document.form1.estado.value;

			xmlhttp.open("GET", url, true);
			xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4) {
					//show_comentario.innerHTML = xmlhttp.responseText;
					document.getElementById("div_mun_ajax").innerHTML = xmlhttp.responseText;
					document.form1.municipio.disabled = false;
			//		document.getElementById("buscando_cep").style.display = 'none';
					if (selec_mun) { //Se está vindo da busca por CEP....
						cont = document.form1.municipio.options.length;
		//				alert(selec_mun);
						for (i=0;i<cont;i++) {
							if (document.form1.municipio.options[i].text == selec_mun)  {
								document.form1.municipio.options[i].selected = true;
								document.form1.numero.focus();
							}
						}
					}
					//alert(xmlhttp.responseText);
				}
			}
			 xmlhttp.send(null)
		  }

//FUNÇÕES RESPONSÁVEIS PELO MENU VERTICAL
var mmOpenContainer = null;
var mmOpenMenus = null;
var mmHideMenuTimer = null;

function MM_menuStartTimeout(hideTimeout) {
	mmHideMenuTimer = setTimeout("MM_menuHideMenus()", hideTimeout);	
}

function MM_menuHideMenus() {
	MM_menuResetTimeout();
	if(mmOpenContainer) {
		var c = document.getElementById(mmOpenContainer);
		c.style.visibility = "inherit";
		mmOpenContainer = null;
	}
	if( mmOpenMenus ) {
		for(var i = 0; i < mmOpenMenus.length ; i++) {
			var m = document.getElementById(mmOpenMenus[i]);
			m.style.visibility = "hidden";			
		}
		mmOpenMenus = null;
	}
}

function MM_menuHideSubmenus(menuName) {
	if( mmOpenMenus ) {
		var h = false;
		var c = 0;
		for(var i = 0; i < mmOpenMenus.length ; i++) {
			if( h ) {
				var m = document.getElementById(mmOpenMenus[i]);
				m.style.visibility = "hidden";
			} else if( mmOpenMenus[i] == menuName ) {
				h = true;
			} else {
				c++;
			}
		}
		mmOpenMenus.length = c+1;
	}
}

function MM_menuOverMenuItem(menuName, subMenuSuffix) {
	MM_menuResetTimeout();
	MM_menuHideSubmenus(menuName);
	if( subMenuSuffix ) {
		var subMenuName = "" + menuName + "_" + subMenuSuffix;
		MM_menuShowSubMenu(subMenuName);
	}
}

function MM_menuShowSubMenu(subMenuName) {
	MM_menuResetTimeout();
	var e = document.getElementById(subMenuName);
	e.style.visibility = "inherit";
	if( !mmOpenMenus ) {
		mmOpenMenus = new Array;
	}
	mmOpenMenus[mmOpenMenus.length] = "" + subMenuName;
}

function MM_menuResetTimeout() {
	if (mmHideMenuTimer) clearTimeout(mmHideMenuTimer);
	mmHideMenuTimer = null;
}

function MM_menuShowMenu(containName, menuName, xOffset, yOffset, triggerName) {
	MM_menuHideMenus();
	MM_menuResetTimeout();
	MM_menuShowMenuContainer(containName, xOffset, yOffset, triggerName);
	MM_menuShowSubMenu(menuName);
}

function MM_menuShowMenuContainer(containName, x, y, triggerName) {	
	var c = document.getElementById(containName);
	var s = c.style;
	s.visibility = "inherit";
	
	mmOpenContainer = "" + containName;
}