/* This file NEEDS TO BE DEPLOYED as a template in EE */ 
google.load("jquery", "1"); google.setOnLoadCallback(function(){
	$(function(){
		var required = $(":input[required],:input.required")
		var form = $("#contact_form").submit( function(e) {
			var errors = false;
			required.each(function(){
				if( $(this).val().match(/^\s*$/) ) {
					var name = $(this).parent().parent().find("label").text().split(": ")[0];
					if( errors ) {
						errors += "\n";
					} else {
						errors = "";
					}
					errors += '"' + name + '" is a required field, please specify a value.';
				} else {
					if( $(this).attr("type") == "email" || $(this).hasClass("email") ) {
						if( !$(this).val().match(/^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/) ) {
							var name = $(this).parent().parent().find("label").text().split(": ")[0];
							if( errors ) {
								errors += "\n";
							} else {
								errors = "";
							}
							errors += '"' + name + '" is not a valid email address.';
						}
					}
				}
			});
			
			$(":input[type=tel], :input.tel").each(function() {
				if( !$(this).val().match(/^\s*$/) ) {
					if( $(this).attr("type") == "tel" || $(this).hasClass("tel") ) {
						if( !$(this).val().match(/^\D*(\d{3})\D*(\d{3})\D*(\d{4})$/) ) {
							var name = $(this).parent().parent().find("label").text().split(": ")[0];
							if( errors ) {
								errors += "\n";
							} else {
								errors = "";
							}
							errors += '"' + name + '" is not a valid phone number.';
						} else {
							$(this).val( RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3 )
						}
					}
				}
			})
			
			if( errors ) {
				alert( errors );
				event.preventDefault();
				return false;
			}
		});

		var fn = function( dest ) {
			return function( e ) {
				dest.val( $(this).val() );
				dest.change();
			}
		}
		
		$("#name, #from").each(function() {
			var f = fn( form.find( "#" + this.id + "Hidden" ) );
			$(this).keyup( f ).change( f );
		});
		
		
		
	});
});

