﻿/**
 * If needed, it's possible to overwrite the Validators event handler functions here. The original functions are defined in
 * validator.js
 * Uncomment the code below and define your own event handlers. An example is shown for the onFormSuccess event handler
 * @ignore
 */

/* BROCHURE ACTIONS */
Validator.Config.BrochureConfig.onFieldError = function (e, fieldValidation) {
    Log.Debug("Validator: Showing error for field " + fieldValidation.getName());
	var msg = $(fieldValidation.getHtml()).find(".errorText").text();
	$(".form-close .errorText").text(msg).show();
};
Validator.Config.BrochureConfig.onFieldSuccess = function (e, fieldValidation) {
    if (fieldValidation.getIsRequired()) {
        Log.Debug("Validator: Showing success for field " + fieldValidation.getName());
    }
	var msg = $(fieldValidation.getHtml()).find(".errorText").text();
	if ($(".form-close .errorText").text() == msg) {
		$(".form-close .errorText").hide();
	}
};
Validator.Config.BrochureConfig.onSectionError = function (e, invalidFields) {};
Validator.Config.BrochureConfig.onSectionSuccess = function (e) {};

/*
 * Form action methods, independent from types
 */
Validator.onFormError = function (e, invalidSections) {
    /* Show the error text of the first invalid field */
    var firstSection = invalidSections[0];
    var invalidFields = firstSection.getInvalidFields();
    var firstField = invalidFields[0];
    var msg = $(firstField.getHtml()).find(".errorText").text();
    $(".form-close .errorText").text(msg).show();
};

Validator.onFormSuccess = function (e) {};

Validator.onFormSubmit = function(valid) {
    if (!valid) {
        return false;
    }

    $('#btn-brochure').attr('disabled', true);

    var title = $('#mag-title').val(); // * verplicht
    var firstletter = $('#mag-firstletter').val(); // * verplicht
    var middlename = $('#mag-middlename').val();
    var lastname = $('#mag-lastname').val(); // * verplicht
    var birthday = $('#mag-birthday').val();
    var birthmonth = $('#mag-birthmonth').val();
    var birthyear = $('#mag-birthyear').val();
    var street = $('#mag-street').val(); // * verplicht
    if (street == '') {
        street = $('#dd-mag-street').val();
    }
    var streetnr = $('#mag-streetnr').val(); // * verplicht
    var streetnrext = $('#mag-streetnrext').val();
    var postal = $('#mag-postal').val(); // * verplicht
    var city = $('#mag-city').val(); // * verplicht
    var country = $('#mag-country').val(); // * verplicht
    var phoneprivate = $('#mag-phoneprivate').val();
    var phonework = $('#mag-phonework').val();
    var email = $('#mag-email').val(); // * verplicht
    var agree = $('#mag-agree')[0].checked;

    // Submit a request for each brochure checked by the user
    var productCodes = [""]; // default product code is empty string, used on be-nl version for example, which has no magazine checkboxes
    if ($(".brochures input[type=checkbox]:checked").length > 0) {
        $(".brochures input[type=checkbox]:checked").each(function(i) {
            productCodes[i] = $(this).val();
        });
    }

    // make a request for each of the magazines
    $.each(productCodes, function() {
        var code = this;
        $.ajax({
            type: "POST",
            url: Resource.GetText('path_prefix') + '/utilpages/addbrochure.ashx',
            data: '&productcode=' + code +
                    '&title=' + title +
                    '&firstletter=' + firstletter +
                    '&middlename=' + middlename +
                    '&lastname=' + lastname +
                    '&birthday=' + birthday +
                    '&birthmonth=' + birthmonth +
                    '&birthyear=' + birthyear +
                    '&street=' + street +
                    '&streetnr=' + streetnr +
                    '&streetnrext=' + streetnrext +
                    '&postal=' + postal +
                    '&city=' + city +
                    '&country=' + country +
                    '&phoneprivate=' + phoneprivate +
                    '&phonework=' + phonework +
                    '&email=' + email +
                    '&agree=' + agree,
            async: false/*,
            success: function(){
                window.location.replace(Resource.GetText('path_prefix') + '/thankyou-brochure');
            },
            error:  function(){//priv.errorMagazine
                        Log.Alert('error');
                    }*/
        });
    });

    window.location.href = Resource.GetText('path_prefix') + '/thankyou-brochure';

    return false;
};
