function setErrorMesg(num, mode) {
    if(mode) {
        $("td.mesg:eq("+num+")").addClass("error");
    } else {
        $("td.mesg:eq("+num+")").removeClass("error");
    }
}

function validateContactForm(formData, jqForm, options) {
    
    //alert('Validate');
    
    
    var errorcount = 0;
    
    if($.trim($("#voornaam").val()) == "") {
        setErrorMesg(0, true);
        errorcount = errorcount + 1;
    } else {
        setErrorMesg(0, false);
    }
    
    if($.trim($("#achternaam").val()) == "") {
        setErrorMesg(1, true);
        errorcount = errorcount + 1;
    } else {
        setErrorMesg(1, false);
    }
    
    if($.trim($("#straatnaam").val()) == "" || $.trim($("#huisnummer").val()) == "") {
        setErrorMesg(2, true);
        errorcount = errorcount + 1;
    } else {
        setErrorMesg(2, false);
    }
    
    if($.trim($("#plaats").val()) == "" || $.trim($("#postcode1").val()) == "" || $.trim($("#postcode1").val()) == "") {
        setErrorMesg(3, true);
        errorcount = errorcount + 1;
    } else {
        setErrorMesg(3, false);
    }
    
    if(!checkEmail($("#email").val())) {
        setErrorMesg(5, true);
        errorcount = errorcount + 1;
    } else {
        setErrorMesg(5, false);
    }
    
    if($.trim($("#opmerking").val()) == "") {
        setErrorMesg(6, true);
        errorcount = errorcount + 1;
    } else {
        setErrorMesg(6, false);
    }
    
    /**/
    if(errorcount > 0) {
        return false;
    } else {
        $('#dialog').jqmShow();
        return true;
        //return false;
    }
    
}

function doReset() {
	$("#contactform").clearForm();
	$("#subscribe").attr({checked: true});
	$("td.error").removeClass("error");
	return false;
}

function responseContactForm(responseText, statusText) {
    var data = responseText;
    if(statusText == 'success' && data.result == 'ok') {
        $('#dialog').jqmHide();
        $("#contactform").fadeOut("fast");
        var html = "" + unescape(data.html);
        $("#formresponse").html(html).fadeIn("fast");
    }
}

$(document).ready(function(){

    /* Contact form */
    
    var formoptions = {
    	/*target: '#formoutput',*/
    	beforeSubmit: validateContactForm,
    	success: responseContactForm,
    	url: '/INC/contact.json.inc.php',
    	dataType: 'json',
    	clearForm: true
    }
    
    $("#contactform").submit(function(){
        $(this).ajaxSubmit(formoptions); 
        return false;
    });
    
    $("#reset").click(function(){
        doReset();
        return false;
    });
    
    $('#dialog').jqm({
        modal: true
    });
    
});
