// 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 uma nova janela
function abreNovaJanela(url,nomeJanela){
	//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;
	var atributos = '';
	window.open(url,nomeJanela,atributos);
}

// 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');
}

// 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: abreDiv('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 mascaraNumeros(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));"
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 hora "10:00"
// uso: onkeypress="mascaraHora(this,event);"
function mascaraHora(vdateValue,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 (vdateValue.value.length == 2 && whichCode != '8' && whichCode != '0') 
		vdateValue.value = vdateValue.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 data e hora "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;
}

// mascara de data e hora "01/01/2000 10:00"
// uso: onkeypress="return(mascaraDataHora(this,event));"
function mascaraDataHora(vdateValue,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 (vdateValue.value.length == 2 && whichCode != '8' && whichCode != '0') 
		vdateValue.value = vdateValue.value+'/';
	
	if (vdateValue.value.length== 5 && whichCode != '8' && whichCode != '0') 
		vdateValue.value = vdateValue.value+'/';
	
	if (vdateValue.value.length== 10 && whichCode != '8' && whichCode != '0') 
		vdateValue.value = vdateValue.value+" ";
	
	if (vdateValue.value.length== 13 && whichCode != '8' && whichCode != '0') 
		vdateValue.value = vdateValue.value+":";
	
	// Retorno da função (se a tecla for válida, aparece no campo)	
	if (teclaValida == true) return true;
	if (teclaValida == false) return false;
	
}



// valida CPF
// retornoCPF = validaCPF('33200983345')
function validaCPF(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 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
}

