/******************************************************
	Index:
	  
	1) Form
		1.0) Class
		1.1) Instances
		
	2) General Functions
		2.1) Get Radio Value
		2.2) Get Select Value
		2.3) Get Select Text
		
******************************************************/


/* --------------------------------------------------------------------------------------------- */
/* 1) Form
/* --------------------------------------------------------------------------------------------- */

	var formular = {
		prefix:			'_FORM',
		labelTag:		'small',
		topAnchor:		'form',
		mergePrefix:	'merge'
	};
	
	function Formular(name) {
		this.name = name;
	}
	
// 1.0) Class
// .......................................................................

	Formular.prototype = {		
		prefix:			formular.prefix
	}
	
	
	/* ---------------------------------- */
	/* Init Form
	/* ---------------------------------- */
	Formular.prototype.init = function() {
	};


	/* ---------------------------------- */
	/* Update Field
	/* ---------------------------------- */
	Formular.prototype.updateValue = function(element, inputType) {
	
		var inputField = $(this.prefix, this.name, element, inputType);
		var text;
		
		switch(inputType) {
		
			case 'select':
				text = formular.f.getSelectText(inputField);
				break;
		}
		
		// Display Input Text
		setText($(this.prefix, this.name, element, 'content'), text);
	}
	

	/* ---------------------------------- */
	/* ~CSS FUNCTIONALITY~
	/* .................................. */
	/* Enable Field Edit
	/* ---------------------------------- */
	Formular.prototype.CSS_enableEdit = function(element) {
		
		setClass($(this.prefix, this.name, element, 'saved'), 'hide');
		unsetClass($(this.prefix, this.name, element, 'change'), 'hide');
	};


	/* ---------------------------------- */
	/* ~CSS FUNCTIONALITY~
	/* .................................. */
	/* Disable Field Edit
	/* ---------------------------------- */
	Formular.prototype.CSS_disableEdit = function(element) {
	
		unsetClass($(this.prefix, this.name, element, 'saved'), 'hide');
		setClass($(this.prefix, this.name, element, 'change'), 'hide');
	}

	/* ---------------------------------- */
	/* ~CSS FUNCTIONALITY~
	/* .................................. */
	/* Show Fields
	/* ---------------------------------- */
	Formular.prototype.CSS_showFields = function(number) {

		unsetClass($(this.prefix, this.name, 'step', number), 'hide');
	}


	/* ---------------------------------- */
	/* ~CSS FUNCTIONALITY~
	/* .................................. */
	/* Hide Fields
	/* ---------------------------------- */
	Formular.prototype.CSS_hideFields = function(element) {

		setClass($(this.prefix, this.name, element, 'fields'), 'hide');
	}


