	var DCAjaxClass = new Class({

		initialize: function() {
 			window.addEvent( "domready", function(){ this.load(); }.bind(this) );
		},

		load: function() {

			// Used to manage errors when checking the form:
			this.num_errors = 0;

			this.response = "";
		},

		// ****************************
		add_error: function(input_id, err_string) {

			obj = document.getElementById(input_id+'_err');
			if (obj.innerHTML == '') {
				this.num_errors++;
			}
			obj.innerHTML = err_string;
			obj.style.display = 'block';
		},

		// ****************************
		remove_error: function(input_id) {

			obj = document.getElementById(input_id+'_err');
			if (obj != null && obj.innerHTML != '') {
				obj.style.display = 'none';
				obj.innerHTML = '';
				this.num_errors--;
				if (this.num_errors < 0) { this.num_errors = 0; }
			}
		},

		// ****************************
		check_form: function() {

			//cleanup
			obj = document.getElementById('submit_result');
			obj.style.display = 'none';
			obj.innerHTML = '';

			f = document.search_form;
			//firstname
			if (!f.firstName.value.length)			{ this.add_error('group', 'First Name required.'); }
			else if (f.firstName.value.length > 30) { this.add_error('group', 'Only max 30 letters for First Name.'); }
			else { this.remove_error('group'); }

			//lastname
			if (this.num_errors < 1) {
				if (!f.lastName.value.length)			{ this.add_error('group', 'Last Name required.'); }
				else if (f.lastName.value.length > 30)	{ this.add_error('group', 'Only max 30 letters for Last Name.'); }
				else { this.remove_error('group'); }
			}

			//phone
			if (this.num_errors < 1) {
				if (f.phone.value.length < 10)			{ this.add_error('group', 'Phone xxx-yyy-zzzz required.'); }
				else if (f.phone.value.length > 14)		{ this.add_error('group', 'Reformat Phone to xxx-yyy-zzzz'); }
				else { this.remove_error('group'); }
			}

			//email
			if (this.num_errors < 1) {
				if (!f.email.value.length) 			 { this.add_error('group', 'Email address required.'); }
				else if (f.email.value.length > 100) { this.add_error('group', 'Only max 100 characters for Email.'); }
				else if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(f.email.value) ) { this.add_error('group', 'Enter a valid email.'); }
				else { this.remove_error('group'); }
			}

			//zip
			if (this.num_errors < 1) {
				if (f.zip.value.length < 5)			{ this.add_error('group', 'Use valid zip xxxxx or xxxxx-xxxx'); }
				else if (f.zip.value.length > 10)	{ this.add_error('group', 'Use valid zip xxxxx or xxxxx-xxxx'); }
				else { this.remove_error('group'); }
			}

			if (this.num_errors < 1) {
				document.search_form.button_submit.disabled = true;
				this.submit_offer();
			}
		},

		// ****************************
		submit_offer: function() 
		{

			//display processing
			obj = document.getElementById('submit_result');
			obj.innerHTML = '...Sending your Information...';
			obj.style.display = 'block';

//obj.innerHTML = 'Test #0.1';

			var urlString = "ajax.php?action=submit_offer";
			urlString += "&make_id="+$('vehicle_make').value;
			urlString += "&model_id="+$('vehicle_model').value;
			urlString += "&year_id="+$('vehicle_year').value;
			urlString += "&first_name="+document.search_form.firstName.value;
			urlString += "&last_name="+document.search_form.lastName.value;
			urlString += "&phone="+document.search_form.phone.value;
			urlString += "&email="+document.search_form.email.value;
//			urlString += "&comment="+$('comment').value;
			urlString += "&comment="+document.search_form.comment.value;//		<--- Bad
			urlString += "&zip="+document.search_form.zip.value;
			urlString += "&voucher_link="+document.search_form.voucher_link.value;
			urlString += "&domain="+document.search_form.domain.value;

//obj.innerHTML = 'Test #0.2';

//return;
			// Send the offer to the server (and then dealer)
			var ajaxRequest = null;
			ajaxRequest = new Ajax(urlString,
			{
				method: 'get',
				onComplete: function(transport)
					{
//obj.innerHTML = 'Test #1';
						if (false == transport) 
						{
//obj.innerHTML = 'Test #2';
							alert("No response from the server.");
						} 
						else if( transport.substring(0,5) == 'goto:' )
						{
//obj.innerHTML = 'Test #3';
                            window.location=transport.substring(5);

						}
						else 
						{
//obj.innerHTML = 'Test #4';

							this.response = transport;
    						obj.innerHTML = (this.response) ? this.response : '';
    						
						    if (this.response && (this.response == 'Your offer has been submitted.')) 
						    {
							    // Clear the form fields:
							    document.search_form.firstName.value = '';
							    document.search_form.lastName.value = '';
							    document.search_form.phone.value = '';
							    document.search_form.email.value = '';
							    document.search_form.zip.value = '';
						    }
						    document.search_form.button_submit.disabled = false;
						}


					}.bind(this),
				onFailure: function()
					{
//obj.innerHTML = 'Test #5';
						document.search_form.button_submit.disabled = false;
						alert('Error submitting offer.');
					}
			});

//obj.innerHTML = 'Test #0.3';
			ajaxRequest.request();
//obj.innerHTML = 'Test #0.4';

		},

		// ****************************
		update_make: function(e) {
			var urlString = "ajax.php?action=update_make";
			urlString += "&year_id="+$('vehicle_year').value;

			// Send the offer to the server (and then dealer)
			var ajaxRequest = null;
			ajaxRequest = new Ajax(urlString,
			{
				method: 'get',
				onComplete: function(transport)
					{
						if (false == transport) {
							alert("No response from the server.");
						} else {
							this.response = transport;

							if (this.response && (this.response != '')) {
								$('make_select').setHTML('');
								$('model_select').setHTML('');

								$('make_select').setHTML(this.response);
								$('model_select').setHTML('<select id="vehicle_model" name="vehicle_model"></select>');
							}
						}
					}.bind(this),
				onFailure: function()
					{
						alert('Error submitting offer.');
					}
			});

			ajaxRequest.request();
		},

		// ****************************
		update_model: function(e) {
			var urlString = "ajax.php?action=update_model";
			urlString += "&year_id="+$('vehicle_year').value;
			urlString += "&make_id="+$('vehicle_make').value;

			// Send the offer to the server (and then dealer)
			var ajaxRequest = null;
			ajaxRequest = new Ajax(urlString,
			{
				method: 'get',
				onComplete: function(transport)
					{
						if (false == transport) {
							alert("No response from the server.");
						} else {
							this.response = transport;

							if (this.response && (this.response != '')) {
								$('model_select').setHTML('');
								$('model_select').setHTML(this.response);
							}
						}
					}.bind(this),
				onFailure: function()
					{
						alert('Error submitting offer.');
					}
			});

			ajaxRequest.request();
		}

	});

