	
	var mcFormMailer = Class.create();
	
	mcFormMailer.prototype = {
	
		classname: "mcFormMailer",
				
		initialize: function($format) {
		
			if ($format != "undefined") {
				this.$format = $format;
			} else {
				this.$format = "static";
			}
		
			this.$interests = Array();
			
			if (this.$format == "float") {
				
				this.$origin = $(e.target).cumulativeOffset();
	
				/* load the mc scripts */
				this.mcLoadScripts();
				
				/* show the mc form */
				this.mcShowForm();
				
			} else {

				this.$form = $("mc_embed_signup");
				this.mcAddObserver();
			
			}

		},
		
		mcLoadScripts: function() {
			trace("mcLoadScripts");
			loadStyle("mailchimp/style.css");
			return;
		},
		
		mcShowForm: function() {
			trace("mcShowForm");
			var $top = this.$origin["top"];
			var $left = this.$origin["left"];		
			this.$form = new Windowframe("",$left,$top,250,"","",1,"");
		
			/* begin ajax post request */
			var $ajx = new Ajax.Request(
				'mailchimp/form.php',
					{
						method:'get',
						onFailure: function(response) {
							trace("ajax request failed");
							trace(enumerate(response));
						},
						onComplete: function(response) {
							this.$form._update(response.responseText);							
							this.mcAddObserver();
						}.bind(this)	
					}
				);
			/* end ajax post request */
		},
		
		mcAddObserver: function() {
			Event.observe($("mcSubscribe"),"click",this.mcValidate.bindAsEventListener(this));
		},
		
		mcShowResponse: function() {
			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 subscription to our mailing list.</p>\n \
				<input type='button' id='responseCloseButton' value='Done' />";
			this.$form.update($response);
			try {
				doLeadConversion();
			} catch(e) {
				trace("ERROR calling doUDRTrigger at mcFormMailer.js line 61");
				trace(e);
				return false;
			}
			this.observeResponse();
		},
				
		observeResponse: function() {
			Event.observe($("responseCloseButton"),"click",this.mcHideForm.bindAsEventListener(this));
		},
		
		mcShowError: function($err) {
			var $response = "<h3 id='errorhead'>Oops! There&#8217;s a problem!</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.$form._update($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(
					'mailchimp/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");
			}

		}		
	
	}
	
/* END */

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