window.addEvent('domready', function() {

	var my_options = $('my_options');
	my_options.setStyle('display', 'none');
	
	var choices = {};
	
	var request = new Request.JSON({
		url: 'http://www.usg.edu/js/contacts.json',
		onComplete: function(data){
			choices.json = data.Topics.optgroup;	
		}
	}).send();
	
		
	
	
	var addContacts = function(options) {
	
		my_options.setStyle('display', 'block');
		my_options.empty();
		
		var el = new Element('label', {'html': '<strong>I have a question / comment about (required)</strong><br />'});
		var sel = new Element('select', {'id': 'question_or_comment_selection', 'name': 'to', 'title': 'Choose question or comment topic', 'class': 'required'}).inject(el);

		options.each(function(option) {
			
			var name = new Element('option', {'html': option.choice, 'value': option.value}).inject(sel);
			
		});
		
		el.inject(my_options);
	
		$('question_or_comment_selection').addEvent('change', function(){
			var w = this.selectedIndex;
		    $('display_topic').setProperty('value', this.options[w].text);
		    $('subject').setProperty('value', 'USG CONTACT FORM - ' + this.options[w].text);
		});
	};
	
	$$('input[type=radio]').each(function(item) {
	
		item.addEvent('click', function(){
			id = item.id;
			addContacts(choices.json[id]);		
		});
	
	});
	
	
	var validator = new FormValidator.Inline('contact_form').add('minLength', {
		errorMsg: function(element, props){
		if($type(props.minLength))
		 return 'Please enter at least ' + props.minLength + ' characters (you entered ' + element.get('value').length + ' characters).';
		else return '';
	  }
	});
	
	new UvumiTextarea({selector:'textarea#message',maxChar:1500});
	
		
});

