frontend.request_custom_form = {};
frontend.request_custom_form.init = function() {
    $('#add-location').click(frontend.request_custom_form.addLocations);

    $('#custom-form input, #custom-form select').focus(function() {
        $(this).addClass('focus');
    }).blur(function() {
        $(this).removeClass('focus')
    });

    // Hack fuer opera, weil ein label kein "click"-Event ausloest, sondern nur ein focus-Event und Opera daraufhin die Form nicht submitted
    $('#custom-form #formbox-4 .button-style label').bind('click', function() {$('#submit').click()});
}

frontend.request_custom_form.addLocations = function() {
    var newLocationCount      = 3;
    var existingLocationCount = $('#formbox-3 input.first').length;
    var currCount             = 0;

    var table                 = $('#formbox-3 table');
    for (var i = 0; i < newLocationCount; i++) {
        currCount = i + existingLocationCount;
        table.append($.ajax({
            url: '/form-location/' + currCount + '.html',
            async: false
        }).responseText);
        frontend.request_custom_form.bindMandatoryFields(table);
    }
}

frontend.request_custom_form.bindMandatoryFields = function(container) {
    $('input.mandatory', container).bind('blur', form.extend.mandatory.blur);
}

$(document).ready(frontend.request_custom_form.init);
