<!--
/**	Inland Revenue
	Date: 18 December 2000
	Copyrighted
	
	Pay Adjustment on-line calculation	
*/

   if((navigator.appVersion.indexOf("Mac") != -1)){document.write('<link href="../mac.css" rel="styleSheet" type="text/css">')} 

   function strip_blanks(from_this) {
        var isitablank = true;
        i= 0 
        do {
          if (from_this.substring(i,i+1) != " ") isitablank = false;
          i ++
        } while (isitablank)
        
        start = i - 1
        isitablank = true;
        i = from_this.length;
        do {
          i --
          if (from_this.substring(i,i+1) != " ") isitablank = false;
        } while (isitablank)
        end = i + 1
        from_this= from_this.substring(start,end) 
       
        return from_this }        

   function Roundp(amount) 

/*      Pay adjust version To round to nearest penny (ie 2 dp)
        shortened version - normal rounding only */

{
        ans2=amount 
 
        if (((ans2*100) - Math.floor(ans2*100) ) > 0) {
             		        var rndans2  = (Math.round(ans2*100+.5)/100) }     // rounds up ONLY if necessary  
                        else{
                                rndans2=ans2 	}
        amount=rndans2

        return amount
}

        function forminit()

        {
           today = new Date();

           day   = today.getDate() -1;
           month = today.getMonth(); 
             
           var str = today.getFullYear() + "";
           var year = eval(str.substring(str.length - 2, str.length));
           document.freepay.year.selectedIndex  = year-2
           document.freepay.month.selectedIndex = month
           document.freepay.day.selectedIndex   = day

         return
         }

        function checkifnumb(field)
