pranayaryal
8/17/2017 - 7:58 PM

Angular Controller Example

Angular Controller Example

angular
	.module('sarApp')
	.controller('AdminDonationsController', AdminDonationsController)

// function AdminDonationsController($scope, $http, DonationValidationService, PaymentService, CommentService, $window, toastr) {
function AdminDonationsController($scope, $http, PaymentService, CommentService, $window, toastr) {


	// //This will run in the init function
	// $scope.getAllDonors = function () {
	// 	$http.post('/api/v1/donation/all_donors').success(function(data) {
	//
	// 		if (data.success) {
	// 			$scope.donors_list = data.success;
	// 		}
	// 	});
	// };

	$scope.nullifyDonor = function() {
		$scope.donor = {
			id: 0,
			showExistingCards: false,
			acn: '',
			nssar_id: '',
			type_id: 1,
			do_not_solicit: 0,
			gwf: '',
			gwf_date: '',
			status_id: '',
			state_id: '',
			chapter_id: '',
			state_number: '',
			chapter_number: '',
			user_id: 0,
			referral_source: '',

			//donor data form
			contact: {
				title: '',
				chapter_id: '',
				prefix: '',
				suffix: '',
				first_name: '',
				middle_name: '',
				last_name: '',
				address: '',
				address_2: '',
				city: '',
				state: 0,
				country: '',
				postal_code: '',
				email: '',
				phone_number: '',
				phone_type: '',
				phone_number_2: ''
			}

		}
	};
	$scope.init = function() {
		// $scope.getAllDonors();
		//get the donor cards
		$scope.donor_search = '';
		$scope.donorHasBeenValidated = false;
		$scope.emailValidated = true;
		$scope.nssarValidated = true;
		$scope.acnValidated = true;
		$scope.paymentDetails = PaymentService.getPaymentDetails();
		//is a string which is displayed in the blade file if there are no comments in the database;
		$scope.noComments = '';

		//this is used to get the fields of the comments table
		$scope.comment = CommentService.getComment();
		$scope.disableSubmit = false;
		CommentService.setComment($scope.comment);
		//payload sets the object_id andResponse

		$scope.allComments = '';
		$scope.showProcessingSpinner = false;
		$scope.storedComments = '';

		$scope.showSubmitButton = false;
		$scope.stripeReturnedErrors = false;
		//Donor related
		$scope.showDonationSection = false;
		// $scope.showDonationSubmit = false;

		$scope.replyEnabled = {};
		$scope.replyMessage = {};
		$scope.replyRecipients = {};
		$scope.replyComplete = {};

		$scope.stripe_error = {
			present: false,
			content: ''
		};


		$scope.errors = {
			donor: {
				//donor data form
				state: '',
				contact: {
					first_name: false,
					last_name: false,
					address: false,
					city: false,
					state: false,
					postal_code: false,
					phone_number: false,
					email: false
				}

			},
			donation: {
				type_id: 1,
				status_id: 1,
				transaction_id: '',
				attribution_id: '',
				state_id: 0,
				chapter_id: '',
				committee_id: '',
				employer_matched: '',
				employer_contact_id: '',
				is_tribute: 0,
				tribute_name: '',
				anonymous: 0,
				do_not_solicit: 0,
				attribution_nssar_id: false,
				attribution_acn: false,

			},

			employer_contact: {
				company: false,
				email: false,
				phone_number: false,
				first_name: false,
				last_name: false
			},

			paymentDetails: {
				payment: {
					card_id: '',
					amount: '',
					card_number: '',
					name_on_card: '',
					expiration_month: '',
					expiration_year: '',
					card_cvv: '',
					store_card: '',
					description: '',
					check_number: ''
				}
			},

			comment: ''
		};


		//Payment Related
		//$scope.showPaymentSection = false;
		$scope.user = {
			id: 0
		};

		//$scope.nullifyDonor();

		//donation data form
		$scope.donation = {
			donor_id: '',
			type_id: '',
			status_id: 2,
			transaction_id: '',
			attribution_id: '',
			attribution_nssar_id: '',
			attribution_acn: '',
			state_id: 0,
			chapter_id: '',
			committee_id: '',
			employer_matched: '',
			is_tribute: false,
			tribute_name: '',
			anonymous: 0,
			do_not_solicit: 0,
			fund_id: 0,
			campaign_id: 0,

			employer_contact: {
				nssar_id: '',
				title: '',
				type_id: 5,
				company: '',
				position: '',
				title: '',
				prefix: '',
				first_name: '',
				middle_name: '',
				last_name: '',
				suffix: '',
				address: '',
				address_2: '',
				city: '',
				state: '',
				postal_code: '',
				country: '',
				phone_number: '',
				phone_number_2: '',
				email: ''

			}

		};
		$scope.nullifyDonor();

		$scope.donors_list = {};

		if (typeof donor !== 'undefined' && donor) {
			var payload = {
				donor_id: donor.id
			};
			if (donor.id != 0) {
				$http.post('/api/v1/donor/get_detail', payload).success(function(data) {
					$scope.donor = data.donor;
					$scope.donor.cards = data.cards;
					$scope.showDonationSection = true;
					$scope.showDonationSubmit = true;
					$scope.donation.donor_id = $scope.donor.id;
					angular.forEach($scope.errors.donor.contact, function(value, key) {
						$scope.errors.donor.contact[key] = '';
					});
					$scope.getUser(donor.user_id);

					$scope.showChapters = true;
					setTimeout(function() {
						$scope.getChapters($scope.donor.state_id);
					}, 100);
				});
			}

		}

	};

	$scope.init();

	$scope.getUser = function(user_id) {

		CommentService.setUser(user_id);
		CommentService.getTheUser(function(data) {

			$scope.selectedUser = data.user;
			console.log($scope.selectedUser.profile);

			if ($scope.selectedUser.profile !== null) {
				$scope.errors.donor.user = false;
				console.log('you are in not null section');
				$scope.donor.contact.city = $scope.selectedUser.profile.city ? $scope.selectedUser.profile.city : '';
				$scope.donor.contact.full_name = $scope.selectedUser.profile.full_name;
				var full_name = $scope.selectedUser.profile.full_name;
				var split = full_name.split(" ");
				$scope.donor.contact.first_name = split[0];
				$scope.donor.contact.last_name = split[1];
				$scope.donor.contact.email = $scope.selectedUser.profile.email ? $scope.selectedUser.profile.email : '';
				$scope.donor.contact.middle_name = $scope.selectedUser.profile.middle_name;
				$scope.donor.contact.state = $scope.selectedUser.profile.state;
				$scope.donor.contact.postal_code = $scope.selectedUser.profile.postal_code;
				$scope.donor.contact.address = $scope.selectedUser.profile.street_address;

				$scope.errors.donor.contact.email = false;
				$scope.errors.donor.contact.phone_number = false;
				//return;
			} else {
				//if $scope.selectedUser.profile(response) returns a null object create an error
				console.log('the user returned is null');
				$scope.errors.donor.user = true;
				$scope.nullifyDonor(); //The donor has to be nulled if error
			}


		});

	};


	$scope.selectedUserCallback = function(user) {
		if (user.originalObject.id !== 'undefined') {
			$scope.getUser(user.originalObject.id);
			$scope.donor.user_id = user.originalObject.id;
		}

	};

	//this function is to choose payment option
	$scope.toggleForms = function(form_type) {

		switch (form_type) {

			case 1:
				$scope.paymentDetails.payment.type_id = 1;
				break;
			case 2:
				$scope.paymentDetails.payment.type_id = 2;
				break;
			case 3:
				$scope.paymentDetails.payment.type_id = 3;
				break;
			case 4:
				$scope.paymentDetails.payment.type_id = 4;
			default:

		}

	};

	//Donor specific
	$scope.validateDonorInfo = function() {
        console.log($scope.donor.contact.phone_number_2);

		//If user has been selected from dropdown, no need to validate
		// if ($scope.donor.user_id > 0) {
		// 	$('#after_donor').hide();
		// 	$scope.showDonationSection = true;
		// 	$scope.showDonationSubmit = true;
		// 	return true;
		// }
		var valid = true;
		$scope.errors.donor.state = '';

		angular.forEach($scope.errors.donor.contact, function(value, key) {
			$scope.errors.donor.contact[key] = '';
		});


		$('#after_donor').show();
		$scope.showDonationSection = false;
		$scope.showDonationSubmit = false;

		if ($scope.donor.contact.first_name === null || $scope.donor.contact.first_name.length === 0) {
			console.log("herek");
			$scope.errors.donor.contact.first_name = true;
			valid = false;
		}

		if ($scope.donor.contact.last_name === null || $scope.donor.contact.last_name.length === 0) {
			$scope.errors.donor.contact.last_name = true;
			valid = false;
		}


		if ($scope.donor.contact.address === null || $scope.donor.contact.address.length === 0) {
			$scope.errors.donor.contact.address = true;
			valid = false;
		}


		if ($scope.donor.contact.city === null || $scope.donor.contact.city.length === 0) {
			$scope.errors.donor.contact.city = true;
			valid = false;
		}

		if ($scope.donor.contact.state == 0) {
			$scope.errors.donor.contact.state = true;
			valid = false;
		}

		/*if ($scope.donor.contact.email === null || $scope.donor.contact.email.length === 0) {
		    $scope.errors.donor.contact.email = true;
		}*/

		if ($scope.donor.contact.postal_code === null || $scope.donor.contact.postal_code.length === 0) {
			$scope.errors.donor.contact.postal_code = true;
			valid = false;
		}


		var payloadAcn = {
			email: $scope.donor.contact.email,
			nssar: $scope.donor.nssar_id,
			acn: $scope.donor.acn
		};
		//No need to check existing donors
		if ($scope.donor.id > 0 & valid) {

			$('#after_donor').hide();
			$scope.showDonationSection = true;
			$scope.showDonationSubmit = true;
			return valid
		}


		if (valid) {
			CommentService.setPayload(payloadAcn);
			var promise = CommentService.emailNssarAcnExists();
			promise.then(function(data) {
				if (data.email > 0) {
					console.log('you are in email');
					$scope.errors.donor.contact.email = true;
					toastr.error('Error!', 'That email is already in use');
					valid = false;

				}
				if (data.nssar > 0) {
					console.log('you are in nssar')
					$scope.errors.donor.contact.nssar_id = true;
					toastr.error('Error!', 'A donor with that NSSAR # already exists');
					valid = false;
				}

				if (data.acn > 0) {
					console.log('you are in acn');
					$scope.errors.donor.contact.acn = true;
					toastr.error('Error!', 'A donor with that ACN # already exists');
					valid = false;
				}
				if (valid) {

					$('#after_donor').hide();
					$scope.showDonationSection = true;
					$scope.showDonationSubmit = true;
				}

				return valid;

			});
		}


	};

	$scope.updateDonorDetails = function() {
		var payload = {};
		if ($scope.validateDonorInfo() && $scope.donor.id) {
			payload.donor = $scope.donor;
			$http.post('/api/v1/donor/update', payload).success(function(data) {
				if (data == 'success') {
					toastr.success('Success!', 'Donor was successfully updated');
				} else {
					toastr.error('Error!', 'There was an error updating the donor: ' + data);
				}
			});
		} else {
			toastr.error('Error!', 'There is a problem with the Donor Information');
		}
	};


	$scope.validateDonationInfo = function() {

		var validate = true;

		angular.forEach($scope.errors.donation, function(key, value) {
			$scope.errors.donation[value] = '';
		});

		if ($scope.donation.is_tribute) {

			if ($scope.donation.attribution_nssar_id.length == 0) {
				$scope.errors.donation.attribution_nssar_id = true;
				validate = false;
			}

			if ($scope.donation.attribution_acn.length == 0) {
				$scope.errors.donation.attribution_acn = true;
				validate = false;

			}

		}



		if ($scope.donation.type_id.length === 0) {
			$scope.donation.type_id = 1;
		}


		if ($scope.donation.status_id.length === 0) {
			$scope.donation.status_id = 1;
		}

		if ($scope.donation.anonymous.length === 0) {
			$scope.errors.donation.anonymous = "Anonymous is required";
			validate = false;
		}
		if ($scope.donation.do_not_solicit.length === 0) {
			$scope.errors.donation.do_not_solicit = "Do Not Solicit is required";
			validate = false;
		}

		// if ($scope.donation.employer_matched) {
		// 	if ($scope.donation.employer_contact.company === 0) {
		// 		$scope.errors.donation.employer_contact.company = "Employer Company Name is required";
		// 		validate = false;
		// 	}

		// 	if ($scope.donation.employer_contact.email === 0) {
		// 		$scope.errors.donation.employer_contact.email = "Employer email is required";
		// 		validate = false;
		// 	}

		// 	if ($scope.donation.employer_contact.phone_number === 0) {
		// 		$scope.errors.donation.employer_contact.phone_number = "Employer phone_number is required";
		// 		validate = false;
		// 	}

		// 	if ($scope.donation.employer_contact.first_name === 0) {
		// 		$scope.errors.donation.employer_contact.first_name = "Employer First Name is required";
		// 		validate = false;
		// 	}

		// 	if ($scope.donation.employer_contact.last_name === 0) {
		// 		$scope.errors.donation.employer_contact.last_name = "Employer Last Name is required";
		// 		validate = false;
		// 	}


		// }


		if (validate) {
			//$('#after_donation').hide();
			$scope.showPaymentSection = true;

			// $scope.showDonationSubmit = false;
		}

		return validate;

	};

	$scope.validateCardForm = function() {

		var reg = /^\d*(\.\d+)?$/; //regular expre digit check
		var valid = true;

		angular.forEach($scope.errors.paymentDetails.payment, function(key, value) {
			$scope.errors.paymentDetails.payment[value] = '';
		});


		if ($scope.paymentDetails.payment.amount === 0) {

			$scope.errors.paymentDetails.payment.amount = 'Amount is required';
			valid = false;
		}

		if (!reg.test($scope.paymentDetails.payment.amount)) {

			$scope.errors.paymentDetails.payment.amount = 'Amount has to be a number';
			valid = false;
		}


		if ($scope.paymentDetails.payment.name_on_card.length == 0) {
			$scope.errors.paymentDetails.payment.name_on_card = 'The name in card is required';
			valid = false;
		}

        if ($scope.paymentDetails.payment.card_number.length == 0) {
            $scope.errors.paymentDetails.payment.card_number = 'This cannot be empty';
        }

        if ($scope.paymentDetails.payment.expiration_month.length == 0) {
            $scope.errors.paymentDetails.payment.expiration_month = 'Required';
            valid = false;
        }

        if ($scope.paymentDetails.payment.expiration_year.length == 0) {
            $scope.errors.paymentDetails.payment.expiration_year = 'Required';
            valid = false;
        }

        if ($scope.paymentDetails.payment.card_cvv.length == 0) {
            $scope.errors.paymentDetails.payment.card_cvv = 'Required';
            valid = false;
        }


		if ($scope.paymentDetails.payment.amount === 0) {

			$scope.errors.paymentDetails.payment.amount = 'Amount is required';
			valid = false;
		}



		//Checking whether donor has checked previous cards
		if (typeof $scope.paymentDetails.payment.card_id !== 'undefined' && $scope.paymentDetails.payment.card_id.length > 0) {

			//negating all card errors
			angular.forEach($scope.errors.paymentDetails.payment, function(value, key) {
				$scope.errors.paymentDetails.payment[key] = '';
			});
			valid = true;

		}

		return valid;


	}


	$scope.validateAmount = function() {

		var valid = true;
		$scope.errors.paymentDetails.payment.amount = '';
		var reg = /^\d*(\.\d+)?$/; //regular expre digit check


		if ($scope.paymentDetails.payment.amount.length === 0) {

			$scope.errors.paymentDetails.payment.amount = 'Amount is required';
			valid = false;
		}

		if (!reg.test($scope.paymentDetails.payment.amount)) {
			$scope.errors.paymentDetails.payment.amount = 'Amount has to be a number';
			valid = false;
		}
		return valid;
	};

	$scope.validateCheckForm = function() {

		var valid = true;
		$scope.errors.paymentDetails.payment.check_number = '';


		if (!$scope.validateAmount()) {
			valid = false;
		}

		if ($scope.paymentDetails.payment.check_number.length == 0) {

			$scope.errors.paymentDetails.payment.check_number = 'Check number is required';

			valid = false;
		}

		return valid;
	};


	$scope.validateCashForm = function() {
		var valid = true;
		if (!$scope.validateAmount()) {
			valid = false;
		}
		return valid;

	};


	$scope.submitForm = function() {
        console.log("month: " + $scope.errors.paymentDetails.payment.expiration_month);
		// console.log($scope.paymentDetails.payment.type_id);
		var valid = true;
		$scope.disableSubmit = true;
		//valid = $scope.validateEmployerInfo();

		switch ($scope.paymentDetails.payment.type_id) {
			case 1:
				if (!$scope.validateCashForm()) {
					valid = false;
				}
				break;
			case 2:
				if (!$scope.validateCheckForm()) {
					valid = false;
				}
				break;
			case 3:
				if (!$scope.validateCardForm()) {
					valid = false;
				}
				break;
			default:
				break;

		}

		//reset all attribution-related values to null if the donation was not a tribute
		if (!$scope.donation.is_tribute) {
			$scope.donation.attribution_id = '';
			$scope.donation.attribution_nssar_id = '';
			$scope.attribution_acn = '';
		}

		var payload = {
			donor: $scope.donor,
			donation: $scope.donation,
			paymentDetails: $scope.paymentDetails
		};

		if (valid) {
			$scope.showProcessingSpinner = true;
			$scope.disableSubmit = true;
			$http.post('/api/v1/donation/process', payload).success(function(data) {
				if (data.error) {
					$scope.showProcessingSpinner = false;
					$scope.disableSubmit = false;
					$scope.stripeReturnedErrors = true;
					$scope.stripeErrorContent = data.error;
				}

				if (data.success) {
				    //data.success is equal to the id of the donation just created
					$scope.stripeReturnedErrors = false;
					$scope.showProcessingSpinner = false;
					//$window.location.href = '/donations/manage';
					toastr.success('Success', 'You payment has processed successfully!');
					setTimeout(function() {
                        $window.location.href = '/donations/donation/' + data.success + '/edit';
					}, 1000);
					// alert(data.success);
					// $scope.disableSubmit = true;
				}

			});
		}


	};


	$scope.saveComment = function() {
		var valid = true;
		if ($scope.comment.body.length == 0) {
			$scope.errors.comment = 'You must fill the body';
			var valid = false;
		}

		if (valid) {
			$scope.errors.comment = '';

			$scope.payload = {
				object_id: object_id,
				object_type_id: object_type_id,
				comment: $scope.comment

			};

			CommentService.setPayload($scope.payload);
			CommentService.storeComment(function(data) {
				if (data.success) {
					$scope.noComments = '';
					$scope.allComments = data.success;

					$scope.comment = {
						body: '',
						type_id: '',
						follow_up_date: '',
						follow_up_link: '',
						completed: '',
						completed_date: '',
						reply_message: ''
					};
				}
			});
		}
	};


	$scope.removeComment = function(index) {

		var deleteComment = $window.confirm('Are you  sure you want to delete?');

		if (deleteComment) {
			$scope.payload = {
				object_id: object_id,
				object_type_id: object_type_id,
				comment: index
			};

			CommentService.setPayload($scope.payload);
			CommentService.removeComment(function(data) {
				$scope.handleResponse(data);
			});
		}


	};

	$scope.getTheComments = function() {
		$scope.payload = {
			object_id: object_id,
			object_type_id: object_type_id,
			comment: ''
		};
		CommentService.setPayload($scope.payload);
		CommentService.getComments(function(data) {
			$scope.handleResponse(data);
		});
	};

	$scope.handleResponse = function(data) {
		//success returns a list of all comments associated with the model instance
		if (data.success) {
			$scope.noComments = '',
				$scope.allComments = data.success;
		} else if (data.error) { //error returns a string
			$scope.allComments = '',
				$scope.noComments = 'Sorry there are no comments in the database, please create one on the left';

		}
	};

	$scope.getChapters = function(state_id) {
		// $scope.donation.state_id = state_id;
		// console.log($scope.donation.state_id);
		$scope.chapters = [];
		$http.get('/api/v1/chapters/list/' + state_id)
			.success(function(data) {

				angular.forEach(data[0], function(id, chapter) {

					var selected = false;
					if (id == $scope.donor.chapter_id) {
						selected = true;
					}

					var tempChapter = {
						'id': id,
						'name': chapter,
						'selected': selected
					};
					$scope.chapters.push(tempChapter);
				});
				$scope.showChapters = true;
			});
	};

	setTimeout(function() {
		console.log($scope.donor.state_id);
		if ($scope.donor.state_id !== 'undefined' && $scope.donor.state_id > 0) {
			$scope.getChapters($scope.donor.state_id);
			$scope.showChapters = true;
			$scope.selectedChapter = parseInt($scope.donor.chapter_id);

		}
	}, 100);

	$scope.getExistingDonor = function(donor_id) {
		var payload = {
			donor_id: donor_id
		};
		if (donor_id != 0) {

			$http.post('/api/v1/donor/get_detail', payload).success(function(data) {

				$scope.donor = data.donor;
				$scope.donor.cards = data.cards;
				$scope.showDonationSection = false;
				$scope.showDonationSubmit = false;
				$scope.donation.donor_id = $scope.donor.id;
				$scope.donor.acn = parseInt($scope.donor.acn);
				// angular.forEach($scope.errors.donor.contact, function (value, key) {
				//     $scope.errors.donor.contact[key] = '';
				// });

				$scope.showChapters = true;
				setTimeout(function() {
					$scope.getChapters($scope.donor.state_id);
				}, 100);

			});

		}
		$scope.showDonationSection = false;
		$scope.showDonationSubmit = false;
		// if user selects '-select donor-' dropdown, then we nullify the donor model
		$scope.nullifyDonor();
		var payload = {
			donor: $scope.donor,
			donation: $scope.donation,
			paymentDetails: $scope.paymentDetails
		};
	};

	$scope.editDonation = function() {

		$http.post('/api/v1/donation/' + object_id).success(function(data) {

			if (data) {
				$scope.donor = data.donor
				$scope.donation = data.donation;
				window.location = '/donations/donation/' + data.donation.id + '/edit';

			}
		});
	};

	$scope.useCardForDonation = function(card_id) {

		$scope.paymentDetails.payment.card_id = card_id;


	};


	$scope.getUserDetails = function(userid) {
		if (userid != 0) {

			$http.post('/api/v1/donation/user_details/' + userid).success(function(data) {
				if (data.success) {
					$scope.showNewForm = false;
					$scope.donor.contact = {
						last_name: data.success.last_name,
						email: data.success.email,
						city: data.success.city,
						state: data.success.state,
						first_name: data.success.first_name,
						address_2: data.success.street_address_2,
						address: data.success.street_address,
						postal_code: data.success.postal_code,
						phone_number: data.success.phone
					};
					$scope.showDonationSection = false;
					$scope.showDonationSubmit = false;
					// nullify all errors
					angular.forEach($scope.errors.donor.contact, function(value, key) {
						$scope.errors.donor.contact[key] = '';
					});
				}


			});
		}

		$scope.showDonationSection = false;
		$scope.showDonationSubmit = false;


		//if user selects '-select user-' dropdown, then we nullify the donor model
		$scope.nullifyDonor();

		var payload = {
			donor: $scope.donor,
			donation: $scope.donation,
			paymentDetails: $scope.paymentDetails
		};

	};

	$scope.validateEmployerInfo = function() {
		var valid = true;
		if ($scope.donation.employer_matched == 0) {
			return true;
		}

		angular.forEach($scope.errors.employer_contact, function(value, key) {
			$scope.errors.employer_contact[key] = false;
		});

		if ($scope.donation.employer_contact.company == null || $scope.donation.employer_contact.company.length === 0) {
			$scope.errors.employer_contact.company = true;
			valid = false;
		}

		if ($scope.donation.employer_contact.email == null || $scope.donation.employer_contact.email.length === 0) {
			$scope.errors.employer_contact.email = true;
			valid = false;
		}

		if ($scope.donation.employer_contact.phone_number == null || $scope.donation.employer_contact.phone_number.length === 0) {
			$scope.errors.employer_contact.phone_number = true;
			valid = false;
		}

		if ($scope.donation.employer_contact.first_name == null || $scope.donation.employer_contact.first_name.length === 0) {
			$scope.errors.employer_contact.first_name = true;
			valid = false
		}

		if ($scope.donation.employer_contact.last_name == null || $scope.donation.employer_contact.last_name.length === 0) {
			$scope.errors.employer_contact.last_name = true;
			valid = false
		}

		return valid;
	};

	$scope.analyzeCard = function(card_number) {
		var card_regs = [
			/^(?:3[47][0-9]{13})$/, // AmEx
			/^(?:4[0-9]{12}(?:[0-9]{3})?)$/, // Visa
			/^(?:5[1-5][0-9]{14})$/, // MasterCard
			/^(?:6(?:011|5[0-9][0-9])[0-9]{12})$/ // Discover
		];
		var validated = false;
		var card_formatted = $scope.paymentDetails.payment.card_number.replace(/^[a-z]+$/i, '');
		$scope.paymentDetails.payment.card_number = card_formatted;

		if ($scope.paymentDetails.payment.card_number && $scope.paymentDetails.payment.card_number.length >= 19) {
			$scope.paymentDetails.payment.card_number = $scope.paymentDetails.payment.card_number.slice(0, -1);
		}
		if ($scope.paymentDetails.payment.card_number.length > 14) {
			angular.forEach(card_regs, function(card_reg) {
				var match = $scope.paymentDetails.payment.card_number.match(card_reg);

				if (match !== null) {
					validated = true;
				}
			});
		}
		if (!validated) {
			$scope.errors.paymentDetails.payment.card_number = 'Card error, please use a valid American Express, Visa, Mastercard, Discover card number';
		} else {
			$scope.errors.paymentDetails.payment.card_number = '';
		}

	};


	$scope.selectedDonor = function(object) {
		$scope.getExistingDonor(object.originalObject.id)
	};


	$scope.toggleReplyEnabled = function(comment_id) {
		$scope.replyEnabled = []; // Just reset, we only want one box at a time
		$scope.replyEnabled[comment_id] = true;
	};

	$scope.replyToComment = function(comment_id) {
		var comment = {
			id: comment_id,
			reply_message: $scope.replyMessage[comment_id],
			recipients: $scope.replyRecipients[comment_id],
			completed: $scope.replyComplete[comment_id]
		}
		if (comment.reply_message.length > 0) {
			toastr.success('Success', 'You reply has been recorded!');
			$http.post('/api/v1/comments/reply', comment).success(function(data) {

				/*if (data == 'exists') {
				 $scope.errors.donor.contact.email = true;
				 $scope.emailNotValidated = true;
				 toastr.error('Error!', 'A donor with that email already exists. Are you sure you want to proceed?');

				 }
				 if (data == 'nope') {
				 $scope.errors.donor.contact.email = false;
				 $scope.emailNotValidated = false;
				 }*/
				$scope.replyEnabled = [];
				$scope.getAllComments();


			});
		} else {
			toastr.error('Error', 'You are trying to reply with no message. Please check and try again!');
		}
	};

	$scope.checkThatDonor = function() {

		var valid = true;
		//$scope.errors.donor.state = '';

		//angular.forEach($scope.errors.donor.contact, function (value, key) {
		//$scope.errors.donor.contact[key] = '';
		//});

		console.log(CommentService.checkDonorDetails($scope.donor));
		//console.log( CommentService.getDonorErrors());


		$('#after_donor').show();
		$scope.showDonationSection = false;
		$scope.showDonationSubmit = false;

	};

	// $scope.showStripeForm = function () {
	//     //the global variable stripe has been set in 'donations.donate.blade.php'
	//     stripe.open({
	//         name: 'Donation',
	//         description: 'By' +  $scope.donor.contact.first_name,
	//         zipCode: false,
	//         amount: $scope.paymentDetails.payment.amount * 100,
	//         token : function (token) {
	//             $scope.stripeToken = token.id;
	//             $scope.stripeEmail = token.email;
	//
	//             var payload = {
	//                 donor: $scope.donor,
	//                 donation: $scope.donation,
	//                 token: token.id
	//             };
	//             $http.post('/api/v1/donation/process', payload).success(function (data) {
	//                 if (data.error) {
	//                     $scope.showProcessingSpinner = false;
	//                     $scope.disableSubmit = false;
	//                     $scope.stripeReturnedErrors = true;
	//                     $scope.stripeErrorContent = data.error;
	//                 }
	//
	//                 if (data.success) {
	//                     $scope.showProcessingSpinner = false;
	//                     $window.location.href = '/donations/manage';
	//                     // alert(data.success);
	//                     // $scope.disableSubmit = true;
	//                 }
	//
	//             });
	//
	//         }
	//     });
	// };

	// $scope.getDonorCards = function ()
	//     if ($scope.donor.id > 0) {
	//     var payload = {
	//         'donor_id': $scope.donor.id
	//     };
	//     $http.post('/api/v1/donation/getCards', payload).success(function (data) {
	//         if (data) {
	//             $scope.donor.cards = data;
	//         }
	//
	//
	//     });
	// }

	// };



}