// Set variabelen
 var imagesSponsor = new Array();
 var i = 0;
setInterval( "slideSwitch()", 6500 );
setInterval( "slideSwitch2()", 3000 );
$(document).ready(function() {
	
	$('#masform').submit(function() {
		if($('#naam').val() == '') {
		$('#error').html('Geef a.u.b. uw naam op');
		return false;
		}
		if($('#famnaam').val() == '') {
		$('#error').html('Geef a.u.b. uw achternaam op');
		return false;
		}
		if($('#straat').val() == '' || $('#nr').val() == '') {
		$('#error').html('Geef a.u.b. uw volledige adres');
		return false;
		}
		if($('#postcode').val() == '' || $('#postnr').val() == '') {
		$('#error').html('Geef a.u.b. uw volledige postcode');
		return false;
		}
		if($('#woonplaats').val() == '') {
		$('#error').html('Geef a.u.b. uw woonplaats');
		return false;
		}
		if($('#telnr').val() == '') {
		$('#error').html('Geef a.u.b. uw telefoonnummer');
		return false;
		}
		if($('#email').val() == '') {
		$('#error').html('Geef a.u.b. uw email adres');
		return false;
		}
		if($('#maand').val() == '' || $('dag').val() == '' || $('jaar').val() == '' || $('#maand').val() == 'mm' || $('#dag').val() == 'dd') {
		$('#error').html('Geef a.u.b. uw volledige geboortejaar');
		return false;
		}
		if($('#school').val() == '') {
		$('#error').html('Geef a.u.w. uw school');
		return false;
		}
		if($('#klas').val() == '') {
		$('#error').html('Geef a.u.w. uw klas');
		return false;
		}
		if($('#mon1').attr('checked')) {
			if($('#monuren').val() == '') {
			$('#error').html('Geef a.u.b. het aantal beschikbare uren op maandag');
			return false;
			}
		}
		if($('#din1').attr('checked')) {
			if($('#dinuren').val() == '') {
			$('#error').html('Geef a.u.b. het aantal beschikbare uren op dinsdag');
			return false;
			}
		}
		if($('#won1').attr('checked')) {
			if($('#wonuren').val() == '') {
			$('#error').html('Geef a.u.b. het aantal beschikbare uren op woensdag');
			return false;
			}
		}
		if($('#don1').attr('checked')) {
			if($('#donuren').val() == '') {
			$('#error').html('Geef a.u.b. het aantal beschikbare uren op donderdag');
			return false;
			}
		}
		if($('#vrij1').attr('checked')) {
			if($('#vrijuren').val() == '') {
			$('#error').html('Geef a.u.b. het aantal beschikbare uren op vrijdag');
			return false;
			}
		}
	});
	
	$('#submitform').click(function() {
	if($('#naam').val() == '') {
	$('#errordiv').html('Geef a.u.b. uw naam op');
	return false;
	}
	if($('#bericht').val() == '') {
	$('#errordiv').html('Geef a.u.b. een bericht op');
	return false;
	}
	if($('#vrij').val() == 'y' && $('#vrijuren').val() == '') {
	$('#errordiv').html('Geef a.u.b. het aantal beschikbare uren op vrijdag');
	return false;
	}
});
	
	 $('#Send').click(function() {  
	 	
			// name validation
			
			var nameVal = $("#name").val();
			if(nameVal == '') {
				
				$("#name_error").html('');
				$("#name").after('<label class="error" id="name_error">Geef uw naam op.</label>');
				return false
			}
			else
			{
				$("#name_error").html('');
			}
			
			$.post("verwerkscript.php?"+$("#MYFORM").serialize(), {
		
			}, function(response){
			
			if(response==1)
			{
				$("#after_submit").html('');
				$("#Send").after('<label class="success" id="after_submit">Your message has been submitted.</label>');
				change_captcha();
				clear_form();
			}
			else
			{
				$("#after_submit").html('');
				$("#Send").after('<label class="error" id="after_submit">Error ! invalid captcha code .</label>');
			}
			
			
		});
				
		return false;
	 });
	 
	 // refresh captcha
	 $('img#refresh').click(function() {  
			
			change_captcha();
	 });
	 
	 function change_captcha()
	 {
	 	document.getElementById('captcha').src="get_captcha.php?rnd=" + Math.random();
	 }
	 
	 function clear_form()
	 {
	 	$("#name").val('');
		$("#email").val('');
		$("#message").val('');
	 }	

 $.getJSON('include/getSponsor.php', function(data) {
        $.each(data, function(i) {
                imagesSponsor[i] = data[i];
        });
		buildSponsor();
    }); 
		setInterval(function() {
		nextSponsor()
		}, 8000);



function buildSponsor() {
	$('#sponsorenbox').html('<div><a target="new" href="'+ imagesSponsor[i].link +'"><img class="sponsorfoto" src="/'+ imagesSponsor[i].afbeelding + '"></a></div><div id="sponsorworden"><p><a href="mailto:apeldoornsewandelfederatie@kpnmail.nl">Ook sponsor worden? klik hier</a></p></div>');
	i = i + 1;	
	
}

function nextSponsor() {
	if(!imagesSponsor[i]){
		i = 0;
	$('#sponsorenbox').html('<div><a target="new" href="'+ imagesSponsor[i].link +'"><img class="sponsorfoto" src="/'+ imagesSponsor[i].afbeelding + '"></a></div><div id="sponsorworden"><p><a href="mailto:apeldoornsewandelfederatie@kpnmail.nl">Ook sponsor worden? klik hier</a></p></div>');
	i = i+1;
		
	}
	else {
	$('#sponsorenbox').html('<div><a target="new" href="'+ imagesSponsor[i].link +'"><img class="sponsorfoto" src="/'+ imagesSponsor[i].afbeelding + '"></a></div><div id="sponsorworden"><p><a href="mailto:apeldoornsewandelfederatie@kpnmail.nl">Ook sponsor worden? klik hier</a></p></div>');
	i = i + 1;
	}
}

});


//Functies
//slideSwitch

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
     var $sibs  = $active.siblings();
     var rndNum = Math.floor(Math.random() * $sibs.length );
     var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 2500, function() {
            $active.removeClass('active last-active');
        });
}

function slideSwitch2() {
    var $active = $('#sponsorfade IMG.active');

    if ( $active.length == 0 ) $active = $('#sponsorfade IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#sponsorfade IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
     //var $sibs  = $active.siblings();
     //var rndNum = Math.floor(Math.random() * $sibs.length );
     //var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 2500, function() {
            $active.removeClass('active last-active');
        });
}
