/**
 * econfence.com - Content Management System
 *
 * This is the "auto_registration" extension JS DOCUMENT
 * of the econfence.com - content managament system frontend
 *
 * @file    auto_registration/res/javascript.js
 * @author  dpi one <www.dpi-one.de>
 */

var tx_autoregistration_pi1 = {
	inputField: function(field, option) {
		if (document.getElementById('tx_autoregistration_pi1-' + field)) {
			var value = document.getElementById('tx_autoregistration_pi1-' + field).value;
			switch (option) {
				case 'int':
					value = tx_autoregistration_pi1.getNumChars(value);
					break;
				case 'email':
					value = tx_autoregistration_pi1.noSpace(value);
					break;
				case 'trim':
				default:
					value = tx_autoregistration_pi1.trim(value);
					break;
			}
			document.getElementById('tx_autoregistration_pi1-' + field).value = value;
		}
	},
	getNumChars: function(value) {
		var theVal = '' + value;
		if (!value)	return 0;
		var outVal = "";
		for (var i = 0; i < theVal.length; i++) {
			if (theVal.substr(i, 1) == parseInt(theVal.substr(i, 1))) {
				outVal += theVal.substr(i, 1);
			}
		}
		return outVal;
	},
	noSpace: function(value) {
		var theVal = '' + value;
		var newString = "";
		for (var a = 0; a < theVal.length; a++) {
			var theChar = theVal.substr(a, 1);
			if (theChar != ' ')	{
				newString += theChar;
			}
		}
		return newString;
	},
	trim: function(value) {
		// Replace leading whitespaces
		value = value.replace(/^\s+/, '');
		// Replace ending whitespaces
		value = value.replace(/\s+$/, '');
		return value;
	}
};
