function insertClearDiv()
{
	var html_break = new Element('div', {
		'class': 'clear',
		'styles': {
			'height': '5px'
		}
	});
	return html_break;
}

function swapdivs()
{
	var container = $('mail_table');
	var successDiv = $('successDiv');
	if(successDiv.getStyle('display') == 'none')
	{
		container.setStyle('display','none');
		successDiv.setStyle('display','block');
	}
	else
	{
		$('mail_form').reset();
		successDiv.setStyle('display','none');
		container.setStyle('display','block');
	}
}

function contactForm(form)
{
	var error = false;
	switch(form)
	{
		case 'Contact':
		case 'Neighborhood Filming':
			var postJSON = JSON.encode({
				'f_name:alpha': $('f_name:alpha').value,
				'l_name:alpha': $('l_name:alpha').value,
				'email:email': $('email:email').value,
				'phone:us_phone': $('phone:us_phone').value,
				'company:safe_text': $('company:safe_text').value,
				'subject:safe_text': $('subject:safe_text').value,
				'memo:safe_text': $('memo:safe_text').value
			});
		break;
		case 'Work':
			var postJSON = JSON.encode({
				'f_name:alpha': $('f_name:alpha').value,
				'l_name:alpha': $('l_name:alpha').value,
				'email:email': $('email:email').value,
				'phone:us_phone': $('phone:us_phone').value,
				'position:safe_text': $('position:safe_text').value,
				'resume:safe_text': $('resume_file:safe_text').value,
				'memo:safe_text': $('memo:safe_text').value,
				'policy_agreement:boolean': $('policy_agreement:boolean').checked
			});
			//	'resume:safe_text': $('resume_file:safe_text').value,
		break;
	}

	var jsonRequest = new Request.JSON({
		url: '/services/json/swiftMail.php',
		onComplete: function(response){
			var invalidColor = '#FAA';
			var invalidText = 'Provided data is invalid.';
			var requiredColor = '#FFA';
			var requiredText = 'Required data is missing.';
			var successText = 'Thank you for contacting FilmL.A. Inc.<br /><a href="javascript:swapdivs()">Send another</a>';
			var container = 'mail_form';
			var table = 'mail_table';

			if (typeof(response.invalid) != 'undefined')
			{
				for(var i in response.invalid)
				{
					consoleLog(i+' = '+response.invalid[i]);
					$(i).setStyle('background-color',invalidColor);
					$(i).setProperty('title',invalidText);
					//alert(this.title);
					$(i).setProperty('onchange',"this.setStyle('background-color','#FFF');this.removeProperties('onchange', 'title')"); //doesnt work in IE
					//$(i).addEvent('change', function(){
					//	this.setStyle('background-color','#FFF');
					//	this.removeProperties('onchange', 'title');	
					//});
					error = true;
				}
			}
			if (typeof(response.required) != 'undefined')
			{
				for(var i in response.required)
				{
					consoleLog(i+' = '+response.required[i]);
					if($(i).type == 'checkbox')
					{
						$(i).getParent().getParent().setStyle('background-color',requiredColor);
						$(i).addEvent('click', function(){
							this.getParent().getParent().setStyle('background-color','transparent');
						});
						//$(i).setProperty('onchange',"this.getParent().getParent().setStyle('background-color','transparent');");
						//$(i).setProperty('onclick',"(function(){if(this.checked == true) {this.getParent().getParent().setStyle('background-color','transparent');this.removeProperties('onchange', 'title');}}).bind(this);");
					}
					else
					{
						$(i).setStyle('background-color',requiredColor);
						$(i).addEvent('change', function(){
							this.setStyle('background-color','#FFF');
							this.removeProperties('onchange', 'title');	
						});
						//$(i).setProperty('onchange',"this.setStyle('background-color','#FFF');this.removeProperties('onchange', 'title')");
					}
					$(i).setProperty('title',requiredText);
					error = true;
				}
			}

			if (error != false || typeof(response.error) != 'undefined')
			{
				if ($defined($('error_legend')))
				{
					$('error_legend').dispose();
				}
				var legend = new Element('div', {
					'id': 'error_legend',
					'styles': {
						'margin': '5px auto 5px auto',
						'clear':'both',
						'width': $(table).getWidth() - 20
					}
				}).inject($(container),'top');

				if (response.invalid)
				{
					if ($('error_legend').getElement('#invalid_div') == null)
					{
						var invalid_div = new Element('div', {
							'id': 'invalid_div',
							'styles': {
								'width': 215,
								'margin': '0 auto'
							}
						}).inject(legend);
						var invalid_block = new Element('div', {
							'styles': {
								'float': 'left',
								'width': '15px',
								'height': '15px',
								'margin': '0 10px 0 0',
								'border': '1px solid #999',
								'background': invalidColor
							}
						}).inject(invalid_div);
						var invalid_text = new Element('div', {
							'text': invalidText,
							'styles': {
								'float': 'left',
								'width': '180px'
							}
						}).inject(invalid_div);
					}
					
					insertClearDiv().inject(invalid_div);
				}
				if (response.required)
				{
					if ($('error_legend').getElement('#required_div') == null)
					{
						var required_div = new Element('div', {
							'id': 'required_div',
							'styles': {
								'width': 215,
								'clear':'both',
								'margin': '0 auto'
							}
						}).inject(legend);
						var required_block = new Element('div', {
							'styles': {
								'float': 'left',
								'width': '15px',
								'height': '15px',
								'border': '1px solid #999',
								'margin': '0 10px 0 0',
								'background': requiredColor
							}
						}).inject(required_div);
						var required_text = new Element('div', {
							'text': requiredText,
							'styles': {
								'float': 'left',
								'width': '180px'
							}
						}).inject(required_div);
					}
					insertClearDiv().inject(required_div);
				}
				consoleLog('failed, correct errors');
			}
			else
			{
				consoleLog('mailed form');
				if ($defined($('error_legend')))
				{
					$('error_legend').dispose();
				}
				$(table).setStyle('display','none');
				var successDiv = new Element('div', {
					'id':'successDiv',
					'html': successText,
					'styles': {
						'color':'#339900',
						'clear':'both'
					}
				}).inject($(container));
			}
		}
	}).get({'form':form,'json':postJSON});
}

function consoleLog(string)
{
	var alerts = false;
	if(typeof(console) == 'object') {
		console.log(string);
	}
	else
	{
		if (alerts == true)
		{
			alert(string);
		}
	}
}
