// uso: var retorno = selecionaItemCombo(document.frm2.b_state, 'SP')
// funcção que seleciona um option de um combo
function selecionaItemCombo(sel_name, abbr_name) {
	if(abbr_name!='') {
		if(sel_name.length > 0) {
			var maxIndex = sel_name.length;
			for(var i=0; i<maxIndex; i++) {
				if(abbr_name== sel_name.options[i].value) {
					sel_name.selectedIndex = i;
					break;
				}
			}
		}
	}
	return true;    
}

// Envia um form para ser processado em uma página
// uso: ExecutarASP(formCadastro,'idExecASP','proprietariosBD.asp');
function ExecutarASP(oForm,target,URL) {
	oForm.target = target;
	oForm.action = URL;
	oForm.submit();
}

// limpa o iFrame (abre um apagina em branco)
// uso: limpaPagina('iDados','branco.asp','document');
function limpaPagina(idObjeto,url,escopo) {
	with(eval(escopo)) {
		document.getElementById(idObjeto).src = url;
	}
}

// Envia apenas os campos desejados para ser processado em uma página, 
// por exemplo em uma página em background
function enviarRequisicao(idObjeto, url, parametros) {
	document.getElementById(idObjeto).src = url + parametros;
}

// Abre um pop-up centralizado (sem Scroll)
function openPopUp(url,nomeJanela,largura,altura){
	var left = (screen.width-largura)/2;
	var top = (screen.height-altura)/2;
	top = top - 30;
	var atributos = "left="+left+",top="+top+",width="+largura+",height="+altura;
	window.open(url,nomeJanela,atributos);
}

// Abre um pop-up centralizado (com Scroll)
// sintaxe: abrePopUp('popUp.html','?id=5&categoria=14','nomeDaJanela','450','300');
function abrePopUp(URL,parametros,nameDaJanela,larguraJanela,alturaJanela) {
	larguraBrowser = screen.width;
	alturaBrowser = screen.height;
	larguraJanela = larguraJanela;
	alturaJanela = alturaJanela;

	posX = (larguraBrowser-larguraJanela)/2
	posY = (alturaBrowser-alturaJanela)/2
	URL = URL + parametros
	
	window.open(URL,nameDaJanela,'height='+alturaJanela+',width='+larguraJanela+',top='+posY+',left='+posX+',toolbar=no,minimize=no,status=no,memubar=no,location=no,scrollbars=yes');
}

// Abre um pop-up centralizado (com Scroll e menubar)
// sintaxe: abrePopUp_memubar('popUp.html','?id=5&categoria=14','nomeDaJanela','450','300');
function abrePopUp_memubar(URL,parametros,nameDaJanela,larguraJanela,alturaJanela) {
	larguraBrowser = screen.width;
	alturaBrowser = screen.height;
	larguraJanela = larguraJanela;
	alturaJanela = alturaJanela;

	posX = (larguraBrowser-larguraJanela)/2
	posY = (alturaBrowser-alturaJanela)/2
	URL = URL + parametros
	
	window.open(URL,nameDaJanela,'height='+alturaJanela+',width='+larguraJanela+',top='+posY+',left='+posX+',toolbar=no,minimize=no,status=no,menubar=yes,location=no,scrollbars=yes');
}

// Muda a cor do background de um elemento
// uso: onmouseover="setBackground(this,'green');"
function setBackground(obj,cor) {
	obj.style.backgroundColor = cor;
}

// Muda a cor da linha(TR) da tabela
// uso: onmouseout="setColorTR(this,'green');"
function setColorTR(obj,cor) {
	obj.style.backgroundColor = cor;
}

//  Moldura que abre e fecha (+ e -)
/*
uso:
<!-- cabeçalho -->
<div align="left" id="divCabecalho" style="border-bottom: 1 solid #00000; width: 98%;">
<INPUT TYPE="IMAGE" ID="iconeCabecalho" BORDER="0" STYLE="vertical-align: middle; " SRC="adminImagens/mais_preto.gif" ONCLICK="Mostrar_Ocultar('iconeCabecalho','divConteudo','adminImagens/');" ACCESSKEY="1">
<font class="fonte"><b>Cabeçalho</b></font>
</DIV>

<!-- conteudo -->
<div id="divConteudo" style="display: none; width: 98%; height:300px; border-left: 1 solid #000000; border-right: 1 solid #000000; border-bottom: 1 solid #000000">
<IFRAME ID="iframeConteudo" NAME="iframeConteudo" frameborder="0" SRC="" SCROLLING="AUTO" STYLE="display: block; width: 100%; height:100%;"></IFRAME>
</div>
<br>
*/
function abreFechaDiv(icone,conteudo,caminhoImagens) {
	if (document.getElementById(conteudo).style.display == 'none') {
		document.getElementById(icone).src = caminhoImagens + 'menos_preto.gif';
		document.getElementById(conteudo).style.display = 'block';
	} else {
		document.getElementById(icone).src = caminhoImagens + 'mais_preto.gif';
		document.getElementById(conteudo).style.display = 'none';
	}
}

