A Novel Algorithm for Controlling Active and Reactive Power Flows of Electric Vehicles in Buildings and Its Impact on the Distribution Network