// Printer page javascript by <Markus> 21/11/2001
//modify by <CY> 14/10/2003

function getParentValues(){

// All values are imported here

		document.printForm.THL1.value = parent.frames[0].document.cpbc.THL1.value;
		document.printForm.THL2.value = parent.frames[0].document.cpbc.THL2.value;
		document.printForm.THL3.value = parent.frames[0].document.cpbc.THL3.value;
		document.printForm.FPM1.value = parent.frames[0].document.cpbc.FPM1.value;
		document.printForm.FPM2.value = parent.frames[0].document.cpbc.FPM2.value;
		document.printForm.ODP1.value = parent.frames[0].document.cpbc.ODP1.value;
		
		document.printForm.TD1.value = parent.frames[0].document.cpbc.TD1.value;
		document.printForm.ML1.value = parent.frames[0].document.cpbc.ML1.value;
		document.printForm.PR1.value = parent.frames[0].document.cpbc.PR1.value;

		document.printForm.HI1.value = parent.frames[0].document.cpbc.HI1.value;
		document.printForm.NRI1.value = parent.frames[0].document.cpbc.NRI1.value;
		document.printForm.NTL1.value = parent.frames[0].document.cpbc.NTL1.value;
		document.printForm.NIP1.value = parent.frames[0].document.cpbc.NIP1.value;
		
		document.printForm.NFX1.value = parent.frames[0].document.cpbc.NFX1.value;
		document.printForm.NNATM1.value = parent.frames[0].document.cpbc.NNATM1.value;
		var list = document.printForm.GCC1;
		list.selectedIndex = parent.frames[0].document.cpbc.GCC1.value;
		var list2 = document.printForm.GRP1;
		list2.selectedIndex = parent.frames[0].document.cpbc.GRP1.value;
		var list1 = document.printForm.SCC1;
		list1.selectedIndex = parent.frames[0].document.cpbc.SCC1.value;

		document.printForm.FPM3.value = parent.frames[0].document.cpbc.FPM3.value;
		document.printForm.VHL1.value = parent.frames[0].document.cpbc.VHL1.value;
		document.printForm.FHL1.value = parent.frames[0].document.cpbc.FHL1.value;
		document.printForm.OPF1.value = parent.frames[0].document.cpbc.OPF1.value;
		
		document.printForm.subTotal.value = parent.frames[0].document.cpbc.subTotal.value;
		document.printForm.packageFee.value = parent.frames[0].document.cpbc.packageFee.value;
		document.printForm.savingsTotal.value = parent.frames[0].document.cpbc.savingsTotal.value;
}