// uso: abreDiv('iconeResultadoPesquisa','divResultadoPesquisa','adminImagens/','document')
function abreDiv(icone,conteudo,caminhoImagens,escopo) {
	with(eval(escopo)) {
		document.getElementById(icone).src = caminhoImagens + 'menos_preto.gif';
		document.getElementById(conteudo).style.display = 'block';
	}
}

// uso: fechaDiv('iconeResultadoPesquisa','divResultadoPesquisa','adminImagens/','parent')
function fechaDiv(icone,conteudo,caminhoImagens,escopo) {
	with(eval(escopo)) {
		document.getElementById(icone).src = caminhoImagens + 'mais_preto.gif';
		document.getElementById(conteudo).style.display = 'none';
	}
}

// Passa para o próximo campo do formulário(auto tab) após um determinado numero de caracteres no campo atual
function autoTab(input,len, e) {
	var isNN = (navigator.appName.indexOf("Mozilla")!=-1);
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].select();
	}
}

// Mascara para campos de valores fincanceiros ex: "1.200,00"
// uso: onKeyPress="return mascaraDinheiro(this,'.',',',event);"
function mascaraDinheiro(fld, milSep, decSep, 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;

	/* TECLAS 
	* 0 -> Del
	* 8 -> BackSpace
	* 13 -> Enter
	*  -> Barra de Espaço
	*/

    if (whichCode == 13) return true; // Enter
    if (whichCode == 0) return true;  // Del
    if (whichCode == 8) return true;  // BackSpace
    if (whichCode == 32) return true;  // Barra de Espaço

    key = String.fromCharCode(whichCode);  // Get key value from key code
 
    if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
    len = fld.value.length;
    for(i = 0; i < len; i++)
    if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
    aux = '';
    for(; i < len; i++)
    if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) fld.value = '';
    if (len == 1) fld.value = '0'+ decSep + '0' + aux;
    if (len == 2) fld.value = '0'+ decSep + aux;
    if (len > 2) {
    aux2 = '';
    for (j = 0, i = len - 3; i >= 0; i--) {
    if (j == 3) {
    aux2 += milSep;
    j = 0;
    }
    aux2 += aux.charAt(i);
    j++;
    }
    fld.value = '';
    len2 = aux2.length;
    for (i = len2 - 1; i >= 0; i--)
    fld.value += aux2.charAt(i);
    fld.value += decSep + aux.substr(len - 2, len);
    }
    return false;
}

// mascara de números
// uso: onkeypress="return(mascaraNumeros(this,event));"
function mascaraNumeros2(campo,e) {
	var teclaValida = true;
	
	// Pega o código da tecla digitada
	var whichCode = (window.Event) ? e.which : e.keyCode;
	// Transforma o código da tecla digitada em um valor real(ex: cod. 49 -> valor. 1) 
	tecla = String.fromCharCode(whichCode);
	
	/* TECLAS 
	* 0 -> Del
	* 8 -> BackSpace
	* 48-57 -> numeros de 0 a 9
	*/
	if (whichCode == '8' || whichCode == '0') {
		teclaValida = true;
	} else if (whichCode >= '48' && whichCode <= '57') {
		teclaValida = true;
	} else {
		teclaValida = false;
	}

	// Retorno da função (se a tecla for válida, aparece no campo)	
	if (teclaValida == true) return true;
	if (teclaValida == false) return false;
	
} 


// mascara de CEP "01234-100"
// uso: onkeypress="return (mascaraCEP(this,event));" maxlength="9"
function mascaraCEP(campo,e) {

	var teclaValida = true;
	
	// Pega o código da tecla digitada
	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	// Transforma o código da tecla digitada em um valor real(ex: cod. 49 -> valor. 1) 
	tecla = String.fromCharCode(whichCode);
	
	/* TECLAS 
	* 0 -> Del
	* 8 -> BackSpace
	* 48-57 -> numeros de 0 a 9
	*/
	if (whichCode == '8' || whichCode == '0') {
		teclaValida = true;
	} else if (whichCode >= '48' && whichCode <= '57') {
		teclaValida = true;
	} else {
		teclaValida = false;
	}
	
	if (campo.value.length == 5 && whichCode != '8' && whichCode != '0') 
		campo.value = campo.value + '-';

	// Retorno da função (se a tecla for válida, aparece no campo)	
	if (teclaValida == true) return true;
	if (teclaValida == false) return false;
	
} 


