$(document).ready(function() {
	
	$("#minReiziger").click(function() {
		var aantal = $('#aantalReizigers').val();
		$('#reiziger' + aantal).remove();
		aantal--;
		if(aantal == 1)
		{
			$("#minReiziger").hide();
			$(".minReis").show();
		}
		$('#aantalReizigers').val(aantal);
		$('#huidigAantal').text(aantal);				
		
		return false;
	});
	
	
	$("#nieuweReiziger").click(function() {
		var aantal = $('#aantalReizigers').val();
		aantal++;
		if(aantal > 1)
		{
			$(".minReis").hide();
			$("#minReiziger").show();
		}
		$('#aantalReizigers').val(aantal);
		$('#huidigAantal').text(aantal);
		
		var html = '';
		html += '<div id="reiziger' + aantal + '" class="reiziger">';
		html += '<div>';
		html += '	<label for="naam' + aantal + '">Naam</label>';
		html += '<input type="naam' + aantal + '" id="naam' + aantal + '" name="naam' + aantal + '" size="40" value="">';
		html += '</div>';
		html += '<div>';
		html += '<label for="voornaam' + aantal + '">Voornaam</label>';
		html += '<input type="voornaam' + aantal + '" id="voornaam' + aantal + '" name="voornaam' + aantal + '" size="40" value="">';
		html += '</div>';
		html += '<div>';
		html += '<label for="geboortedatum' + aantal + '">Geboortedatum</label>';
		html += '<input type="geboortedatum' + aantal + '" id="geboortedatum' + aantal + '" name="geboortedatum' + aantal + '" size="40" value="">';
		html += '</div>';
		html += '</div>';
		
		$('.reizigers').append(html);
		
		return false;
	});

	$('#thumbs_indekijker').cycle({ 
		delay: 1000, 
		speed: 500,
		prev:   '#prev_indekijker', 
    	next:   '#next_indekijker'
	});
	
	$('#thumbs_promoties').cycle({ 
		delay: 2000, 
		speed: 500,
		prev:   '#prev_promoties', 
    	next:   '#next_promoties'
	});
	
	$('#thumbs_nieuwsflits').cycle({ 
		delay: 3000, 
		speed: 500,
		prev:   '#prev_nieuwsflits', 
    	next:   '#next_nieuwsflits'
	});
	
	$('a[rel*=lightbox]').lightBox();
	
	doforms();
	
	dohome();

});

function changeHome() {
	$('#home'+counter2).attr("className","pos"+resChange);
	$('#home'+counter).attr("className","pos"+resChange);
	$('#home'+counter2).fadeIn('slow');
	$('#home'+counter).fadeOut('slow');
	counter++;
	counter2++;
	resChange++;
	if (counter > aantalHome) {
		counter = 1;
	}
	if (counter2 > aantalHome) {
		counter2 = 1;
	}
	if (resChange == 4) {
		resChange = 1;
	}
	setTimeout("changeHome()",3000);
}

var doHome = false;
function dohome() {
	if (doHome) {
		setTimeout("changeHome()",3000);
	}
}

function doforms() {
	var contactform = document.getElementById("contactform");
	if (contactform) {
		contactform.onsubmit = function() {
			return validateContact(this);
		}
	}
	var sendform = document.getElementById("sendform");
	if (sendform) {
		sendform.onsubmit = function() {
			return validateDoorsturen(this);
		}
	}
	var subscribeform = document.getElementById("subscribeform");
	if (subscribeform) {
		subscribeform.onsubmit = function() {
			return validateInschrijven(this);
		}
	}
	var offerform = document.getElementById("offerform");
	if (offerform) {
		offerform.onsubmit = function() {
			return validateOfferte(this);
		}
	}
	var uitschrijfform = document.getElementById("uitschrijfform");
	if (uitschrijfform) {
		uitschrijfform.onsubmit = function() {
			return validateUitschrijven(this);
		}
	}
	var changeform = document.getElementById("changeform");
	if (changeform) {
		changeform.onsubmit = function() {
			return validateWijzigen(this);
		}
	}
}

