function isValidPhone(phonenumber){
	if (phonenumber != "") {
		var goodChars = "+- 1234567890()"
		for (i = 0; i < phonenumber.length; i++){   
		    var c = phonenumber.charAt(i);
		    if (goodChars.indexOf(c) < 0) return false;
		}
		return true;
	} else {
		return false;
	}
}

$("form.email div.go div").live('click', 
	function() {
		var error = false;  
		var errors = new Array;
		var form = $(this).parent().parent();
		var formName = form.find('input.name').val();
		var formCompany = form.find('input.company').val();
		var formPosition = form.find('input.position').val();
		var formPhone = form.find('input.phone').val();
		var formEmail = form.find('input.email').val();
		var formComments = form.find('input.comments').val();
	    
		
		if(formName !== undefined && formName.length == 0){  
			var error = true;
			form.find('div.label span.name').css({'color': '#ae3636', 'fontWeight': 'bold'});
		}
		
		if((formEmail !== undefined && formEmail.length == 0) || formEmail.indexOf('@') == '-1'){  
			var error = true;
			form.find('div.label span.email').css({'color': '#ae3636', 'fontWeight': 'bold'});
		}

		var phonePattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;  
		if(formPhone.length > 0 && isValidPhone(formPhone) == false){  
			var error = true;
			form.find('div.label span.phone').css({'color': '#ae3636', 'fontWeight': 'bold'});
		}

		if(error == false) {  
			$.post("/email.php", $(this).parent().parent().parent().serialize(),function(result){
				form.find('div.go').fadeOut(500);
				form.find('div.sent p').html("<strong>Thank you for your inquiry.</strong> We'll be in touch shortly.");
				form.find('div.sent').delay(500).fadeIn(500);
			});
		} else {
			form.find('div.error span').html('There is something missing or incorrect.');
			form.find('div.error').slideDown(250).delay(2000).slideUp(250)
		}
	}
);

$("form.newsletter div.go div").live('click', 
	function() {
		$("form.newsletter").submit();	
	}
);


$(".form").live('keypress', function(e) {
	if(e.keyCode == 13) {
		$(this).find('div.go div').click();
	}
});