function propertyCalculate()
{

// All variables are decalared here

		var valTHL1 = removeComma(document.printForm.THL1.value);
		var valTHL1Total = removeComma(document.printForm.THL1Total.value);
		var valTHL2 = removeComma(document.printForm.THL2.value);
		var valTHL2Total = removeComma(document.printForm.THL2Total.value);
		var valTHL3 = removeComma(document.printForm.THL3.value);
		var valTHL3Total = removeComma(document.printForm.THL3Total.value);
		var valFPM1 = removeComma(document.printForm.FPM1.value);
		var valFPM1Total = removeComma(document.printForm.FPM1Total.value);
		var valFPM2 = removeComma(document.printForm.FPM2.value);
		var valFPM2Total = removeComma(document.printForm.FPM2Total.value);
		var valODP1 = removeComma(document.printForm.ODP1.value);
		var valODP1Total = removeComma(document.printForm.ODP1Total.value);
		
        var valpropertyTotal = removeComma(document.printForm.propertyTotal.value);

	

// Test all inputs to see if they are valid (ie. numbers, not letters or characters) If the
// input entered is not valid, issue an alert message to the user, reset the value to zero,
// and set the focus back in the text box that had the invalid input. Also test to see if more
// than one of the fields is filled in.

        if (isNaN(valTHL1))
        {
                alert("The last figure entered is not valid!");
                valTHL1 = 0;
                document.printForm.THL1.value = valTHL1;
                document.printForm.THL1.focus();
        }
		else
        {
        		document.printForm.THL1.value = formatAmount(valTHL1,0);
        }
		
        if (isNaN(valTHL2))
        {
                alert("The last figure entered is not valid!");
                valTHL2 = 0;
                document.printForm.THL2.value = valTHL2;
                document.printForm.THL2.focus();
        }
		else
        {
        		document.printForm.THL2.value = formatAmount(valTHL2,0);
        }
		
        if (isNaN(valTHL3))
        {
                alert("The last figure entered is not valid!");
                valTHL3 = 0;
                document.printForm.THL3.value = valTHL3;
                document.printForm.THL3.focus();
        }
        else
        {
        		document.printForm.THL3.value = formatAmount(valTHL3,0);
        }
		
		if (isNaN(valFPM1))
        {
                alert("The last figure entered is not valid!");
                valFPM1 = 0;
                document.printForm.FPM1.value = valFPM1;
                document.printForm.FPM1.focus();
        }
        else
        {
        		document.printForm.FPM1.value = formatAmount(valFPM1,0);
        }
		
		if (isNaN(valFPM2))
        {
                alert("The last figure entered is not valid!");
                valFPM2 = 0;
                document.printForm.FPM2.value = valFPM2;
                document.printForm.FPM2.focus();
        }
		else
        {
        		document.printForm.FPM2.value = formatAmount(valFPM2,0);
        }

		if (isNaN(valODP1))
        {
                alert("The last figure entered is not valid!");
                valODP1 = 0;
                document.printForm.ODP1.value = valODP1;
                document.printForm.ODP1.focus();
        }
		else
        {
        		document.printForm.ODP1.value = formatAmount(valODP1,0);
        }
        
// Calculate the totals and display them to screen. Send the total to the main screen,
// and recalculate the totals there.
	
	valTHL1Total = valTHL1 * 96;
	valTHL2Total = valTHL2 * 120;
	valTHL3Total = valTHL3 * 0.005;
	valFPM1Total = valFPM1 * 250;
	valFPM2Total = valFPM2 * 0.0015;
	valODP1 = (Math.round(eval(valODP1) * 100) / 100);
	if ((valODP1 > 0) && (valODP1 < 1001)) {valODP1Total = 70;}
	else if ((valODP1 >= 1001) && (valODP1 < 2001)) {valODP1Total = 100;}
	else if ((valODP1 >= 2000) && (valODP1 < 5001)) {valODP1Total = 120;}
	else if ((valODP1 >= 5000) && (valODP1 < 10001)) {valODP1Total = 140;}
	else {valODP1Total = 0;}
	
	document.printForm.THL1Total.value = formatAmount(valTHL1Total,2);
	document.printForm.THL2Total.value = formatAmount(valTHL2Total,2);
	document.printForm.THL3Total.value = formatAmount(valTHL3Total,2);
	document.printForm.FPM1Total.value = formatAmount(valFPM1Total,2);
	document.printForm.FPM2Total.value = formatAmount(valFPM2Total,2);
	document.printForm.ODP1Total.value = formatAmount(valODP1Total,2);
	
	valpropertyTotal = valTHL1Total + valTHL2Total + valTHL3Total + valFPM1Total
		 + valFPM2Total + valODP1Total;
	document.printForm.propertyTotal.value = formatAmount(valpropertyTotal,2);		
}

