
//recebe o nome do ficheiro e verifica a extensao...
function checkImgExt(fileName) {
	var ext = fileName.split('.').pop().toLowerCase();
	if($.inArray(ext, ['pdf','doc','docx']) == -1) {
		//getPopUpReady('genericError','Please choose a supported image file: gif, jpg, jpeg, png',-1);
		alert('Por favor escolha um ficheiro com uma das seguintes extenções: pdf, doc, docx');
	    return false;
	} else {
		return true;
	}
}

$(document).ready(function() {
	
	$("#formContact").validate();

	$('.reply').live('click',function() {
		var artIdThis = $(this).attr('artId');
		getPopUpReady("replyContact", { artId: artIdThis  });
	});

	/* navegacao pelos menus*/
	  var firstMenu=null;
	  $('.optionMenu').live('click',function() {
		  
		  if(firstMenu==$(this).attr("id")){
			$("."+firstMenu).slideToggle();
			firstMenu=null;  
		  }
		  else if(firstMenu!=null){
			$("."+firstMenu).slideToggle();
			firstMenu =$(this).attr("id");
			$("."+firstMenu).slideToggle();
		  }
		  else{
			firstMenu =$(this).attr("id");
			$("."+firstMenu).slideToggle();
		  }
	  });
	  
	  var specialSelected=null;
	  $('ul li .clickDetect').live('click',function() {
		if($(this).hasClass('special')){
			if(specialSelected==$(this)){
				$(specialSelected).parent().find(".completDetails").slideToggle('slow');
				specialSelected=null;
			}
			else if(specialSelected!=$(this)){
				$(specialSelected).parent().find(".completDetails").slideToggle('slow');
				
				$(this).parent().find(".completDetails").slideToggle('slow');
				specialSelected=$(this);
				
			}
			else{
				$(this).parent().find(".completDetails").slideToggle('slow');
				specialSelected=$(this);
			}
		  }
		  
		  else{
		  
			$(this).parent().find(".completDetails").slideToggle('slow', function() {
				
				if ($('#candExpontSubmitTab').attr('isIE')=='true') {
					if ($('#candExpontSubmitTab').css('display')=='none') {
						$("#candExpont2").find('#fileInput').css('visibility','hidden');
					} else $("#candExpont2").find('#fileInput').css('visibility','visible');
				}
			  });
		  }
	  });

	 
	 $("#popupForm").find("#submit").live('click', function(e) {
		 e.preventDefault();
		 
		 if ($("#popupForm").find("#popupContact").valid()) {
			 var paramsCont = $("#popupForm").find("#popupContact").serialize();
			 
			 $.post("/php/lib/sendCV.php", paramsCont,
					   function(data) {
				 		var tmphtml = '<h3 style="text-align:center;padding-top:20px;">';
				 		if (data==1 || data == "1")
				 			$("#popupForm").html(tmphtml+'Candidatura enviada com sucesso</h3>');
				 		else $("#popupForm").html(tmphtml+'Erro ao enviar a candidatura, por favor tente novamente mais tarde</h3>');
			 });
		 }
		 
	 });

	 $("#popupForm").find('#uploadFile').live('click',function() {
		 $("#popupForm").find('#fileInput').click();
	 });
	 
	 $("#popupContactFile").find('#fileInput').live('change',function(){
		 if (jQuery.trim($(this).val()) != '') {
                 //verifica a extensao do ficheiro
                 if (!checkImgExt($(this).val())) {
                         return false;
                 }

                 $("#popupForm").find('#fileInput').upload('/php/lib/saveFile.php', function(res) {
                         if (jQuery.trim(res).slice(0,4) != 'http') {
                        	 alert(res);
		                 } else {
		                	 $("#popupForm").find('#fileName').html(' ( ' + $("#popupForm").find('#fileInput').val().replace("C:\\fakepath\\","")+' )');
		                	 $("#popupForm").find('#fileUrl').val(jQuery.trim(res));
		                	 $("#popupForm").find('#fileUrlError').html(" ");
		                 }
                 }, 'text');
         }
	 });
	 
	 // Cand expontaneas
	$('#candExpontSubmit').live('click',function(e) {
		e.preventDefault();
		
		$('#candExpont').find(".completDetails").slideDown();
		
		if ($("#candExpont").valid()) {
			var paramsCont = $('#candExpont').serialize();
			 
			 $.post("/php/lib/sendCVExp.php", paramsCont,
					   function(data) {
				 		if (data==1 || data == "1")
				 			getPopUpReady("simpleMessage", { message: 'Candidatura enviada com sucesso'  });
				 		else getPopUpReady("simpleMessage", { message: 'Erro ao enviar a candidatura, por favor tente novamente mais tarde' });
			 });
		}
		
	});
	
	
	 $("#candExpont").find('#uploadFile').live('click',function() {
		 $("#candExpont2").find('#fileInput').click();
	 });
	 
	 $("#candExpont2").find('#fileInput').live('change',function(){
		 if (jQuery.trim($(this).val()) != '') {
                 //verifica a extensao do ficheiro
                 if (!checkImgExt($(this).val())) {
                         return false;
                 }

                 $("#candExpont2").find('#fileInput').upload('/php/lib/saveFile.php', function(res) {
                         if (jQuery.trim(res).slice(0,4) != 'http') {
                        	 alert(res);
		                 } else {
		                	 $("#candExpont").find('#fileName').html(' ( ' + $("#candExpont2").find('#fileInput').val().replace("C:\\fakepath\\","")+' )');
		                	 $("#candExpont").find('#fileUrl').val(jQuery.trim(res));
		                	 $("#candExpont").find('#fileUrlError').html(" ");
		                 }
                 }, 'text');
         }
	 });
	 
	// validacoes candidatura expontanea
	$('.optionMenu').live('click',function() {
		
		$('#candExpont').validate({
			rules: {
				nome: {
					required: true,
					minlength: 2
				},
				nacionalidade: {
					required: true,
					minlength: 2
				},
				morada: {
					required: true,
					minlength: 2
				},
				email: {
					required: true,
					email: true,
					minlength: 2
				}
			},
			messages: {
				nome: "Por favor preencha o nome",
				nacionalidade: "Este campo é obrigatório",
				morada: "Este campo é obrigatório",
				email: "Este campo é obrigatório",
				cpostal4: "Este campo é obrigatório",
				cpostal3: "Este campo é obrigatório",
				localidade: "Este campo é obrigatório",
				"areaCandidata[]": "Este campo é obrigatório"
			}
		});
		
	});

});

