// This file contains ajax submissions for all forms that
// feed into SalesForce via their web-to-lead functionality.

// It also adds virtual pageviews to onSubmit actions
// to enable Google Analytics goal tracking.

// We commented out all ajax form submissions.
// Using the ajax method did not work in IE8 & below, or Firefox.

// Homepage Promo Form
$(function() {
	// Hide error messages
	$('.error').hide();
  
	$("#btn-homepage-promo").click(function() {	
		// Get values from input fields
    var homepage_first_name = $("input#homepage_first_name").val();
		var homepage_last_name = $("input#homepage_last_name").val();
		var homepage_email = $("input#homepage_email").val();
		var homepage_company = $("input#homepage_company").val();
		var homepage_phone = $("input#homepage_phone").val();
		var homepage_street = $("input#homepage_street").val();
		var homepage_city = $("input#homepage_city").val();
		var homepage_state = $("#homepage_state").val();
		var homepage_zip = $("input#homepage_zip").val();
		var homepage_oid = $("input#homepage_oid").val();
		var homepage_lead_source = $("input#homepage_lead_source").val();
		var homepage_source_form = $("input#homepage_source_form").val();
		var homepage_source_form_details = $("input#homepage_source_form_details").val();
		var homepage_url = $("input#homepage_url").val();
		
		// Validate form fields. Make sure they aren't blank.
		if( $("#homepage_first_name").length){
			if (homepage_first_name == "") {
				$("label#homepage_first_name_error").show();
				$("input#homepage_first_name").focus();
				return false;
			}
		}
		if( $("#homepage_last_name").length){
			if (homepage_last_name == "") {
				$("label#homepage_last_name_error").show();
				$("input#homepage_last_name").focus();
				return false;
			}
		}
		if( $("#homepage_email").length && $("#homepage_phone").length){
			if (homepage_email == "" && homepage_phone == "") {
				$("label#homepage_email_error").show();
				$("input#homepage_email").focus();
				return false;
			}
		}
		else if ($("#homepage_email").length){
			if (homepage_email == "") {
				$("label#homepage_email_error").show();
				$("input#homepage_email").focus();
				return false;
			}
		}
		else if ($("#homepage_phone").length){
			if (homepage_email == "") {
				$("label#homepage_email_error").show();
				$("input#homepage_email").focus();
				return false;
			}
		}
		else {}	
		if( $("#homepage_street").length){
			if (homepage_street == "") {
				$("label#homepage_street_error").show();
				$("input#homepage_street").focus();
				return false;
			}
		}
		if( $("#homepage_city").length){
			if (homepage_city == "") {
				$("label#homepage_city_error").show();
				$("input#homepage_city").focus();
				return false;
			}
		}
		if( $("#homepage_state").length){
			if (homepage_state == "") {
				$("#homepage_state_error").show();
				$("#homepage_state").focus();
				return false;
			}
		}
		if( $("#homepage_zip").length){
			if (homepage_zip == "") {
				$("label#homepage_zip_error").show();
				$("input#homepage_zip").focus();
				return false;
			}
		}
	}); // end click function
}); // end form main function
/*
		// Put all input values in one data string
		var dataString = 'oid=' + homepage_oid + '&lead_source=' + homepage_lead_source + '&00N70000002PRxu=' + homepage_source_form + '&00N70000002PRxz=' + homepage_source_form_details + '&00N70000002PRy4=' + homepage_url + '&first_name=' + homepage_first_name + '&last_name=' + homepage_last_name + '&email=' + homepage_email + '&company=' + homepage_company + '&phone=' + homepage_phone + '&street=' + homepage_street + '&city=' + homepage_city + '&state=' + homepage_state + '&zip=' + homepage_zip;

		// Submit form using ajax
		$.ajax({
			type: "POST",
			url: "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8",
			data: dataString,
			success: function() {
				$('#homepage_message').html("<p class='success'>We have received your information. Thank you for taking the time to fill out the form.</p>")
				$('#homepage-promo-form').hide()
				$('.close-after-submit').show()
				.fadeIn(1500, function() {
					$('#homepage_message');
				});
			}
		});
		return false;
*/