function investmentCalculate()
{

// All variables are decalared here

		var valTD1 = removeComma(document.printForm.TD1.value);
		var valTD1Total = removeComma(document.printForm.TD1Total.value);
		var valML1 = removeComma(document.printForm.ML1.value);
		var valML1Total = removeComma(document.printForm.ML1Total.value);
		var valPR1 = removeComma(document.printForm.PR1.value);
		var valPR1Total = removeComma(document.printForm.PR1Total.value);
		
        var valinvestmentTotal = removeComma(document.printForm.investmentTotal.value);

	

// Test all inputs to see if they are valid (ie. numbers, not letters or characters) If the
// input entered is not valid, issue an alert message to the user, reset the value to zero,
// and set the focus back in the text box that had the invalid input. Also test to see if more
// than one of the fields is filled in.

        if (isNaN(valTD1))
        {
                alert("The last figure entered is not valid!");
                valTD1 = 0;
                document.printForm.TD1.value = valTD1;
                document.printForm.TD1.focus();
        }
		else
        {
        		document.printForm.TD1.value = formatAmount(valTD1,0);
        }
		        
		if (isNaN(valML1))
        {
                alert("The last figure entered is not valid!");
                valML1 = 0;
                document.printForm.ML1.value = valML1;
                document.printForm.ML1.focus();
        }
		else
        {
        		document.printForm.ML1.value = formatAmount(valML1,0);
        }

		if (isNaN(valPR1))
        {
                alert("The last figure entered is not valid!");
                valPR1 = 0;
                document.printForm.PR1.value = valPR1;
                document.printForm.PR1.focus();
        }
        else
        {
        		document.printForm.PR1.value = formatAmount(valPR1,0);
        }

// Calculate the totals and display them to screen. Send the total to the main screen,
// and recalculate the totals there.
	
	if ((valTD1 >= 5000) && (valTD1 <= 250000)) {
		valTD1Total = valTD1 * 0.0025;
	}
	else {
		valTD1Total = 0;
	}
	valML1Total = valML1 * 0.002;
	valPR1Total = valPR1 * 96;
	
	document.printForm.TD1Total.value = formatAmount(valTD1Total,2);
	document.printForm.ML1Total.value = formatAmount(valML1Total,2);
	document.printForm.PR1Total.value = formatAmount(valPR1Total,2);
	
	
	valinvestmentTotal = valTD1Total + valML1Total + valPR1Total; //+ valNOT1Total + valNOT2Total ;
	document.printForm.investmentTotal.value = formatAmount(valinvestmentTotal,2);
}

function protectionCalculate()
{

// All variables are decalared here

		var valHI1 = removeComma(document.printForm.HI1.value);
		var valHI1Total = removeComma(document.printForm.HI1Total.value);
		var valNRI1 = removeComma(document.printForm.NRI1.value);
		var valNRI1Total = removeComma(document.printForm.NRI1Total.value);
		var valNTL1 = removeComma(document.printForm.NTL1.value);
		var valNTL1Total = removeComma(document.printForm.NTL1Total.value);
		var valNIP1 = removeComma(document.printForm.NIP1.value);
		var valNIP1Total = removeComma(document.printForm.NIP1Total.value);
				
        var valprotectionTotal = removeComma(document.printForm.protectionTotal.value);

	

// Test all inputs to see if they are valid (ie. numbers, not letters or characters) If the
// input entered is not valid, issue an alert message to the user, reset the value to zero,
// and set the focus back in the text box that had the invalid input. Also test to see if more
// than one of the fields is filled in.

        if (isNaN(valHI1))
        {
                alert("The last figure entered is not valid!");
                valHI1 = 0;
                document.printForm.HI1.value = valHI1;
                document.printForm.HI1.focus();
        }
		else
        {
        		document.printForm.HI1.value = formatAmount(valHI1,2);
        }

        if (isNaN(valNRI1))
        {
                alert("The last figure entered is not valid!");
                valNRI1 = 0;
                document.printForm.NRI1.value = valNRI1;
                document.printForm.NRI1.focus();
        }
		else
        {
        		document.printForm.NRI1.value = formatAmount(valNRI1,2);
        }
        
		if (isNaN(valNTL1))
        {
                alert("The last figure entered is not valid!");
                valNTL1 = 0;
                document.printForm.NTL1.value = valNTL1;
                document.printForm.NTL1.focus();
        }
		else
        {
        		document.printForm.NTL1.value = formatAmount(valNTL1,2);
        }
        
		if (isNaN(valNIP1))
        {
                alert("The last figure entered is not valid!");
                valNIP1 = 0;
                document.printForm.NIP1.value = valNIP1;
                document.printForm.NIP1.focus();
        }
		else
        {
        		document.printForm.NIP1.value = formatAmount(valNIP1,2);
        }

        
// Calculate the totals and display them to screen. Send the total to the main screen,
// and recalculate the totals there.
	
	valHI1Total = (Math.round(eval(valHI1) * 100) / 100) * (1/12);
	valHI1Total = (Math.round(eval(valHI1Total) * 100) / 100);
	valNRI1Total = (Math.round(eval(valNRI1) * 100) / 100) * (1/12);
	valNRI1Total = (Math.round(eval(valNRI1Total) * 100) / 100);
	valNTL1Total = (Math.round(eval(valNTL1) * 100) / 100) * 0.05;
	valNTL1Total = (Math.round(eval(valNTL1Total) * 100) / 100);
	valNIP1Total = (Math.round(eval(valNIP1) * 100) / 100) * 0.05;
	valNIP1Total = (Math.round(eval(valNIP1Total) * 100) / 100);
	
	document.printForm.HI1Total.value = formatAmount(valHI1Total,2);
	document.printForm.NRI1Total.value = formatAmount(valNRI1Total,2);
	document.printForm.NTL1Total.value = formatAmount(valNTL1Total,2);
	document.printForm.NIP1Total.value = formatAmount(valNIP1Total,2);
	
	valprotectionTotal = valHI1Total + valNRI1Total + valNTL1Total + valNIP1Total;
	document.printForm.protectionTotal.value = formatAmount(valprotectionTotal,2);

}

