function isValidEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1){
	  return false;
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	  return false;
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	  return false;
	}
	
	if (str.indexOf(at,(lat+1))!=-1){
	  return false;
	}
	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	  return false;
	}
	
	if (str.indexOf(dot,(lat+2))==-1){
	  return false;
	}
	
	if (str.indexOf(" ")!=-1){
	  return false;
	}
	
	return true;
}

jQuery(function(){
	jQuery('#performance-submit').click(function(e){
		e.preventDefault();

		var email = jQuery('#performance-email').val();
		var verify = jQuery('#performance-verify')[0].checked;
	
		if(email != "" && isValidEmail(email) && verify == true){
			jQuery.ajax({  
				type: 'POST',  
				url: jQuery('form#performance-form').attr('action'),  
				data: jQuery('form#performance-form').serialize(),  
				dataType: 'text',
				success: function(data, textStatus){
					data = eval('(' + data + ')');
					
					if(data.status == 'true' || data.status == true){
						jQuery('#performance-form div.errors').addClass('success');
						jQuery('#performance-form div.errors').html('Thanks for requesting performance information.  Someone from our office will contact you as soon as possible.');
						jQuery('#performance-form div.form').css('display','none');
						window.open('http://www.radyassets.com/images/performance.pdf');
					}
					else{
						jQuery('#performance-form div.errors').removeClass('success');
						jQuery('#performance-form div.errors').html('There was an error processing your submission');
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown){}
			}); 
		}
		else{
			var response = "<ul>";
			if(email == "" || !isValidEmail(email)){
				response = response + "<li>You must enter a valid e-mail address to request</li>";
			}
			if(verify == false){
				response = response + "<li>You must confirm that you meet at least one of the above criteria</li>";
			}
			
			response = response + "</ul>";
			jQuery('#performance-form div.errors').html(response);
		}
	});
});
