function formSendController(formId, datasetId, type)
{
	var inputFields = new Array();
	
	var inputFieldButtonName = '';
	
	var inputFormObj = null;
	
	var isRunning = false;
	
	var instance = this;
	
	this.registerField = function(fieldName, fieldLabel, fieldIsRequired) {	
		inputFields.push({
			name: fieldName,
			label: fieldLabel,
			obj: null,
			required: fieldIsRequired
		});		
	}
	
	this.registerSubmitButton = function(buttonName) {
		inputFieldButtonName = buttonName;
	}
	
	this.onSuccess = function() {
		for(i=0; i<inputFields.length; i++) {
			inputFields[i].obj.val('');
		}
		if (type == 'microsite_kontakt') {
			alert('Ihre Kontaktanfrage wurde erfolgreich verschickt.');
		}
		if (type == 'ihre_meinung') {
			alert('Ihre Meinung wurde erfolgreich verschickt.');
		}
	}
	
	this.onError = function(text) {
		alert('Es ist ein Fehler aufgetreten. Bitte versuchen Sie es sp\u00E4ter noch einmal.');
	}
	
	var send = function() {
		if (inputFormObj !== null) {
			var submitFields = {};
			var isError = false;
			
			for(i=0; i<inputFields.length; i++) {
				inputField = inputFields[i];
				inputFieldVal = inputField.obj.val();
				inputFieldLabel = inputFormObj.find('label[for=' + inputField.name + ']');
				if (inputField.required && !inputFieldVal) {
					isError = true;
					inputFieldLabel.css('color', '#ff0000').css('font-weight', 'bold');
				} else {
					inputFieldLabel.removeAttr('style');
				}
				submitFields[inputField.name] = {
					label: inputField.label,
					value: inputFieldVal			
				};
			}
			
			if (!isError) {
				if (!isRunning) {
					isRunning = true;
					xajax_contactFormSendController(submitFields, datasetId, type);
					
					window.setTimeout(releaseFormular, 10000);
				} else {
					alert('Sie haben das Formular schon abgesendet.');
				}
			}
		}
	}
	
	var releaseFormular = function() {
		isRunning = false;
	}
	
	var init = function() {
		inputFormObj = $('form#' + formId);
		for (i=0; i<inputFields.length; i++) {
			inputFields[i].obj = inputFormObj.find('*[name=' + inputFields[i].name + ']:input');
		}
		inputFormObj.find('input[name=' + inputFieldButtonName + ']').click(send);
	};
	
	$().ready(init);
}
