var ContactForm = {
	Init:function() {
		ContactForm.WriteFlashObjs();
		ContactForm.SetupForm();
		
		// move the footer below the lowest element in the page:
		var footerWidth = $('footer').getStyle('width').toInt();
		var textAreaBottom = $$('textarea')[0].getCoordinates().bottom.toInt() + 60;		
		$('footer').setStyles({
			'position':'absolute',
			'top':textAreaBottom,
			'left':'50%',
			'margin-left':'0px'
		});
	},
	WriteFlashObjs:function() {
	},
	SetupForm:function() {
		var inputs = $$('input');
		var textareas = $$('textarea');
		
		inputs.each(function(input) {
			if (input.getProperty('type') == 'text') {
				input.addEvents({
					'mouseenter':function() {
						if (!input.hasClass('required')) {
							input.setStyle('border', '1px solid #9999FF');
						} else {
							input.setStyles({
								'border':'1px solid #9999FF',
								'border-right':'2px solid #FF0000'
							});
						}
					},
					'mouseleave':function() {
						if (!input.hasClass('required')) {
							input.setStyle('border', '1px solid #999');
						} else {
							input.setStyles({
								'border':'1px solid #999',
								'border-right':'2px solid #FF0000'
							});
						}
					}
				});
			}
		});
		
		textareas.each(function(textarea) {
			textarea.addEvents({
				'mouseenter':function() {
					textarea.setStyle('border', '1px solid #9999FF');
				},
				'mouseleave':function() {
					textarea.setStyle('border', '1px solid #999');
				}
			});	
		});
	},
	SetupMask:function() {
		var maskHeight = '490px';
		var maskWidth = '776px';
		var mask = new Element('div', {
			'styles':{
				'position':'absolute',
				'top':$('middleRow').getCoordinates().bottom.toInt() + 3,
				'left':$('secondaryNavBar').getLeft(),
				'height':maskHeight,
				'width':maskWidth,
				'opacity':'0',
				'background-color':'#181818'
			},
			'class':'isLoading',
			'id':'mask'
		});
		
		$$('body')[0].adopt(mask);
		
		var showMask = new Fx.Style(mask, 'opacity', {
			duration:400,
			transition:Fx.Transitions.Quad.easeOut,
			wait:false
		}).start(mask.getStyle('opacity'), '.75');
	},
	MoveMask:function() {
		if (!window.ie) {
			if ($('mask')) {
				var fx = new Fx.Style('mask', 'left', {
					duration:250,
					wait:false
				}).start($('frm_contactForm').getLeft());
			}
		} else {
			if ($('mask')) {
				$('mask').setStyle('left', $('frm_contactForm').getLeft());
			}
		}
	},
	SubmitForm:function() {
		if (ContactForm.Validate()) {
			var chkBoxes = $$('input[type=checkbox]');
			var inputs = $$('input[type=text]');
			var selects = $$('select');
			var textareas = $$('textarea');
			var postData = '';			
			var constructPostData = function() {
				$$(chkBoxes).each(function(el, i) {
					if (postData.length < 1) {
						postData = el.getProperty('id') + '=' + el.checked;
					} else {
						postData += el.getProperty('id') + '=' + el.checked;
					}
					
					if (i < chkBoxes.length - 1) {				
						postData += '&';
					}	
				});
				
				$$(inputs).each(function(el, i) {
					if (i == 0) postData += '&'
					
					postData += el.getProperty('id') + '=' + el.getValue();
					
					if (i < inputs.length - 1) {
						postData += '&';
					}
				});
				
				$$(selects).each(function(el, i) {
					if (i == 0) postData += '&';
					
					postData += el.getProperty('id') + '=' + el.getValue();
					
					if (i < selects.length - 1) {
						postData += '&';
					}
				});
				
				$$(textareas).each(function(el, i) {
					if (i == 0) postData += '&';
					
					postData += el.getProperty('id') + '=' + el.getValue();
					
					if (i < textareas.length - 1) {
						postData += '&';
					}
				});
				
				return postData;
			};
			var postData = constructPostData();
			//var submitForm = new Ajax('./submitContactForm.php', {
			var submitForm = new Ajax('./processContactForm.php', {
				method:'post',
				data:postData,
				async:true,
				onRequest:function() {
					ContactForm.SetupMask();
				},
				onSuccess:function(responseText, responseXML) {
					if (!responseText.test('failed')) {
						var hideMask = new Fx.Style('mask', 'opacity', {
							duration:400,
							transition:Fx.Transitions.Quad.easeOut,
							wait:false,
							onComplete:function() {
								$('mask').remove();
								
								if (!$('mask')) {
									alert('Thank you! Your responses were received!');
								}
								
								$('frm_contactForm').reset();
							}
						});
						
						hideMask.start.delay(600, hideMask, [.75, 0]);
					} else {
						$('mask').remove();						
						alert('Some of your form input contains characters that could be construed as malicious code or simply did not pass our validator.\nPlease re-enter the data in this form and try again.');
						// $('frm_contactForm').reset();
					}
				}
			}).request();			
		} else {
			alert('You must enter data in the required fields\nbefore submitting this form.');
		}
	},
	Validate:function() {
		var noFail = true;
		var requiredFields = $$('.required');
		requiredFields.each(function(el) {
			if (el.getValue() == '') {
				el.setStyle('background-color', '#FF3333');
				noFail = false;
			} else {
				if (el.getStyle('background-color') != '#FFFFFF') {
					el.setStyle('background-color', '#FFFFFF');
				}
			}
		});		
		
		return noFail;
	}
};

window.addEvent('resize', ContactForm.MoveMask);
