	// increase the default animation speed to exaggerate the effect
	$.fx.speeds._default = 1000;

	$(function() {
		/* declare global var to store notify request id */
		var notifyProductID;
		
		var name = $( "#name" ),
			email = $( "#email" ),
			password = $( "#password" ),
			allFields = $( [] ).add( email ),
			tips = $( ".validateTips" ),
			feedback = $( "#feedback" );

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

		function updateFeedback( t ) {
			feedback
				.html( t );	
		}

		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;
			}
		}
		
		$( "#notify-dialog-form" ).dialog({
			autoOpen: false,
			height: 300,
			width: 350,
			modal: true,
			show: "fade",
			hide: "fade",
			buttons: {
				"Notify Me": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength( email, "email", 6, 80 );

					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. mrtee@loudclothing.com" );

					if ( bValid ) {
						
						// put info in notifyme table
						var dataString = 'email='+ email.val() + '&pID=' + notifyProductID;
						//alert (dataString);return false;
						// NB: target url must be on same server as this page for this to work
						var jqxhr = $.ajax({
							type: "POST",
							url: "./vsadmin/ajax_notifyme.php",
							data: dataString,
							success: function() {
							  // Do stuff when ajax call succeeds
							},
							error: ajaxError
						});
						
						// hide the input form
						$("#notifyForm").hide();
						// show a message
						updateFeedback( '<p>Thank you - we\'ll be in touch as soon as stock arrives.</p><p>This window will close in a moment.</p>' );
						// disable the clicked button
						var notifyGoButton = $(":button:contains('Notify Me')");
						notifyGoButton.button( "option", "disabled", true );
						// pause, then autoclose window
						setTimeout(function() {
							$( "#notify-dialog-form" ).dialog( "close" );
							// re-enable go button
							notifyGoButton.button( "option", "disabled", false );
						}, 3000 );
					}
				},
				"Cancel": function() {
					updateFeedback( '' );
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
				// reset validation info
				tips.text( 'Enter your email address below and we\'ll send you an email when this product is available to buy.' )
				// reset thankyou message
				updateFeedback( '' );
				// unhide the form for next time
				$("#notifyForm").show();
			}
		});

		$( ".notify-me" )
			.click(function() {
				notifyProductID = $(this).attr("value");
				$( "#notify-dialog-form" ).dialog( "open" );
				// don't open original link
				return false;
			});
	});
	
	function ajaxError(request, type, errorThrown)
	{
		var message = "There was an error with the AJAX request.\n";
		switch (type) {
			case 'timeout':
				message += "The request timed out.";
				break;
			case 'notmodified':
				message += "The request was not modified but was not retrieved from the cache.";
				break;
			case 'parseerror':
				message += "XML/Json format is bad.";
				break;
			default:
				message += "HTTP Error (" + request.status + " " + request.statusText + ").";
		}
		message += "\n";
		// Uncomment for debugging
		// alert(message);
	}
