$(function() {

	var industryVal;


	$('#industry').change(function (){
		
		industryVal = $("#industry option:selected").val();
		
		if (industryVal == 'other')
		{
			$('#industry_other').show();
			$('#industry_other').focus();
		}
		else
		{
			$('#industry_other').hide();
		}
         
   });
   
   
   $('#form_submit').click( function(){
   	
   	
   	var isValid = checkForm();
   	if (isValid)
   	{
   		
   		var message = 'Name: ' + firstNameVal + " " + lastNameVal + '\n';
			message += 'Job title: ' + jobVal + '\n';
			message += 'Email: ' + emailVal + '\n';			
			message += 'Company: ' + companyVal + '\n';
			message += 'Industry: ' + industryVal + '\n';
			message += 'Country: ' + countryVal + '\n';
			message += 'Newsletter opt-in: ' + signupVal + '\n';

		   $('#form_submit').hide();
		   $('#progress').show();

		   $.ajax({
		   	type: "POST",  
		   	url: "/cgi-bin/gobi_contact_mime.pl",  
		   	data: 'message=' + message,  
		   	success: function(data)
				{  
		   		// Fade out intro
					$('#sign_up_container').hide();
				
					if(parseInt(data) == 1)
					{
						$('#thank_you').show();
						if (signupVal == true) {
							$('#thank_you').append('<form action="http://login.sendmetric.com/phase2/bullseye/contactupdate1.php3" target="signup" method="post" target="signup" name="register" id="register"><input type="hidden" name="email" value="'+ emailVal +'"><input type="hidden" name="cid" value="3c77522666ce0765710213f9b1d14cfd" /><input type="hidden" name="grp[]" value="566575"><input type="hidden" name="message" value="Thank you. Your information has been submitted. To ensure delivery of your newsletter(s), please add info@gobianywhere.com to your address book, spam filter whitelist, or tell your companys IT group to allow this address to pass through any filtering software they may have set up."></form><iframe name="signup" style="display:none"></iframe>');						
							$('#register').submit();
						}
					}
					else
					{
						$('#submit_error').show();
					}
		   	}
		   	
		   });

   		
   	}
   	
   	
   	
   	
   	
   	return false;
   });
	
	
	
	


});

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '').replace(/\s{2,}/g, ' ');
};

String.prototype.removeSemis = function() {
  // remove semicolons and &
  noSemis = /[\;|\&]/ig; 
  return this.replace(noSemis,''); 	
}

var firstNameVal;
var lastNameVal;
var emailVal;
var jobVal;
var companyVal;
var industryVal;
var countryVal;
var signupVal;

checkForm = function()
{
	var isValid = false;

	var firstNameValid = false;
	var lastNameValid = false;
	var emailValid = false;
	var companyValid = false;
	var industryValid = false;
	var countryValid = false;
	var signupValid = false;
	
	// Name input
	firstNameVal = $('#name_first').val().trim().removeSemis();
	if(firstNameVal && firstNameVal.length>0) firstNameValid = true;	
	lastNameVal = $('#name_last').val().trim().removeSemis();
	if(lastNameVal && lastNameVal.length>0) lastNameValid = true;
	if (!firstNameValid || !lastNameValid)
	{
		$('#name_group .warning').show();
	}
	else
	{
		$('#name_group .warning').hide();	
	}
			
	// Job title
	jobVal =  $('#job_title').val().trim().removeSemis();
	
	
	// E-mail input
	emailVal = $('#email').val().trim().removeSemis();
	if (emailVal && emailVal.length>0 && validate_email(emailVal)) emailValid = true;
	if (!emailValid)
	{
		$('#email_group .warning').show();
	}
	else
	{
		$('#email_group .warning').hide();	
	}
	
	// Company input
	companyVal = $('#company').val().trim().removeSemis();
	if(companyVal && companyVal.length>0) companyValid = true;
	if (!companyValid)
	{
		$('#company_group .warning').show();
	}
	else
	{
		$('#company_group .warning').hide();	
	}
	
	
	// Industry input
	industryVal = $("#industry option:selected").val();
	if (industryVal == 'choose')
	{
		$('#industry_group .warning').show();
	}
	else if (industryVal == 'other')
	{
		industryVal = $('#industry_other').val().trim().removeSemis();
		if(industryVal && industryVal.length>0) industryValid = true;
		if (!industryValid)
		{
			$('#industry_group .warning').show();
		}
		else
		{
			$('#industry_group .warning').hide();	
		}
	}
	else
	{
		$('#industry_group .warning').hide();
		industryValid = true;
	}
	
	
	// Country input
	countryVal = $('#country').val().trim().removeSemis();
	if(countryVal && countryVal.length>0) countryValid = true;
	if (!countryValid)
	{
		$('#country_group .warning').show();
	}
	else
	{
		$('#country_group .warning').hide();	
	}
	
	signupVal = $("#signup").attr("checked");
	if (signupVal && signupVal!=''){ signupVal = true; }		

		
	// Check validity
	if (firstNameValid && lastNameValid && emailValid && companyValid && industryValid && countryValid)
	{
		isValid = true;
	}
	
	return isValid;
}

function validate_email(string)
{
	return (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}