// Subscribe Form
$(function() {
	// Hide error messages
	$('.error').hide();
  
	$("#btn-subscribe").click(function() {
		// Get values from input fields
    var first_name = $("input#first_name").val();
		var last_name = $("input#last_name").val();
		var email = $("input#email").val();
		var company = $("input#company").val();
		var oid = $("input#oid").val();
		var lead_source = $("input#lead_source").val();
		var source_form = $("input#source_form").val();
		var source_form_details = $("input#source_form_details").val();
		var page_url = $("input#page_url").val();
		
		// Validate form fields. Make sure they aren't blank.
		if (first_name == "") {
      $("label#first_name_error").show();
      $("input#first_name").focus();
      return false;
    }
		if (last_name == "") {
      $("label#last_name_error").show();
      $("input#last_name").focus();
      return false;
    }
		if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }
	}); // end click function
}); // end form main function
/*
		// Put all input values in one data string
		var dataString = 'first_name='+ first_name + '&last_name=' + last_name + '&email=' + email + '&company=' + company + '&oid=' + oid + '&lead_source=' + lead_source + '&00N70000002PRxu=' + source_form + '&00N70000002PRxz=' + source_form_details + '&00N70000002PRy4=' + page_url;

		// Submit form using ajax
		$.ajax({
			type: "POST",
			url: "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8",
			data: dataString,
			success: function() {
				$('#message').html("<p class='success'>Thank you for subscribing. You&#8217;ll begin receiving emails about AeroMetric news &amp; events shortly.</p>")
				$('#newsletter-subscribe-form').hide()
				$('.close-after-submit').show()
				.fadeIn(1500, function() {
					$('#message');
				});
			}
		});
		return false;
*/

// Contact Form - Markets Pages
$(function() {
	// Hide error messages
	$('.error').hide();
  
	$("#btn-contact-markets").click(function() {	
		// Get values from input fields
    var contact_first_name = $("input#contact_first_name").val();
		var contact_last_name = $("input#contact_last_name").val();
		var contact_email = $("input#contact_email").val();
		var contact_phone = $("input#contact_phone").val();
		var contact_company = $("input#contact_company").val();
		var contact_oid = $("input#contact_oid").val();
		var contact_lead_source = $("input#contact_lead_source").val();
		var contact_source_form = $("input#contact_source_form").val();
		var contact_source_form_details = $("input#contact_source_form_details").val();
		var contact_page_url = $("input#contact_page_url").val();
		
		// Validate form fields. Make sure they aren't blank.
		if (contact_first_name == "") {
      $("label#contact_first_name_error").show();
      $("input#contact_first_name").focus();
      return false;
    }
		if (contact_last_name == "") {
      $("label#contact_last_name_error").show();
      $("input#contact_last_name").focus();
      return false;
    }
		if (contact_email == "" && contact_phone == "") {
      $("label#contact_email_error").show();
      $("input#contact_email").focus();
      return false;
    }
	}); // end click function
}); // end form main function
/*
		// Put all input values in one data string
		var dataString = 'first_name='+ contact_first_name + '&last_name=' + contact_last_name + '&email=' + contact_email + '&phone=' + contact_phone + '&company=' + contact_company + '&oid=' + contact_oid + '&lead_source=' + contact_lead_source + '&00N70000002PRxu=' + contact_source_form + '&00N70000002PRxz=' + contact_source_form_details + '&00N70000002PRy4=' + contact_page_url;

		// Submit form using ajax
		$.ajax({
			type: "POST",
			url: "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8",
			data: dataString,
			success: function() {
				$('#contact_message').html("<p class='success'>Thank you for getting in touch. One of our business development directors will be contacting you shortly.</p>")
				$('#markets-contact-form').hide()
				$('.close-after-submit').show()
				.fadeIn(1500, function() {
					$('#contact_message');
				});
			}
		});
		return false;
*/

// Contact Form - Services, Solutions & Project Pages
$(function() {
	// Hide error messages
	$('.error').hide();
  
	$("#btn-contact-projects").click(function() {	
		// Get values from input fields
    var contact_first_name = $("input#contact_first_name").val();
		var contact_last_name = $("input#contact_last_name").val();
		var contact_email = $("input#contact_email").val();
		var contact_phone = $("input#contact_phone").val();
		var contact_company = $("input#contact_company").val();
		var contact_state = $("input#contact_state").val();
		var contact_oid = $("input#contact_oid").val();
		var contact_lead_source = $("input#contact_lead_source").val();
		var contact_source_form = $("input#contact_source_form").val();
		var contact_page_url = $("input#contact_page_url").val();
		
		// Validate form fields. Make sure they aren't blank.
		if (contact_first_name == "") {
      $("label#contact_first_name_error").show();
      $("input#contact_first_name").focus();
      return false;
    }
		if (contact_last_name == "") {
      $("label#contact_last_name_error").show();
      $("input#contact_last_name").focus();
      return false;
    }
		if (contact_email == "" && contact_phone == "") {
      $("label#contact_email_error").show();
      $("input#contact_email").focus();
      return false;
    }
	}); // end click function
}); // end form main function
/*
		// Put all input values in one data string
		var dataString = 'first_name='+ contact_first_name + '&last_name=' + contact_last_name + '&oid=' + contact_oid + '&email=' + contact_email + '&phone=' + contact_phone + '&company=' + contact_company + '&state=' + contact_state + '&oid=' + contact_oid + '&lead_source=' + contact_lead_source + '&00N70000002PRxu=' + contact_source_form + '&00N70000002PRy4=' + contact_page_url;
		
		// Submit form using ajax
		$.ajax({
			type: "POST",
			url: "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8",
			data: dataString,
			success: function() {
				$('#contact_message').html("<p class='success'>Thank you for getting in touch. One of our business development directors will be contacting you shortly.</p>")
				$('#services-contact-form').hide();
				$('#projects-contact-form').hide();
				$('.close-after-submit').show()
				.fadeIn(1500, function() {
					$('#contact_message');
				});
			}
		});
		return false;
*/

