	
	var splitRouter = Class.create();
	
	splitRouter.prototype = {
	
		classname: "splitRouter",
				
		initialize: function() {
		
			/* removing references to eventSwitch for blog integration */
		
//			this._eventswitch = new __eventswitch();
			this.$container = $("mc_embed_signup");
//			this.$eventsID = this._eventswitch.register();
			this.watchShortForm();
		},
		
		_triggerGoalCompletion: function() {
			/* event tracking from google async tracking reference			
			http://code.google.com/apis/analytics/docs/tracking/asyncMigrationExamples.html#EventTracking
			_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', opt_value]);
			*/
			_gaq.push(['_trackEvent', 'goals', 'blog consult request']);
			_gaq.push(['_trackPageview', '/consult_request_complete']);
		},

		watchShortForm: function() {
			Event.observe($("mcSubscribe"),"click",this.checkShortForm.bindAsEventListener(this));
		},
		
		checkShortForm: function(e) {
		
			e.stop();
			
			this.$phone = Array();
			this.$error = 0;
		
			this.$email = $F("MERGE0");
			this.$firstname = $F("MERGE1");
			this.$lastname = $F("MERGE2");
			this.$phone[0] = $F("MERGE4a");
			this.$phone[1] = $F("MERGE4b");
			this.$phone[2] = $F("MERGE4c");
			
			if (!checkEmail(this.$email,0)) {
				this.$error += 1;
			}
			
			if (this.$firstname.length < 1) {
				this.$error += 2;
			}
			
			if (this.$lastname.length < 1) {
				this.$error += 4;
			}

			if (this.$error == 0) {
			
				/* get checkbox value */
				this.$optout = $F("optout");
							
				this.$params = {
					"MERGE0":this.$email,
					"MERGE1":this.$firstname,
					"MERGE2":this.$lastname,
					"MERGE4[0]":this.$phone[0],
					"MERGE4[1]":this.$phone[1],
					"MERGE4[2]":this.$phone[2],
					"optout":this.$optout
				}
				
				this.postConsultRequest(this.$params);
				
			} else {

				this.mcShowError("unknown");
				return false;
				
			}

		},
		
		postConsultRequest: function($params) {

			/* swap div with progress indicator */											
			this.$progress = new ProgressIndicator(this.$container);
											
			/* begin ajax post request */
			var $ajx = new Ajax.Request(
				'__contact/consultRequest.php',
					{
						method:'post',			
						parameters: $params,
						onFailure: function(response) {
							this.mcShowError("server");
						}.bind(this),
						onComplete: function(response) {						
							if (response.responseText === "1") {

								/* need google goal conversion here */
								this._triggerGoalCompletion();
	
								if (this.$optout==null) {
									this.maillistSubscribe($params);
									this.getMCInterestGroups();
								} else {
									this.showConsultResponse();
								}
								return true;
							} else if (response.responseText == 0) {
								this.mcShowError("server");
								return false;
							} else if (response.responseText == 'email') {
								this.mcShowError("email");
								return false;
							} else if (response.responseText == 'duplicate') {
								this.mcShowError("duplicate");
								return false;
							}
						}.bind(this)	
					}
				);
			/* end ajax post request */
			
		},
				
		killresponsewindow: function() {
			this.$ui = null;
		},
		
		createUI: function() {
			if (this.$ui == null) {
				var $left = 600;
				var $top = 300;
				var $width = 300;
				this.$ui = new Windowframe("",$left,$top,$width,"","",1,"","consultresponsewindow");
				/* EventSwitch.subscribe(this.$eventsID,"consultresponsewindow","kill",this.killresponsewindow,this); */
			}
			return this.$ui;
		},


		maillistSubscribe: function($params) {
			
			/* begin ajax post request */
			var $ajx = new Ajax.Request(
				'__contact/mcSubscribe.php',
					{
						method:'post',
						parameters: $params,
						onFailure: function(response) {
							this.mcShowError("server");
						}.bind(this),
						onComplete: function(response) {
							if (response.responseText === "1") {
								this.mcShowResponse();
								return true;
							} else if (response.responseText == 0) {
								this.mcShowError("server");
								return false;
							} else if (response.responseText == 'email') {
								this.mcShowError("email");
								return false;
							} else if (response.responseText == 'duplicate') {
								this.mcShowError("duplicate");
								return false;
							}
						}.bind(this)	
					}
				);

			/* end ajax post request */
			
		},

		updateSubscriberInterestGroups: function() {
				
			var $interests = Array();
			var $checkboxContainers = $('interestgroups').childElements();
			$checkboxContainers.each(
				function($cbc) {
					if ($cbc.firstDescendant().checked) {
						$interests.push("1");
					} else {
						$interests.push("0");
					}
				}.bind(this)
			);
						
			for (var $i=0; $i<$checkboxContainers.length; $i++) {
				this.$params["interest["+$i+"]"]=$interests[$i];
			}
			
				/* swap div with progress indicator */											
				this.$progress = new ProgressIndicator(this.$container);
			
				/* begin ajax post request */
				var $ajx = new Ajax.Request(
					'__contact/mcUpdateInterestGroups.php',
						{
							method:'post',			
							parameters: this.$params,
							onFailure: function(response) {
								this.mcShowError("server");
							}.bind(this),

							onComplete: function(response) {	
								trace("mailchimp interest group update complete");
								trace("response: " + response.responseText);						
								if (response.responseText === "1") {
									this.showInterestGroupResponse();
									return true;
								} else if (response.responseText == 0) {
									this.mcShowError("server");
									return false;
								} else if (response.responseText == 'email') {
									this.mcShowError("email");
									return false;
								} else if (response.responseText == 'duplicate') {
									this.mcShowError("duplicate");
									return false;
								}
							}.bind(this)	
						}
					);
				/* end ajax post request */
			
		},


		showConsultResponse: function() {
		
			/* consult request response */
			var $msg = "<h3 id='formhead'>Thank you for your request.</h3><p>We will contact you within one business day to schedule your consultation. If you have any questions, please feel free to call us any time.</p>\n";

			/* create submit button */
			var $close = document.createElement("INPUT");
			$close.type = "button";
			$close.id = "consultresponsecloser";
			$close.value = "close";
			
			var $tmp = $msg;
			this.$progress.replace($tmp);
			
			this.$container.insert({"bottom":$close});

			this.$consultresponsecloser = $("consultresponsecloser");
			
			Event.observe(this.$consultresponsecloser,"click",
				function() {
					this.kill();
				}.bindAsEventListener(this)
			);
		},
		
		showInterestGroupResponse: function() {
		
			/* consult request response */
			var $msg = "<h3 id='formhead'>Thank you for your request.</h3><p>We will contact you within one business day to schedule your consultation. If you have any questions, please feel free to call us any time.</p>\n";

			/* create submit button */
			var $close = document.createElement("INPUT");
			$close.type = "button";
			$close.id = "consultresponsecloser";
			$close.value = "close";
			
			trace("progress: " + this.$progress);
			trace("message: " + $msg);
			
			this.$progress.replace($msg);

			this.$container.insert({"bottom":$close});
			
			this.$consultresponsecloser = $("consultresponsecloser");
			
			Event.observe(this.$consultresponsecloser,"click",
				function() {
					this.kill();
				}.bindAsEventListener(this)
			);
		},
		
		showMCInterestGroupForm: function() {
				
			var $msg = "<h3 id='formhead'>Thank you for your request.</h3><p>We will contact you within one business day to schedule your consultation. If you're interested in any specific treatments, please check the boxes below and click submit.</p>\n";
		
			/* create submit button */
			var $submit = document.createElement("INPUT");
			$submit.type = "button";
			$submit.id = "interestgroupsubmit";
			$submit.value = "submit";
			
			var $cancel = document.createElement("INPUT");
			$cancel.type = "button";
			$cancel.id = "interestgroupcancel";
			$cancel.value = "cancel";
			
			var $buttons = "<div id=\"interestgroupformbuttons\">\n";
			$buttons += "<input type=\"button\" id=\"interestgroupcancel\" value=\"cancel\">\n";
			$buttons += "<input type=\"button\" id=\"interestgroupsubmit\" value=\"submit\">\n";
			$buttons += "</div>\n";
		
			$tmp = $msg+"<div id='interestgroups'>"+this.$interestgroups+"</div>"+$buttons;
		
			this.$progress.replace($tmp);
			
			this.$consultresponsecloser = $("interestgroupcancel");
			this.$interestgroupsubmit = $("interestgroupsubmit");

			Event.observe(this.$consultresponsecloser,"click",
				function() {
					this.kill();
				}.bindAsEventListener(this)
			);
			
			Event.observe(this.$interestgroupsubmit, "click", this.updateSubscriberInterestGroups.bindAsEventListener(this));

		},
		
		getMCInterestGroups: function() {

			/* may not need this? */
			this.$interests = Array();

			/* begin ajax post request */
			var $ajx = new Ajax.Request(
				'__contact/mcGetInterestGroups.php',
					{
						method:'get',
						onFailure: function(response) {
							trace("ajax request failed");
							trace(enumerate(response));
						},
						onComplete: function(response) {
							this.$interestgroups = response.responseText;
							this.showMCInterestGroupForm();
						}.bind(this)	
					}
				);
			/* end ajax post request */
		},


		mcShowResponse: function() {
			trace("mcShowResponse");		
			var $response = "\
				<h3 id='formhead'>Thanks for your interest</h3>\n \
				<p>You should receive a confirmation email shortly. Please click on the included link to confirm your consultation request.</p>\n \
				<input type='button' id='responseCloseButton' value='Done' />";
			this.$form.update($response);
			this.observeResponse();
		},
				
		observeResponse: function() {
			Event.observe($("responseCloseButton"),"click",this.mcHideForm.bindAsEventListener(this));
		},
		
		mcShowError: function($err) {
		
			return false;
		
			if (!this.$status) {
				this.$status
				 = new Windowframe("",150,150,250,"","",1,"");
			}
		
			var $response = "<h3 id='errorhead'>FYI</h3>";
			
			if ($err == "email") {
				$response += "<p>Your email address didn&#8217;t check out. Can you check it and try again?</p>";
			}
			
			if ($err == "server") {
				$response += "<p>We&#8217;re sorry, but our server isn&#8217;t currently able to process your subscription. You can try again later, or give us a call and we&#8217;ll be happy to add you to our list.</p>";
			}
			
			if ($err == "duplicate") {
				$response += "<p>It looks like you're already on our list. You should be receiving information on specials from us once or twice a month.";
			}
			
			$response += "<input type='button' id='responseCloseButton' value='Close' />";
			
			this.$container.insert({"bottom":$response});
			
			

			this.observeResponse();

		},
		
		mcHideForm: function(e) {
			e.stop();
			// this.$form.kill();
			return false;
		},
		
		mcGetInterestGroupFields: function() {
			var $interests = Array();
			var $checkboxContainers = $('interestgroups').childElements();			
			$checkboxContainers.each(
				function($cbc) {
					if ($cbc.firstDescendant().checked) {
						$interests.push(1);
					} else {
						$interests.push(0);
					}
				}.bind(this)
			);
			
			return $interests;
		},
		


		mcValidate: function(e) {

			e.stop();

			this.$interests = this.mcGetInterestGroupFields();
	
			this.$phone = Array();
			this.$error = 0;
		
			this.$email = $F("MERGE0");
			this.$firstname = $F("MERGE1");
			this.$lastname = $F("MERGE2");
			this.$phone[0] = $F("MERGE4a");
			this.$phone[1] = $F("MERGE4b");
			this.$phone[2] = $F("MERGE4c");
			
			if (!checkEmail(this.$email,0)) {
				this.$error += 1;
			}
			
			if (this.$firstname.length < 1) {
				this.$error += 2;
			}
			
			if (this.$lastname.length < 1) {
				this.$error += 4;
			}
	
			if (this.$error == 0) {
			
				var $params = {
					"MERGE0":this.$email,
					"MERGE1":this.$firstname,
					"MERGE2":this.$lastname,
					"MERGE4[0]":this.$phone[0],
					"MERGE4[1]":this.$phone[1],
					"MERGE4[2]":this.$phone[2]
				}
				
				for ($i in this.$interests) {
					$params["interest["+$i+"]"]=this.$interests[$i];
				}
				
				/* begin ajax post request */
				var $ajx = new Ajax.Request(
					'__contact/mcFormMailer.php',
						{
							method:'post',			
							parameters: $params,
							onFailure: function(response) {
								this.mcShowError("server");
							}.bind(this),
							onComplete: function(response) {
								if (response.responseText === "1") {
									this.mcShowResponse();
									return true;
								} else if (response.responseText == 0) {
									this.mcShowError("server");
									return false;
								} else if (response.responseText == 'email') {
									this.mcShowError("email");
									return false;
								} else if (response.responseText == 'duplicate') {
									this.mcShowError("duplicate");
									return false;
								}
							}.bind(this)	
						}
					);
				/* end ajax post request */
			
			
			} else {
				this.mcShowError("unknown");
			}
	
		},
		
		kill: function() {
		
			$tmp = "<h3 class='formhead'>Thanks for your interest! Someone will contact you shortly.<h3>";
		
			this.$container.update($tmp);
		
			// this.$container.remove();
			
			
		
		}
	
	}
	
/* END */

Event.observe(window,"load",function() {
		$splitrouter = new splitRouter();
	}
);
