var SubmitQuote = {
	_sending:		false,
	_cache:		null,
	
	initialize: function () {
		this.Verify.All = this.Verify.All.bind(this);
		
		this._cache = {
			btnSend: {
				click: this.Send.bind(this)
			},
			name: {
				blur: this.Verify.Name
			},
			email: {
				blur: this.Verify.Email
			},
			quote: {
				blur: this.Verify.Quote
			}
		};
		
		Event.observe('name', 'blur', this._cache.name.blur);
		Event.observe('email', 'blur', this._cache.email.blur);
		Event.observe('quote', 'blur', this._cache.quote.blur);
		Event.observe('btnSend', 'click', this._cache.btnSend.click);
		
		Event.observe(window, 'unload', this.deinitialize.bind(this));
	},
	
	deinitialize: function () {
		Event.stopObserving('name', 'blur', this._cache.name.blur);
		Event.stopObserving('email', 'blur', this._cache.email.blur);
		Event.stopObserving('quote', 'blur', this._cache.quote.blur);
		Event.stopObserving('btnSend', 'click', this._cache.btnSend.click);

		this._cache = null;
	},
	
	Send: function () {
		if (this._sending || !this.Verify.All())
			return;
		
		this._sending = true;
		this._updateForm();
		
		this._transmit();
	},
	
	Verify: {
		All: function () {
			var fail = !this.Verify.Name();
			fail = !this.Verify.Email() || fail;
			fail = !this.Verify.Quote() || fail;
			return !fail;
		},
		
		Name: function () {
			$('name').value = $('name').value.trim();
			var name_failed = ($('name').value.length == 0);
			$('v_name').style.color = (name_failed ? 'red' : '');
			return !name_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;
		},
		
		Quote: function () {
			$('quote').value = $('quote').value.trim();
			var quote_failed = ($('quote').value.length == 0);
			$('v_quote').style.color = (quote_failed ? 'red' : '');
			return !quote_failed;
		}
	},
	
	_transmit: function () {
		new Ajax.Request('/ajax/submit-quote/', {
				method: 'post',
				parameters: {
					name:		$('name').value,
					email:		$('email').value,
					quote:		$('quote').value,
					anonymous:	($('anonymous').checked ? 'on' : '')
				},
				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 () {
		$('name').disabled = this._sending;
		$('email').disabled = this._sending;
		$('quote').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', SubmitQuote.initialize.bind(SubmitQuote));