angular.module('AddressBook',[])
.service("contactService",function($http,$q, validationService){
	var contactUrl = 'http://localhost:3000/contacts';
	var contacts = undefined;	
	
	function getContacts(){
		var deferral = $q.defer();
		
		$http.get(contactUrl)
		.then(function(res){
			deferral.resolve(res.data);
		})
		
		return deferral.promise;
	};
	
	
	return {
		getContacts:getContacts	
	}
})

.service("validationService",function(){
	
	function isValidContact(contact){
		if (!contact) return null;
		
		var valid = true;
		
		if (!contact.name || !isValidName(contact.name)) valid = false;
		if (!contact.email) valid = false;
//		if (contact.age && !validAge()) valid = false;
//		if (contact.occupation && !validOccupation()) valid = false;
		
		return valid;
	}
	
	function isValidName(name){
		return typeof name === 'string' && name.length > 1
	};
	
	return {
		isValidName:isValidName,
		isValidContact:isValidContact
	}
})

.controller("ContactList",function($scope,contactService){
	console.log("contactl ist init.");
	contactService.getContacts().then(function(contacts){
		console.log("got contacts.");
		$scope.contacts = contacts;
	})
})

.controller("AddContact",function($scope,contactService,validationService){
	$scope.addContact = function(){
		console.log("Add new contact...",$scope.contact);
		if (!validationService.isValidContact($scope.contact)){
			return false;
		}
	}
})
@@ -550,16 +550,32 @@
 .name %7C%7C
+ contact.name &&
  !isVali
@@ -630,16 +630,65 @@
 ct.email
+ %7C%7C contact.email && !isValidEmail(contact.email)
 ) valid 
@@ -688,34 +688,32 @@
  valid = false;%0A
-//
 %09%09if (contact.ag
@@ -722,17 +722,19 @@
 && !
-v
+isV
 alidAge(
 )) v
@@ -729,16 +729,27 @@
 alidAge(
+contact.age
 )) valid
@@ -758,18 +758,16 @@
  false;%0A
-//
 %09%09if (co
@@ -787,17 +787,19 @@
 ion && !
-v
+isV
 alidOccu
@@ -805,16 +805,34 @@
 upation(
+contact.occupation
 )) valid
@@ -946,16 +946,409 @@
 ngth %3E 1
+;%0A%09%7D;%0A%09%0A%09function isValidOccupation(occupation)%7B%0A%09%09return isValidName(occupation);%0A%09%7D%0A%09%0A%09function isValidEmail(email)%7B%0A%09%09/* this is a great function to write tests for, since it certainly isn't clear%0A%09%09 by reading it if it works correctly. */%0A%09%09return /%5E%5Cw+(%5B-+.'%5D%5Cw+)*@%5Cw+(%5B-.%5D%5Cw+)*%5C.%5Cw+(%5B-.%5D%5Cw+)*$/.exec(email);%0A%09%7D;%0A%09%0A%09function isValidAge(age)%7B%0A%09%09return typeof age === 'number' && age %3E= 18;
 %0A%09%7D;%0A%09%0A%09

add rest of validation functions