
	var Estimator = Class.create();
	
	Estimator.prototype = {
		
		classname: "Estimator",
		
		initialize: function() {		
		},
		
		
		showResponse: function() {
		
			this.$content.update("<div id=\"estimate\"><h1>Request Received</h1><p>Our Move Coordinators have received your request. You should receive an email confirmation including your estimate and contact information. You will be contacted as requested on your submission, but please feel free to call us if you have questions prior to that time. Thank you for allowing us the opportunity to assist in your move.</p></div>");
		
		},
		

		submitConfirmation: function() {
		
			if (checkForm("estimateConfirmation")) {
				
				$params = {
				
					'email':$F('email'),
					'firstname':$F('firstname'),
					'lastname':$F('lastname'),
					'phone':$F('phoneinput'),
					'fromcity':$F('fromcity'),
					'fromstate':$F('fromstate'),
					'tocity':$F('tocity'),
					'tostate':$F('tostate'),
					'date1':$F('date1'),
					'ttc':$F('ttc'),
 					'estNumberOfMen':this.$men,
 					'estNumberOfHours':this.$hours
				
				};
				
				var $xx = 

					new Ajax.Request(
				
						'../script/confirmEstimateRequest.php',
					
							{
										
								method:'post',
										
								parameters: $params,
											
								onFailure: function(response) {
								}.bind(this),
				
								onComplete: function(response) {
									this.showResponse();			
								}.bind(this)
							}
							
						);
			
			} else {
				// trace("form not completed");
			}
			
			return false;

		},	
		

		showConfirmation: function() {

			this.$content = $("ltcol");
						
			this.$confirmationForm = "\
<div id=\"estimate\"><h1>Book It</h1>"+this.$estimateSummary+" \
	<br/><div id='confirmationFormContainer'>\
		<form id=\"estimateConfirmation\" name=\"estimateConfirmation\" onsubmit=\"return checkForm('estimate');\" action=\"cms/script/estimateRequest.php\" method=\"post\"> \
			<div id='formleft'> \
				<p><b>Please fill out this form to confirm your estimate. All fields are required. After completing this form, you will be contacted by one of our Move Coordinators who will schedule your move, and help with any additional needs.</b></p>\
			</div> \
			<div id='formcenter'> \
				<p class=\"formlabel\">First Name<br/> \
				<input type=\"text\" id=\"firstname\" name=\"firstname\" value=\"First\" onfocus=\"this.value='';\" /> \
				</p> \
				<p class=\"formlabel\">Last Name<br/> \
				<input type=\"text\" id=\"lastname\" name=\"lastname\" value=\"Last\" onfocus=\"this.value='';\"  /> \
				</p> \
				<p class=\"formlabel\">Phone<br/> \
				<input type=\"text\" id=\"phoneinput\"  name=\"phone\" value=\"phone\" onfocus=\"this.value='';\"  /> \
				<p class=\"formlabel\">E-mail<br/> \
				<input type=\"text\" id=\"email\"  name=\"email\" value=\"E-mail\" onfocus=\"this.value='';\"  /> \
				<p class=\"formlabel\">Best time to contact <br/> \
				<select id=\"ttc\" name=\"ttc\" > \
				<option value=\"not_selected\">Please select an option</option> \
				<option value=\"morning\">Morning</option> \
				<option value=\"afternoon\">Afternoon</option> \
				<option value=\"evening\">Evening</option> \
				<option value=\"email\">Email only</option> \
				</select> \
				</p> \
			</div> \
			<div id='formright'> \
				<p class=\"formlabel\">Moving from<br/> \
				<input type=\"text\" id=\"fromcity\" name=\"fromcity\" value=\"City\" onfocus=\"this.value='';\"  /> \
				<input type=\"text\" id=\"fromstate\" name=\"fromstate\" value=\"State\" onfocus=\"this.value='';\"  /> \
				</p> \
				<p class=\"formlabel\">Moving to<br/> \
				<input type=\"text\" id=\"tocity\"  name=\"tocity\" value=\"City\" onfocus=\"this.value='';\" /> \
				<input type=\"text\" id=\"tostate\" name=\"tostate\" value=\"State\" onfocus=\"this.value='';\" /> \
				</p> \
				<p class=\"formlabel\">Move date <br/> \
				<input type=\"text\" id=\"date1\"  name=\"date1\" value=\"\" onfocus=\"showCal();\"  /> \
				<a href=\"#\" onClick=\"cal.select(document.forms['estimateConfirmation'].date1,'anchor1','MM/dd/yyyy'); return false;\" name=\"anchor1\" id=\"anchor1\"><br/>select date</a> \
				</p> \
				<p><br/>\
			<input type=\"button\" id=\"confirmEstButton\" value=\"submit my request\" /> \
				</p> \
			</div> \
			<div class='clear'> \
			</div> \
		</form> \
	</div>\
</div>";
			
			this.$content.update(this.$confirmationForm);
			
			this.$confEstButton = $("confirmEstButton");
			
			Event.observe(this.$confEstButton,"click",this.submitConfirmation.bindAsEventListener(this));
			
		},
		
		checkQuickForm: function(e) {
			this.$men = $F("estNumberOfMen");
			this.$hours = $F("estNumberOfHours");			
			if (this.$men && this.$hours) {
				this.submitQuickForm();
			} else {
				this.showQuickFormHelp();
				return false;
			}
			return false;
		},
		
		showQuickFormHelp: function() {
			alert("Please select both number of men and number of hours before submitting. If you're not sure how to answer these questions, please click the \"I Need Help\" button below the form.");
		},
		
		submitQuickForm: function() {
		
			$params = {"form":"1","estNumberOfMen":this.$men,"estNumberOfHours":this.$hours};
			
			var $xx = new Ajax.Request(

				"../script/estimator_v3.php",
			
					{
								
						method:'post',
								
						parameters: $params,
								
						onFailure: function(response) {
							// trace("request failed");
							return false;
						}.bind(this),
			
						onComplete: function(response) {
							// trace("ajax request complete");
							if (response.status >= 300) {
								this.generateServerError();
							} else {
								this.showEstimate(response.responseText);
							}
						}.bind(this)
			
					}
			
				);
			
			/* end ajax post request */

			
			return false;
		},
		
		generateServerError: function() {
			// trace("serverError called");
		},
		
		showEstimate: function($est) {
		
			if (this.$men == 1) {
				$menz = "man";
			} else {
				$menz = "men";
			}
			
			if (this.$hours == 1) {
				$hourz = "hour";
			} else {
				$hourz = "hours";
			}
			
			this.$estimateSummary = "<p>Your estimated cost, based on "+this.$men+" "+$menz+" for "+this.$hours+" "+$hourz+" is &nbsp;<span class='price'>$"+$est+" *</span></p>";
			
			this.$disclaimer = "<p id=\"disclaimer\">*This estimate is for moves within the greater Dallas&nbsp;/&nbsp;Ft.&nbsp;Worth&nbsp;Metroplex. However, Albert&nbsp;Moving provides moving services across the US and around the world. For moves outside of Dallas&nbsp;/&nbsp;Ft.&nbsp;Worth, please call us at 866.419.6170</p>";
		
			this.$estimate = "<div id=\"estimate\"><h1>Your Estimate</h1>"+this.$estimateSummary+" \
<img src='img/guarantee_small.png' style='float:right; width:145px; margin-left:10px;' /><h2>We&#8217;ve got you covered</h2>\
<p>Our estimates include <b>replacement cost coverage</b> for your household goods, and we go the extra mile to make sure you never have to use it. Please <b>call us with any questions</b> about your estimate, moving preparation, or other assistance.</p><br/>\
	<div id='estimateNextSteps1'>\
		<form id='emailRequest'>\
			<input type='button' id='emailRequestButton' value='Send My Estimate' />\
			<input type='text' id='emailRequestAddress' value='enter your email address' onfocus='this.value=\"\";'></input><br/>\
		</form>\
	</div>\
\
	<div id='estimateNextSteps2'>\
		<form id='printRequest'>\
			<input type='button' onclick='window.print();' value='Print My Estimate' />\
			<p>&nbsp;<br/><br/></p> \
		</form>\
	</div>\
\
	<div id='estimateNextSteps3'>\
		<form id='confirmRequest'>\
			<input type='button' id='confirmRequestButton' value='Book It' />\
			<p>To schedule your move.</p>\
		</form>\
	</div>\
	</div><div class=\"clear\"></div>"+this.$disclaimer;
			
			this.$contentBackup = $("ltcol").update(this.$estimate);

			this.$emailRequestButton = $("emailRequestButton");
			Event.observe(this.$emailRequestButton, "click", this.sendEstimateEmail.bindAsEventListener(this));

			this.$confirmRequestButton = $("confirmRequestButton");
			Event.observe(this.$confirmRequestButton, "click", this.showConfirmation.bindAsEventListener(this));
		
		},
		
		showEmailConfirmation: function($address) {
		
			$toswap = $("estimateNextSteps1");
			
			$text = "<h3>Estimate Sent</h3>\
			<p>This estimate has been sent to "+$address+". To confirm your move or speak to \
			one of our move coordinators, please click the 'Book It' button to the right of your screen.</p>";
			
			$toswap.update($text);
		
		},
		
		sendEstimateEmail: function(e) {

			$email = $F("emailRequestAddress");

			if (this.checkEmail($email) == true) {
			
				$params = {"email":$email, "estNumberOfMen":this.$men, "estNumberOfHours":this.$hours};
			
				this.$request = new Ajax.Request(
	
					'script/sendEstimateRequest.php',
				
						{

							method:'post',
									
							parameters: $params,
									
							onFailure: function(response) {
							},
				
							onSuccess: function(response) {
							},
															
							onComplete: function(response) {
								this.showEmailConfirmation($email);
							}.bind(this)
				
						}
				
					);
				
			} else {
				// trace("email check failed - need an alert for this");
			}
		},
		
		setEventHandlers: function() {
		},
		
		createHelpInterface: function() {
			var $tmp = "<div id='formhelpcontainer'>\
			<div id='formhelpui'> \
			<form id='formhelpform' method='post'>\
			<h3>Estimate Assistance</h3>\
			<p id='formhelpintro'>Please answer the following questions below. Using your answers, we can calculate the man hours necessary to complete your move, based on national averages.</p>\
			<div id='helpformleft'>\
			\
			<p class='formlabel'>\
			What type of residence are you moving from?<br/>\
			<select id='residencefrom' name='residencefrom' >\
				<option value='0' selected >Select an option</option>\
				<option value='1'>Efficiency</option>\
				<option value='2'>Studio</option>\
				<option value='3'>1 BR Apartment</option> \
				<option value='4'>2 BR Apartment</option> \
				<option value='5'>2 BR House</option> \
				<option value='6'>3 BR Apartment</option> \
				<option value='7'>3 BR House</option> \
				<option value='8'>4 BR House</option> \
				<option value='9'>5 BR House</option> \
				<option value='10'>&gt; 5 BR House</option> \
		   </select> \
		   </p> \
		   \
		   <p class='formlabel'>How many people are in your household?<br/> \
			<select id='householdmembers' name='householdmembers'> \
				<option value='0' selected >Select an option</option> \
				<option value='1'>1</option> \
				<option value='2'>2</option> \
				<option value='3'>3</option> \
				<option value='4'>4</option> \
				<option value='5'>5</option> \
				<option value='6'>6</option> \
				<option value='7'>7</option> \
				<option value='8'>8</option> \
				<option value='9'>9</option> \
				<option value='10'>10</option> \
				<option value='11'>&gt; 10</option> \
		   </select> \
		   </p> \
		   \
		   <p class='formlabel'>\
		   How long have you been at your current residence?</span><br/> \
				<select id='yearsinres' name='yearsinres'> \
					<option value='0' selected>Select an option</option> \
					<option value='1'>&lt; 1</option> \
					<option value='2'>1 - 3</option> \
					<option value='3'>3 - 5</option> \
					<option value='4'>5 - 7</option> \
					<option value='5'>7 - 10</option> \
					<option value='6'>&gt; 10</option> \
			   </select> \
		   </p> \
		   </div>\
		   <div id='helpformright'>\
		   <p class='formlabel'> \
		   How many appliances need to be moved?<br/> \
				<select id='appliances' name='appliances'> \
					<option value='' selected >Select an option</option> \
					<option value='0'>0</option> \
					<option value='1'>1</option> \
					<option value='2'>2</option> \
					<option value='3'>3</option> \
					<option value='4'>4</option> \
					<option value='5'>5</option> \
					<option value='6'>6+</option> \
				</select> \
		   </p> \
			\
		   <p class='formlabel'>What&#8217;s the size of your storage space?<br/> \
				<select id='storage' name='storage'> \
					<option value='0' selected >Select an option</option> \
					<option value='1' >none</option> \
					<option value='2'>&lt;50 sq ft (5ft x 10ft)</option> \
					<option value='3'>50 - 100 sq ft (10ft x 10ft)</option> \
					<option value='4'>100 - 150 sq ft (10ft x 15ft)</option> \
					<option value='5'>150 - 200 sq ft (10ft x 20ft)</option> \
					<option value='6'>&gt;200 sq ft (10ft x 20ft)</option> \
				</select> \
		 </p> \
		 \
		 <p><br/></p> \
		 <p> \
 		 <input type='button' id='formhelpsubmit' value='Get My Estimate'> \
		 </p> \
		 </div> \
		 <div class='clear'></div> \
		 </form> \
		 </div> \
		 </div> \
		 \
		 ";
		 
		 $body = $(document.body);
		 
		 $target = $body.firstDescendant();
		 
		 $target.insert($tmp);
		 
		 this.$ui = $("formhelpcontainer");
		 
		 $tmp = "<div id='topbar'><img id='closebox' src='templates/landing/img/topbar_win_closer_up.jpg' /></div>";
		 this.$ui.insert({"top":$tmp});
		 
		 this.$closebox = $("closebox");
		 
		 Event.observe(this.$closebox,"click",this.removeHelpInterface.bindAsEventListener(this));
		 
		 new Draggable(this.$ui);
		 
		 this.$submit = $("formhelpsubmit");
		 Event.observe(this.$submit,"click",this.checkHelpForm.bindAsEventListener(this));
		 
		 return false;
		 
		},
		
		
		showHelpFormAlert: function() {
			alert("Please enter all of the information requested, or close the Estimate Assistance form and use the Quick Estimate form at bottom right.");
		},


		checkHelpForm: function() {
		
			var $residence = $F('residencefrom');
			var $householdmembers = $F('householdmembers');
			var $yearsinres = $F('yearsinres');
			var $appliances = $F('appliances');
			var $storage = $F('storage');
			
			if (
				$residence == 0 ||
				$householdmembers == 0 ||
				$yearsinres == 0 ||
				$appliances == "" ||
				$storage == 0
			) {
				this.showHelpFormAlert();
				return false;
			}
			
			var $params = {
				'residence':$residence,
				'householdmembers':$householdmembers,
				'yearsinres':$yearsinres,
				'appliances':$appliances,
				'storage':$storage
			};
			
			
			this.$getHelp = new Ajax.Request(

				'script/estimateHelp.php',
			
					{
								
						method:'post',
								
						parameters: $params,
								
						onFailure: function(response) {
						},
			
						onSuccess: function(response) {
						},
														
						onComplete: function(response) {
							this.$ui.remove();
							$response = response.responseText.split(",");
							this.$men = $response[0];
							this.$hours = $response[1];
							this.submitQuickForm();
						}.bind(this)
			
					}
			
			);
			
		},


		removeHelpInterface: function() {
			this.$ui.remove();
		},
		
		checkEmail: function(address) {					
			if (address != '') {
				if (address.length > 6  &&  address.indexOf('@') != -1 && address.indexOf('.') != -1 && address.indexOf('@') < address.indexOf('.') && address.indexOf('@') > 0 && address.indexOf('.') > 2 ) {
					/* begin additional checks here */
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} // end method

	} // end class definition
