function printerfriendly(){
if(
(document.calc.contractdate.value =="")||
(document.calc.overduedate.value =="")||
(document.calc.creditempcount.value =="")||
(document.calc.debtempcount.value =="")||
(document.calc.invoiceval.value =="")||
(document.calc.outputfield.value =="")
)
{
alert("Please calculate data before printing results")
}else{
var cd = document.calc.contractdate.value;
var od = document.calc.overduedate.value;
var pd = document.calc.paymentmade.value;
if (document.calc.paymentmade.value==""){
var PaymentMade = new Date();
	var writemonth  = new String(PaymentMade.getMonth()+1)
		if (writemonth.length == '1'){
			writemonth = "0" + writemonth
		}
	var todaysdate = PaymentMade.getDate() + '/' + writemonth + '/' + PaymentMade.getFullYear()
	PaymentMade = todaysdate
pd=PaymentMade;
}

var psb = document.calc.pubsecbod.checked;
if (psb == true){
    psb = "Yes";
} else {
    psb = "No";
}
var cc = document.calc.creditempcount.value;
var dc = document.calc.debtempcount.value;
var iv = document.calc.invoiceval.value;
var out = document.calc.outputfield.value;

document.location.href="statutory_pf.html?cd="+cd+"&od="+od+"&pd="+pd+"&psb="+psb+"&cc="+cc+"&dc="+dc+"&iv="+iv+"&out="+out

}
//return false
}
strResponse1 = "You cannot charge interest as the contract date predates 1st November 1998.  There was no legislation in place at that time.";
strResponse2 = "At the time the contract was made the legislation did not permit public sector companies to charge interest on late payments, This right was introduced on 7th August 2002, however it only applies to commercial contracts made on or after that date.";
strResponse3 = "At the time the contract was made, the legislation did not permit large businesses (51 or more employees) to charge interest on late payments from small businesses (50 or fewer employees). This right was introduced on 7th August 2002, however only applies to commercial contracts made on or after this date.";
strResponse31 = "At the time the contract was made, the legislation did not permit large businesses (51 or more employees) to charge one another interest on late payments.  This right was introduced on 7th August 2002, however only applies to commercial contracts made after this date.";
strResponse4 = "At the time the contract was made the legislation did not permit the charging of interest on late payments, as your debtor was a small business (50 or fewer employees).  The legislation allowing small businesses to charge interest from other small businesses came into effect from 1st November 2000.";
strResponse5 = "You cannot charge interest as the debt is greater than six years old.";

// get Miliseconds
function gm(InDate){ 
//return the number of milliseconds in a given date
return Date.UTC(InDate.getFullYear(), 
		InDate.getMonth(), 
		InDate.getDate(), 
		InDate.getHours(),
		InDate.getMinutes(),
		InDate.getSeconds());
}

