

// ==============================================================================================================
// ==============================================================================================================
// FUNÇÕES PARA FORMATAR CAMPOS GERAL ***************************************************************************
// ==============================================================================================================
// ==============================================================================================================





// PERMITE APENAS NÚMERO *****************************************************************************************

function numero(evento) {
	
	/************************************************
	backspace = 8
	enter = 13
	tab = 0 ou 9
	*************************************************/
	
	if( navigator.appName == "Netscape" ) {	var tecla = evento.which; } else { var tecla = evento.keyCode; }
	
	if ((tecla > 47 && tecla < 58) || tecla == 8 || tecla == 13 || tecla == 0) {
		return true; 
	}else{
		return false;
	}            
	
}




// FORMATA TELEFONE *****************************************************************************************

function telefone(objeto, evento){
	
	if( navigator.appName == "Netscape" ) {	var tecla = objeto.which; } else { var tecla = objeto.keyCode; }
	var telefone = evento;
	if(tecla != 8 && tecla != 9){
		if(telefone.value.length == 0)  telefone.value += '(';
		if(telefone.value.length == 3)  telefone.value += ') ';
		if(telefone.value.length == 9)  telefone.value += '-';
		
	}
}




// FORMATA CEP *****************************************************************************************

function cep(objeto, evento){
	
	var tecla = objeto.keyCode;
	var cep = evento;
	if(tecla != 8){
		if(cep.value.length == 5)  cep.value += '-';
		
	}
}




// FORMATA VALORES *****************************************************************************************

function limpa_valor(valor, validos) {
	// retira caracteres invalidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0) {
		result += aux;
		}
	}
	return result;
}

function valor(campo,tammax,teclapres,decimal) {
	
	var tecla = teclapres.keyCode;
	vr = limpa_valor(campo.value,"0123456789");
	tam = vr.length;
	dec=decimal
	
	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
	
	if (tecla == 8 )
	{ tam = tam - 1 ; }
	
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
	
	if ( tam <= dec )
	{ campo.value = vr ; }
	
	if ( (tam > dec) && (tam <= 5) ){
	campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 6) && (tam <= 8) ){
	campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ;
	}
	if ( (tam >= 9) && (tam <= 11) ){
	campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 12) && (tam <= 14) ){
	campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 15) && (tam <= 17) ){
	campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
	}

}




// VALIDA CPF *****************************************************************************************

function cpf(numcpf)
{
	x = 0;
	soma = 0;
	dig1 = 0;
	dig2 = 0;
	texto = "";
	numcpf1="";
	len = numcpf.length; x = len -1;
	// var numcpf = "12345678909";
	for (var i=0; i <= len - 3; i++) {
		y = numcpf.substring(i,i+1);
		soma = soma + ( y * x);
		x = x - 1;
		texto = texto + y;
	}
	dig1 = 11 - (soma % 11);
	if (dig1 == 10) dig1=0 ;
	if (dig1 == 11) dig1=0 ;
	numcpf1 = numcpf.substring(0,len - 2) + dig1 ;
	x = 11; soma=0;
	for (var i=0; i <= len - 2; i++) {
		soma = soma + (numcpf1.substring(i,i+1) * x);
		x = x - 1;
	}
	dig2= 11 - (soma % 11);
	if (dig2 == 10) dig2=0;
	if (dig2 == 11) dig2=0;
	//alert ("Digito Verificador : " + dig1 + "" + dig2);
	if ((dig1 + "" + dig2) == numcpf.substring(len,len-2)) {
		return true;
	}
	return false;
}





// VALIDA EMAIL *****************************************************************************************

function email(email)
{
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))){ 
		return false;
	} 	
	else {
		return true;
	}
}




// ABRE POPUP CPF *************************************************************************************

function popup(theURL,winName,features) {
	window.open(theURL,winName,features); 
}



// VALIDA CAMPOS *************************************************************************************

function limpa_campo(input){document.getElementById(input).value = '';}





// ==============================================================================================================
// FUNÇÕES PARA DO SITE PROCESSO SELETIVO 2010 ******************************************************************
// ==============================================================================================================





// VALIDA CAMPOS *************************************************************************************

