// JavaScript Document

/* Set a cookie to be sure that one exists.
   Note that this is outside the function*/
document.cookie = 'killme' + escape('nothing');

function init_form(form) {
	cc();	// initialize cookie
}

function cc()
{
 /* check for a cookie */
  if (document.cookie == "") 
  {
    /* if a cookie is not found - alert user -
     change cookieexists field value to false */
    alert("Your browser does not support cookies or they are turned off.  You need to enable cookies to properly use the Canadian Automobile Benefits Calculator.");

    /* If the user has Cookies disabled an alert will let him know 
        that cookies need to be enabled to log on.*/ 

    document.autoben.cookieexists.value ="false";
  } else {
   /* this sets the value to true and nothing else will happen,
       the user will be able to log on*/
    document.autoben.cookieexists.value ="true";
  }
}

function calcPersonalKm(form)
{
	if ( parseInt(form.business_km.value) > parseInt(form.total_km.value) )
	{
		alert('Business kilometers cannot exceed total kilometers');
		form.business_km.value = form.total_km.value;
	}
	form.personal_km.value = parseInt(form.total_km.value) - parseInt(form.business_km.value);
	return true;
}

function ValidateForm(form)
{
	switch (form.costType.value)
	{
		case 'abc1':		// Employer-owned automobile
			/* 
			// oops!  can't validate as numeric if a decimal point is included in input?
			if (!IsNumeric(form.owned_cost))
			{
				alert('Please enter a value for the cost of employer-owned automobile');
				form.owned_cost.focus();
				return false;
			} 
			else		*/
			if (parseFloat(form.owned_cost.value) <= 0)
			{
				alert('Cost of employer-owned automobile must be greater than 0');
				form.owned_cost.focus(); 
				return false; 
			}
			break;
		case 'abc2':	// Employer-leased automobile
			/* 
			// oops!  can't validate as numeric if a decimal point is included in input?
			if (!IsNumeric(form.lease_cost))
			{
				alert('Please enter a value for the monthly cost of empoyer-leased automobile');
				form.lease_cost.focus();
				return false;
			} 
			else 	*/
			if (parseFloat(form.lease_cost.value) <= 0)
			{
				alert('Monthly cost of employer-leased automobile must be greater than 0');
				form.lease_cost.focus(); 
				return false; 
			}
			break;
		case 'abc3':		// Employee employed selling/leasing autos
			/* 
			// oops!  can't validate as numeric if a decimal point is included in input?
			if ( !IsNumeric(form.avgcost_all) && !IsNumeric(form.avgcost_new))
			{
				alert('Please enter a value for the average cost of all automobiles or all new automobiles acquired during the year.');
				form.avgcost_all.focus(); 
				return false; 
			}
			else 	*/
			if (parseFloat(form.avgcost_all.value) <= 0)
			{
				alert('Average cost value must be greater than 0');
				form.avgcost_all.focus(); 
				return false; 
			}
			else if (parseFloat(form.avgcost_new.value) <= 0 )
			{
				alert('Average cost value must be greater than 0');
				form.avgcost_new.focus(); 
				return false; 
			}
			break;
		default:
			return false;
	}	// end switch

	/* 
	// oops!  can't validate as numeric if a decimal point is included in input?
	if (!IsNumeric(form.total_km))
	{
		alert('Please enter an integer value for total kilometers driven during the year.');
		form.total_km.focus();
		return false;
	} 
	else 	*/
	if (parseFloat(form.total_km.value) <= 0)
	{
		alert('Total kilometers driven during the year must be greater than 0');
		form.total_km.focus();
		return false;
	}

	/* 
	// oops!  can't validate as numeric if a decimal point is included in input?
	if (!IsNumeric(form.business_km))
	{
		alert('Please enter a numeric value for business kilometers driven during the year.');
		form.business_km.focus();
		return false;
	} 
	else 	*/
	if (parseFloat(form.business_km.value) < 0)
	{
		alert('Business kilometers driven during the year cannot be less than 0');
		form.business_km.focus();
		return false;
	}

	/* 
	// oops!  can't validate as numeric if a decimal point is included in input?
	if (!IsNumeric(form.days_available))
	{
		alert('Please enter a value for total days automobile was available to employee during the year.');
		form.days_available.focus();
		return false;
	} 
	else 	*/
	if (parseFloat(form.days_available.value) <= 0)
	{
		alert('Total days available during the year must be greater than 0');
		form.days_available.focus();
		return false;
	} 
	else if(parseFloat(form.days_available.value) > 366)
	{
		alert('Total days available during the year cannot exceed 366');
		form.days_available.value = 366;
		form.days_available.focus();
		return false;
	}
	
	var abcWinHeight 	= "650";	// width (pixels) for Benefits Calculation screen
	var abcWinWidth 	= "700";	// height (pixels) for Benefits Calculation screen
	var abcWinScroll 	= "yes";
	var abcWinResize 	= "yes";

	return popupWin('','webTODabcalc',abcWinWidth,abcWinHeight,abcWinScroll,abcWinResize);

} 	// end function


