//===============================================================================
//All contents of this file are confidential and 
//copyright (c) Inland Revenue Authority of Singapore
//All rights reserved.
//===============================================================================

/****************************************************************
* 
* File Name		 :	TaxCalculation.js
* 
* Created		 :	19-Feb-2010
* Original Author:	Alexander Daniel
* 
***************************************************************
* MODIFICATION HISTORY
* ------------------------------------------------------------
* When			Who				Why
* ------------------------------------------------------------
* 19-Feb-2010	Daniel			Created
*/


/*
*---------------------------------------------------------------
 * FUNCTION		: checkAnnualValue(form)
 *---------------------------------------------------------------
  DESCRIPTION	: Check AnnualValue Field
 * PARAMETER    : form
 *---------------------------------------------------------------
 */
function CheckAnnualValue(form)
{
	if(IsNumeric(form.AnnualValue.id,this.form)==false)
	{
	return false;
	}
	else 
	{
	return true;
	}
}


/*
*---------------------------------------------------------------
 * FUNCTION		: CheckEmptyFromDate(form)
 *---------------------------------------------------------------
 * DESCRIPTION  : Check for the empty FromDate Field
 * PARAMETER    : form
 *---------------------------------------------------------------
 */
 
function CheckEmptyFromDate(form)
{
	if(form.FromDate.value=="" || form.FromDate.value==null)
	{      
	     form.FromDate.focus();
         alert("Please enter the From date");
         return false;
	}
	else if(!ValidateDate(form.FromDate.id,form))
	{
		return false;
	}
	else 
	return true;
}
/*
*---------------------------------------------------------------
 * FUNCTION		: CheckEmptyToDate(form)
 *---------------------------------------------------------------
 * DESCRIPTION  : Check for the empty ToDate Field
 * PARAMETER    : form
 *---------------------------------------------------------------
 */
function CheckEmptyToDate(form)
{
	if(!(form.FromDate.value ==null || form.FromDate.value==""))
	{
		if(form.ToDate.value=="" || form.ToDate.value==null)
		{     
           form.ToDate.focus();
           alert("Please enter the To date");
           return false;
		}
		else
		return true;
	}
}
/*
*---------------------------------------------------------------
 * FUNCTION		: checkDateDiff(form)
 *---------------------------------------------------------------
 * DESCRIPTION  : To Check ToDate greater than FromDate
 * PARAMETER    : form
 *---------------------------------------------------------------
 */
function checkDateDiff(form)
{	
	if(form.FromDate.value!="" && form.ToDate.value!="")
	{
		if( ValidateDate(form.FromDate.id,form)==true && ValidateDate(form.ToDate.id,form)==true)
		{	
			var FromDate=GetDate(form.FromDate.value,form); 
			var ToDate=GetDate(form.ToDate.value,form);  
			if(ToDate<=FromDate)
			{	form.ToDate.focus();
				alert("To Date must be greater than From Date");
				return false;	
			}
			else 
			{	
				taxPayable(form);
				
			}
		}   
	}
}
/*
*---------------------------------------------------------------
 * FUNCTION		: ValidateDate(date,form)
 *---------------------------------------------------------------
 * DESCRIPTION  : To validate date entered
 * PARAMETER    : date,form
 *---------------------------------------------------------------
 */
function ValidateDate(date,form)
{	var match;
    match=IsMatch(date,form);
	return match;
}
/*
*---------------------------------------------------------------
 * FUNCTION		: IsMatch(str,form)
 *---------------------------------------------------------------
 * DESCRIPTION  : To validate date entered in the required format and range
 * PARAMETER    : date,form
 *---------------------------------------------------------------
 */
function IsMatch(str,form)
{
		var mystring = document.getElementById(str).value;
		var yr=mystring.substring(6);
        var mm=mystring.substring(3,5);
        var dd=mystring.substring(0,2);
		var check=false;
        if (mystring.length==10)
        {
		    if (mystring.match(/^\d{2}\/\d{2}\/\d{4}$/) )
            {   
				if (yr < 2005 || yr > 3000)
				{
					alert ("Please enter the year between 2005 and 3000");document.getElementById(str).focus(); check=false;
				}
				else if (mm > 12 || mm < 1 ) 
				{alert ("Month should be between 1 and 12 ");document.getElementById(str).focus(); check=false;
				}
				else if (mm==4 || mm == 6 || mm == 9 || mm==11 )
				{
					if (dd > 30 )
					{
						alert ("Please check the day entered");
						document.getElementById(str).focus(); check=false;
					}
					else {check=true;}
				}
				else if (mm==2)
				{
					var b=parseInt(yr/4);
					if (isNaN(b) ||(dd>29)) 
					{
						alert( "Please check the no.of days in February");
						document.getElementById(str).focus();check=false;
					}
					else if (dd==29 && ((yr/4)!=parseInt(yr/4))) 
					{
						alert( "This is not a leap year. Please check the no. of days in February.");
						document.getElementById(str).focus();check=false;
					}
					else check=true;
				}
				else
				{
					if (dd < 1||dd > 31)
					{
						alert ("Day of month should be between 1 and 31 ");
						document.getElementById(str).focus();check=false;
					}	
					else {check= true;}
				}                
            }	
			else
			{
				alert("Please enter date in this format dd/mm/yyyy.");document.getElementById(str).focus();
				check=false;
            }
		}
		else
		{
			alert("Please enter date in this format dd/mm/yyyy");document.getElementById(str).focus();check=false;
		}
		return check;
    }
	
