
      jQuery(document).ready(function() {




	email = jQuery("#newsletter_email"),
	
			allFields = jQuery([]).add(email),
			tips = jQuery(".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("Error. Length of " + n + " must be between "+min+" and "+max+" characters.");
				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;
			}

		}
		
		jQuery("#newsletter_dialog").dialog({
			autoOpen: false,
			height: 270,
			width: 350,
			modal: true,
			buttons: {
				'Add my email': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

//					bValid = bValid && checkLength(name,"username",3,16);
					bValid = bValid && checkLength(email,"email",6,80);
//					bValid = bValid && checkLength(password,"password",5,16);

//					bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+jQuery/i,"Username may consist of a-z, 0-9, underscores, begin with a letter.");
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp(email,/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i,"Email address doesn't seem to be valid.");
//					bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+jQuery/,"Password field only allow : a-z 0-9");
					
					if (bValid) {
					   alert("Valid");
//						jQuery('#users tbody').append('<tr>' +
//							'<td>' + name.val() + '</td>' + 
//							'<td>' + email.val() + '</td>' + 
//							'<td>' + password.val() + '</td>' +
//  							'</tr>'); 
						jQuery(this).dialog('close');
					}
					else
					{
					   alert("Invalid");
					}
				},
				Cancel: function() {
					jQuery(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		





/*
jQuery('#newsletter a').click(function(event) {
    event.preventDefault();

    
    jQuery('#newsletter_dialog').dialog('open');

});*/

      });
      
      
      
      
      
      
				


