Function.prototype.calledOn = function (objInstance) {
	var functionObj = this;
	return function () {
		return functionObj.apply(objInstance, arguments);
	}
};

var ConsumerOrganisationField = function (fieldName, warningMessage) {
	this.fieldName = fieldName;
	this.warningMessage = warningMessage;
	this.init();
}

ConsumerOrganisationField.prototype = {
	init:function () {
		var row = $('.fieldname-' + this.fieldName);
		var radios = row.find('input.radio');
		var warningMessage = this.warningMessage;
		radios.click(function () {
			$(this).change();
		});
		radios.change(function () {
			row.find('.flash').remove();
			if (this.checked && ($(this).val() == 'n')) row.append('<div class="flash notice">' + warningMessage + '</div>');
		});
		radios.change();
	}

};

var CountrySelectField = function (fieldName, warningMessage) {
	this.fieldName = fieldName;
	this.warningMessage = warningMessage;
	this.init();
}

CountrySelectField.prototype = {
	init:function () {
		var row = $('.fieldname-' + this.fieldName);
		var select = row.find('select');
		var options = row.find('option');
		var warningMessage = this.warningMessage;

		options.each(function () {
			if (this.text.match(/(ineligible)/)) {
				this.ineligible = true;
				this.text = this.text.replace(' (ineligible)', '');
			}
		});

		select.change(function () {
			row.find('.flash').remove();
			if (this.options[this.selectedIndex].ineligible) row.append('<div class="flash notice">' + warningMessage + '</div>');
		});
		select.change();
	}
};

var LanguagesMultipleCheckboxField = function (fieldName) {
	this.fieldName = fieldName;
	this.init();
}

LanguagesMultipleCheckboxField.prototype = {
	init:function () {
		var row = $('.fieldname-' + this.fieldName);
		var textFieldRow = row.next('.row');

		row.addClass('fix-for-ie');
		textFieldRow.addClass('hidden');

		var inputs = row.find('input');
		inputs.click(function () {
			$(this).change()
		});
		inputs.change(function () {
			if ($(this).next('label').text().match(/other/i)) {
				if (this.checked) {
					row.removeClass('fix-for-ie');
					textFieldRow.removeClass('hidden');
				} else {
					row.addClass('fix-for-ie');
					textFieldRow.addClass('hidden');
					textFieldRow.find('textarea').val('');
				}
			}
		})
		inputs.change();
	}
};


var UserOrganisationTypeField = function (fieldName, optionOtherId, eligibleWarningMessage, optionEligibleId) {
	this.fieldName = fieldName;
	this.optionOtherId = optionOtherId;
	this.eligibleWarningMessage = eligibleWarningMessage;
	this.optionEligibleId = optionEligibleId;
	this.init();
	this.showWarning = true;
};

UserOrganisationTypeField.prototype = {
	init:function () {
		var row = $('.fieldname-' + this.fieldName);
		var textFieldRow = row.next('.row');

		textFieldRow.addClass('hidden');

		var select = row.find('select');
		select.change((function () {

			//Textfeld "other" einblenden
			if (select.val() == this.optionOtherId) {
				textFieldRow.removeClass('hidden');
			} else {
				textFieldRow.addClass('hidden');
			}

			//Warnung, wenn ein nicht-eligible Organisationstyp ausgewählt wurde
			row.find('.flash').remove();
			if (this.showWarning && select.val() && select.val() != this.optionEligibleId) {
				row.append('<div class="flash notice">' + this.eligibleWarningMessage + '</div>');
			}

		}).calledOn(this));
		select.change();
	},
	disableWarning:function () {
		this.showWarning = false;
	}
};


$(document).ready(function () {
	$("div.helptext").hide();

	$("form div.row:has(div.helptext)").each(function () {

		var helptext = $(this).find("div.helptext");
		var label = $(this).find("label:first");

		label.append('<img src="' + WFD_STATIC + 'img/help.png" />');
		label.find("img").mouseover(
			function () {
				helptext.fadeIn();
			}).mouseout(function () {
				if (!helptext.keepOnMouseout) helptext.fadeOut();
			});

		var pending = false;

		$(this).find("input, select, textarea").focus(
			function () {
				if (pending) window.clearTimeout(pending);
				helptext.fadeIn();
				helptext.keepOnMouseout = true;
			}).blur(function () {
				pending = window.setTimeout(function () {
					helptext.fadeOut();
					helptext.keepOnMouseout = false;
				}, 0);
			});
	});
});
	