// Contact Form - Contact Pages (regional account managers)
$(function() {
	// Hide error messages
	$('.error').hide();
  
	$("#btn-contact-contact").click(function() {	
		// Get values from input fields
    var contact_first_name = $("input#contact_first_name").val();
		var contact_last_name = $("input#contact_last_name").val();
		var contact_email = $("input#contact_email").val();
		var contact_phone = $("input#contact_phone").val();
		var contact_company = $("input#contact_company").val();
		var contact_oid = $("input#contact_oid").val();
		var contact_lead_source = $("input#contact_lead_source").val();
		var contact_source_form = $("input#contact_source_form").val();
		var contact_source_form_details = $("input#contact_source_form_details").val();
		var contact_page_url = $("input#contact_page_url").val();
		
		// Validate form fields. Make sure they aren't blank.
		if (contact_first_name == "") {
      $("label#contact_first_name_error").show();
      $("input#contact_first_name").focus();
      return false;
    }
		if (contact_last_name == "") {
      $("label#contact_last_name_error").show();
      $("input#contact_last_name").focus();
      return false;
    }
		if (contact_email == "" && contact_phone == "") {
      $("label#contact_email_error").show();
      $("input#contact_email").focus();
      return false;
    }
	}); // end click function
}); // end form main function
/*
		// Put all input values in one data string
		var dataString = 'first_name='+ contact_first_name + '&last_name=' + contact_last_name + '&email=' + contact_email + '&phone=' + contact_phone + '&company=' + contact_company + '&oid=' + contact_oid + '&lead_source=' + contact_lead_source + '&00N70000002PRxu=' + contact_source_form + '&00N70000002PRxz=' + contact_source_form_details + '&00N70000002PRy4=' + contact_page_url;
		
		// Submit form using ajax
		$.ajax({
			type: "POST",
			url: "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8",
			data: dataString,
			success: function() {
				$('#contact_message').html("<p class='success'>Thank you for getting in touch. We will be contacting you shortly.</p>")
				$('#contacts-contact-form').hide()
				$('.close-after-submit').show()
				.fadeIn(1500, function() {
					$('#contact_message');
				});
			}
		});
		return false;
*/

// Contact Form - Insight Pages (blog posts)
$(function() {
	// Hide error messages
	$('.error').hide();
  
	$("#btn-contact-insight").click(function() {	
		// Get values from input fields
    var insight_first_name = $("input#insight_first_name").val();
		var insight_last_name = $("input#insight_last_name").val();
		var insight_email = $("input#insight_email").val();
		var insight_phone = $("input#insight_phone").val();
		var insight_company = $("input#insight_company").val();
		var insight_oid = $("input#insight_oid").val();
		var insight_lead_source = $("input#insight_lead_source").val();
		var insight_source_form = $("input#insight_source_form").val();
		var insight_source_form_details = $("input#insight_source_form_details").val();
		var insight_page_url = $("input#insight_page_url").val();
		
		// Validate form fields. Make sure they aren't blank.
		if (insight_first_name == "") {
      $("label#insight_first_name_error").show();
      $("input#insight_first_name").focus();
      return false;
    }
		if (insight_last_name == "") {
      $("label#insight_last_name_error").show();
      $("input#insight_last_name").focus();
      return false;
    }
		if (insight_email == "" && insight_phone == "") {
      $("label#insight_email_error").show();
      $("input#insight_email").focus();
      return false;
    }
	}); // end click function
}); // end form main function

