/***********************************
*
* file: process_form.js
* n: Antonis Antoniades @ Minus40
* e: a.antoniades.85@gmail.com
*
***********************************/

// Globals
var ASPscriptPath = 'scripts/process_form.asp'; // ASP script that will handle the form submission
var confirmation_page = 'contact-us-thankyou.asp'; // this is where the form will redirect to show confirmation page
var confirmation_text = "Your email was sent successfully"; // confirmation text to show when form is submitted

function process_form() {
	// hide any existing error messages
	$("span#first_name_error").hide();
	$("span#last_name_error").hide();
	$("span#job_title_error").hide();
	$("span#company_name_error").hide();
	$("span#email_error").hide();
	$("span#phone_error").hide();
	$("span#interests_error").hide();
	$("span#comments_error").hide();

	// concatenate checked interests into a single string
	var sInterests = "";
	$('input[name="interest"]:checked').each(function() {
		sInterests += '[ ' + $(this).val() + ' ] ';
	});

	// POST form to server for processing
	$.post(ASPscriptPath, // ASP script that will handle the form submission
		{ // Get the values from the form and POST them
			first_name: $('input#first_name').val(),
			last_name: $('input#last_name').val(),
			job_title: $('input#job_title').val(),
			company_name: $('input#company_name').val(),
			email: $('input#email').val(),
			phone: $('input#phone').val(),
			interests: sInterests,
			comments: $('textarea#comments').val()
		},
		function(oJSON) { // handles postback by server
			if(oJSON.validation_success) { // check if form data passed validation
				if(oJSON.email_success) { // check if email was dispatched successfully
					//window.location = confirmation_page; // redirect to confirmation page
					$("form#contact-form").slideUp();
					$("span#confirmation_message").html(confirmation_text).fadeIn("slow");
				}
				else { // email failed to dispatch
					alert("Failed to send email. Check mail server.");
				}				
			}
			else { // show validation error-messages
				$("span#first_name_error").html(oJSON.first_name_error).fadeIn('slow');
				$("span#last_name_error").html(oJSON.last_name_error).fadeIn('slow');
				$("span#job_title_error").html(oJSON.job_title_error).fadeIn('slow');
				$("span#company_name_error").html(oJSON.company_name_error).fadeIn('slow');
				$("span#email_error").html(oJSON.email_error).fadeIn('slow');
				$("span#phone_error").html(oJSON.phone_error).fadeIn('slow');
				$("span#interests_error").html(oJSON.interests_error).fadeIn('slow');
				$("span#comments_error").html(oJSON.comments_error).fadeIn('slow');
			}
		}, 'json'); // The postback data is sent in JavaScript Object Notation

	// disable refreshing the page when form is submitted
	return false;
}

// Wait for everything to load
$(document).ready(function() {
	// when the form with id="contact-form" is submitted, call the process_form() function
	$("form#contact-form").live('submit', process_form);
});

