	$(function() {
		var name = $( "#dgname" ),
			company = $( "#dgcompany"),
			email = $( "#dgemail" ),
			phone = $( "#dgphone" ),
			msg = $( "#dgmsg" ),
			allFields = $( [] ).add( name ).add( email ).add( phone ).add( msg ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( n + " must be atleast " +
					min + " letters long" );
				return false;
			} else {
				return true;
			}
		}

		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
		
		$( "#dialog-form" ).dialog({
			autoOpen: false,
			height: 650,
			width: 450,
			modal: true,
			draggable: true,
			resizeable: true,
			show: "blind",
			buttons: {
				"Sign up": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength( name, "name", 3, 80 );
					bValid = bValid && checkLength( email, "email", 6, 80 );
					bValid = bValid && checkLength( company, "company", 2, 80 );
					bValid = bValid && checkRegexp( name, /^[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]+$/i, "Name must be all letters." );
					bValid = bValid && checkRegexp( email, /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/, "please enter a valid e-mail address" );
					if (phone.val().length != 0) {
						bValid = bValid && checkRegexp (phone, /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/, "Phone must be formatted as xxx-xxx-xxxx");
					}
					if ( bValid ) {
						var fullName = jQuery.trim($("#dgname").val());
						
						var names = fullName.split(" ");  //split on spaces.

						if (names.length == 1) {
							$( "#first_name").val(names[0]);
							$( "#last_name").val("");
						} else {
							var fname = "";
							for (var i = 0;i < names.length-1; i++) {
								fname = fname.concat(" " + names[i]);
							}
							$( "#first_name").val(fname);
							$( "#last_name").val(names[names.length - 1]);		
						}

						$( "#company").val($("#dgcompany").val());
						$( "#phone").val($("#dgphone").val());
						$( "#email").val($("#dgemail").val());
						$( "#description").val($("#dgmsg").val());
						
						$("#salesforceForm").submit();
					
						$( this ).dialog( "close" );
					}
				},
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});

		$( "#email_submit" )
			//.button()
			.click(function() {
			   $( "#dgemail" ).val($( "#email_input").val());
				$( "#dialog-form" ).dialog( "open" );
			});
	});

