// JavaScript Document

	function montajanela(url)
	{
	sList = window.open(url,'Ajuda','top=0,left=0 toolbar=no,scrollbars=yes,resizable=yes,status=no,dependent=yes');
	}
	
	function comboChange(comoBox,usado){ 
	  window.open( comoBox.options[comoBox.selectedIndex].value);
	  if (usado) comoBox.selectedIndex=0;
	}
	function PopUp2(theURL,winName,features) 
	{
	window.open(theURL,winName,features); 
	}
	
	function PopUp(theURL,winName,features) 
	{
	window.open(theURL,winName,features); 
	}
	
	function submetep(pagina){
	window.open(pagina,'Correios')
	}
	
	function submete_busca(){
		
		if(document.links.NUM_CONTRATO.value=='' || document.links.NUM_CONTRATO.value== 0){
			alert('Número do contrato inválido!');
			document.links.NUM_CONTRATO.focus();
		}else{
			if(document.links.ANO_CONTRATO.value=='' || document.links.ANO_CONTRATO.value== 0){
				alert('Ano do contrato inválido!');
				document.links.ANO_CONTRATO.focus();			
			}else{
				ano = document.links.ANO_CONTRATO.value;
				numero = document.links.NUM_CONTRATO.value;
				if (ano.length != 4){
					alert('Digite o ano com quatro dígitos!');
					document.links.ANO_CONTRATO.focus();	
				}else{
					//document.links.action = 'institucional/licit_compras_contratos/compras_contratos/outras_pesquisas_contr.cfm';
					location.href('outras_pesquisas_contr.cfm?nu_contrato='+numero+'&ano='+ano);
					//document.links.hd_controle.value = 1;
					//document.links.submit(); 
				}										 
//				 monta(document.links.N_ANO_CONTRATO.value,param);
			}
		}
		
	}
	function submete_busca_for(param){
		//alert(param);
		if (param == 'c' || event.keyCode == 13){
			if(document.links.no_fornecedor.value==''){
				alert('O nome do fornecedor é de preenchimento obrigatório !');
				document.links.no_fornecedor.focus();
			}else{
				nome = document.links.no_fornecedor.value; 			
				nome = nome.replace( /[" "]+/g, "" );	//retira os espaços em branco		
				if (nome.length < 2){
					alert('O nome do fornecedor deve conter no mínimo dois caracteres!');
					document.links.no_fornecedor.focus();				
				}else{
					location.href('outras_pesquisas_fornec.cfm?no_fornecedor='+document.links.no_fornecedor.value);
				}
			}
		}
	}
	
	function submete_busca_dt(param1,param2){		
		if((document.links.DOU_inicio.value=='') && (document.links.DOU_fim.value=='')){
			alert('Informe pelo menos uma data !');	
			document.links.DOU_inicio.focus();		
		}else{	
			if(document.links.DOU_inicio.value != ''){
				if(!valida_data(document.links.DOU_inicio.value.substring(0,2),document.links.DOU_inicio.value.substring(3,5),document.links.DOU_inicio.value.substring(6),document.links.DOU_inicio.value.length))
				{
					alert("Data Inválida !");
					document.links.DOU_inicio.focus();
					return false;
				}	
			}				
			if(document.links.DOU_fim.value != ''){
				if(!valida_data(document.links.DOU_fim.value.substring(0,2),document.links.DOU_fim.value.substring(3,5),document.links.DOU_fim.value.substring(6),document.links.DOU_fim.value.length))
				{
					alert("Data Inválida !");
					document.links.DOU_fim.focus();
					return false;
				}
			}	 		
			location.href('outras_pesquisas_dtpublica.cfm?dou_inicio='+param1+'&dou_fim='+param2);
		}
	}
	
	/*
	
	function submete_busca_dt(param1,param2){
		if((document.links.DOU_inicio.value=='') && (document.links_pc.DOU_fim.value=='')) {
			alert('Informe pelo menos uma data !');			
			document.links.DOU_inicio.focus();
		}else{
			location.href('outras_pesquisas_contr.cfm?dou_inicio='+param1+'&dou_fim='param2);
		}
	}
	*/ 
	function submete_busca_pc(param){
		if (param == 'c' || event.keyCode == 13){
			if(document.links.pchave.value==''){
				alert('A Palavra-Chave é de preenchimento obrigatório !');
				document.links.pchave.focus();
			}else{
				pal_chave = document.links.pchave.value; 				 							
				
				// Rotina para verificar se pelo menos uma das palavras chaves tem mais de 3 caracteres.
				var condicao = 1;				
				var i=0;
				var y=1;
				var cont = 0;
				while (condicao ==1 && i <= (pal_chave.length - 1)){					
					//alert ((pal_chave.substring(i,y)));
					if ((pal_chave.substring(i,y)) != " "){
						cont = cont + 1;						
					}
					else{
						if(cont >= 3){
							condicao = 0;
						}else{
							cont = 0;	
						}
					}
					i = i + 1;
					y = y + 1
				} 
				//------------------------------------------------------------------------------------
				//alert (cont);
				pal_chave = pal_chave.replace( /[" "]+/g, "" );	//retira os espaços em branco	
				if (pal_chave.length < 3 || cont < 3){
					alert('A Palavra-Chave deve conter no mínimo três caracteres!');
					document.links.pchave.focus();			
				}else{	
					//document.links.hd_pchave.value = document.links.pchave.value;
					//document.links.action = 'institucional/licit_compras_contratos/compras_contratos/outras_pesquisas_pchave.cfm';
					//document.links.submit();
					//document.links.pchave.focus();
					location.href('outras_pesquisas_pchave.cfm?pchave='+document.links.pchave.value);
				}			
			}
		}
	}
	
	function submete_busca_cpfcnpj(param){
		CPFCNPJ = document.links.cpfcnpj.value;
		if(document.links.cpfcnpj.value==''){
			alert('O CPF/CNPJ é de preenchimento obrigatório!');
			document.links.cpfcnpj.focus();
		}else{			
			if (CPFCNPJ.length != 11 && CPFCNPJ.length != 14) {
				alert('CPF/CNPJ Inválido!');
				document.links.cpfcnpj.focus();			
				
			}else{
				if (CPFCNPJ.length == 11){					
					validap();
				}else{
					valida();
				}
			}
				
			/*location.href('outras_pesquisas_contr.cfm?pchave='+param);*/
		}
	}
	
	function mascara(formato, keypress, objeto)
	{
	campo = eval(objeto);
	//alert(campo.value);
	if (formato=='parte')
		{
		if (campo.value.length <=  1 ){
			caracteres = 'cpavtdcieglCPAVTDCIEGL';
			}	else if (campo.value.length ==  2 ){
			caracteres = 'Ee01234567890';
		}else if(campo.value.length > 1 ){
			caracteres = '01234567890/';
	
		}
		separacoes = 1;
			
			if(campo.value.indexOf("/") != -1) {
				separacao  = '';
				}else{
				separacao  = '/';
			}
	
		conjunto1 = 8;
		conjunto2 = 4;
		
		if ((caracteres.search(String.fromCharCode(keypress))!=-1) && campo.value.length < (conjunto1+conjunto2+1))
			{	
			
						
			if (campo.value.length == conjunto1) campo.value = campo.value + separacao;
			
			}
		else event.returnValue=false;
		}
	}
	
	function bloqueia_nao_numerico(x)	
	{
		//alert(x);
		//alert (event.keyCode);				
		
		if((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode != 13)){
			event.keyCode = 0;
			alert("Digitar somente números nesse campo!");
		}else{
			if (event.keyCode == 13){
				if (x == '1'){
					submete_busca();
				}else{
					if (x == '2'){
						submete_busca_dt(document.links.DOU_inicio.value,document.links.DOU_fim.value);
					}else{
						if (x == '3'){
							submete_busca_cpfcnpj(document.links.cpfcnpj.value);
						}
					}
				}
			}
			return true;
		}

	}
	

	
	function formatar(src, mask) 
	{
	  var i = src.value.length;
	  var saida = mask.substring(0,1);
	  var texto = mask.substring(i)
		if (texto.substring(0,1) != saida) 
		  {
			src.value += texto.substring(0,1);
		  }
	}
 function valida_numero()
   {
     if (window.event.keyCode < 48 || window.event.keyCode > 57)
     event.returnValue = false;
   }   

 //Formata CNPJ

