$(function(){
	$('#fotoReactie').submit(function(){
		if(formValidate('fotoReactie')){
			var formData = $(this).serialize();
			$.ajax({
				type: 'POST',
				url: baseUrl()+'/timeline/inc/reactie_save.inc.php',
				data: formData,
				success: function(data){
					var melding = data.split('#');
					
					if(melding[0]=="ok"){
						$('#foto_reactie_load').html('<img src="timeline/img/loading.gif">').load(baseUrl()+'/timeline/inc/foto_overzicht_reacties.inc.php?foto_id='+melding[1], function(){
							alert('Uw bericht is opgeslagen');						
							$('#foto_reactie_form').hide();
							$('#fotoReactie .required').val('');
						});
					}else{
						alert('Uw bericht is niet opgeslagen. Probeer het opnieuw');	
					}
				}
			});				
		}
		return false;
	});
	
	
	$('#fotoVriend').submit(function(){
		if(formValidate('fotoVriend')){
			var formData = $(this).serialize();
			$.ajax({
				type: 'POST',
				url: baseUrl()+'/timeline/inc/vrienden_uitnodigen.inc.php',
				data: formData,
				success: function(data){
					var melding = data.split('#');
					
					if(melding[0]=="ok"){
						$('#fotoVriend .required').each(function(){
							if(!$(this).hasClass('no_empty')){
								$(this).val('');	
							}
						});
						alert('Uw vriend is op de hoogte gebracht. \n\r\n\rU kunt nu direct nog meer vrienden uitnodigen!');						
						$('#fotoVriend #vriend_naam').focus();
					}else{
						alert('Uw bericht is niet verzonden. Probeer het opnieuw');	
					}
				}
			});				
		}
		return false;
	});	
});

function baseUrl(){
	return document.getElementsByTagName('base')[0].href;
}
function gotoUrl(url){
	location.href = document.getElementsByTagName('base')[0].href + url;
}

function loadAjax(url, vars, titel){
	$.get(url,vars,function(data){
		$.facebox(data, titel);
	});
}


function checkemail(adres){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(adres))
		return true;
	else{
		return false;
	}
}

function formValidate(formID){
	var clear=false;
	var error='';
	$('.required').css("background-color","");	
	
	if($("#"+formID+" #fielder").val()==""){
	
		$("#"+formID+" .required").each(function(){
			if($(this).attr('type')=="checkbox"){
				if(!$(this).attr('checked')){
					$(this).parent().css("background-color","#ff0000");
					$(this).focus(function(){$(this).parent().css("background-color","")});	
					clear=true;
				}else{
					$(this).parent().css("background-color","");
				}
			}else{
				if($(this).hasClass('email')){
					if(!checkemail($(this).val())){
						$(this).css("background-color","red").click(function(){$(this).css("background-color","")});
						$(this).focus(function(){$(this).css("background-color","")});		
						error+="Er is een ongeldig e-mailadres ingevoerd. \n";
						clear=true;
					}
				}			
				else if($(this).val()==""){
					$(this).css("background-color","red").click(function(){$(this).css("background-color","")});
					$(this).focus(function(){$(this).css("background-color","")});
					clear=true;
				}else{
					$(this).css("background-color","");
				}
			}
		});
	}else{
		clear=true;	
	}
	
	if(clear){
		alert(error+'\nGelieve alle rood gemarkeerde velden in te vullen.');
		return false;
	}else{
		return true;
	}
}


var searchTimeout = null;
function searchTimer(){
	if(searchTimeout) clearTimeout(searchTimeout);		
	searchTimeout = setTimeout(function(){getSearchResult();}, 500);
}

function getSearchResult(){
	
	if($("#zoekForm #jaar_tot").val()>0 && $("#zoekForm #jaar_vanaf").val()>0){
		if($("#zoekForm #jaar_vanaf").val()>$("#zoekForm #jaar_tot").val()){
			$("#zoekForm #jaar_tot").val($("#zoekForm #jaar_vanaf").val());
		}
	}
	
	var formData = $('#zoekForm').serialize();
	$('#zoekResult').html('<img src="'+baseUrl()+'/timeline/img/icons/loading.gif">');		
	$.ajax({
		type: 'POST',
		url: baseUrl()+'/timeline/inc/zoeken_result.inc.php',
		data: formData,
		success: function(data){
			$('#zoekResult').html(data);		
		}
	});				
	return false;
}
