$(document).ready(function(){
	$("#CEP").mask("99999-999");
	$(".ddd").mask("99");
	$(".telefone").mask("9999-9999");
	
	$(".data").mask("99/99/9999");
	$(".cpf").mask("999.999.999-99");
	
	$("#enviarForm").click(function(){
		$("#erroPadrao").html("");
		//$(this).attr("alt","Validando Informações...");
		//$(this).attr("src","");

			error=0;
			returnMsg = new Array();
			menor = 0;
			
			//Validando os inputs Selects e Textareas Obrigatorios
			$(".obgt").each(function(i){
				if(this.value==""){
					$("#label"+this.id).css('background', '#E8C5AB');
					returnMsg[this.tabIndex] = '<span>'+ $("#label"+this.id).attr('title')+ '</span>';
					error++;
					if(menor==0){
						menor = this.id;
					}
				}
			});
			
			//Validando Funções
			funcs = $("#funcoesReal").val();
			if (funcs == ""){
				$("#labelfuncoesReal").css('background', '#E8C5AB');
				returnMsg[this.tabIndex] = '<span>Adicione pelo menos uma função</span>';
				error++;
				if(menor==0){
					menor = "funcoes"
				}
			}
			
			
			//Validando os CheckBox
			sexo = $(".radioSex:checked").val();
			if(sexo == "undefined" || sexo == "" || sexo == null){
				$("#labelsexo").css('background', '#E8C5AB');
				returnMsg[$("input[name='sexo']").attr('tabIndex')] = '<span>Informe seu Sexo</span>';
				error++;
				if(menor==0){
					menor = "sexo";
				}
			}
			
			portNecEsp = $(".portNecEsp:checked").val();
			if(portNecEsp == "undefined" || portNecEsp == "" || portNecEsp == null){
				$("#labelPortNecEsp").css('background', '#E8C5AB');
				returnMsg[$("input[name='PortNecEsp']").attr('tabIndex')] = '<span>Informe se Você tem Necessidade Especial</span>';
				error++;
				if(menor==0){
					menor = "PortNecEsp"
				}
			}else{
				if(portNecEsp == 1 && ( $("#PortNecEspQual").val() == "undefined" || $("#PortNecEspQual").val() == "" || $("#PortNecEspQual").val() == null)){
					//limpando o label do sim nao nec esp
					$("#labelPortNecEsp").css('background', '#DAE9F3');
					
					$("#labelPortNecEspQual").css('background', '#E8C5AB');
					returnMsg[$("input[name='PortNecEspQual']").attr('tabIndex')] = '<span>Informe qual a sua Necessidade Especial</span>';
					error++;
					if(menor==0){
						menor = "PortNecEspQual"
					}
				}else{
					//limpando o campo caso preenchido
					$("#labelPortNecEspQual").css('background', '#DAE9F3');
				}
			}
			
			estCivil = $(".radioCivil:checked").val();
			if(estCivil == "undefined" || estCivil == "" || estCivil == null){
				$("#labelEst_Civil").css('background', '#E8C5AB');
				returnMsg[$("input[name='Est_Civil']").attr('tabIndex')] = '<span>Informe seu Estado Civil</span>';
				error++;
				if(menor==0){
					menor = "Est_Civil"
				}
			}
			
			experiencia = $(".radioExp:checked").val();
			if(experiencia == "undefined" || experiencia == "" || experiencia == null){
				$("#labelpossuiExperiencia").css('background', '#E8C5AB');
				returnMsg[$("input[name='possuiExperiencia']").attr('tabIndex')] = '<span>Informe se Possui Experiência Profissional</span>';
				error++;
				if(menor==0){
					menor = "possuiExperiencia"
				}
			}
			
			//Limpando os campos		
			$(".obgt").blur(function(){
				$("#label"+this.id).css('background', '#DAE9F3');
			});	
			
			$(".inputRadio").blur(function(){
				$("#label"+this.name).css('background', '#DAE9F3');
			});
			
			$(".inputCheck").blur(function(){
				$("#label"+this.name).css('background', '#DAE9F3');
			});
			
			$("#funcoes").blur(function(){
				$("#labelfuncoesReal").css('background', '#DAE9F3');
			});
			
			if(experiencia == 1){
				//Validando os inputs Selects e Textareas Obrigatorios Se tiver Experiencia
				$(".obgtPro").each(function(i){
					if(this.value==""){
						$("#label"+this.id).css('background', '#E8C5AB');
						returnMsg[this.tabIndex] = '<span>'+ $("#label"+this.id).attr('title')+ '</span>';
						error++;
						if(menor==0){
							menor = this.id;
						}
					}
				});
			
				radioTrabalhando = $(".radioTrabalhando:checked").val();
				if(radioTrabalhando == "undefined" || radioTrabalhando == "" || radioTrabalhando == null){
					$("#labelTrab_Atual").css('background', '#E8C5AB');
					returnMsg[$("input[name='Trab_Atual']").attr('tabIndex')] = '<span>Informe se Você esta Trabalhando Atualmente</span>';
					error++;
					if(menor==0){
						menor = "Trab_Atual"
					}
				}
				
				
				//Limpando os campos		
				$(".obgtPro").blur(function(){
					$("#label"+this.id).css('background', '#DAE9F3');
				});	
			}
				

			
			if(error==0){
				$("#curriculo").submit();
			}else{
				if (error > 1){ 
					plural = "s";
				}else{ 
					plural = "";
				}
				
				mensagemFinal = '<h2 class="verifique">Por favor, verifique o'+plural+' campo'+plural+' em vermelho:</h2>';
				mensagemFinal += returnMsg.join("");
				
				$("#erroPadrao").html(mensagemFinal);
				$("#erroPadrao").show();
				$("#"+menor).focus();
				$.scrollTo('#erroPadrao', 900);
				
				$(this).removeAttr("disabled");
				$(this).val("Cadastrar");
			}		
		});
			
	$("#temExp").click(function(){
		$("#expEscondida").slideDown("slow");
	});
			
	$("#naoExp").click(function(){
		$("#expEscondida").slideUp("slow");
	});
	
	$("#PortNecEspSim").click(function(){
		$("#necQual").slideDown("slow");
	});
	
	$("#PortNecEspNao").click(function(){
		$("#necQual").slideUp("slow");
	});
	
	$("#uf").change(function(){
		if ( $(this).val() != "" )
		{
			$('#Cidade').html('<option>Carregando...</option>');
			$.get('curriculo/cidades',{ uf : $(this).val() },function(resposta){
	      		$('#Cidade').html(resposta);    	
	      	});
		}
    });
    
    $("#ufEmp").blur(function(){
		if ( $(this).val() != "" )
		{
			$('#CidadeEmp').html('<option>Carregando...</option>');
			$.get('curriculo/cidades',{ uf : $(this).val() },function(resposta){
	      		$('#CidadeEmp').html(resposta);
	      	});
		} 
    });
    
   $("#ufEmp2").blur(function(){
		if ( $(this).val() != "" )
		{
			$('#CidadeEmp2').html('<option>Carregando...</option>');
			$.get('curriculo/cidades',{ uf : $(this).val() },function(resposta){
	      		$('#CidadeEmp2').html(resposta);     	
	      	});
		}
    });

   $("#abrirFoto").click(function(){
		$("#iframeFoto").slideDown("slow");
   });
});