// REFERENCE RATES FOR CONTRACT FORMED AFTER 6/8/02
// if date payment became overdue has a new rate as of 01/07/2003 enter 01/06/2003
var e1 = gm(new Date(1992,04,05));
var e2 = gm(new Date(1992,08,22));
var e3 = gm(new Date(1992,09,16));
var e4 = gm(new Date(1992,10,13));
var e5 = gm(new Date(1993,00,26));
var e6 = gm(new Date(1993,10,23));
var e7 = gm(new Date(1994,01,08));
var e8 = gm(new Date(1994,10,13));
var e9 = gm(new Date(1994,11,07));
var e10 = gm(new Date(1995,01,02));
var e11 = gm(new Date(1995,11,13));
var e12 = gm(new Date(1996,00,19));
var e13 = gm(new Date(1996,02,08));
var e14 = gm(new Date(1996,06,06));
var e15 = gm(new Date(1996,09,30));
var e16 = gm(new Date(1997,04,06));
var e17 = gm(new Date(1997,06,06));
var e18 = gm(new Date(1997,07,10));
var e19 = gm(new Date(1997,07,07));
var e20 = gm(new Date(1997,10,06));
var e21 = gm(new Date(1998,00,04));
var e22 = gm(new Date(1998,09,08));
var e23 = gm(new Date(1998,10,05));
var e24 = gm(new Date(1998,11,10));
var e25 = gm(new Date(1999,00,07));
var e26 = gm(new Date(1999,01,04));
var e27 = gm(new Date(1999,03,08));
var e28 = gm(new Date(1999,06,10));
var e29 = gm(new Date(1999,08,08));
var e30 = gm(new Date(1999,10,04));
var e31 = gm(new Date(2000,00,13));
var e32 = gm(new Date(2000,01,10));
var e33 = gm(new Date(2001,01,08));
var e34 = gm(new Date(2001,03,05));
var e35 = gm(new Date(2001,04,10));
var e36 = gm(new Date(2001,07,02));
var e37 = gm(new Date(2001,08,18));
var e38 = gm(new Date(2001,09,04));
var e39 = gm(new Date(2001,10,08));//08/11/01 - 12%
var e40 = gm(new Date(2003,06,01));//01/07/03 - 11.75%
var e41 = gm(new Date(2004,06,01));//01/07/04 - 12.5%
var e42 = gm(new Date(2005,00,01));//01/01/05 - 12.75%
var e43 = gm(new Date(2006,00,01));//01/01/06 - 12.5%
var e44 = gm(new Date(2007,00,01));//01/01/07 - 13%
var e45 = gm(new Date(2007,06,01));//01/07/07 - 13.5%
var e46 = gm(new Date(2008,06,01));//01/07/08 - 13%
var e47 = gm(new Date(2009,00,01));//01/01/09 - 2% + 8% = 10%
var e48 = gm(new Date(2009,06,01));//01/07/09 - 0.5% + 8% = 8.5%


InterestDates = new Array( e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46, e47, e48);

// These are Bank of England base rates until 6/8/02 then they become reference rates
InterestRates = new Array( 9.87, 8.87, 7.87, 6.87, 5.87, 5.37, 5.12, 5.62, 6.12, 6.62, 6.37, 6.12, 5.93, 5.68, 5.93, 6.25, 6.50, 6.75, 7.00, 7.25, 7.50, 7.25, 6.75, 6.25, 6.00, 5.50, 5.25, 5.00, 5.25, 5.50, 5.75, 6.00, 5.75, 5.50, 5.25, 5.00, 4.75, 4.50, 4.00, 3.75, 4.50, 4.75, 4.50, 5.00, 5.50, 5.00, 2.00, 0.5);

//BANK OF ENGLAND RATES FOR CONTRACT FORMED BETWEEN 31/10/98 AND 6/8/02
// if date payment became overdue has a new rate as of 01/07/2003 enter 01/06/2003; Date is yyyy, mm, dd
//Latest rate change dates are 6/5/04    :  10/06/04    :  05/08/04    : 04/08/05  : 04/08/06
var d1 = gm(new Date(1992,04,05))
var d2 = gm(new Date(1992,08,22))
var d3 = gm(new Date(1992,09,16))
var d4 = gm(new Date(1992,10,13))
var d5 = gm(new Date(1993,00,26))
var d6 = gm(new Date(1993,10,23))
var d7 = gm(new Date(1994,01,08))
var d8 = gm(new Date(1994,10,13))
var d9 = gm(new Date(1994,11,07))
var d10 = gm(new Date(1995,01,02))
var d11 = gm(new Date(1995,11,13))
var d12 = gm(new Date(1996,00,19))
var d13 = gm(new Date(1996,02,08))
var d14 = gm(new Date(1996,06,06))
var d15 = gm(new Date(1996,09,30))
var d16 = gm(new Date(1997,04,06))
var d17 = gm(new Date(1997,05,06))
var d18 = gm(new Date(1997,06,10))
var d19 = gm(new Date(1997,07,07))
var d20 = gm(new Date(1997,10,06))
var d21 = gm(new Date(1998,05,04))
var d22 = gm(new Date(1998,09,08))
var d23 = gm(new Date(1998,10,05))
var d24 = gm(new Date(1998,11,10))
var d25 = gm(new Date(1999,00,07))
var d26 = gm(new Date(1999,01,04))
var d27 = gm(new Date(1999,03,08))
var d28 = gm(new Date(1999,05,10))
var d29 = gm(new Date(1999,08,08))
var d30 = gm(new Date(1999,10,04))
var d31 = gm(new Date(2000,00,13))
var d32 = gm(new Date(2000,01,10))
var d33 = gm(new Date(2001,01,08))
var d34 = gm(new Date(2001,03,05))
var d35 = gm(new Date(2001,04,10))
var d36 = gm(new Date(2001,07,02))
var d37 = gm(new Date(2001,08,18))
var d38 = gm(new Date(2001,09,04))
var d39 = gm(new Date(2001,10,07))
var d40 = gm(new Date(2003,01,06))
var d41 = gm(new Date(2003,06,10))
var d42 = gm(new Date(2003,10,06))
var d43 = gm(new Date(2004,01,05))
var d44 = gm(new Date(2004,04,06))
var d45 = gm(new Date(2004,05,10))
var d46 = gm(new Date(2004,07,05))
var d47 = gm(new Date(2005,07,04))
var d48 = gm(new Date(2006,07,04))