// mascara de mês/ano "01/2000"
// uso: onkeypress="return(mascaraMesAno(this,event));"
function mascaraMesAno(vdateValue,e) {
	var strSeparator='/';
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	var mesCheck ='01,02,03,04,05,06,07,08,09,10,11,12';
	if (whichCode == 13) return true;
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		//alert('Voce pressionou a tecla invalida');
		//vdateName.value=vdateName.value.substr(0,(vdateValue.length-1));
		return false;
	}
	
	if (vdateValue.value.length == 2) {
	 	vdateValue.value=vdateValue.value+strSeparator;
	}    
	
	return true;
}


// valida EMAIL
// retornoEmail = validaEmail('email@email.com.br')
function validaEmail(email) {
	if(email.indexOf("@") < 2) {
		return false;
	}
	if(email.indexOf("@",email.indexOf("@") + 1) != -1) {
		return false;
	}
	if(email.indexOf(".") < 1) {
		return false;
	}
	if(email.indexOf(" ") != -1) {
		return false;
	}
	if(email.indexOf("zipmeil.com") > 0) {
		return false;
	}
	if(email.indexOf("hotmeil.com") > 0) {
		return false;
	}
	if(email.indexOf(".@") > 0) {
		return false;
	}
	if(email.indexOf("@.") > 0) {
		return false;
	}
	if(email.indexOf(".com.br.") > 0) {
		return false;
	}
	if(email.indexOf("/") > 0) {
		return false;
	}
	if(email.indexOf("[") > 0) {
		return false;
	}
	if(email.indexOf("]") > 0) {
		return false;
	}
	if(email.indexOf("(") > 0) {
		return false;
	}
	if(email.indexOf(")") > 0) {
		return false;
	}
	if(email.indexOf("..") > 0) {
		return false;
	}

return true
}


function validacao() {
	var enviar = true
	
	var cpf = document.formulario.cpf.value;
	cpfPreparado = preparaCPFValidacao(cpf);
	
	if(validaCPF(cpfPreparado) == false && enviar == true) {
		alert('erro cpf');
		enviar = false;
	}

	var cnpj = document.formulario.cpf.value;
	cnpjPreparado = preparaCPFValidacao(cpf);
	
	if(ValidaCNPJ(cnpjPreparado) == false && enviar == true) {
		alert('erro cnpj');
		enviar = false;
	}
	
	if (enviar == true) {
		alert("Formulario Enviado");
	}
}

function preparaCPFValidacao(cpf) {
	cpf = cpf.replace('.','');
	cpf = cpf.replace('.','');
	cpf = cpf.replace('-','');
	cpfValidado = cpf;
	//alert(cpfValidado)
	return cpfValidado;
}

// valida CPF
// retornoCPF = validaCPF('33200983345')
function validaCPF(s){
 	s = preparaCPFValidacao(s)
	var i;  
	var c = s.substr(0,9);  
	var dv = s.substr(9,2);  
	var d1 = 0;
  
  	for (i = 0; i < 9; i++){   
		d1 += c.charAt(i)*(10-i); 
	} 
  
	if (d1 == 0){  
		var valido = false;
		return false;  
	} 
  
	d1 = 11 - (d1 % 11); 
  
	if (d1 > 9) d1 = 0; 
  
	if (dv.charAt(0) != d1){  
		var valido = false;
		return false;  
	} 
   
	d1 *= 2; 
  
	for (i = 0; i < 9; i++){  
		d1 += c.charAt(i)*(11-i); 
	} 
  
	d1 = 11 - (d1 % 11); 
  
	if (d1 > 9) d1 = 0; 
  
	if (dv.charAt(1) != d1){ 
		var valido = false;
  		return false;   
	} 

return true; 
} 



//--------------------------------------------------------------------
// Valida um campo text que deve conter um CGC DESFORMATADO
// Entradas: e - referência ao campo a ser validado
//           d - mensagem a ser mostrada (alert) em caso de erro
//--------------------------------------------------------------------
function ValidaCNPJ(e) {
	if (e.type == 'text') 	{
		if (!isCNPJ(e.value)) 	{
			if (!e.disabled) e.focus();
			return false;
		}
	}
	return true;
}

