$(document).ready(function(){

    $('#submitForm').click(function(){
        var obj = {};
        obj.newType = 'Poll';
        obj.newMethod = 'vote';
        obj.vote = 1;
        obj.anwserId = $('#pollForm input[name=pollValue]:checked').val();
        ARRM(obj, handleResponse);
    });
    $('#showAnswers').click(function(){
        var obj = {};
        obj.newType = 'Poll';
        obj.newMethod = 'show';
        ARRM(obj, handleResponse);
    });
    $('#inschrijvenForm').submit(function(){
        handleInschrijven();
        return false;
    });
    
    $('#inschrijvenLink').click(function(){
        handleInschrijven();
        return false;
    });
    
});
function checkemail(email){
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    if (filter.test(email)) {
        return true;
    }
    else {
        return false;
    }
}

/**
 * validate the user input and return if false
 */
function handleInschrijven(){
    var form = true;
    if (
	$('#nieuwsbrief').val() === "" ||
	$('#bijzonderheden').val() === "" ||
	$('#Deelnemeremail').val() === "" ||
	$('#Deelnemerachternaam').val() === "" ||
	$('#Deelnemervoornaam').val() === "" ||
	$('#email').val() === "" ||
	$('#telefoon').val() === "" ||
	$('#plaats').val() === "" ||
	$('#postcode').val() === "" ||
	$('#straat').val() === "" ||
	$('#bedrijf').val() === "" ||
	$('#contact').val() === "") {
        form = false;
    }
    if (form) {
	form = checkemail($('#email').val());
	if(form){
		var obj = {};
		obj.ajax = 1;
		obj.newType = "Training";
		obj.newMethod = "Inschrijven";
		if ($('#nieuwsbrief').attr("checked")) {
		obj.nieuwsbrief = 1;
		}
		else {
		obj.nieuwsbrief = 0;
		}
		obj.bijzonderheden = $('#bijzonderheden').val();
		obj.Deelnemeremail = $('#Deelnemeremail').val();
		obj.Deelnemerachternaam = $('#Deelnemerachternaam').val();
		obj.Deelnemervoornaam = $('#Deelnemervoornaam').val();
		obj.email = $('#email').val();
		obj.telefoon = $('#telefoon').val();
		obj.plaats = $('#plaats').val();
		obj.postcode = $('#postcode').val();
		obj.straat = $('#plaats').val();
		obj.bedrijf = $('#bedrijf').val();
		obj.contact = $('#contact').val();
		obj.plaatsFactuur = $('#plaatsFactuur').val();
		obj.postcodeFactuur = $('#postcodeFactuur').val();
		obj.straatFactuur = $('#straatFactuur').val();
		obj.inschrijving = $('#inschrijving').val();
		ARRM(obj, returnResult);
	}else{
		$('#error').show().html("Graag een correct e-mail adres invullen.");
	}
    }
    else {
        $('#error').show().html("Graag alle verplichte velden invullen.");
    }
}

function returnResult(){
	$('#inschrijvenForm')[0].reset();
	$('#error').html("Uw inschrijving is ontvangen, bedankt voor het inschrijven.");
}

function ARRM(obj, func){
    var url = '/application/classes/ajax/AjaxController.php';
    $.ajax({
        type: "POST",
        url: url,
        data: obj,
        dataType: 'json',
        success: function(serverData){
            func(serverData);
        }
    });
}

function handleResponse(obj){
    $('#questionsPoll').hide();
    $('#resultPoll').html(obj.html);
    $('#resultPoll').show();
	if ($('#backToForm')) {
		$('#backToForm').click(function(){
		    $('#questionsPoll').show();
		    $('#resultPoll').hide();
    	});
	}
}

function initMenu(){
    $('#menu a').hover(function(){
        $(this).addClass('over');
    }, function(){
        $(this).removeClass('over');
    });
}