BOEInterestDates = new Array( d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15, d16, d17, d18, d19, d20, d21, d22, d23, d24, d25, d26, d27, d28, d29, d30, d31, d32, d33, d34, d35, d36, d37, d38, d39, d40, d41, d42, d43, d44, d45, d46, d47, d48);

// Bank of England base rates  
BOEInterestRates = new Array( 9.87, 8.87, 7.87, 6.87, 5.87, 5.37, 5.12, 5.62, 6.12, 6.62, 6.37, 6.12, 5.93, 5.68, 5.93, 6.25, 6.50, 6.75, 7.00, 7.25, 7.50, 7.25, 6.75, 6.25, 6.00, 5.50, 5.25, 5.00, 5.25, 5.50, 5.75, 6.00, 5.75, 5.50, 5.25, 5.00, 4.75, 4.50, 4.00, 3.75, 3.5, 3.75, 4.00, 4.25, 4.50, 4.75, 4.50, 4.75);
	
//get our constants handy...
dtmPhaseOne = new Date("October 31, 1998 23:59:59");
dtmPhaseTwo = new Date("October 31, 2000 23:59:59");
dtmPhaseThree = new Date("August 6, 2002 23:59:59");
today = new Date();

//get them as something we can compare...
P1secs = gm(dtmPhaseOne);
P2secs = gm(dtmPhaseTwo);
P3secs = gm(dtmPhaseThree);
todaysecs = gm(today);

