// TestStarted - A redirection page to notify the user that the test has been started
func TestStarted(w http.ResponseWriter, r *http.Request) {
	valid := auth.CheckSessionID(r)

	if valid {
		servePageStatic(w, r, "../html/dashboard/testnotify.html")
	} else {
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
// Dashboard - Handles URLs referencing dashboard
func Dashboard(w http.ResponseWriter, r *http.Request) {
	valid := auth.CheckSessionID(r)

	if valid {
		servePageStatic(w, r, loc+"/html/dashboard.html")
	} else {
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
// Reports - serves the reports web page
func Reports(w http.ResponseWriter, r *http.Request) {
	valid := auth.CheckSessionID(r)

	if valid {
		rd := reporter.ReportData{}
		buildReport(&rd)
		servePageDynamic(w, r, loc+"/html/dashboard/reports.html", rd)
	} else {
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
// SaveConfig - Save the configuration settings and then reload the configuration
// page (which will automatically reload the new settings)
func SaveConfig(w http.ResponseWriter, r *http.Request) {
	valid := auth.CheckSessionID(r)

	if valid {
		// Save Configuration Settings
		saveConfigToStruct(r)
		http.Redirect(w, r, "/dashboard/configure", http.StatusFound)
	} else {
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
// Configure - serves the configuration page
func Configure(w http.ResponseWriter, r *http.Request) {
	valid := auth.CheckSessionID(r)

	// Build struct to serve page with:
	configStruct := configPage{}
	buildConfigStruct(&configStruct)

	if valid {
		servePageDynamic(w, r, loc+"/html/dashboard/config.html", configStruct)
	} else {
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
// StartTest - serves the start test web page
func StartTest(w http.ResponseWriter, r *http.Request) {
	valid := auth.CheckSessionID(r)

	ts := testPage{}
	buildTestStruct(&ts)

	// Copy the error message over and display it and then erase it
	// this way the message will only be displayed when there is an
	// actual error
	if errMsg != "" {
		ts.Msg = errMsg
		errMsg = ""
	}

	if valid {
		servePageDynamic(w, r, loc+"/html/dashboard/starttest.html", ts)
	} else {
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
// SaveTest - The test has had it's "test specific settings "
func SaveTest(w http.ResponseWriter, r *http.Request) {
	valid := auth.CheckSessionID(r)

	if valid {
		if r.FormValue("runlength") == "" || r.FormValue("location") == "" {
			errMsg = "You need to enter a run length and a location!"
			http.Redirect(w, r, "/dashboard/start_test", http.StatusFound)
		} else {
			td := tools.TestData{}
			td.Runlen = r.FormValue("runlength")
			td.Ext_ip = r.FormValue("externalIP")
			td.Ext_url = r.FormValue("externalURL")
			td.Location = r.FormValue("location")
			td.Ping_delay = r.FormValue("pingdelay")
			td.Speedtest_delay = r.FormValue("speedtestedelay")
			td.Speedtestfile = r.FormValue("stestfileloc")

			pi, err := tools.SetupTest(td)

			if err != nil {
				errMsg = "No ip associated with the key could be found."
				http.Redirect(w, r, "/dashboard/start_test", http.StatusFound)
			}

			reporter.SetStartTime(time.Now().Format("Jan 02, 2006 - 15:04"))
			reporter.SetLocation(td.Location)

			runlen, _ := strconv.Atoi(td.Runlen)
			logger.WriteString("Running test for: " + td.Runlen)
			// go tools.RunTest(pi, runlen)
			tools.RunTest(pi, runlen)
			http.Redirect(w, r, "/dashboard/reports", http.StatusFound)
		}
	} else {
		http.Redirect(w, r, "/", http.StatusFound)
	}
}