function FormataCNPJ(Campo,teclapres) 
 {
	var tecla = teclapres.keyCode;
	vr = Campo.value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "-", "" );
	tam = vr.length+1;
	if ( tam > 2 && tam < 6 )
			   Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3);
	if ( tam >= 6 && tam < 9 )
			   Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3);
	if ( tam >= 9 && tam < 13 )
			   Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4);
	if ( tam >= 13 && tam < 15 )
			   Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4) + '-' + vr.substr(12,2);
 }
 
function valida()

{
	var i, j, k, Soma;
	cnpj = document.links.cpfcnpj.value;       
	cnpj = cnpj.replace( ".", "" );
	cnpj = cnpj.replace( ".", "" );
	cnpj = cnpj.replace( "/", "" );
	cnpj = cnpj.replace( "-", "" );
	if (cnpj.length != 14)
	{
 		alert('CNPJ Inválido!');
		document.links.cnpj.focus();
		return false;
	}
  k = 0;
	  while (k <= 1)
	  {
		 Soma = 0;
		 j = 5 + k;
		 for (i=0; i <= 11+k; i++)
		 {
			Soma += parseInt(cnpj.charAt(i),10)*j;
		   	if ( j != 2)
			  	j--;
		    else
			  j = 9;
		  }
		 Soma = 11 - Soma%11;
		 if (Soma >= 10)
			Soma = 0;
		 if ( Soma != parseInt(cnpj.charAt(12+k)))
	     {
			alert('CNPJ Inválido!');
			document.links.cpfcnpj.focus();
			return false;
	   	 }
		 k++;
	  }
	  location.href('outras_pesquisas_cpfcnpj.cfm?cpfcnpj='+cnpj);
}