/* this version has less data checking than library copy */
	{
		clearentitlement()
                var badvaluesindex = 0
                var spaces         = 0
                var dpcount        = 0
                var minuscount     = 0
                var valids         = 0

        //  sets values that are acceptable as input 
		var numbers = " .-0123456789"   /* include a space!! */
                
		var bad          = "no"
		var hold         = ""
                var badvalues    = ""
                var spaces_msg   = ""
                var badvaluesmsg = ""
                var alertmsg     = ""
                var sinvch       = ""
                var sblank       = ""
                
        // remove preceding and trailing blanks
              
                field.value = strip_blanks(field.value)
                
	// checks each character in string 

		for (var i=0; i < field.value.length; i++) {
		  hold = "" + field.value.substring(i, i+1)
                  if (numbers.indexOf(hold) < 0) {
                    badvaluesindex ++
                    badvalues = badvalues + hold 
                  } 
                  else if (numbers.indexOf(hold) == 0) {spaces ++} 
                  else if (numbers.indexOf(hold) == 1) {dpcount ++}   
                  else if (numbers.indexOf(hold) == 2) {minuscount ++}

                  else if (numbers.indexOf(hold) <= numbers.length) {valids ++}              
                }         
                totalbads = 0
                totalbads = spaces + dpcount + minuscount + badvaluesindex
		if (totalbads > 0) {

                  spaces_msg = ""
                  if (spaces > 0) {spaces_msg = spaces + " space(s)" + "\n"  }

                  decimals_msg = ""
                  if (dpcount > 0) {decimals_msg = dpcount + " decimal point(s)" + "\n" }

                  minus_msg = ""
                  if (minuscount > 0) {minus_msg = minuscount + " minus sign(s)" + "\n"}

                  badvaluesmsg = " "
                    if (badvaluesindex > 0) {badvaluesmsg = badvaluesindex + " invalid character(s): " + badvalues}

                  if (totalbads > 1) {sinvch = "s"}
                  alertmsg = "The tax code you typed in (" + document.freepay.taxcode.value+ ") is unacceptable because it contains"  + "\n" 
                  if (spaces > 0) {alertmsg = alertmsg + spaces_msg}
                  if (dpcount > 0) {alertmsg = alertmsg + decimals_msg}
                  if (minuscount > 0) {alertmsg = alertmsg + minus_msg}
                  if (badvaluesindex > 0) {alertmsg = alertmsg+ badvaluesmsg}
                                                    
                  alert(alertmsg + "\nThe tax code for this calculator must be a whole number. Please re-enter.")
                  document.freepay.taxcode.value=""
		  field.focus()
		  field.select()  
                }  
	        return
        } 

	

	function cpaValidate()
	{
		clearentitlement()

		daysinmonth = new Array(12)
		daysinmonth[0] = 31
		daysinmonth[1] = 28 
		daysinmonth[2] = 31
		daysinmonth[3] = 30
		daysinmonth[4] = 31
		daysinmonth[5] = 30
		daysinmonth[6] = 31
		daysinmonth[7] = 31
		daysinmonth[8] = 30
		daysinmonth[9] = 31
		daysinmonth[10] = 30
		daysinmonth[11] = 31

                monthnames = new Array(12)
                monthnames[0] = "January"
                monthnames[1] = "February"
                monthnames[2] = "March"
                monthnames[3] = "April"
                monthnames[4] = "May"
                monthnames[5] = "June"
                monthnames[6] = "July"
                monthnames[7] = "August"
                monthnames[8] = "September"
                monthnames[9] = "October"
                monthnames[10] = "November"
                monthnames[11] = "December"

		var txtwrongyear ="Please enter a date that is within the 2009/2010 tax year"
		var year =  document.freepay.year.selectedIndex+2009
		var month =  document.freepay.month.selectedIndex
		var day =  document.freepay.day.selectedIndex+1


               if ((year == 2009) && (month < 3)){
			alert (txtwrongyear)
			return}
			
               if ((year == 2010) && (month > 3)){
			alert (txtwrongyear)
			return}			

				
		if ((year == 2009) && (month==3) && (day < 6) ){
			alert (txtwrongyear)
			return}	

                if ((year == 2010) && (month==3) && (day > 5) ){
			alert (txtwrongyear)
			return} 

		if (Math.floor(year/4)==year/4){
			daysinmonth[1]=29}						

		if (daysinmonth[month] < day){
			alert (monthnames[month] + " only has " + daysinmonth[month] + " days. Please try again.")
			return}		 

		var taxcode1 = document.freepay.taxcode.value

		if (taxcode1==""){
			alert ("Please enter the tax code")
			return}

/*		if (taxcode1 > 1000){ */
/*			alert ("The tax code '"+taxcode1+"' is invalid. A tax code cannot be greater than 1000; please try again.") */
/*                        document.freepay.taxcode.value=""   */
	/*		return}   */

			freepay()
	}

	function add_Zeros(field)
	{
		var txtnum=""
		var result=0
		var len=0
		var hold=""+field
		len=hold.length

	// finds decimal point
		result=hold.indexOf(".")
		var posdec=result

	// finds number of characters after decimal point
		var afterdec=(len-posdec)-1

		if(afterdec==1){
			txtnum="0"}

		if (afterdec==0){
			txtnum="00"}
	
	// -1 is result if no decimal point found
		if (result== -1){
			txtnum=".00"}

	// return original number plus any zeros	
		return (hold+txtnum)
	}
	
	function monthnumber(month,day)
	{

		if (month < 3){
			month=month + 10}
		else{
			month=month-2}

		diffinmonth=month
		if (day < 6){
			diffinmonth = month-1}
		
		if (diffinmonth==0){
			diffinmonth=12}

		return (diffinmonth)
	}

	function weeknumber(year,month,day)
	{
		var startyear=year
			
		if (month < 3){
			startyear = year-1}
		if (month == 3 && day <6){
			startyear = year-1}

		var startdate=new Date(startyear,3,5)
		
		var paydate=new Date(year,month,day)

		diffindays=Math.round((paydate-startdate)/(1000*60*60*24))
		diffinweeks=parseInt(diffindays/7+.9999)
		
		return (diffinweeks)
	}

	function freepay()
	{
		var year = document.freepay.year.selectedIndex+2000
		var month = document.freepay.month.selectedIndex
		var day = document.freepay.day.selectedIndex +1
		var weekmonth = 52
		var quotientamount = 96.16
		var taxcode1 = document.freepay.taxcode.value-1
		var txt2=""

		if (document.freepay.period[1].checked){
			var txt1 = " month "
			monthnumber(month,day)
			periodnumb =diffinmonth}
		else{
			var txt1 = " week "
			weeknumber(year,month,day)

			if (diffinweeks==53){
			diffinweeks=1
			var txt2=" basis"}

			periodnumb =diffinweeks}

		if (document.freepay.periodonebasis[0].checked){
			periodnumb=1
			var txt2 = " basis"}

		if (taxcode1 == -1){
			result = 0}
		else{
			var ans1     = (taxcode1 * 1)/500
			var quotient = Math.floor(ans1)   /* get integer part - rounded down */
			var remainder= taxcode1 - (quotient * 500)+1
		
			// for monthly pay period

			if (document.freepay.period[1].checked){
				weekmonth =12
				quotientamount = 416.67}
		
			var ans2     = ((remainder*10) + 9)/ weekmonth

                        rndans2 = Roundp(ans2)

        	var ans3     = quotient * quotientamount
			result       = Math.round(((rndans2 + ans3)* periodnumb)*100)/100
/*  leave below statement until debugging complete
window.alert("ans1=(taxcode1*1)/500"+"\n"+"    =("+taxcode1+"*1)/500"+"\n"+"    ="+ans1+"\n"+
             "quotient=Math.floor(ans1)"+"\n"+"        =Math.floor("+ans1+")"+"\n"+"        ="+quotient+"\n"+
             "remainder=taxcode1-(quotient*500)+1"+"\n"+"         ="+taxcode1+"-("+quotient+"*500)+1"+"\n"+"         ="+remainder+"\n"+
             "ans2=((remainder*10) + 9)/ weekmonth"+"\n"+"=(("+remainder+"*10)+9)/"+weekmonth+"\n"+"="+ans2+"\n"+
             "rndans2=(Math.round(ans2*100+.5)/100)"+"\n" + "=(Math.round("+ ans2 + "*100+.5)/100"+"\n" +"="+rndans2+"\n"+
             "ans3=quotient*quotientamount"+"\n" + "="+quotient+"*"+quotientamount +"\n"+"="+ans3+"\n"+
             "result=math.round(((rndans2+ans3)*periodnumb)*100)/100"+"\n"+"="+"=math.round((("+rndans2+"+"+ans3+")*"+periodnumb+")*100)/100"+"\n"+"="+result)
*/
		}
			var txt="£"+add_Zeros(result)

	// display entitlement
			document.freepay.result.value=txt
			document.freepay.txtperiod.value=txt1+periodnumb+txt2	

	}

	function clearentitlement()
/* lib copy changed to clear frrepay values not dptc */
	{
	// clears entitled to textbox
		document.freepay.result.value=""
		document.freepay.txtperiod.value=""
		return
	}

        
        function clearform()
	{
	// clears form
 
		document.freepay.result.value=""
		document.freepay.txtperiod.value=""
                document.freepay.taxcode.value = ""
           return
	}

// -->