function daytodayCalculate()
{

// All variables are decalared here
		var valNFX1 = removeComma(document.printForm.NFX1.value);
		var valNFX1Total = removeComma(document.printForm.NFX1Total.value);
		
		
		var valNNATM1 = removeComma(document.printForm.NNATM1.value);
		var valNNATM1Total = removeComma(document.printForm.NNATM1Total.value);
		

		var list = document.printForm.GCC1;
        var valGCC1 = list.options[list.selectedIndex].value;
		var valGCC1Total = removeComma(document.printForm.GCC1Total.value);
		var list = document.printForm.GRP1;
		var valGRP1 = list.options[list.selectedIndex].value;
		var valGRP1Total = removeComma(document.printForm.GRP1Total.value);
		var list1 = document.printForm.SCC1;
        var valSCC1 = list1.options[list1.selectedIndex].value;
		var valSCC1Total = removeComma(document.printForm.SCC1Total.value);
				
        var valdaytodayTotal = removeComma(document.printForm.daytodayTotal.value);

	

// Test all inputs to see if they are valid (ie. numbers, not letters or characters) If the
// input entered is not valid, issue an alert message to the user, reset the value to zero,
// and set the focus back in the text box that had the invalid input. Also test to see if more
// than one of the fields is filled in.

        if (isNaN(valNFX1))
        {
                alert("The last figure entered is not valid!");
                valNFX1 = 0;
                document.printForm.NFX1.value = valNFX1;
                document.printForm.NFX1.focus();
        }
		else
        {
        		document.printForm.NFX1.value = formatAmount(valNFX1,0);
        }
		
       		
        if (isNaN(valNNATM1))
        {
                alert("The last figure entered is not valid!");
                valNNATM1 = 0;
                document.printForm.NNATM1.value = valNNATM1;
                document.printForm.NNATM1.focus();
        }
        else
        {
        		document.printForm.NNATM1.value = formatAmount(valNNATM1,0);
        }
		
				
				
        
// Calculate the totals and display them to screen. Send the total to the main screen,
// and recalculate the totals there.
	
	valNFX1Total = valNFX1 * 72.00;
	valNNATM1Total = valNNATM1 * 1.50;
	valGCC1Total = valGCC1 * 88.30;
	valGRP1Total = valGRP1 * 33.00;
	valSCC1Total = valSCC1 * 26.40;

	document.printForm.NFX1Total.value = formatAmount(valNFX1Total,2);
	document.printForm.NNATM1Total.value = formatAmount(valNNATM1Total,2);
	document.printForm.GCC1Total.value = formatAmount(valGCC1Total,2);
	document.printForm.GRP1Total.value = formatAmount(valGRP1Total,2);
	document.printForm.SCC1Total.value = formatAmount(valSCC1Total,2);
	
	valdaytodayTotal = valNFX1Total  + valNNATM1Total + valGCC1Total + valGRP1Total + valSCC1Total;
	document.printForm.daytodayTotal.value = formatAmount(valdaytodayTotal,2);
}

