|If it charges fully or at least mostly, it is probably the charge cord as it is probably supplying a reduced amount of current and with the draw of the laptop on, it is also dropping down to the minimum voltage to run the laptop and not enough for charging.|
Another possibility is that the charging circuit in the laptop is going or bad and with the laptop off, the full power of the power cord is getting through to the battery (possibly bypassing the charge circuit).
If the battery takes a good charge at some point, it is not the battery though.
To be absolutely sure you would have to either borrow a power cord from someone with a similar system or purchase an aftermarket one for your system and test it that way.
If it is not the power cord and the system is 3 or 4 years or more old, then seriously consider replacing it rather than spending a lot on trying to fix it.
You have to be a little bit crazy to keep you from going insane.