$(function() {		
		var email = $("#email"),
			password = $("#password"),
			allFields = $([]).add(email).add(password),
			tips = $("#validateTips");

		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}

		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;
			}
		}
	                 

		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 300,
			modal: true,
            title: 'Login into NEplan',
			buttons: {
				'Login': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');
					
					bValid = bValid && checkLength(email,"email",6,80);
					bValid = bValid && checkLength(password,"password",5,16);
					
                    var email1 = email.val();
                    var pass1 = password.val();

                    
                    
					if (bValid)
                    {      
                        
                       $.post("loginAjax.php", { email: email1 , password: pass1 }, function(data) {
                            $("#main").html(data);
                             return false;

                            if (!(data == 'correct'))
                                {
                                  updateTips("Incorrect email address or password.");
                                  return false;
                                }
                                else
                                {
                                  return true;
                                }
                       });
                       
                        $(this).dialog('close');
					}
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		// new lines
        $('#login').click(function() {$('#dialog').dialog('open');});

	});