// JavaScript Document


//	CONTACT FORM
function submit_contactform()
{
	// Validate the form using the required class. 
	var items = $$('.required');
	var error = false;
	
	//	Iterate through each item marked as required. If item length is zero, flag errors.
	items.each( function(element) 
						 { if(element.value.length <= 0 )
						 	{
							  new Effect.Highlight(element, {duration:10});  
							  
							  error = true;
							}
						 } );
	
	
	//	Alert user to the error of his/her ways...
	if( error )
	{
		alert('Please complete the highlighed form fields. These are required.');
		return false;
	}
	
	//	Send window to the top
	scroll(0,0);
	
	new Effect.Fade('form', { duration:.2 } );
	new Effect.Appear('progress-animation', {duration:.2, delay:.2 } );

	//	Send our message through ajaz request.
	new Ajax.Request('request.php', {	
  		method: 'post',
		onSuccess: function(e) 
					{ 
						//	Response comes as true/false followed by ^ and a server message
						var response = e.responseText.split('^');;
						
						var success = response[0];
						success = success.replace(/\s/gi, '');
						
						var message = response[1];
						
						
						if( success != "true" )
						{ 
							//	Hides notice window and shows form
							new Effect.Appear('form', { duration:.2 } );
							new Effect.Fade('progress-animation', {duration:.2, delay: .2 } );
							
							//	Shows the user message
							alert(e.responseText);
						}
						else
						{
							//	Success! Report to the user their success then redirect to home page
							
							alert(message);
							
							new Effect.Appear('form', { duration:.2 } );
							new Effect.Fade('progress-animation', {duration:.2, delay: .2 } );
						
							
							setTimeout("window.location='default.html'", 1);
						}
					},
		onFailure: function(e) { alert('Unable to submit request. Please try again.'); new Effect.Fade('progress', { queue: 'end' }); new Effect.Appear('form', { queue: 'end' } ); },
  		parameters: 
			{
				option: 'submit',
				type: 'contactform',
				values: $('contact').serialize()
			} 
	});
}
