// JavaScript Document

// Edited:
/*
	02 Jan 2007 (KRK)
	- added test for non-resident - chg TD1 claim code to 0 and disable select box

*/
/* 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;
	var pcode = box.options[box.selectedIndex].value;
	var list = new Array();
	var lower_rval = 0;
	var upper_rval = 0;

	//list = getTD1P_claim_ranges(pcode);
	ranges = getTD1P_claim_ranges(pcode);
	if (ranges.length > 1)
	{
		for ( i = 0; i < ranges.length; i++ ) {
			list[i] = pcode + ' Claim Code ' + i;
			upper_rval = new NumberFormat(ranges[i]).toFormatted();
			if (i == 0) {
				list[i] += ' (no claim amount)';
			} else if (i == 1) {
				list[i] += ' (Basic claim: ' + upper_rval + ')';
			} else {
				list[i] += ' (' + lower_rval + ' - ' + upper_rval + ')';
			}
			lower_rval = new NumberFormat(ranges[i] + .01).toFormatted();
		}
	} else {
		list[0] = 'N/A';
	}
	
	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 );

	// if a non-resident - set TD1 claim code to 0 and disable the TD1 claim code select box
	if (pcode == 'NR') {	// non-resident
		box.form.TD1_claim_code.disabled = true;
		box.form.TD1_claim_code.selectedIndex=0;
	} else {
		box.form.TD1_claim_code.disabled = false;
		box.form.TD1_claim_code.selectedIndex=1;
	}
	
	// if the selected province is Quebec, make the TP-1015-3.V tab page visible, otherwise
	// make it hidden
	if (pcode == 'QC')
	{
		displayLayer('tbl_tp1015_3V');	// display the TP-1015.3-V table
		hideLayer('tbl_td1p');			// hide the TD1P table
		document.getElementById("tab-page-employee-td1p-header").innerHTML = "TP1015.3-V";
	} else if (pcode == '') {	// No province selected
		document.getElementById("tab-page-employee-td1p-header").innerHTML = "TD1(P)";
		hideLayer('tbl_td1p');		// hide the TD1P table
		hideLayer('tbl_tp1015_3V');	// hide the TP-1015.3-V table
	} else {
		document.getElementById("tab-page-employee-td1p-header").innerHTML = "TD1" + pcode;
		displayLayer('tbl_td1p');	// display the TD1P table
		hideLayer('tbl_tp1015_3V');	// hide the TP-1015.3-V table
	}
}

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 toggleLayer(whichLayer)
{
	// function will toggle display/hidden status for a <div> element identified by {whichLayer} parameter.
	if (document.getElementById)
	{
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}

function hideLayer(whichLayer)
{
	// function will hide a <div> element identified by {whichLayer} parameter.
	if (document.getElementById)
	{
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = "";
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = "";
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = "";
	}
}

function displayLayer(whichLayer)
{
	// function will toggle display/hidden status for a <div> element identified by {whichLayer} parameter.
	if (document.getElementById)
	{
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = "block";
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = "block";
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = "block";
	}
}

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 'pension':
				if (IsZero(form.pensionIncome))
				{
					alert('Pension income must be greater than 0');
					form.pensionIncome.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 (IsZero(form.grossWages))
				{
					alert('Gross Salary/Wages must be greater than 0');
					form.grossWages.focus(); 
					return false; 
				}
				if (IsZero(form.retroPay))
				{
					alert('Total Pay Increase must be greater than 0');
					form.retroPay.focus(); 
					return false; 
				}
				if (IsZero(form.retroPayPeriods))
				{
					alert('Number of retroactive pay periods must be greater than 0');
					form.retroPayPeriods.focus(); 
					return false; 
				}
				break;
			default:

		}	// end switch

	} else {	// commission income
		if (IsZero(form.TD1X_I1))
		{
			alert('TD1X Annual Remuneration must be greater than 0');
			form.TD1X_I1.focus(); 
			return false; 
		}
		if (IsZero(form.commissions))
		{
			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



