	$j(function() {
		$j( "#dialog" ).dialog( "destroy" );
		
		var sender_name = $j( "#sender_name" ),
			sender_email = $j( "#sender_email" ),
			receiver_name = $j( "#receiver_name" ),
			receiver_email = $j( "#receiver_email" ),
			listing_id = $j( "#listing_id" ),
			saved_search_id  = $j( "#saved_search_id " ),
			development_id  = $j( "#development_id " ),
			nonce = $j( "#nonce " ),
			msg = $j( "#msg" ),
			allFields = $j( [] ).add( sender_name ).add( sender_email ).add( receiver_name ).add( receiver_email ).add( msg ).add( listing_id ).add( saved_search_id).add( development_id).add( nonce ),
			tips = $j( ".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( "Length of " + n + " must be between " +
					min + " and " + max + "." );
				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;
			}
		}
		
		$j( "#dialog-form" ).dialog({
			autoOpen: false,
			//height: 440,
			width: 350,
			modal: true,
			buttons: {
				Cancel: function() {
					$j( this ).dialog( "close" );
				},

				"Send to friend": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength( sender_name, "sender_name", 3, 16 );
					bValid = bValid && checkLength( sender_email, "sender_email", 6, 80 );
					bValid = bValid && checkLength( receiver_name, "receiver_name", 3, 16 );
					bValid = bValid && checkLength( receiver_email, "receiver_email", 6, 80 );

					var emailValidatorRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
					bValid = bValid && checkRegexp( sender_email, emailValidatorRegex, "Please enter a valid email" );
					bValid = bValid && checkRegexp( receiver_email, emailValidatorRegex, "Please enter a valid email" );

					if ( bValid ) {
                        tips.text('Sending email ...');
                        var goodToGo = 1;
                        $j.post('/emailtofriend.php',  
                                {sender_name: sender_name.val(),
                                 sender_email: sender_email.val(),
                                 receiver_name: receiver_name.val(),
                                 receiver_email: receiver_email.val(),
                                 listing_id: listing_id.val(),
                                 saved_search_id: saved_search_id.val(),
                                 development_id: development_id.val(),
                                 nonce: nonce.val(),
                                 msg: msg.val()}, 
                                 function(data) {
                                    if (data) {
                                        updateTips(data);
			                            setTimeout(function() {
						                    $j("#dialog-form").dialog("close");
			                            }, 1500 );
                                    }
                                 });
					}
				}
			}
		});

		$j( ".btn_email_friend" ).click(function() {
                var myListingId = $j(this).siblings('input[name=listingId]').val();
                var mySavedSearchId = $j(this).siblings('input[name=savedSearchId]').val();
                var myDevelopmentId = $j(this).siblings('input[name=developmentId]').val();
				if (mySavedSearchId) {
                	$j( "#saved_search_id" ).val(mySavedSearchId);
                	tips.text( 'Email Saved Search  (' + mySavedSearchId + ') to a Friend' );
				} else if (myDevelopmentId) {
					$j( "#development_id" ).val(myDevelopmentId);
					tips.text( 'Email this Development to a Friend' );
				} else if (myListingId) {
					$j( "#listing_id" ).val(myListingId);
					tips.text( 'Email My Listing (' + myListingId + ') to a Friend' );
				}
				$j( "#dialog-form" ).dialog( "open" );
			});
	});

