var SubmitStory = {
	_sending:	false,
	_cache:		null,
	
	initialize: function () {
		this.Verify.All = this.Verify.All.bind(this);
		this.Verify.Story = this.Verify.Story.bind(this);
		
		this._cache = {
			btnSend: {
				click: this.Send.bind(this)
			},
			title: {
				blur: this.Verify.Title
			},
			story: {
				change:		this.Story_onChange,
				focus:		this.Story_onFocus
			},
			firstname: {
				blur: this.Verify.FirstName
			},
			lastname: {
				blur: this.Verify.LastName
			},
			yob: {
				blur: this.Verify.YearOfBirth
			},
			email: {
				blur: this.Verify.Email
			},
			city: {
				blur: this.Verify.City
			},
			state: {
				blur: this.Verify.State
			},
			stateother: {
				blur: this.Verify.State
			},
			country: {
				change: this.Country_onChange,
				blur:	this.Verify.Country
			}
		};
		
		Event.observe('title', 'blur', this._cache.title.blur);
		Event.observe('story', 'focus', this._cache.story.focus);
		Event.observe('story', 'keydown', this._cache.story.change);
		Event.observe('story', 'keypress', this._cache.story.change);
		Event.observe('story', 'keyup', this._cache.story.change);
		Event.observe('firstname', 'blur', this._cache.firstname.blur);
		Event.observe('lastname', 'blur', this._cache.lastname.blur);
		//Event.observe('yob', 'blur', this._cache.yob.blur);
		Event.observe('email', 'blur', this._cache.email.blur);
		Event.observe('city', 'blur', this._cache.city.blur);
		Event.observe('state', 'blur', this._cache.state.blur);
		Event.observe('stateother', 'blur', this._cache.stateother.blur);
		Event.observe('country', 'blur', this._cache.country.blur);
		Event.observe('country', 'change', this._cache.country.change);
		Event.observe('btnSend', 'click', this._cache.btnSend.click);
		
		Event.observe(window, 'unload', this.deinitialize.bind(this));
	},
	
	deinitialize: function () {
		Event.stopObserving('title', 'blur', this._cache.title.blur);
		Event.stopObserving('story', 'focus', this._cache.story.focus);
		Event.stopObserving('story', 'keydown', this._cache.story.change);
		Event.stopObserving('story', 'keypress', this._cache.story.change);
		Event.stopObserving('story', 'keyup', this._cache.story.change);
		Event.stopObserving('firstname', 'blur', this._cache.firstname.blur);
		Event.stopObserving('lastname', 'blur', this._cache.lastname.blur);
		//Event.stopObserving('yob', 'blur', this._cache.yob.blur);
		Event.stopObserving('email', 'blur', this._cache.email.blur);
		Event.stopObserving('city', 'blur', this._cache.city.blur);
		Event.stopObserving('state', 'blur', this._cache.state.blur);
		Event.stopObserving('stateother', 'blur', this._cache.stateother.blur);
		Event.stopObserving('country', 'blur', this._cache.country.blur);
		Event.stopObserving('country', 'change', this._cache.country.change);
		Event.stopObserving('btnSend', 'click', this._cache.btnSend.click);

		this._cache = null;
	},
	
	Country_onChange: function () {
		if ($('country').options[$('country').selectedIndex].value == 'us') {
			$('s_selectcountry').style.display = 'none';
			$('s_stateother').style.display = 'none';
			$('stateother').value = '';
			$('state').style.display = 'inline';
		} else {
			var selectedCountry = ($('country').options[$('country').selectedIndex].value != '');
			$('state').style.display = 'none';
			$('s_selectcountry').style.display = (selectedCountry ? 'none' : 'inline');
			$('s_stateother').style.display = (selectedCountry ? 'inline' : 'none');
			$('state').selectedIndex = 0;
		}
	},
	
	Story_onFocus: function () {
		if ($('story').value == 'Type your story here! (3000 character limit)')
			$('story').value = '';
	},
	
	Story_onChange: function () {
		if ($('story').value.length > 3000)
			$('story').value = $('story').value.substr(0, 3000);
		$('storycharactersremaining').innerHTML = (3000 - $('story').value.length);
	},
	
	Send: function () {
		if (this._sending || !this.Verify.All())
			return;
		
		this._sending = true;
		this._updateForm();
		
		this._transmit();
	},
	
	Verify: {
		All: function () {
			var fail = !this.Verify.Title() || fail;
			fail = !this.Verify.Story() || fail;
			fail = !this.Verify.FirstName() || fail;
			fail = !this.Verify.LastName() || fail;
			//fail = !this.Verify.YearOfBirth() || fail;
			fail = !this.Verify.Email() || fail;
			fail = !this.Verify.City() || fail;
			fail = !this.Verify.State() || fail;
			fail = !this.Verify.Country() || fail;
			return !fail;
		},
		
		Title: function () {
			$('title').value = $('title').value.trim();
			var title_failed = ($('title').value.length == 0);
			$('v_title').style.color = (title_failed ? 'red' : '');
			return !title_failed;
		},
		
		Story: function () {
			this.Story_onFocus();
			
			$('story').value = $('story').value.trim();
			var story_failed = ($('story').value.length == 0);
			$('v_story').style.color = (story_failed ? 'red' : '');
			return !story_failed;
		},
		
		FirstName: function () {
			$('firstname').value = $('firstname').value.trim();
			var firstname_failed = ($('firstname').value.length == 0);
			$('v_firstname').style.color = (firstname_failed ? 'red' : '');
			return !firstname_failed;
		},
		
		LastName: function () {
			$('lastname').value = $('lastname').value.trim();
			var lastname_failed = ($('lastname').value.length == 0);
			$('v_lastname').style.color = (lastname_failed ? 'red' : '');
			return !lastname_failed;
		},
		
		YearOfBirth: function () {
			var yearofbirth_failed = ($('yob').options[$('yob').selectedIndex].value == '');
			$('v_yob').style.color = (yearofbirth_failed ? 'red' : '');
			return !yearofbirth_failed;
		},

		Email: function () {
			$('email').value = $('email').value.trim();
			var email_failed = (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($('email').value));
			$('v_email').style.color = (email_failed ? 'red' : '');
			return !email_failed;
		},
		
		City: function () {
			$('city').value = $('city').value.trim();
			var city_failed = ($('city').value.length == 0);
			$('v_city').style.color = (city_failed ? 'red' : '');
			return !city_failed;
		},
		
		State: function () {
			if ($('country').options[$('country').selectedIndex].value == 'us') {
				var state_failed = ($('state').options[$('state').selectedIndex].value == '');
			} else {
				$('stateother').value = $('stateother').value.trim();
				var state_failed = ($('stateother').value.length == 0);
			}
			$('v_state').style.color = (state_failed ? 'red' : '');
			return !state_failed;
		},
		
		Country: function () {
			var country_failed = ($('country').options[$('country').selectedIndex].value == '');
			$('v_country').style.color = (country_failed ? 'red' : '');
			return !country_failed;
		}
	},
	
	_transmit: function () {
		new Ajax.Request('/ajax/submit-story/', {
				method: 'post',
				parameters: {
					title:			$('title').value,
					story:			$('story').value,
					firstname:		$('firstname').value,
					lastname:		$('lastname').value,
					anonymous:		($('anonymous').checked ? 'on' : ''),
					yob:			$('yob').value,
					email:			$('email').value,
					city:			$('city').value,
					state:			$('state').value,
					stateother:		$('stateother').value,
					country:		$('country').value
				},
				requestHeaders: {Accept: 'application/json'},
				onFailure: function () {
					alert('There was an issue while processing your request.\n\nPlease try again later.');
					this._sending = false;
					this._updateForm();
				}.bind(this),
				onSuccess: function (transport) {
					var requestFailed = false;
					try {
						json = transport.responseText.evalJSON();
					} catch (e) {
						requestFailed = true;
					}
					
					if (requestFailed) {
						alert('There was an issue while processing your request.\n\nPlease try again later.');
						this._sending = false;
						this._updateForm();
					} else {
						if (json.success) {
							this._success();
						} else {
							this.Verify.All();
							if (json.message) {
								alert(json.message);
							} else {
								alert('There was an issue while processing your request.\n\nPlease try again later.');
							}
							this._sending = false;
							this._updateForm();
						}
					}
				}.bind(this)
			}
		);
	},
	
	_updateForm: function () {
		$('title').disabled = this._sending;
		$('story').disabled = this._sending;
		$('firstname').disabled = this._sending;
		$('lastname').disabled = this._sending;
		$('yob').disabled = this._sending;
		$('email').disabled = this._sending;
		$('city').disabled = this._sending;
		$('state').disabled = this._sending;
		$('stateother').disabled = this._sending;
		$('country').disabled = this._sending;
		$('btnSend').writeAttribute('src', '/images/buttons/' + (this._sending ? 'contactus_send_d.gif' : 'contactus_send.gif'));
		$('btnSend').writeAttribute('srcorg', '/images/buttons/' + (this._sending ? 'contactus_send_d.gif' : 'contactus_send.gif'));
		$('btnSend').writeAttribute('srcover', '/images/buttons/' + (this._sending ? 'contactus_send_d-h.gif' : 'contactus_send-h.gif'));
		$('btnSend').setStyle({cursor:(this._sending ? '' : 'pointer')});
	},
	
	_success: function () {
		$('ajaxform-container').setStyle({height: $('ajaxform-body-content').getHeight()+'px'});
		new Effect.Fade('ajaxform-body-content');
		new Effect.Appear('ajaxform-body-thankyou', {queue: 'end'});
	}
}
Event.observe(window, 'load', SubmitStory.initialize.bind(SubmitStory));