function validap()
{
	var i, j, k, Soma;
	CPF = document.links.cpfcnpj.value;	
	CPF = CPF.replace( ".", "" );
	CPF = CPF.replace( ".", "" );
	CPF = CPF.replace( "-", "" );
	if(document.links.cpfcnpj.value =="")
	{
		alert("Informe o CPF/CNPJ do Fornecedor!");
		document.links.cpfcnpj.focus();
		return false;
	}
	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" || CPF == "22222222222" || CPF == "33333333333" || CPF == "44444444444" ||        CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||     CPF == "88888888888" || CPF == "99999999999")
	{
		alert('CPF Inválido!');
		document.links.cpfcnpj.focus();
		return false;
	}
	soma = 0;
	for (i=0; i < 9; i ++)
		soma += parseInt(CPF.charAt(i)) * (10 - i);	
		resto = 11 - (soma % 11);	
		if (resto == 10 || resto == 11)	
			resto = 0;	
		if (resto != parseInt(CPF.charAt(9)))	
		{	
			alert('CPF Inválido!');	
			document.links.cpfcnpj.focus();	
			return false;		
	}	
	soma = 0;	
	for (i = 0; i < 10; i ++)	
		soma += parseInt(CPF.charAt(i)) * (11 - i);	
	resto = 11 - (soma % 11);	
	if (resto == 10 || resto == 11)	
		resto = 0;
	
	if (resto != parseInt(CPF.charAt(10)))	
	{	
		alert('CPF Inválido!');	
		document.links.cpfcnpj.focus();	
		return false;
	}	
	location.href('outras_pesquisas_cpfcnpj.cfm?cpfcnpj='+CPF);
}

	function setfocus(campo,tam, prox_campo){	
		//alert (prox_campo);
		var valor = campo.value;
		if (valor.length == (tam)){					
			//links.prox_campo.focus();
			//document.write('escrevendo');
			document.links.DOU_fim.focus();
		}	
	}	
		  
	  function submete(tipo) {
	   
			document.pesq_tipo_contrato.tipo_contrato.value=tipo;
			document.pesq_tipo_contrato.submit();	
		}				  					  


function valida_data(dia,mes,ano,tamanho)
  {
    var v_dia;
    var v_mes;
    var v_ano;
    v_dia = dia;
    v_mes = mes;
    v_ano = ano;
    v_tamanho = tamanho; 

    var c = 0;
    for(var x = 1; x < v_tamanho + 1; x++){
      c = c + 1;
    }

    if (c < 8) {
         return(false);
    }   

    if (v_dia.length < 2){
      return(false);
    }   

    if (v_mes.length < 2){
      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) || (v_dia < 1)){
      return(false);
    }
    if ((v_mes > 12) || (v_mes < 1)){
      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);
  } 

