// 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) {
	changeList(form.province);	// initialize the Provincial TD1P Claim Code select box
	//disableYtdFields(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 Payroll Income Tax Deduction Calculator.");

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

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

function changeList( box ) {
	//list = lists[box.options[box.selectedIndex].value];
	//document.cdntaxded.TD1P_claim_code.disabled = false;
	list = getTD1P_claim_ranges(box.options[box.selectedIndex].value);
	newList( box.form.TD1P_claim_code, list );
	if (list.length > 1)
	{
		box.form.TD1P_claim_code.disabled = false;
	} else {
		box.form.TD1P_claim_code.disabled = true;
	}
	//emptyList( box.form.TD1P_claim_code );
	//fillList( box.form.TD1P_claim_code, list );
}

function emptyList( box ) {
	while ( box.options.length ) box.options[0] = null;
}

function fillList( box, arr ) {
	for ( i = 0; i < arr.length; i++ ) {
		option = new Option( arr[i], i );
		box.options[box.length] = option;
	}
	if (arr.length > 1) box.selectedIndex=1;		// default selection is Claim Code 1 - Basic claim
	else box.selectedIndex=0;
}

function newList( box, arr ) {
	emptyList(box);
	var defSelIndex;
	if (arr.length > 1) defSelIndex = 1;
	else defSelIndex = 0;
	for ( i = 0; i < arr.length; i++ ) {
		if (i == defSelIndex) defSel = true; 
		else defSel = false;
		box.options[i] = new Option( arr[i], i, defSel );
	}
	if (arr.length > 1) {
		box.selectedIndex=1;		// default selection is Claim Code 1 - Basic claim
	} else {
		box.selectedIndex=0;
	}
	return true;
}

function toggleYtdFields(ch)
{
	ch.form.ytdCPP.disabled = ! ch.checked;
	ch.form.ytdEI.disabled = ! ch.checked;
	ch.form.ytdITD.disabled = ! ch.checked;
	//ch.form.ytdLSFp.disabled = ! ch.checked;
}

function disableYtdFields(form)
{
	form.ytdCPP.disabled = true;
	form.ytdEI.disabled = true;
	form.ytdITD.disabled = true;
	//form.ytdLSFp.disabled = true;
}

function ValidateForm(form)
{
	if(IsEmpty(form.province)) 	// Province of employment is required for all types of income
	{
		alert('You must select a valid Province of Employment');
		form.province.focus(); 
		return false; 
	}

	if (form.incomeType.value != "comm")	// NOT commission income
	{
		if(IsEmpty(form.annualPayPeriods)) 	// Annual Pay Periods definition required
		{
			alert('You must select a valid Annual Pay Period definition');
			form.annualPayPeriods.focus(); 
			return false; 
		}
		
		switch (form.incomeType.value)
		{
			case 'reg':
				if (IsZero(form.grossWages))
				{
					alert('Gross Salary/Wages must be greater than 0');
					form.grossWages.focus(); 
					return false; 
				}
				break;
			case 'bonus':
				if (IsZero(form.grossWages))
				{
					alert('Gross Salary/Wages must be greater than 0');
					form.grossWages.focus(); 
					return false; 
				}
				if (IsZero(form.currentBonus))
				{
					alert('Current Bonus Payable must be greater than 0');
					form.currentBonus.focus(); 
					return false; 
				}
				break;
			case 'retro':
				if (parseInt(form.grossWages.value) == 0)
				{
					alert('Gross Salary/Wages must be greater than 0');
					form.grossWages.focus(); 
					return false; 
				}
				if (parseInt(form.retroPay.value) == 0)
				{
					alert('Total Pay Increase must be greater than 0');
					form.retroPay.focus(); 
					return false; 
				}
				if (parseInt(form.retroPayPeriods.value) == 0)
				{
					alert('Number of retroactive pay periods must be greater than 0');
					form.retroPayPeriods.focus(); 
					return false; 
				}
				break;
			default:

		}	// end switch

	} else {	// commission income
		if (parseInt(form.annualCommIncome.value) == 0)
		{
			alert('TD1X Annual Remuneration must be greater than 0');
			form.annualCommIncome.focus(); 
			return false; 
		}
		if (parseInt(form.commissions.value) == 0)
		{
			alert('Gross Commission Payment must be greater than 0');
			form.commissions.focus(); 
			return false; 
		}
	
	} 	// end else commission income

	return popupWin('','payadvice',paWinWidth,paWinHeight,paWinScroll,paWinResize);

} 	// end function