function additionalCalculate()
{

// All variables are decalared here
		var valFPM3 = removeComma(document.printForm.FPM3.value);
		var valFPM3Total = removeComma(document.printForm.FPM3Total.value);
		var valVHL1 = removeComma(document.printForm.VHL1.value);
		var valVHL1Total = removeComma(document.printForm.VHL1Total.value);
		var valFHL1 = removeComma(document.printForm.FHL1.value);
		var valFHL1Total = removeComma(document.printForm.FHL1Total.value);
		var valOPF1 = removeComma(document.printForm.OPF1.value);
		var valOPF1Total = removeComma(document.printForm.OPF1Total.value);
		
        var valadditionalTotal = removeComma(document.printForm.additionalTotal.value);

// Test all inputs to see if they are valid (ie. numbers, not letters or characters) If the
// input entered is not valid, issue an alert message to the user, reset the value to zero,
// and set the focus back in the text box that had the invalid input. Also test to see if more
// than one of the fields is filled in.

        if (isNaN(valFPM3))
        {
                alert("The last figure entered is not valid!");
                valFPM3 = 0;
                document.printForm.FPM3.value = valFPM3;
                document.printForm.FPM3.focus();
        }
		else
        {
        		document.printForm.FPM3.value = formatAmount(valFPM3,0);
        }
		
        if (isNaN(valVHL1))
        {
                alert("The last figure entered is not valid!");
                valVHL1 = 0;
                document.printForm.VHL1.value = valVHL1;
                document.printForm.VHL1.focus();
        }
		else
        {
        		document.printForm.VHL1.value = formatAmount(valVHL1,0);
        }
		
        if (isNaN(valFHL1))
        {
                alert("The last figure entered is not valid!");
                valFHL1 = 0;
                document.printForm.FHL1.value = valFHL1;
                document.printForm.FHL1.focus();
        }
        else
        {
        		document.printForm.FHL1.value = formatAmount(valFHL1,0);
        }
		
	
		if (isNaN(valOPF1))
        {
                alert("The last figure entered is not valid!");
                valOPF1 = 0;
                document.printForm.OPF1.value = valOPF1;
                document.printForm.OPF1.focus();
        }
		else
        {
        		document.printForm.OPF1.value = formatAmount(valOPF1,0);
        }
		
        
// Calculate the totals and display them to screen. Send the total to the main screen,
// and recalculate the totals there.
	
	valFPM3Total = valFPM3 * 600;
	valVHL1Total = valVHL1 * 600;
	valFHL1Total = valFHL1 * 600;
	valOPF1Total = valOPF1 * 500;
	
	document.printForm.FPM3Total.value = formatAmount(valFPM3Total,2);
	document.printForm.VHL1Total.value = formatAmount(valVHL1Total,2);
	document.printForm.FHL1Total.value = formatAmount(valFHL1Total,2);
	document.printForm.OPF1Total.value = formatAmount(valOPF1Total,2);
	
	valadditionalTotal = valFPM3Total + valVHL1Total + valFHL1Total + valOPF1Total;
	document.printForm.additionalTotal.value = formatAmount(valadditionalTotal,2);
}

function removeComma(num)
{
// All variables are decalared here
	var re =/,/g;
	var str = num.toString();

// This function looks at the incoming string and removes all the commas from it.
	str = str.replace(re,"");

	return parseFloat(str);
}

function formatAmount(Expr,DecPlaces)
{

// All variables are decalared here

        //var DecPlaces = 2;

        var str = "" + Math.round(eval(Expr) * Math.pow(10, DecPlaces));
        var DecPoint;
        var Result;
        var re;

// This function looks at the incoming string and formats it with 'DecPlaces' decimal places
// and a comma for every 3 digits.

        while (str.length <= DecPlaces)
        {
                str = "0" + str;
        }
        
        DecPoint = str.length - DecPlaces;

        if (DecPlaces > 0) {
			Result = str.substring(0, DecPoint) + "." + str.substring(DecPoint, str.length);
		}
		else {
			Result = str.substring(0, DecPoint);
		}
		
        re = /(-?\d+)(\d{3})/;

        while (re.test(Result))
        {
                Result = Result.replace(re, "$1,$2");
        }

        return Result;
}