function validateWijzigen(theForm) {
	var msg = "";
	var error = 0;
	if(theForm.old_email.value=="") {
		msg+="\n- " + lbl_nieuwsbrief_oud_email;
		error++;
	}
	if(theForm.new_email.value=="") {
		msg+="\n- " + lbl_nieuwsbrief_nieuw_email;
		error++;
	}
	if(error){
		alert(lbl_nieuwsbrief_verplicht + ": " + msg);
		return false;
	}
	if(!validateEmail(theForm.old_email.value)){
		alert(lbl_nieuwsbrief_geldig_oud_email);
		return false;
	}
	if(!validateEmail(theForm.new_email.value)){
		alert(lbl_nieuwsbrief_geldig_nieuwmail);
		return false;
	}
	return true;
}	

function validateUitschrijven(theForm) {
	var msg = "";
	var error = 0;
	if(theForm.email.value=="") {
		msg+="\n- " + lbl_nieuwsbrief_email;
		error++;
	}
	if(error){
		alert(lbl_nieuwsbrief_verplicht + ": " + msg);
		return false;
	}
	if(!validateEmail(theForm.email.value)){
		alert(lbl_nieuwsbrief_geldig_email);
		return false;
	}
	return true;
}	

function validateOfferte(theForm) {
	var msg = "";
	var error = 0;
	if(theForm.familienaam.value=="") {
		msg+="\n- " + lbl_offer_familienaam;
		error++;
	}
	if(theForm.voornaam.value=="") {
		msg+="\n- " + lbl_offer_voornaam;
		error++;
	}
	if(theForm.straatnr.value=="") {
		msg+="\n- " + lbl_offer_straat_en_nr;
		error++;
	}
	if(theForm.postcode.value=="") {
		msg+="\n- " + lbl_offer_postcode;
		error++;
	}
	if(theForm.plaats.value=="") {
		msg+="\n- " + lbl_offer_plaats;
		error++;
	}
	if(theForm.land.value=="") {
		msg+="\n- " + lbl_offer_land;
		error++;
	}
	if(theForm.email.value=="") {
		msg+="\n- " + lbl_offer_email;
		error++;
	}
	if(theForm.telefoon.value=="") {
		msg+="\n- " + lbl_offer_tel_overdag;
		error++;
	}
	if(theForm.bestemming.value=="") {
		msg+="\n- " + lbl_offer_bestemming;
		error++;
	}
	if(theForm.vertrekdatum_dag.value=="" || theForm.vertrekdatum_maand.value=="" || theForm.vertrekdatum_jaar.value=="") {
		msg+="\n- " + lbl_offer_vertrekdatum;
		error++;
	}
	if(theForm.aantaldagen.value=="") {
		msg+="\n- " + lbl_offer_aantal_dagen;
		error++;
	}

	if(theForm.aantaldagen.value=="") {
		msg+="\n- " + lbl_offer_aantal_dagen;
		error++;
	}	
	if (theForm.annuleringsverzekering.checked == false && theForm.bijstandsverzekering.checked == false && theForm.annuleringenbijstand.checked == false && theForm.geenverzekeringen.checked == false) {
		msg+="\n- " + lbl_offer_verzekeringen;
		error++;
	}
	
	var aantal = theForm.aantalReizigers.value;
	
	for(i=1;i<=aantal;i++)
	{
		var naam = "naam" + i;
		var testnaam = document.getElementById(naam).value;
		
		var voornaam = "voornaam" + i;
		var testvoornaam = document.getElementById(voornaam).value;
		
		var geboortedatum = "geboortedatum" + i;
		var testgeboortedatum = document.getElementById(geboortedatum).value;
		
		if(testnaam == "" || testvoornaam == "" || testgeboortedatum == "")
		{
			error++;
			if(testnaam == "")
			{
				msg+="\n- " + lbl_offer_naam + i + ")";
			}
			if(testvoornaam == "")
			{
				msg+="\n- " + lbl_offer_voornaam2 + i + ")";
			}
			if(testgeboortedatum == "")
			{
				msg+="\n- " + lbl_offer_geboortedatum + i + ")";
			}
		}
	}
	
	if(error){
		alert(lbl_offer_verplicht_invullen + ": " + msg);
		return false;
	}
	if(!validateEmail(theForm.email.value)){
		alert(lbl_offer_geldig_email);
		return false;
	}
	theForm.x.value = "abc123";
	return true;
}