function GetDate(date,form)
{
	var fromDate=date.split("/");       
	var FromDate=new Date(fromDate[2],fromDate[1]-1,fromDate[0]);        
	return FromDate;	
}
function getStartDate(FromDate)
{
	var fromDate= new Date(FromDate);
	return fromDate;
}
function getEndDate(ToDate)
{
	var toDate=new Date(ToDate);
	return toDate;
}
/*
*---------------------------------------------------------------
 * FUNCTION		: noDaysMonth(month1,year)
 *---------------------------------------------------------------
  DESCRIPTION	: To return the number of days in a month
 * PARAMETER    : month,year
 *---------------------------------------------------------------
 */
function noDaysMonth(month1,year)
{
	var month=parseInt(month1);
	if(month==0||month==2||month==4||month==6||month==7||month==9||month==11)
	{
		return 31;
	}
	else if(month==3||month==5||month==8||month==10)
	{
		return 30;
	}
	else 
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}  
/*
*---------------------------------------------------------------
 * FUNCTION		: convertDaysToMonth(FromDate,ToDate)
 *---------------------------------------------------------------
  DESCRIPTION	: To convert the prorated days in the FromDate and ToDate entered into month
 * PARAMETER    : FromDate,ToDate
 *---------------------------------------------------------------
 */
function convertDaysToMonth(FromDate,ToDate)
{
	startDate=getStartDate(FromDate);
	endDate=getEndDate(ToDate);
	var startDay=startDate.getDate();
	var startYear=startDate.getFullYear();
	var startMonth=startDate.getMonth();
	var endDay=endDate.getDate();
	var endMonth=endDate.getMonth();
	var endYear=endDate.getFullYear();
	var noDaysMonth1=noDaysMonth(startMonth,startYear);
	var noDaysMonth2=noDaysMonth(endMonth,endYear);
	var proRateDays1=noDaysMonth1-startDay+1;
	var partialMonths=(proRateDays1/noDaysMonth1)+(endDay/noDaysMonth2);
	return partialMonths;
}
/*
*---------------------------------------------------------------
 * FUNCTION		: NumMonths(FromDate,ToDate)
 *---------------------------------------------------------------
  DESCRIPTION	: To return the whole number of months between the FromDate and ToDate
 * PARAMETER    : FromDate,ToDate
 *---------------------------------------------------------------
 */   
function NumMonths(FromDate,ToDate)
{
	var fromDate=getStartDate(FromDate);
	var toDate=getEndDate(ToDate);
	var monthsBetween;
	if(toDate.getFullYear()>fromDate.getFullYear())
	{
		monthsBetween=((12-fromDate.getMonth())+toDate.getMonth()-1)+(toDate.getFullYear()-fromDate.getFullYear()-1)*12;
	}
	else
	{
		monthsBetween=toDate.getMonth()-fromDate.getMonth()-1;
	}
	return monthsBetween;
}

/*
*---------------------------------------------------------------
 * FUNCTION		: taxPayable(form)
 *---------------------------------------------------------------
  DESCRIPTION	: To call other methods to calculate TaxPayable, GST RebateGranted, PT Rebate Grnated and Balance Tax Payable
 * PARAMETER    : form
 *---------------------------------------------------------------
 */
 
function taxPayable(form)
{
		if(form.FromDate.value!="" && form.ToDate.value!="")
		{
			if( ValidateDate(form.FromDate.id,form)==true && ValidateDate(form.ToDate.id,form)==true)
			{	
				var FromDate=GetDate(form.FromDate.value,form); 
				var ToDate=GetDate(form.ToDate.value,form);  
				var partialMonths=convertDaysToMonth(FromDate,ToDate);
				var monthsBetween=NumMonths(FromDate,ToDate);
				var months=partialMonths+monthsBetween;
				
				calTaxPayable(months,form);

			}
		}
}
/*
*---------------------------------------------------------------
 * FUNCTION		: calTaxPayable(months,form)
 *---------------------------------------------------------------
  DESCRIPTION	: Calculate the Tax Payable
 * PARAMETER    : months,form
 *---------------------------------------------------------------
 */
