var Register = Class.create();

Register.prototype =
{	
	initialize: function() 
	{
		$$('#register input[type=radio]').each(function(x) {
			switch (x.value)
			{
				case 'yes':
					x.onclick = function() { this.question('yes', x.name) }.bindAsEventListener(this)
					break;
			
				case 'no':
					x.onclick = function() { this.question('no', x.name) }.bindAsEventListener(this)
					break;

				case 'assistant':
					x.onclick = function() { this.section('assistant') }.bindAsEventListener(this)
					break;

				case 'student':
					x.onclick = function() { this.section('student') }.bindAsEventListener(this)
					break;

				case 'physician':
					x.onclick = function() { this.section('physician') }.bindAsEventListener(this)
					break;

				case 'resident':
					x.onclick = function() { this.show('resident') }.bindAsEventListener(this)
					break;
					
				case 'fellow':
					x.onclick = function() { this.show('fellow') }.bindAsEventListener(this)
					break;
				
				case 'attending':
					x.onclick = function() { this.show('attending') }.bindAsEventListener(this)
					break;
					
				case 'academic':
					x.onclick = function() { this.show('academic') }.bindAsEventListener(this)
					break;
					
				case 'private':
					x.onclick = function() { this.show('private') }.bindAsEventListener(this)
					break;
			}
		}.bind(this));
		
		$$('#register input[type=text]').each(function(x) {
			if (!x.hasClassName('active'))
				x.onfocus = function() { app.clearOnFocus(x) }
		}.bind(this));
		
		if (document.forms['registerForm'] && document.forms['registerForm'].block.value)
		{
			this.block = document.forms['registerForm'].block.value;
			this.open = document.forms['registerForm'].block.value;
		}
	},
	
	question: function(x, n)
	{
		if (x == 'no') {
			Effect.Fade(n);
			return;
		}
		Effect.Appear(n);
	},
	
	section: function(x)
	{
		if (this.openSection) $(this.openSection).hide();
		this.openSection = x;

		if (x == 'student')
		{
			this.block = x;
		}
		else if (x == 'assistant')
		{
			this.block = x;
		}		
		else
		{
			this.block = (this.open == 'attending') ? '' : this.open;
		}

		document.forms['registerForm'].block.value = this.block;

		Effect.Appear(x);
	},
	
	show: function(x)
	{
		this.block = x;		
		if (x == 'resident' || x == 'fellow')
		{
			$('attending').hide();
		}
		if (x == 'attending')
		{
			this.block = '';						
			$$('#register input[name=attendingType]').each(function(x) { x.checked = false});
		}

		if (this.open && this.open != 'attending') $(this.open).hide();
		Effect.Appear(x);
		this.open = x;
		
		document.forms['registerForm'].block.value = this.block;		
	},
	
	validate: function(f)
	{

		if (!this.block)
		{
			alert('Please choose your level of training.');			
			return false;
		}
		
		document.forms['registerForm'].block.value = this.block;
		
		switch(this.block)
		{
			case 'resident':
				return validate_residentRegisterForm(f);
			case 'fellow':
				return validate_fellowRegisterForm(f);			
			case 'academic':
				return validate_academicRegisterForm(f);			
			case 'private':
				return validate_privateRegisterForm(f);			
			case 'student':
				return validate_studentRegisterForm(f);			
			case 'assistant':
				return validate_assistantRegisterForm(f);							
			default:
				alert('Please choose your level of training.');
				return false;
		}
	}
}

window.windowOnload.push(function() {			
	window.register = new Register();
});
