Not part of fixing the problem, but just want to throw this nit pick out there:
mainContent.setAttribute.style.textAlign = "center";
The div align element is not technically supported in HTML5. Basically, most any HTML formatting (non-CSS) is considered to be deprecated. Using these attributes may cause headaches down the road because CSS overrides them.