Playray.Support = Class.create({

	initialize: function() {
		this.supportForm = $('supportForm');
		if (!this.supportForm) { return; }
		this.currentQAField = null;
		this.specifyLabels = [];
		this.specifyOptions = [];
		this._initForm();
		this._attachBehaviours();
	},

	updateFields: function(e) {
		var val = $('problem').options[$('problem').selectedIndex].value;
		if(val.charAt(0) == '/' || val.substring(0,4) == 'http') {
			// If problem seems like an URL, redirect user
			Playray.Navigate(e, val);
			return;
		}
		
		this.updateSpecifySelect(e);
		this.updateAdditionalFields(e);
		this._clearErrors();
	},

	updateSpecifySelect: function(e) {
		var eTarget, specifyFieldset, qaFields, tag, labl, select, option;
		eTarget = e.target ? e.target : e.srcElement;
		if (!eTarget.value) { return; }
		specifyFieldset = $('fs_specify');
		qaFields = $('qafields');
		tag = eTarget.options[eTarget.selectedIndex].className;
		if (this.currentQAField) { this.currentQAField.addClassName('hidden'); }
		if (this.specifyLabels[eTarget.value] && this.specifyOptions[eTarget.value]) {
			while (specifyFieldset.firstChild) { specifyFieldset.removeChild(specifyFieldset.firstChild); }
			labl = document.createElement('LABEL');
			labl.appendChild(document.createTextNode(this.specifyLabels[eTarget.value]));
			select = document.createElement('SELECT');
			Element.extend(select);
			select.setAttribute('name', 'support[specify]');
			select.setAttribute('id', 'specify');
			this._unshiftChoose(select);
			for (i = 0; i < this.specifyOptions[eTarget.value].length; i++) {
				option = this.specifyOptions[eTarget.value][i];
				select.appendChild(option.cloneNode(true));
			}
			Event.observe(select, 'change', this.updateAdditionalFields.bindAsEventListener(this));
			specifyFieldset.appendChild(labl);
			specifyFieldset.appendChild(select);
			specifyFieldset.removeClassName('hidden');
		} else {
			specifyFieldset.addClassName('hidden');
			qaFields.addClassName('hidden');
		}
		if (tag && (tag.match(/feedback/) || tag.match(/games/) || tag.match(/report/) || tag.match(/suggestion/))) {
			$('label_feedback').innerHTML = $('label_feedback_general').innerHTML;
			if ($('label_feedback_'+ tag)) { $('label_feedback').innerHTML += $('label_feedback_'+ tag).innerHTML; }
		} else if ($('label_feedback_'+ tag)) {
			$('label_feedback').innerHTML = $('label_feedback_'+ tag).innerHTML;
		} else {
			$('label_feedback').innerHTML = '';
		}

		this._toggleBasicFields(false);
	},

	updateAdditionalFields: function(e) {
		var eTarget, qaFields, qaField, problemValue, specifyValue;
		eTarget = e.target ? e.target : e.srcElement;
		qaFields = $('qafields');
		problemValue = $('problem').value;
		specifyValue = ($('specify')) ? $('specify').value : null;
		if ($('qa_'+ specifyValue)) { qaField = $('qa_'+ specifyValue); }
		else if ($('qa_'+ problemValue)) { qaField = $('qa_'+ problemValue); }
		if (this.currentQAField) { this.currentQAField.addClassName('hidden'); }
		if (qaField) {
			qaFields.removeClassName('hidden');
			qaField.removeClassName('hidden');
			this.currentQAField = qaField;
		} else {
			qaFields.addClassName('hidden');
		}
	},

	checkRequiredFields: function(e) {
		var problemValue, specifyValue, errorCount, specifyFieldset, legend;
		var qaField, me, inputFields, inputFieldset, inputField, filter;
		problemValue = $('problem').value;
		specifyValue = ($('specify')) ? $('specify').value : null;
		errorCount = 0;
		specifyFieldset = $('fs_specify');
		if (!$('fs_specify').hasClassName('hidden') && !$F('specify')) {
			specifyFieldset.addClassName('error');
			if (specifyFieldset.firstChild.nodeName != 'LEGEND') {
				legend = document.createElement('LEGEND');
				legend.appendChild(document.createTextNode($('legend_error_subcategory').firstChild.nodeValue));
				specifyFieldset.insertBefore(legend, specifyFieldset.firstChild);
			}			
			errorCount++;
		} else {
			specifyFieldset.removeClassName('error');
		}
		if ($('qa_'+ specifyValue)) { qaField = $('qa_'+ specifyValue); }
		else if ($('qa_'+ problemValue)) { qaField = $('qa_'+ problemValue); }
		if (qaField) {
			me = this;
			inputFields = $A(qaField.getElementsByTagName('input'));
			inputFields.each( function(inputField) {
				var fieldValue, legend;
				fieldValue = inputField.value;
				if (inputField.type == 'radio' || inputField.type == 'checkbox') {
					fieldValue = '';
					for (i = 0; i < me.supportForm.elements[inputField.name].length; i++) {
						if (me.supportForm.elements[inputField.name][i].checked) {
							fieldValue = me.supportForm.elements[inputField.name][i].value;
							break;
						}
					}
				}
				if (!fieldValue) {
					if (inputField.parentNode.firstChild.nodeName != 'LEGEND') {
						legend = document.createElement('LEGEND');
						legend.appendChild(document.createTextNode($('legend_error').firstChild.nodeValue));
						inputField.parentNode.insertBefore(legend, inputField.parentNode.firstChild);
					}
					inputField.parentNode.addClassName('error');
					errorCount++;
				} else {
					inputField.parentNode.removeClassName('error');
				}
			});
		}
		inputFieldset = $('fs_feedback');
		inputField = inputFieldset.getElementsByTagName('textarea')[0];
		if (inputField.value.length < 10) {
			inputFieldset.addClassName('error');
			if (inputFieldset.firstChild.nodeName != 'LEGEND') {
				legend = document.createElement('LEGEND');
				legend.appendChild(document.createTextNode($('legend_error_body').firstChild.nodeValue));
				inputFieldset.insertBefore(legend, inputFieldset.firstChild);
			}
			errorCount++;
		} else {
			inputFieldset.removeClassName('error');
		}

		inputFieldset = $('fs_sender');
		inputField = inputFieldset.getElementsByTagName('input')[0];
		filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!inputField.value || !filter.test(inputField.value)) {
				inputFieldset.addClassName('error');
				if (inputFieldset.firstChild.nodeName != 'LEGEND') {
					legend = document.createElement('LEGEND');
					legend.appendChild(document.createTextNode($('legend_error_sender').firstChild.nodeValue));
					inputFieldset.insertBefore(legend, inputFieldset.firstChild);
				}
				errorCount++;
		} else {
			inputFieldset.removeClassName('error');
		}

		if (errorCount > 0) {
			Event.stop(e);
			return false;
		}
	},

	_toggleBasicFields: function(bool) {
		if (bool) {
			$('fs_feedback').addClassName('hidden');
			$('fs_sender').addClassName('hidden');
			$('fs_submit').addClassName('hidden');
		} else {
			$('fs_feedback').removeClassName('hidden');
			$('fs_sender').removeClassName('hidden');
			$('fs_submit').removeClassName('hidden');
		}
	},

	_clearErrors: function() {
		$A(this.supportForm.getElementsByClassName('error')).each( function(elem) {
			elem.removeClassName('error');
		});
	},

	_unshiftChoose: function(select) {
		var firstOption, secondOption;
		firstOption = document.createElement('OPTION');
		firstOption.setAttribute('value', '');
		firstOption.setAttribute('selected', 'selected');
		firstOption.appendChild(document.createTextNode($('option_choose_title').firstChild.nodeValue));
		secondOption = document.createElement('OPTION');
		secondOption.setAttribute('value', '');
		secondOption.setAttribute('disabled', 'disabled');
		secondOption.appendChild(document.createTextNode(''));
		select.appendChild(firstOption);
		select.appendChild(secondOption);
	},

	_initForm: function() {
		var optgroups;
		optgroups = $A(this.supportForm.getElementsByTagName('OPTGROUP'));
		this._toggleBasicFields(true);
		optgroups.each( function(optgroup) {
			var classNames, tag, option;
			classNames = $A(Element.classNames(optgroup));
			tag = classNames[0];
			this.specifyOptions[tag] = $A(optgroup.getElementsByTagName('OPTION'));
			this.specifyLabels[tag] = optgroup.getAttribute('title');
			option = document.createElement('OPTION');
			Element.extend(option);
			option.setAttribute('value', tag);
			option.addClassName(classNames[1]);
			option.appendChild(document.createTextNode(optgroup.getAttribute('label')));
			optgroup.parentNode.replaceChild(option, optgroup);
		}.bind(this));
	},

	_attachBehaviours: function() {
		Event.observe(this.supportForm, 'submit', this.checkRequiredFields.bindAsEventListener(this));
		Event.observe($('problem'), 'change', this.updateFields.bindAsEventListener(this));
		Event.observe($('supportSubmit'), 'click', this.checkRequiredFields.bindAsEventListener(this));
	}

});