function preparaCNPJValidacao(cnpj) {
	cnpj = cnpj.replace('.','');
	cnpj = cnpj.replace('.','');
	cnpj = cnpj.replace('/','');
	cnpj = cnpj.replace('-','');
	cnpjValidado = cnpj;
	return cnpjValidado;
}
//--------------------------------------------------------------------
// isCNPJ
//-------------------------------------------------------------------- 
function isCNPJ(cnpj) {
	strNum = "";  
	if (cnpj == "") return (false);
	l = cnpj.length;
	for (i = 0; i < l; i++) 
	{
	  caracter = cnpj.substring(i,i+1)
	  if ((caracter >= '0') && (caracter <= '9'))
	  {
	     strNum = strNum + caracter;
	  }
	}
	
	strMul = "6543298765432";
	iValido = 1;
	if(strNum.length != 14) return(false);
	iSoma = 0;
	strNum_base = strNum.substring(0,12); 
	iLenNum_base = strNum_base.length - 1;
	iLenMul = strMul.length - 1;
	
	for(i = 0;i < 12; i++)
	{
	   iSoma = iSoma +
						 parseInt(strNum_base.substring((iLenNum_base-i),(iLenNum_base-i)+1),10) *
						 parseInt(strMul.substring((iLenMul-i),(iLenMul-i)+1),10)
	}
	
	iSoma = 11 - (iSoma - Math.floor(iSoma/11) * 11);

	if(iSoma == 11 || iSoma == 10) iSoma = 0;
	
	strNum_base = strNum_base + iSoma; 
	iSoma = 0;
	iLenNum_base = strNum_base.length - 1
	
	for(i=0; i < 13; i++)
	{
		iSoma = iSoma +
						parseInt(strNum_base.substring((iLenNum_base-i),(iLenNum_base-i)+1),10) *
						parseInt(strMul.substring((iLenMul-i),(iLenMul-i)+1),10)
	}

	iSoma = 11 - (iSoma - Math.floor(iSoma/11) * 11);

	if(iSoma == 11 || iSoma == 10)
	{
		iSoma = 0;
	}
	strNum_base = strNum_base + iSoma; 
	if(strNum != strNum_base)
	{
	  return(false);
	}

	return(true);
}





// uso: onkeypress="return mascaraRG(this,event);" maxlength="12"
function mascaraRG(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789x';
		
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	
	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 6) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 10) vdateValue.value=vdateValue.value + '-';

	return true;
}



// uso: onkeypress="return mascaraCPF(this,event);" maxlength="14"
function mascaraCPF(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	
	if (vdateValue.value.length == 3) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 7) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 11) vdateValue.value=vdateValue.value + '-';

	return true;
}


 
// uso: onkeypress="return mascaraCNPJ(this,event);" maxlength="18"
function mascaraCNPJ(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	//e.preventDefault();
	//var tecla = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 6) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 10) vdateValue.value=vdateValue.value + '/';
	if (vdateValue.value.length == 15) vdateValue.value=vdateValue.value + '-';
	
	
	return true;
}

// uso: onkeypress="return mascaraData(this,event);" maxlength="10"
function mascaraData(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + '/';
	if (vdateValue.value.length == 5) vdateValue.value=vdateValue.value + '/';

	
	return true;
}

// uso: onkeypress="return mascaraHora(this,event);" maxlength="5"
function mascaraHora(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + ':';
	
	return true;
}

// uso: onkeypress="return mascaraDataHora(this,event);" maxlength="16"
function mascaraDataHora(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + '/';
	if (vdateValue.value.length == 5) vdateValue.value=vdateValue.value + '/';
	if (vdateValue.value.length == 10) vdateValue.value=vdateValue.value + ' ';
	if (vdateValue.value.length == 13) vdateValue.value=vdateValue.value + ':';
	
	
	return true;
}

// uso: onkeypress="return mascaraNumeros(this,event);"
function mascaraNumeros(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	
	return true;
}



// Autor: jair.issa@linksat.com.br 
// Modificações e melhorias envie email 
function verificacaoDeVariasTeclas() {
// Verificação das Teclas  
var tecla = window.event.keyCode; 
var alt = window.event.altKey; // Para Controle da Tecla ALT 
var ctrl = window.event.ctrlKey; // Para Controle da Tecla CTRL 

// Evita feclar via Teclado 
if (alt && tecla==115) { 
	alert("Pra que utilizar o F4 Se eu Coloquei o Botão Fechar?"); 
	event.keyCode=116; event.returnValue=false; 
} 

// Evita Organizar Favoritos
if (ctrl && tecla==66) {
	alert("CTRL+B bloqueado");event.keyCode=0; 
	event.returnValue=false;
}  

if (ctrl && tecla==68) {
	alert("CTRL+D bloqueado");
	event.keyCode=0; 
	event.returnValue = false;
} 

// Evita Adicionar aos Favoritos 
if (ctrl && tecla==73) {
	alert("CTRL+I bloqueado");
	event.keyCode=0; 
	event.returnValue=false;
} 

// Evita Favoritos no Navegador 
if (ctrl && tecla==78) {
	alert("CTRL+N bloqueado");
	event.keyCode=0; 
	event.returnValue=false;
} 

}
