// functions for alarmdotcom.cfm
// tests whether the user has cookies enabled by setting a cookie
//	and then trying to retrieve it back.  If a "true" argument is passed
//	then this function attempts to test whether persistant cookies
//	(i.e. non session cookies) are enabled.
function testClientCookies() {
	
	var testPersistant = false;
	
	if (arguments.length > 0) testPersistant = arguments[0];
	
	if (testPersistant) {
		// save a new cookies with an expiration date set to 5 seconds from now
		var now = new Date();
		var cookiedate = new Date(now.valueOf() + 1000*5);
		document.cookie = "cookieTest=persist;expires=" + cookiedate.toGMTString();
	}
	else {
		// save a session cookie (no expiration specified = lives for the
		//	lifetime of the browser) so that we check for any kind of cookies
		document.cookie = "cookieTest=session"
	}
	
	if ((document.cookie).indexOf("cookieTest") == -1) {
		// cookie not found; all cookies must be disabled
		return false;
	} else {
		return true;
	}
}  // testClientCookies

// function of client-side check(s) that are made when the user tries to log in
function doLoginTimeChecks() {
	// test if Session-cookies (at least) are enabled
	if (testClientCookies()) {
		document.forms['loginForm'].cookieTest.value = '1';
	}
	
	// we cannot test if JavaScript is enabled (because the test itself would
	//	have to be written in JavaScript) but we can set a variable that will
	//	be looked at by login.asp when the user submits the form
	document.forms['loginForm'].JavaScriptTest.value = '1';
	return true;
}

// functions for gpscalculator.cfm
function r2(n) { 

  ans = n * 1000 
  ans = Math.round(ans /10) + "" 
  while (ans.length < 3) {ans = "0" + ans} 
  len = ans.length 
  ans = ans.substring(0,len-2) + "." + ans.substring(len-2,len)
  return ans 
} 

function calcROI(form) 
{
	if ((form.vehicles.value.length == 0) || (form.vehicles.value != parseInt(form.vehicles.value)))
	{
		alert("Please enter the number of vehicles you have.");
		return false;
	}

	if ((form.employees.value.length == 0) || (form.employees.value != parseInt(form.employees.value)))
	{
		alert("Please enter the number of employees assigned to a vehicle per day.");
		return false;
	}

	if ((form.hourlywage.value.length == 0) || (form.hourlywage.value != parseFloat(form.hourlywage.value)))
	{
		alert("Please enter the average hourly wage for your employees.");
		return false;
	}

	if ((form.overtime.value.length == 0) || (form.overtime.value != parseFloat(form.overtime.value)))
	{
		alert("Please enter the collective hours of overtime you pay each month.");
		return false;
	}

	if ((form.jobs.value.length == 0) || (form.jobs.value != parseInt(form.jobs.value)))
	{
		alert("Please enter the number of jobs your employees complete each day.");
		return false;
	}

	if ((form.revenue.value.length == 0) || (form.revenue.value != parseFloat(form.revenue.value)))
	{
		alert("Please enter your average revenue per job.");
		return false;
	}

	if ((form.workdays.value.length == 0) || (form.workdays.value != parseInt(form.workdays.value)))
	{
		alert("Please enter the number of work days in your month.");
		return false;
	}

	if ((form.miles.value.length == 0) || (form.miles.value != parseFloat(form.miles.value)))
	{
		alert("Please enter the average number of miles your vehicles drive per day.");
		return false;
	}

	if ((form.permile.value.length == 0) || (form.permile.value != parseFloat(form.permile.value)))
	{
		alert("Please enter the cost per mile to operate your fleet.");
		return false;
	}

	if ((form.monthlyrate.value.length == 0) || (form.monthlyrate.value != parseFloat(form.monthlyrate.value)))
	{
		alert("Please enter the quoted monthly rate per vehicle.");
		return false;
	}

	if (form.hardware.value.length != 0) 
	{
		if (form.hardware.value != parseFloat(form.hardware.value))
		{
			alert("Please enter the up front hardware investment per vehicle.");
			return false;
		}
	}
	else
	{
		form.hardware.value = 0;
	}

	if (form.installation.value.length != 0)
	{
		if (form.installation.value != parseFloat(form.installation.value))
		{
			alert("Please enter the up front installation per vehicle.");
			return false;
		}
	}
	else
	{
		form.installation.value = 0;
	}

	form.monthlyexpense.value = r2((form.vehicles.value * form.monthlyrate.value));

	form.overtimereduction.value = r2(((parseFloat(form.overtime.value) * .1) * (parseFloat(form.hourlywage.value) * 1.5)));
	form.increasejobs.value = r2((parseFloat(form.vehicles.value) * parseFloat(form.revenue.value)));
	form.decreasemileage.value = r2(((parseFloat(form.vehicles.value) * parseFloat(form.workdays.value)) * parseFloat(form.permile.value)));

	form.monthlysavings.value = r2(((parseFloat(form.overtimereduction.value) + parseFloat(form.decreasemileage.value)) - parseFloat(form.monthlyexpense.value)));
	form.increasemonthrevenue.value = r2(parseFloat(form.increasejobs.value));
	form.cashflow1.value = r2(((parseFloat(form.monthlysavings.value) + parseFloat(form.increasemonthrevenue.value)) * 12));
	form.cashflow3.value = r2((parseFloat(form.cashflow1.value) * 3));
	form.roi.value = r2((((((parseFloat(form.hardware.value) + parseFloat(form.installation.value)) * parseFloat(form.vehicles.value)) / parseFloat(form.cashflow1.value)) * 12)));
}