function calTaxPayable(months,form)
{
	if(document.getElementsByTagName('AnnualValue').value!="")
	{  
		var income = form.AnnualValue.value;	
		if (!(isNaN(income)|| (income.indexOf(".")>-1)))
		{
			if(form.Option.value=="Y" || form.Option.value=="Z")
			{			
				var taxPayable= Math.round((income*(10/100)*100))/100;
				form.TaxPayable.value=taxPayable.toFixed(2);
				updateDOM(form.TaxPayable);	
			}
			else if (form.Option.value=="X")
			{	
				var taxPayable= 0;
				
				if(income <= 6000)
				{
					taxPayable= 0;
				}
				else if (income > 6000 && income  <= 65000)
				{
					taxPayable = (income - 6000) * 4/100;
				}
				else
				{
					taxPayable = ((income - 65000) * 6/100) + (59000*(4/100));
				}		
				
				form.TaxPayable.value=taxPayable.toFixed(2);
				updateDOM(form.TaxPayable);	
			}
			else //default
			{
			 	form.TaxPayable.value="";
				updateDOM(form.TaxPayable);	
			}
				
	
		}		
	}
}



/*
*---------------------------------------------------------------
 * FUNCTION		: CheckEmptyAnnualValue(form)
 *---------------------------------------------------------------
  DESCRIPTION	: Check Empty AnnualValue Field
 * PARAMETER    : form
 *---------------------------------------------------------------
 */
function CheckEmptyAnnualValue(form)
{
		if(form.AnnualValue.value=="" || form.AnnualValue.value==null)
		{
			alert("Please enter Annual Value");
			form.AnnualValue.focus();
			return false;
		}
}

function IsNumeric(strValue,form)
{
	var check;
	var str=document.getElementById(strValue).value;
	if (isNaN(str)|| (str.indexOf(".")>-1))
	{
		alert("Numbers only. $, decimals, alphabets are invalid entries. Please re-enter.");
		document.getElementById(strValue).focus();
		check= false;
	}
	else 
	check= true;
	return check;
} 


function updateDOM(inputField) 
{
    inputField.setAttribute("value",inputField.value);
}

function taxPayable(form)
{
		if(form.FromDate.value!="" && form.ToDate.value!="")
		{
			if( ValidateDate(form.FromDate.id,form)==true && ValidateDate(form.ToDate.id,form)==true)
			{	
				var FromDate=GetDate(form.FromDate.value,form); 
				var ToDate=GetDate(form.ToDate.value,form);  
				var partialMonths=convertDaysToMonth(FromDate,ToDate);
				var monthsBetween=NumMonths(FromDate,ToDate);
				var months=partialMonths+monthsBetween;
				calTaxPayable(months,form);
										
			}
		}
}


/*
*---------------------------------------------------------------
 * FUNCTION		: Select(form)
 *---------------------------------------------------------------
  DESCRIPTION	: To Reset the Select option
 * PARAMETER    : form
 *---------------------------------------------------------------
 */	
function Select(form)
{
	var option=document.getElementById(form.Option.id).value;
	
	if(option == "default")
	{
	    document.getElementById("default").setAttribute("selected",true);
		document.getElementById("X").removeAttribute("selected");
		document.getElementById("Y").removeAttribute("selected");
		document.getElementById("Z").removeAttribute("selected");
	}
	else if(option=="X")
	{
		document.getElementById("X").setAttribute("selected",true);
		document.getElementById("Y").removeAttribute("selected");
		document.getElementById("Z").removeAttribute("selected");
		document.getElementById("default").removeAttribute("selected");
	}
	else if(option=="Y")
	{
		document.getElementById("Y").setAttribute("selected",true);
		document.getElementById("X").removeAttribute("selected");
		document.getElementById("Z").removeAttribute("selected");
		document.getElementById("default").removeAttribute("selected");
	}
	else
	{
		document.getElementById("Z").setAttribute("selected",true);
		document.getElementById("X").removeAttribute("selected");
		document.getElementById("Y").removeAttribute("selected");
		document.getElementById("default").removeAttribute("selected");
	}

}
/*
*---------------------------------------------------------------
 * FUNCTION		: resetFields(form)
 *---------------------------------------------------------------
  DESCRIPTION	: To Reset the fields
 * PARAMETER    : form
 *---------------------------------------------------------------
 */	
function resetFields(form)
{
	document.getElementById(form.AnnualValue.id).value="";
	document.getElementById(form.TaxPayable.id).value="";
	document.getElementById(form.Option.id).value="default";
	updateDOM(form.FromDate);
	updateDOM(form.ToDate);
	updateDOM(form.AnnualValue);
	updateDOM(form.TaxPayable);
	Select(form);
}