/*
		// Put all input values in one data string
		var dataString = 'first_name='+ insight_first_name + '&last_name=' + insight_last_name + '&email=' + insight_email + '&phone=' + insight_phone + '&company=' + insight_company + '&oid=' + insight_oid + '&lead_source=' + insight_lead_source + '&00N70000002PRxu=' + insight_source_form + '&00N70000002PRxz=' + insight_source_form_details + '&00N70000002PRy4=' + insight_page_url;
		
		// Submit form using ajax
		$.ajax({
			type: "POST",
			url: "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8",
			data: dataString,
			success: function() {
				$('#insight_message').html("<p class='success'>Thank you for getting in touch. One of our business development directors will be contacting you shortly.</p>")
				$('#insight-contact-form').hide()
				$('.close-after-submit').show()
				.fadeIn(1500, function() {
					$('#insight_message');
				});
			}
		});
		return false;
*/

// Landing Page & Ad Campaign Forms
$(function() {
	$('.error').hide(); // Hide error messages
	
	$("#btn-lp").click(function() {
		// Get values from input fields
    var lp_first_name = $("input#lp_first_name").val();
		var lp_last_name = $("input#lp_last_name").val();
		var lp_email = $("input#lp_email").val();
		var lp_company = $("input#lp_company").val();
		var lp_phone = $("input#lp_phone").val();
		var cb_aerial = $("#cb-aerial").val();
		var cb_lidar = $("#cb-lidar").val();
		var cb_heliphoto = $("#cb-heliphoto").val();
		var cb_helivideo = $("#cb-helivideo").val();
		var cb_helilidar = $("#cb-helilidar").val();
		var cb_mobile = $("#cb-mobile").val();
		var cb_satellite = $("#cb-satellite").val();
		var lp_project_location = $("input#lp_project_location").val();
		var lp_project_details = $("#lp_project_details").val();
		var lp_oid = $("input#lp_oid").val();
		var lp_lead_source = $("input#lp_lead_source").val();
		var lp_source_form = $("input#lp_source_form").val();
		var lp_source_form_details = $("input#lp_source_form_details").val();
		var lp_page_url = $("input#lp_page_url").val();
		
		// Validate form fields. Make sure they aren't blank.
		if (lp_first_name == "") {
      $("label#lp_first_name_error").show();
      $("input#lp_first_name").focus();
      return false;
    }
		if (lp_last_name == "") {
      $("label#lp_last_name_error").show();
      $("input#lp_last_name").focus();
      return false;
    }
		if (lp_email == "" && lp_phone == "") {
      $("label#lp_email_error").show();
      $("input#lp_email").focus();
      return false;
    }
	}); // end click function
}); // end form main function

/*
		// Put all input values in one data string
		var dataString = 'first_name='+ lp_first_name + '&last_name=' + lp_last_name + '&email=' + lp_email + '&company=' + lp_company + '&phone=' + lp_phone + '&oid=' + lp_oid + '&lead_source=' + lp_lead_source + '&00N70000002PRxu=' + lp_source_form + '&00N70000002PRxz=' + lp_source_form_details + '&00N70000002PRy4=' + lp_page_url + '&00N70000002Q5nM=' + lp_project_location + '&description=' + lp_project_details + '&00N70000002Q69D=' + cb_aerial + '&00N70000002Q69I=' + cb_lidar + '&00N70000002Q69N=' + cb_heliphoto + '&00N70000002Q69S=' + cb_helivideo + '&00N70000002Q69X=' + cb_helilidar + '&00N70000002Q69c=' + cb_mobile + '&00N70000002Q69h=' + cb_satellite;

		// Submit form using ajax
		$.ajax({
			type: "POST",
			url: "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8",
			data: dataString,
			success: function() {
				$('form#lp-form').hide();
				$('#lp_confirmation').html("<p class='success'>Thank you. An AeroMetric Geospatial Solutions consultant will contact you shortly.</p>")
				.fadeIn(1500, function() {
					$('#lp_confirmation');
				});
			}
		});
		return false;
*/

// Track Form Submissions w/ Google Analytics
// Attach virtual pageviews on submit
$('#lp-form').submit(function() {
	_gaq.push(['_trackPageview','/forms/campaign-promotion']);
});
$('#contacts-contact-form').submit(function() {
	_gaq.push(['_trackPageview','/forms/contact']);
});
$('#insight-contact-form').submit(function() {
	_gaq.push(['_trackPageview','/forms/contact']);
});
$('#markets-contact-form').submit(function() {
	_gaq.push(['_trackPageview','/forms/contact']);
});
$('#projects-contact-form').submit(function() {
	_gaq.push(['_trackPageview','/forms/contact']);
});
$('#services-contact-form').submit(function() {
	_gaq.push(['_trackPageview','/forms/contact']);
});
$('#homepage-promo-form').submit(function() {
	_gaq.push(['_trackPageview','/forms/homepage-promotion']);
});
$('#newsletter-subscribe-form').submit(function() {
	_gaq.push(['_trackPageview','/forms/newsletter-subscribe']);
});