function validateInschrijven(theForm) {
	var msg = "";
	var error = 0;

	if(!theForm.strand.checked && !theForm.cruise.checked && !theForm.golf.checked && !theForm.gastronomie.checked 
	&& !theForm.cultuur.checked && !theForm.concert.checked && !theForm.spa.checked) {
		msg+="\n- " + lbl_nieuwsbrief_info;
		error++;
	}
	if(theForm.achternaam.value=="") {
		msg+="\n- " + lbl_nieuwsbrief_naam;
		error++;
	}
	if(theForm.voornaam.value=="") {
		msg+="\n- " + lbl_nieuwsbrief_voornaam;
		error++;
	}
	if(theForm.email.value=="") {
		msg+="\n- " + lbl_nieuwsbrief_email;
		error++;
	}
	if(error){
		alert(lbl_nieuwsbrief_verplicht + ": " + msg);
		return false;
	}
	if(!validateEmail(theForm.email.value)){
		alert(lbl_nieuwsbrief_geldig_email);
		return false;
	}
	return true;
}	

function validateDoorsturen(theForm) {
	var msg = "";
	var error = 0;

	if(theForm.to.value=="" || theForm.to.value == lbl_nieuwsbrief_naam_vriend) {
		msg+="\n- " + lbl_nieuwsbrief_naam_vriend;
		error++;
	}
	if(theForm.from.value=="" || theForm.from.value== lbl_nieuwsbrief_naam_afzender) {
		msg+="\n- " + lbl_nieuwsbrief_naam_afzender;
		error++;
	}
	if(theForm.email.value=="") {
		msg+="\n- " + lbl_nieuwsbrief_stuur_naar_email;
		error++;
	}
	if(error){
		alert(lbl_nieuwsbrief_verplicht + ": " + msg);
		return false;
	}
	if(!validateEmail(theForm.email.value)){
		alert(lbl_nieuwsbrief_geldig_email);
		return false;
	}
	return true;
}	

function validateContact(theForm) {
	var msg = "";
	var error = 0;
	if(theForm.naam.value=="") {
		msg+="\n- " + lbl_contact_naam;
		error++;
	}
	if(theForm.adres.value=="") {
		msg+="\n- " + lbl_contact_adres;
		error++;
	}
	if(theForm.email.value=="") {
		msg+="\n- " + lbl_contact_email;
		error++;
	}
	if(theForm.opmerking.value=="") {
		msg+="\n- " + lbl_contact_vraag;
		error++;
	}
	if(error){
		alert(lbl_contact_verplicht + ": " + msg);
		return false;
	}
	if(!validateEmail(theForm.email.value)){
		alert(lbl_contact_geldig_email);
		return false;
	}
	return true;
}	

function validateEmail(email) {
		
	if (email != null && email != "") {
		a = email.lastIndexOf("@");
		b = email.lastIndexOf(".");
		c = email.indexOf(":");
		d = email.indexOf("/");
		e = email.substring(0,a);
		f = e.indexOf("@");
		g = email.substring(a+1,email.length);
		h = g.indexOf("[");
		i = g.indexOf("]");
		j = g.indexOf("<");
		k = g.indexOf(">");
		l = email.substring(a+1,b);
		m = email.substring(b+1,email.length);
		n = email.substring(0,a);
		p = email.indexOf("'");
		q = email.indexOf("\"");
		o = 0;
		if (a > b) {o++};
		if (c != -1) {o++};
		if (d != -1) {o++};
		if (f != -1) {o++};
		if (h != -1) {o++};
		if (i != -1) {o++};
		if (j != -1) {o++};
		if (k != -1) {o++};
		if (l.length < 2) {o++};
		if (m.length < 2) {o++};
		if (n.length < 1) {o++};
		if (p != -1) {o++};
		if (q != -1) {o++};
		if (o != 0) {
			return false;
		}
	}
	return true;
}		
