var SubmitTip = {
	_sending:	false,
	_cache:		null,
	
	initialize: function () {
		this.Verify.All = this.Verify.All.bind(this);
		this.Verify.Tip = this.Verify.Tip.bind(this);
		
		this._cache = {
			btnSend: {
				click: this.Send.bind(this)
			},
			tip: {
				change:		this.Tip_onChange,
				focus:		this.Tip_onFocus
			},
			name: {
				blur: this.Verify.Name
			},
			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('tip', 'focus', this._cache.tip.focus);
		Event.observe('tip', 'keydown', this._cache.tip.change);
		Event.observe('tip', 'keypress', this._cache.tip.change);
		Event.observe('tip', 'keyup', this._cache.tip.change);
		Event.observe('name', 'blur', this._cache.name.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('tip', 'focus', this._cache.tip.focus);
		Event.stopObserving('tip', 'keydown', this._cache.tip.change);
		Event.stopObserving('tip', 'keypress', this._cache.tip.change);
		Event.stopObserving('tip', 'keyup', this._cache.tip.change);
		Event.stopObserving('name', 'blur', this._cache.name.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;
		}
	},
	
	Tip_onFocus: function () {
		if ($('tip').value == 'Give us your tip(s) here! (3000 character limit)')
			$('tip').value = '';
	},
	
	Tip_onChange: function () {
		if ($('tip').value.length > 3000)
			$('tip').value = $('tip').value.substr(0, 3000);
		$('tipcharactersremaining').innerHTML = (3000 - $('tip').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.Tip() || fail;
			fail = !this.Verify.Name() || 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;
		},
		
		Tip: function () {
			this.Tip_onFocus();
			
			$('tip').value = $('tip').value.trim();
			var tip_failed = ($('tip').value.length == 0);
			$('v_tip').style.color = (tip_failed ? 'red' : '');
			return !tip_failed;
		},
		
		Name: function () {
			$('name').value = $('name').value.trim();
			var name_failed = ($('name').value.length == 0);
			$('v_name').style.color = (name_failed ? 'red' : '');
			return !name_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-tip/', {
				method: 'post',
				parameters: {
					tip:			$('tip').value,
					name:			$('name').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 () {
		$('tip').disabled = this._sending;
		$('name').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', SubmitTip.initialize.bind(SubmitTip));