// 1.1) Instances
// .......................................................................

	formular['instances'] = {};
	
	/* ---------------------------------- */
	/* Create Form
	/* ---------------------------------- */
	formular.create = function(name) {
		
		this['instances'][name] = new Formular(name);
		
		// Init
		this['instances'][name].init();
	};


	/* ---------------------------------- */
	/* Change Input
	/* ---------------------------------- */
	formular.change = function(index, element) {
		
		// ~CSS FUNCTIONALITY~
		this['instances'][index].CSS_enableEdit(element);
		this['instances'][index].CSS_hideFields(element);
	};


	/* ---------------------------------- */
	/* Save Input
	/* ---------------------------------- */
	formular.save = function(index, element, inputType, number) {

		this['instances'][index].updateValue(element, inputType);

		// ~CSS FUNCTIONALITY~
		this['instances'][index].CSS_disableEdit(element);
		this['instances'][index].CSS_showFields(number);
	};
	

	/* ---------------------------------- */
	/* Transfer Input To Hidden Form
	/* ---------------------------------- */
	formular.transferToHidden = function(sourceForm, hiddenFormIndex) {
	
		// Iterate All Elements In The Source Form............................................
		for(var e=0; sourceForm.elements[e]; e++) {
		
			var sourceField = sourceForm.elements[e];

			// Transfer Current Element To The Hidden Form....................................
			if(sourceField.getAttribute('transfer') == 'yes') {
				
				var targetField = $(this.prefix, hiddenFormIndex)[sourceField.name];
				
				
				// ~~~ ERROR Handling >>
				// *************************************************
				// Unvalid Element Provided
				// ---------------------------------
				if(targetField.type != 'hidden') {
					location.href = GLOBAL['js_error']['error_page'] +
						'?errorMessage=' +
						escape('Fehler in Funktion formular.transferToHidden(): targetField.type');
				}
				// *************************************************
				// ~~~ /ERROR Handling <<
				
				
				// Assert Value...............................................................
				switch(sourceField.type) {
					
					case 'checkbox':
						if(sourceField.checked)
							targetField.value = 'Ja';
						else
							targetField.value = 'Nein';
						break;
						
					default:
						var value = formular.f.getValue(sourceField);

						if(value.length > 0)
							targetField.value = formular.f.getValue(sourceField);
				}
			}
		}
	}
	
	
	/* ---------------------------------- */
	/* Toggle Optional
	/* ---------------------------------- */
	formular.toggleOptional = function(index, element, specification, checkbox) {
		
		var required = $(formular.prefix, index, element, specification, 'required');
		var optional = $(formular.prefix, index, element, specification, 'optional');
		
		if(checkbox.checked) {
			
			unsetClass(required, 'hide');
			setClass(optional, 'hide');
		}
		else {
			setClass(required, 'hide');
			unsetClass(optional, 'hide');
		}
	}
	
	
	/* ---------------------------------- */
	/* Toggle Disabled
	/* ---------------------------------- */
	formular.toggleDisabled = function(index, element, specification, state, checkbox) {
		
		var field = $(formular.prefix, index, element, specification, state);
		
		if(checkbox.checked) {
			field.removeAttribute('disabled');
			field.focus();
		}
		else
			field.setAttribute('disabled', 'disabled');
	}


	/* ---------------------------------- */
	/* Show Next/Previous FormField Step
	/* ---------------------------------- */
	formular.previous = function(index, element, number) {
	
		var thisStep = $(formular.prefix, index, element, String(number));
		var previousStep = $(formular.prefix, index, element, String(number - 1));
		
		hide(thisStep);
		if(number -1 >= 0)
			show(previousStep);
			
		// Jump To Top (See All Content)
		document.location.hash = '#' + this.topAnchor;
	}
	formular.next = function(index, element, number) {
	
		var thisStep = $(formular.prefix, index, element, String(number));
		var nextStep = $(formular.prefix, index, element, String(number + 1));
		
		hide(thisStep);
		show(nextStep);
		
		// Jump To Top (See All Content)
		document.location.hash = '#' + this.topAnchor;
	}


	/* ---------------------------------- */
	/* Check Form Entries
	/* ---------------------------------- */
	formular.check = function(form, entries) {
	
		for(var i=0; entries[i]; i++) {
			
			var o = form[entries[i]];
			var v = this.f.getValue(o);
			
			var incorrectValue = false;
			
			// Check Special Entries
			var filter = /.?/;
			switch(o.name) {
			
				case 'email':
					filter = /^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6}$/;
					
				default:
					if(!filter.test(v))
						incorrectValue = true;
			}
			
			// NOT Correct Value :-(
			if(v.length == 0 || v == o.getAttribute('defaultvalue') || incorrectValue) {
				setClass(form[entries[i]], 'missingField');
				unsetClass(form[entries[i] + '.missing'], 'hide');
				
				// Set Focus On Missing Field
				form[entries[i]].focus();
				
				// Return Incomplete Form
				return false;
			}
			// Correct Value :-)
			else {
				unsetClass(form[entries[i]], 'missingField');
				setClass(form[entries[i] + '.missing'], 'hide');
			}
		}
	
		return true;
	}
	
	
	/* ---------------------------------- */
	/* Merge Checkbox Fields
	/* ---------------------------------- */
	formular.mergeCheckboxFieldsToHidden = function(form, mergeChkbx, targetField) {
		
		var mergedContent = [];
		
		for(m=0; mergeChkbx[m]; m++) {
			
			var currentChkbx = form[this.mergePrefix + '.' + mergeChkbx[m]];
			
			if(currentChkbx.checked)
				mergedContent.push(currentChkbx.value);
		}
	
		form[targetField].value = mergedContent.join(', ');
	}
		
	
	/* ---------------------------------- */
	/* Create Preview With Hidden Form
	/* ---------------------------------- */
	formular.previewHidden = function(formIndex, formElement) {
		
		var f = $(this.prefix, formIndex);
		var output = $(this.prefix, formIndex, formElement, 'preview');
		
		// Clear Old Content
		clear(output);
		
		for(var i=0; f.elements[i]; i++)
		
			if(f.elements[i].value.length > 0) {
				
				var labelTag = $Element(this.labelTag);
				labelTag.appendChild($Text(f.elements[i].title));
				output.appendChild(labelTag);
				output.appendChild($Text(f.elements[i].value));
			}
	}

	/* ---------------------------------- */
	/* Send Form
	/* ---------------------------------- */
	formular.send = function(formIndex, formElement){
		
		var self = this;
		
		ajax.sendForm($(this.prefix, formIndex),

			function (sending_sucessful) {
				
				if(sending_sucessful) {
					unsetClass($(self.prefix, formIndex, formElement, 'send', 'successful'), 'hide');
					setClass($(self.prefix, formIndex, formElement, 'send', 'failed'), 'hide');
				}
				else {
					setClass($(self.prefix, formIndex, formElement, 'send', 'successful'), 'hide');
					unsetClass($(self.prefix, formIndex, formElement, 'send', 'failed'), 'hide');
				}
			}
		);
	}

/* --------------------------------------------------------------------------------------------- */
/* 2) General Functions
/* --------------------------------------------------------------------------------------------- */

	formular.f = {};
	
// 2.0) Get Value
// .......................................................................
	formular.f.getValue = function(field) {
		
		var f = $(field);
		var value;
		
		switch(f.type) {
	
			case 'radio':
				value = getRadioValue(f);
				break;
				
			case 'select-multiple':
				value = getSelectValue(f);
				break;
				
			default:
				value = f.value;
		}
		
		return value;
	}


// 2.1) Get Radio Value
// .......................................................................
	formular.f.getRadioValue = function(form, name) {
		
		var f = $(form);

		for(var i = 0; i < f.elements.length; i++)
			if(f.elements[i].type == 'radio')
				if(f.elements[i].name == name)
					if(f.elements[i].checked)
						return f.elements[i].value;
						
		return '';
	}
	
// 2.2) Get Select Value
// .......................................................................
	formular.f.getSelectValue = function(selectField) {
	
		var s = $(selectField);
		
		return s.options[s.selectedIndex].value;
	}
	
// 2.3) Get Select Text
// .......................................................................
	formular.f.getSelectText = function(selectField) {
	
		var s = $(selectField);
		
		return s.options[s.selectedIndex].text;
	}

