/**
BW_QuickContact

Requires:
	prototype.js
	scriptaculous/effect.js
	cookies.js
*/

function BW_QuickContact(el, url) {
	var self = this;
	this.self = self;
	this.el = $(el);
	this.id = this.el.id;
	this.url = url;
	this.timeout = 15000;
	this.hide = false;
	//this.highlight_options = { };
	
	$(this.el.id+'-form').enable();
	$(this.el.id+'-form').reset();
	
	this.jar = new CookieJar({
		expires:60*60*24*30,
		path: '/'
	});
	if (this.jar) {
		cook = this.jar.get('contact-quick-cook');
		if (cook) {
			if (cook.NAME_FIRST) $(this.el.id+'-NAME_FIRST').value=cook.NAME_FIRST;
			if (cook.NAME_LAST) $(this.el.id+'-NAME_LAST').value=cook.NAME_LAST;
			if (cook.PHONE) $(this.el.id+'-PHONE').value=cook.PHONE;
			if (cook.EMAIL) $(this.el.id+'-EMAIL').value=cook.EMAIL;
			if (cook.newsletter && cook.newsletter == 'false') $(this.el.id+'-newsletter').checked = false;
		}
	}
	
	Event.observe(this.id+'-submit', 'click', function(e){ self.submit(e); }, false);
}
BW_QuickContact.prototype = {
	feedback: function(message) {
		//console.dir($(this.id).viewportOffset());
		if ($(this.id).viewportOffset().top < 0) new Effect.ScrollTo(this.id, { offset: -20 });
		$(this.id+'-feedback').update(message);
		$(this.id+'-feedback').show();
		new Effect.Pulsate(this.id+'-feedback', { duration: 1.0, pulses: 3, queue: 'end' });
	},
	submit: function(event) {
		//console.dir(this);
		$(this.id+'-form').enable();
		
		// validate form
		if ($(this.id+'-PHONE').value == '' && $(this.id+'-EMAIL').value == '') {
			this.feedback('You must provide a valid email address or a 7 or 10 digit phone number.');
			return false;
		} else if ($(this.id+'-NOTE').value == '') {
			this.feedback('You must provide a message.');
			return false;
		}
		
		var formSerialized = Form.serialize(this.id+'-form');
		$(this.id+'-form').disable();
		
		this.feedback('Sending your message ...');
		
		new Effect.Appear(this.id+'-working');
		new Effect.Opacity($(this.el), { from: 1.0, to: 0.3 });
		
		// start the submit timer ... if it takes too long just sumbit it the old way
		this.timer = setTimeout(function(){ this.oldSubmit(); }, this.timeout);
		
		// send the form data via ajax
		new Ajax.Request(
			this.url,
			{
				method: 'post',
				parameters: formSerialized,
				evalScripts: true,
				onComplete: function(request){
					if (this.timer) clearTimeout(this.timer);
					var rval = eval('(' + request.responseText + ')');
					$(this.id+'-working').hide();
					if (rval.status == '200') {
						// ok
						if (rval.update) this.feedback(rval.update);
						else this.feedback("<b>Your message has been sent.</b> You will be contacted by a member of the Brown &amp; West Team as soon as possible.");
						
						if (this.hide == true) {
							// don't need the form any more on this page
							new Effect.BlindUp(this.id+'-form');
							new Effect.Fade(this.id+'-form');
							//$(this.id+'-NOTE').value = '';
						}
						
						// set a cookie to remember what we just submitted
						if (rval.cook != '') {
							//new Ajax.Request(site_url+'/?engine&opt=ping&postsession', { method: 'post', parameters: rval.post });
							this.jar.put('contact-quick-cook', rval.cook);
						}
						
					} else if (rval.status == '100') {
						// missing some required stuff
						if (rval.update) this.feedback(rval.update);
						else this.feedback("Please fill in all required fields.");
						$(this.id+'-form').enable();
						// kill the cookie just in case
						this.jar.empty();
						$(this.id+'-form').enable();
						
					} else {
						// some kind of error happened
						if (rval.error) this.feedback(rval.error);
						else {
							this.feedback("Unfortunately there was a problem while trying to send your message. Until we can resolve the issue, <b>please call us anytime at 302-258-4368</b>.");
						}
					}
					//if ($('contact-quick-feedback').innerHTML != '') {
						//new Effect.BlindDown('contact-quick-feedback');
						//new Effect.Appear('contact-quick-feedback');
					//}
					new Effect.Opacity(this.el, { from: 0.3, to: 1.0 });
					//$(this.id+'-form').enable();
					
				}.bind(this.self)
			}
		);
		
		// stop html form from submiting
		Event.stop(event);
	},
	oldSubmit: function() {
		
	}
}
