function Log(text) {
	if (window.console) console.log(text);
}

var Field = new Class({
	initialize: function( handler, element ) {
		this.form_handler = handler;		// the form handler
		this.elements = $A(new Array());	// the DOM elements
		this.dependants = $A(new Array());	// dependant field names
		this.parent = null;					// name of parent
		this.parent_value = null;			// values the parent may have, if multiple, seperate with |

		this.addElement( element );
		
		this.update();
	},
	
	/*
	 * Adds a new form element to this Field
	 */
	addElement: function (e) {
		// Add events to e
		if (e.get('type') == 'radio' || e.get('type') == 'checkbox')
			e.addEvent('click', this.update.bind(this));
		else
			e.addEvent( ((e.get('tag') == 'select') ? 'change' : 'keyup'), this.update.bind(this));
		if (e.get('parent')) {
			this.parent = e.get('parent');
			this.parent_value = $A(e.get('parent_value').split('|'));
		}
		this.elements.push(e);
	},
	
	setDependant: function( field_name ) {
		this.dependants.push( field_name );
	},
	
	/*
	 * Update the current field and any dependants
	 */
	update: function() {
		this.color( !this.check() );
		this.dependants.each(function (dep) {
			dep.update();
		});
		this.form_handler.updateSubmit( this.check );
	},
	
	/*
	 * Returns the selected value for dependant comparisons.
	 */
	value: function() {
		var r = "";
		this.elements.each(function(e){
			if (e.get('type') == 'radio' || e.get('type') == 'checkbox'){
				if (e.checked)
					r = r + e.value;
			}
			else
				r = r + e.value;
		});
		return r;
	},
	
	/*
	 * Color this field
	 */
	color: function(color){
		this.elements.each(function (e){
			if (e.get('type') == 'radio' || e.get('type') == 'checkbox')
				(color) ? e.getParent().addClass('required') : e.getParent().removeClass('required');
			else
				(color) ? e.addClass('required') : e.removeClass('required');
		});
	},
	
	/*
	 * Check if the field is valid or not
	 */
	check: function() {
		// Check parent -- if parent not set, this is not required
		if (this.parent) {
			//if (this.form_handler.fields.get( this.parent ).value() != this.parent_value)
			if (!this.parent_value.contains( this.form_handler.fields.get( this.parent ).value() ))
				return true;
		}
		
		var r = false;
		this.elements.each(function (e){
			if (e.get('type') == 'radio' || e.get('type') == 'checkbox') {
				if (e.checked)
					r = true;
			}
			else {
				if (e.value.length > 0)
					r = true; 
			}
		});
		return r;
	}
});


var FormHandler = new Class({
	initialize: function() {
		this.form = $('f');
		if (!this.form)
			return;
		
		this.allow_submit = false;
		this.submit = this.form.getElement('input[type=submit]');
		this.location = this.form.getElement('select[name=location]');
				
		this.fields = new Hash();
		
		// Create all the required fields
		$A(this.form.elements).each(function(element) {
			element = $(element);
			if (!this.fields.has(element.get('name'))) {
				if (element.get('required') == 'true') { // if el is required
					field = new Field(this, element);
					this.fields.set(element.get('name'), field);
				}
			}
			else // field object exists, add new element to it
				this.fields.get(element.get('name')).addElement(element);
		}.bind(this));
		
		// Set dependant children
		this.fields.each(function (field) {
			if (field.parent)
				this.fields.get( field.parent ).setDependant( field );
		}.bind(this));
		
		
		/* Custom Events */
		this.location.addEvent('change', this.displayCateringMessage.bind(this));
		$A(this.form['catering']).each( function(button) {
			button.addEvent('click', this.displayCateringMessage.bind(this));	
		}.bind(this) );
		
		// Check Foundation aggreement
		this.location.addEvent('change', function() {
			if (this.location.value == 'foundation')
				$('foundation_agree').removeClass('hidden');
			else
				$('foundation_agree').addClass('hidden');
		}.bind(this));
		
		this.updateSubmit();
	},
	
	displayCateringMessage: function() {
		if (!this.catering_warning) {
			this.catering_warning = new Element('div', {
				'class': 'error hidden',
				'html': 'Based on your location, you must contact catering at 994-3336 before submiting an alcohol request form.'
			});
			this.catering_warning.inject(this.form.getElement('input[name=catering_contact]'), 'after');
		}
	
		if (this.checkCatering())
			this.catering_warning.addClass('hidden');
		else
			this.catering_warning.removeClass('hidden');
		
	},
	
	checkCatering: function() {
		if ((this.location.value == 'sub' || this.location.value == 'sfac' || this.location.value == 'other') &&
			this.fields.get('catering').value() != '1')
			return false;
			
		return true;
	},
	
	updateSubmit: function ( allow_submit ) {
		if ($type(allow_submit) == 'bool' && !allow_submit)	// current is false, no need to check further
			this.submit.disabled = allow_submit;
		else {
			r = true;
			this.fields.each(function(field) {
				if (!field.check())
					r = false;
			}.bind(this));
			
			if (r)	// check custom catering message
				r = this.checkCatering();
			
			this.submit.disabled = !r;
		}
	},
	
	
	updateCateringWarning: function() {
		if (this.catering.checked) {
			//$('warning_catering').removeClass('hidden');
		}
		else {
			if (this.location.value == 'sub' || this.location.value == 'sfac' || this.location.value == 'other') 
				$('warning_catering').removeClass('hidden');
			
		}
	}
});


window.addEvent('domready', function() {
	var f = new FormHandler();
});