var reason = "";

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isValidDate (myDate,sep) {
// checks if date passed is in valid dd/mm/yyyy format
	if (myDate.length == 10) {
        if (myDate.substring(2,3) == sep && myDate.substring(5,6) == sep) {
        	var date  = myDate.substring(0,2);
          	var month = myDate.substring(3,5);
          	var year  = myDate.substring(6,10);
			var test = new Date(year,month-1,date);
          		if (year == y2k(test.getFullYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {
              		reason = "";
          		}else{
              		reason = "The date you have entered is invalid.  Please check and try again.";
          		}
		}else{
			reason = "Please use '/' as the date separator.";
      	}

	}else if (myDate.length > 10) {
		reason = "The date you have entered has too many characters.  Please check and try again.";
	}else{
		reason = "The date you have entered has too few characters.  Please check and try again.";			
	}
}

function nextNearest(value, number) {
	var ceil = Math.ceil(value);
	var remainder = value % number;
	if (remainder > 0){
  		IntegerOverTime = value - remainder + number;
	}else{
	IntegerOverTime = value;
	}
}
function CalcInterest( OverDueDate, InvoiceValue, PaymentMade, RateType){
//Returns a relevantly formatted string. 
var Found = false;
var OverSecs = gm(OverDueDate); 
var Value; 
var PaymentSecs = PaymentMade;
var Days = Math.floor((PaymentSecs - OverSecs)/(1000*60*60*24)); 
//the number of days over which we are making our calculation


nextNearest(Days, 1);
Days = IntegerOverTime;
if (RateType == 'boeRate') {
	// CALCULATE BASED ON BANK OF ENGLAND RATE
	//Search through the dates for the correct match:
	for(Index=0; Index < BOEInterestRates.length; Index++){
		if(BOEInterestDates[Index] > OverSecs){  
			//We need to deal with rounding
			Value = ((BOEInterestRates[Index-1] + 8) * InvoiceValue * Days) / 36500;
			Value = (Math.round(Value*100))/100;
			Value = (Value == Math.floor(Value)) ? Value + '.00' : (  (Value*10 == Math.floor(Value*10)) ? Value + '0' : Value);
			return "You are entitled to charge £" + Value + " in interest";
		}
	}
    
	//We need to deal with rounding
	//This is for when the date is later than the last change of interest rate...
	Index = BOEInterestRates.length - 1;
	//return BOEInterestRates[Index];
	Value = ((BOEInterestRates[Index] + 8) * InvoiceValue * Days) / 36500;
	//return Value + "," + BOEInterestRates[Index];
   		Value = (Math.round(Value*100))/100;
   		Value = (Value == Math.floor(Value)) ? Value + '.00' : (  (Value*10 == Math.floor(Value*10)) ? Value + '0' : Value);
	return "You are entitled to charge £" + Value + " in interest";	
}else{
	// CALCULATE BASED ON REFERENCE RATE
	//Search through the dates for the correct match:
	for(Index=0; Index < InterestRates.length; Index++){
		if(InterestDates[Index] > OverSecs){		
			//We need to deal with rounding
			//return InterestRates[Index];
			Value = ((InterestRates[Index-1] + 8) * InvoiceValue * Days) / 36500;
//			Value = ((InterestRates[Index] + 8) * InvoiceValue * Days) / 36500;
			//return Value + "," + InterestRates[Index-1];
   				Value = (Math.round(Value*100))/100;
   				Value = (Value == Math.floor(Value)) ? Value + '.00' : (  (Value*10 == Math.floor(Value*10)) ? Value + '0' : Value);
			return "You are entitled to charge £" + Value + " in interest";
		}
	}
	//We need to deal with rounding
	//This is for when the date is later than the last change of interest rate...

	Index = InterestRates.length - 1;
	//return InterestRates[Index];
	Value = ((InterestRates[Index] + 8) * InvoiceValue * Days) / 36500;
	//return Value + "," + InterestRates[Index];
   		Value = (Math.round(Value*100))/100;
   		Value = (Value == Math.floor(Value)) ? Value + '.00' : (  (Value*10 == Math.floor(Value*10)) ? Value + '0' : Value);
	return "You are entitled to charge £" + Value + " in interest";			
	}
} 
function ParseDate( DateString ){
	// Gives us a Date object from a dd/mm/yyyy string.
	//We've specified the format, so let's assume that the numbers are in the right place...
	var re = new RegExp("^([^\/]*)\/([^\/]*)\/([^\/]*)$");


	newArray = DateString.match(re, "");
	return new Date(newArray[3], newArray[2]-1, newArray[1]); 
}	

//Our main switching function
function Calculate( ContractDateString, OverDateString, PaymentMade, PubBody, CredEmpCount, DebtEmpCount, InvVal ) {
	//Check not blank
	if((OverDateString=="") || (CredEmpCount=="") || (DebtEmpCount=="") || (InvVal=="") || (ContractDateString=="")) {
		return "Please ensure that you have provided the date the contact was made, the date the payment became overdue, the size of the creditor and debtor companies, and the value of the overdue payment.";
	}


	//Check a number
	if( (isNaN(CredEmpCount)) || (isNaN(DebtEmpCount)) || (isNaN(InvVal)) ) {
		return "Please ensure that you have not used letters or punctuation in fields specified as 'numbers only.'";
	}


	//Date check
	isValidDate(OverDateString, "/")
	if(reason!=""){
		return "Date payment became overdue: " + reason;
	}

	<!-- if NOT blank-->
	if ((PaymentMade !="")&&(PaymentMade !=null)&&(PaymentMade !="undefined")){
		isValidDate(PaymentMade, "/")
			if(reason!=""){
				return "Date of payment: " + reason;
			}
	}else{
	<!-- if blank set todays date-->
	var PaymentMade = new Date();
	var writemonth  = new String(PaymentMade.getMonth()+1)
		if (writemonth.length == '1'){
			writemonth = "0" + writemonth
		}
	var todaysdate = PaymentMade.getDate() + '/' + writemonth + '/' + PaymentMade.getFullYear()
	PaymentMade = todaysdate
	}

	isValidDate(ContractDateString, "/")
	if(reason!=""){
		return "Date contract was formed: " + reason;
	}

	//Deal with our initial input...
	var ContractDate = ParseDate(ContractDateString);
	var ContractSecs = gm(ContractDate);
	var OverDate = ParseDate(OverDateString);
	var OverSecs = gm(OverDate);
	var Payment = ParseDate(PaymentMade);
	var PaymentSecs = gm(Payment);
	if (ContractSecs > OverSecs) {
		return "The payment overdue date precedes the contract date.  Please check your dates and try again."
	}
	if (ContractSecs > todaysecs) {
		return "The contract date occurs in the future. Please check your dates and try again."
	}
	if (PaymentSecs > todaysecs) {
		return "The date of payment occurs in the future. Please check your dates and try again."
	}	
	if (OverSecs > todaysecs) {
		return "The date the payment became overdue occurs in the future.  Please check your dates and try again."
	}
	if (OverSecs > PaymentSecs) {
		return "The payment is not overdue.  Please check your dates and try again."
	}
	//we're before phase one here
	if(ContractSecs < gm(dtmPhaseOne)){
		return strResponse1;
	}

	//we're dealing with debt older than six years here...
	var ExpireDate = new Date();
	ExpireDate.setYear(ExpireDate.getFullYear() - 6);
	if(ContractSecs < gm(ExpireDate)){
		return strResponse5;
	}
	
	//we're in phase one here 31/10/98 - 30/10/00
	if(ContractSecs > gm(dtmPhaseOne) && ContractSecs < gm(dtmPhaseTwo)) {
    if (PubBody ==true){
			return strResponse2;
		}
		if (CredEmpCount < 51 && DebtEmpCount < 51) {	
			return strResponse4;
		}
		if (CredEmpCount > 50){
			if (DebtEmpCount < 51) {
				return strResponse3;					
			}else{
				return strResponse31;
			}	
		}
		var RateType = 'boeRate'
		return CalcInterest( OverDate, InvVal, PaymentSecs, RateType) +" under the Late Payment of Commercial Debts [Interest] Act 1998.";
	}
		
	//we're in phase two here 31/10/00 - 5/8/02
	if (ContractSecs > gm(dtmPhaseTwo) && ContractSecs < gm(dtmPhaseThree)) { 
		if (PubBody ==true){
			return strResponse2;
		}

		if (CredEmpCount > 50){
			if (DebtEmpCount < 51) {
				return strResponse3;					

			}else{
				return strResponse31;
			}	
		}
		var RateType = 'boeRate'
		return CalcInterest( OverDate, InvVal, PaymentSecs, RateType) +" under the Late Payment of Commercial Debts [Interest] Act 1998. ";
	}

	//we're in phase three here after 6/8/02
	if (ContractSecs > gm(dtmPhaseThree)) {
    	var RateType = 'refRate'
		return CalcInterest( OverDate, InvVal, PaymentSecs, RateType) +" under the Late Payment of Commercial Debts [Interest] Act 1998 as amended and supplemented by the Late Payment of Commercial Debts Regulations 2002.";
	}
}


	function Holder(){
		document.calc.outputfield.value = Calculate(document.calc.contractdate.value, document.calc.overduedate.value, document.calc.paymentmade.value, document.calc.pubsecbod.checked, document.calc.creditempcount.value, document.calc.debtempcount.value, document.calc.invoiceval.value);
		return false;
	}

//-->