function valida_campos(acao){
	
	
	if(acao == 'indique-este-site'){

		if(document.getElementById("form_indique_seu_nome").value == ''){
			alert("Digite o seu nome.");
			document.getElementById("form_indique_seu_nome").focus();
			return false;
		}
		
		if(email(document.getElementById("form_indique_seu_email").value) == false){
			alert("Digite o seu email corretamente.");
			return false;
		}

		if(document.getElementById("form_indique_amigo_nome").value == ''){
			alert("Digite o nome da pessoa para qual quer indicar nosso site.");
			document.getElementById("codigo_cliente_a").focus();
			return false;
		}
		
		if(email(document.getElementById("form_indique_amigo_email").value) == false){
			alert("Digite corretamente o email da pessoa para qual quer indicar nosso site.");
			return false;
		}
		
		document.getElementById(acao).submit();
		
	}
	
	
	if(acao == 'contatar'){
		
		$('#contatar-imobiliaria').attr('disabled', true);
		
		if(document.getElementById("contato_nome").value == ''){
			alert("Digite o nome completo.");
			document.getElementById("contato_nome").focus();
			$('#contatar-imobiliaria').removeAttr('disabled');
			return false;
		}
		
		if(email(document.getElementById("contato_email").value) == false){
			alert("Digite o email corretamente.");
			document.getElementById("contato_email").focus();
			$('#contatar-imobiliaria').removeAttr('disabled');
			return false;
		};		
		
		if(document.getElementById("contato_telefone").value.length != 14){
			alert("Digite o telefone corretamente.");
			document.getElementById("contato_telefone").focus();
			$('#contatar-imobiliaria').removeAttr('disabled');			
			return false;
		}
		
		if(document.getElementById("contato_mensagem").value == ''){
			alert("Digite a sua mensagem.");
			document.getElementById("contato_mensagem").focus();
			$('#contatar-imobiliaria').removeAttr('disabled');			
			return false;
		}
		
		document.getElementById(acao).submit();
		
	}
	
	
	if(acao == 'interesse'){
		
		if(document.getElementById("contato_nome").value == ''){
			alert("Digite o nome completo.");
			document.getElementById("contato_nome").focus();
			return false;
		}
		
		if(email(document.getElementById("contato_email").value) == false){
			alert("Digite o email corretamente.");
			document.getElementById("contato_email").focus();
			return false;
		};		
		
		if(document.getElementById("contato_telefone").value.length != 14){
			alert("Digite o telefone corretamente.");
			document.getElementById("contato_telefone").focus();			
			return false;
		}
		
		document.getElementById(acao).submit();
		
	}

}




// SELECT MENU CURSOS *************************************************************************************

function selecionaCurso(targ,selObj,restore){ 
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}




// ALTERNA MENU PRINCIPAL *************************************************************************************

function alterna_menu(selecionado) {
			
	if (selecionado == 'ps'){
		document.getElementById('smProcesso').style.display = "block";
		document.getElementById('smInfraestrutura').style.display = "none";
		document.getElementById('smConhecaoBA').style.display = "none";
	
	} else if (selecionado == 'infra'){
		document.getElementById('smProcesso').style.display = "none";
		document.getElementById('smInfraestrutura').style.display = "block";
		document.getElementById('smConhecaoBA').style.display = "none";
	
	} else {
		document.getElementById('smProcesso').style.display = "none";
		document.getElementById('smInfraestrutura').style.display = "none";
		document.getElementById('smConhecaoBA').style.display = "block";
		
	}
	
}




// JQUERY SELECT CURSOS *************************************************************************************

/*
 * jQuery Select Decorator Plugin v0.0.1
 * http://whynotonline.com/
 *
 * Copyright (c) 2009 Nivanka Fonseka
 * BSD licenses.
 * http://open.whynotonline.com/license/
 * 
 * This plugin is a handy tool which you can use to decorate your HTML select boxes
 * Feel free to use this on your websites, but please leave this message in the files
 * **NOTE: This plugins solution does not work on the IE browsers
 * 	       I have not tested in IE 8 though
 */

jQuery.fn.selectdecorator = function($options) {
	

	var defaults = {
		DIVClass: 'Select_Decorator_Holder'
	};
	
	$current = jQuery(this);
	$ID = $current.attr('id');
	jQuery.extend(defaults, $options);
	
	newSelect = '<div>';
	
	newSelect += '<select';
	newSelect += ' name="' + $current.attr('name');
	newSelect += '" id="' + $current.attr('id');
	newSelect += '" class="' + $current.attr('class');
	newSelect += '">';
	newSelect += $current.html();	
	newSelect += '</select>';
	newSelect += '<span></span>';
	newSelect += '<ul>';
	jQuery.each($current.find('option'), function(){
		newSelect += '<li val="' + jQuery(this).attr('value') + '">';
		newSelect += jQuery(this).html();
		newSelect += '</li>';
	});
	newSelect += '<ul>';
	newSelect += '</div>';
	
	$decorator = jQuery(newSelect);
	$decorator.addClass(defaults['DIVClass']);
	
	
	$current.parent().append($decorator) ;
	$current.remove();
	
	
	$decorator.find('ul').css('position', 'absolute');
	$decorator.find('ul').css('z-index', '999999');
	$decorator.find('ul').hide();
	$decorator.find('span').css('display', 'block');
	
	$decorator.find('span').bind('click',function(){
			$decorator.find('ul').slideDown('fast', function(){				
				jQuery('body').bind('click', function(){
					$decorator.find('ul').hide();												  
				});											
			});
		}
	);
	
	$decorator.find('ul>li').bind('click',function(){
		jQuery('#' + $ID).val(jQuery(this).attr('val'));											
		$decorator.find('span').parent().unbind('click');
		jQuery(this).parent().hide();
		jQuery(this).parent().parent().find('span').html(jQuery(this).html());
	});
	
	$newSelect = jQuery('#' + $ID);
	
	$newSelect.attr('style', 'opacity:0;position:absolute;top:-3000px;');
	
	$setFlag = 0;
	$newSelect.find('option').each(
		function(){
			if($setFlag == 0){
				$newSelect.parent().find('span').html(jQuery(this).html());	
				$newSelect.val(jQuery(this).val());
				$setFlag = 1;
			}
		}
	);
	//$newSelect.bind('change',optionChanged);
}
/*
function optionChanged(){
	$option = jQuery(this);	
	$setFlag = 0;
	$option.find('option').each(
		function(){
			if($setFlag == 0){
				if($option.val() == jQuery(this).attr('value')){
					$option.parent().find('span').html(jQuery(this).html());	
					$setFlag = 1;
				}				
			}
		}
	);
}*/
