$(document).ready(function() {
	
	var options = {
		beforeSubmit: 	validateForm,
		success:		confirmation,
		resetForm: 		false
	};
	
	$('#flextorForm').ajaxForm(options);
	
	$('#resetButton').click(function()  {
		$('#flextorForm').resetForm();
		return false;
	});
	
});

function initializeMontreal() {
  if (GBrowserIsCompatible()) {
  	$('#map').show();
	var map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(45.5595,-73.4285), 12);
	map.addControl(new GSmallMapControl());
	// map.addControl(new GMapTypeControl());

	// Create our "tiny" marker icon
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "http://www.effoxflextor.com/img/contact/marker_flextor_dot.png";
	
	// Set up our GMarkerOptions object
	markerOptions = { icon:blueIcon };

	var latlng = new GLatLng(45.5596,-73.4285);
	map.addOverlay(new GMarker(latlng, markerOptions));
  }
}

function initializeCincinnati() {
  if (GBrowserIsCompatible()) {
  	$('#map').show();
	var map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(39.312466,-84.47689), 12);
	map.addControl(new GSmallMapControl());
	// map.addControl(new GMapTypeControl());

	// Create our "tiny" marker icon
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "http://www.effoxflextor.com/img/contact/marker_flextor_dot.png";
	
	// Set up our GMarkerOptions object
	markerOptions = { icon:blueIcon };

	var latlng = new GLatLng(39.312466,-84.47689);
	map.addOverlay(new GMarker(latlng, markerOptions));
  }
}
	
//window.onload = initializeMTL;
//window.onunload = GUnload;
	
//window.onload = initialize;
//window.onunload = GUnload;

function confirmation(responseText) {
	
	if(responseText != 'Erreur')
	{
		$('#confirmation').slideDown('fast');
		$.scrollTo('#content', 500 );
		$('#confirmation').animate({opacity: 1.0}, 4000);
		$('#confirmation').slideUp('fast');
		$('#flextorForm').resetForm();
	}else{
		$("body.en #captcha_error").show().append("* Incorrect validation string. Please try again.");
		$("body.fr #captcha_error").show().append("* Code de validation incorrect. Veuillez r&eacute;essayer.");
	}
}

function validateForm()
{
	$("#firstname_error").empty().hide();
	$("#name_error").empty().hide();
	$("#address_error").empty().hide();
	$("#city_error").empty().hide();
	$("#state_error").empty().hide();
	$("#zip_error").empty().hide();
	$("#phone_error").empty().hide();
	$("#email_error").empty().hide();
	$("#captcha_error").empty().hide();
	//$("#attachment_error").empty().hide();
	
	var firstname 	= $("#firstname").val();
	var name 		= $("#name").val();
	var address 	= $("#address").val();
	var city 		= $("#city").val();
	var state 		= $("#state").val();
	var zip 		= $("#zip").val();
	var phone 		= $("#phone").val();
	var email 		= $("#email").val();
	//var attachment 		= $("#attachment").val();

	var errors 		= 0;
	
	if (firstname == null || firstname == '')
	{
		$("body.en #firstname_error").show().append("* Please enter your first name.");
		$("body.fr #firstname_error").show().append("* Veuillez entrer votre pr&eacute;nom.");
		errors++;
	}
	if (name == null || name == '')
	{
		$("body.en #name_error").show().append("* Please enter your last name.");
		$("body.fr #name_error").show().append("* Veuillez entrer votre nom de famille.");
		errors++;
	}
	if (address == null || address == '')
	{
		$("body.en #address_error").show().append("* Please enter your address.");
		$("body.fr #address_error").show().append("* Veuillez entrer votre addresse.");
		errors++;
	}
	if (city == null || city == '')
	{
		$("body.en #city_error").show().append("* Please enter your city.");
		$("body.fr #city_error").show().append("* Veuillez entrer votre ville.");
		errors++;
	}
	
	if (state == null || state == '')
	{
		$("body.en #state_error").show().append("* Please enter your state.");
		$("body.fr #state_error").show().append("* Veuillez entrer votre province.");
		errors++;
	}
	
	if (zip == null || zip == '')
	{
		$("body.en #zip_error").show().append("* Please enter your zip code.");
		$("body.fr #zip_error").show().append("* Veuillez entrer votre code postal.");
		errors++;
	}
	
	if (phone == null || phone == '')
	{
		$("body.en #phone_error").show().append("* Please enter your phone number.");
		$("body.fr #phone_error").show().append("* Veuillez entrer votre num&eacute;ro de t&eacute;l&eacute;phone.");
		errors++;
	}

	/*
	if (attachment == null || attachment == '')
	{
		$("body.en #attachment_error").show().append("* Please choose a file to upload.");
		$("body.fr #attachment_error").show().append("* Veuillez choisir un fichier.");
		errors++;
	}*/
	
	if (email == null || email == '')
	{
		$("body.en #email_error").show().append("* Please enter your email address.");
		$("body.fr #email_error").show().append("* Veuillez entrer votre courriel.");
		errors++;
	}
	else if (!isNumeric(email))
	{
		$("body.en #email_error").show().append("* Please enter a valid email address.");
		$("body.fr #email_error").show().append("* Veuillez entrer un courriel valide.");
		errors++;
	}
	
    if(errors > 0)
    {
    	return false;
    }
}
	
function isNumeric(form_value)
{
	if (form_value.match(/^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/) == null)
		return false;
	else
		return true;
}
