var firsttime;
var C1,C2,C3,C4,C5,C6;										// input values
var D1,D2,D3,D4,D5,D6;										// input values
var E1,E2,E3,E4;										// input values
var C1result,C2result,D1result,D2result;					// calcluated values
var E1result,E2result,E3result,E4result,E5result;					// calcluated values
var tempD1,tempD2,tempD3;
function Startup()
{
	C1=C2=C3=C4=C5=C6 = 0; //initialsie to 0 unless there is a different values below
	D1=D2=D3=D4=D5=D6 = 0;
	E1=E2=E3=E4 = 0;	
	C1result=C2result=D1result=D2result = 0;
	E1result=E2result=E3result=E4result=E5result = 0;
	tempD1=tempD2=tempD3 = 0;

	// different values alaway stays the same unless users changes
	C1="1,000,000";
	C2=40;
	C3=10;
	C4=15;
	C5=10;
	C6=10;
		
	D1=300;
	D2=30;
	D3=10;
	D4=10;
	D5=2;
	D6="2,000";

	E1="200,000";
	E2=60;
	E3=50;
	E4=2;
	
	Update(); //format results fields on the FORM sheet according to the values above

	// update values on the form if users changes
	document.sheet.C1.value = C1;
	document.sheet.C2.value = C2;
	document.sheet.C3.value = C3;
	document.sheet.C4.value = C4;
	document.sheet.C5.value = C5;
	document.sheet.C6.value = C6;
	document.sheet.C1result.value = C1result;
	document.sheet.C2result.value = C2result;
	
	document.sheet.D1.value = D1;
	document.sheet.D2.value = D2;
	document.sheet.D3.value = D3;
	document.sheet.D4.value = D4;
	document.sheet.D5.value = D5;
	document.sheet.D6.value = D6;
	document.sheet.D1result.value = D1result;
	document.sheet.D2result.value = D2result;

	document.sheet.E1.value = E1;
	document.sheet.E2.value = E2;
	document.sheet.E3.value = E3;
	document.sheet.E4.value = E4;
	document.sheet.E1result.value = E1result;
	document.sheet.E2result.value = E2result;
	document.sheet.E3result.value = E3result;
	document.sheet.E4result.value = E4result;
	document.sheet.E5result.value = E5result;
	
	Validate();//format values fields on the FORM sheet

	return( true );
} /* Startup */


function Calculate()
{	
	C1result = C1*(C3/100);
	C2result = C1result*(C4/100);

	tempD1 = D1*(100-D3)/100;
	tempD2 = D2*(100-D4)/100;
	tempD3 = tempD1*(100-D5)/100;	
	D1result = parseFloat(D1)+parseFloat(D2)-(parseFloat(tempD3)+parseFloat(tempD2));		
	D2result = D1result*D6;

	E1result = (+E1*(100-C2)/100)*0.1;
	E2result = (C1result*E2)/100;
	E3result = (C2result+D2result+E1result+E4result+E5result);
	E4result = (E1*(100-C2)/100)*(C5/2)/100;
	E5result = E1-E1*(100-E4)/100;

	return(true);
} /* Calculate */


function Update()
{
	document.sheet.C1result.value = formatoutput( C1result ) ;
	document.sheet.C2result.value = formatoutput( C2result ) ;
	document.sheet.D1result.value = formatoutput( D1result ) ;
	document.sheet.D2result.value = formatoutput( D2result ) ;
	document.sheet.E1result.value = formatoutput( E1result ) ;
	document.sheet.E2result.value = formatoutput( E2result ) ;
	document.sheet.E3result.value = formatoutput( E3result ) ;
	document.sheet.E4result.value = formatoutput( E4result ) ;
	document.sheet.E5result.value = formatoutput( E5result ) ;


} /* Update */


function Validate()
{
	if( isnumeric( document.sheet.C1.value ) )
		C1 = formatinput( document.sheet.C1.value ) ;
	else
		document.sheet.C1.value = formatoutput( C1 ) ;

	if( isnumeric( document.sheet.C2.value ) )
		C2 = formatinput( document.sheet.C2.value ) ;
	else
		document.sheet.C2.value = formatoutput( C2 ) ;

	if( isnumeric( document.sheet.C3.value ) )
		C3 = formatinput( document.sheet.C3.value ) ;
	else
		document.sheet.C3.value = formatoutput( C3 ) ;

	if( isnumeric( document.sheet.C4.value ) )
		C4 = formatinput( document.sheet.C4.value ) ;
	else
		document.sheet.C4.value = formatoutput( C4 ) ;

	if( isnumeric( document.sheet.C5.value ) )
		C5 = formatinput( document.sheet.C5.value ) ;
	else
		document.sheet.C5.value = formatoutput( C5 ) ;

	if( isnumeric( document.sheet.C6.value ) )
		C6 = formatinput( document.sheet.C6.value ) ;
	else
		document.sheet.C6.value = formatoutput( C6 ) ;

	
	if( isnumeric( document.sheet.D1.value ) )
		D1 = formatinput( document.sheet.D1.value ) ;
	else
		document.sheet.D1.value = formatoutput( D1 ) ;

	if( isnumeric( document.sheet.D2.value ) )
		D2 = formatinput( document.sheet.D2.value ) ;
	else
		document.sheet.D2.value = formatoutput( D2 ) ;

	if( isnumeric( document.sheet.D3.value ) )
		D3 = formatinput( document.sheet.D3.value ) ;
	else
		document.sheet.D3.value = formatoutput( D3 ) ;

	if( isnumeric( document.sheet.D4.value ) )
		D4 = formatinput( document.sheet.D4.value ) ;
	else
		document.sheet.D4.value = formatoutput( D4 ) ;

	if( isnumeric( document.sheet.D5.value ) )
		D5 = formatinput( document.sheet.D5.value ) ;
	else
		document.sheet.D5.value = formatoutput( D5 ) ;

	if( isnumeric( document.sheet.D6.value ) )
		D6 = formatinput( document.sheet.D6.value ) ;
	else
		document.sheet.D6.value = formatoutput( D6 ) ;


	if( isnumeric( document.sheet.E1.value ) )
		E1 = formatinput( document.sheet.E1.value ) ;
	else
		document.sheet.E1.value = formatoutput( E1 ) ;

	if( isnumeric( document.sheet.E2.value ) )
		E2 = formatinput( document.sheet.E2.value ) ;
	else
		document.sheet.E2.value = formatoutput( E2 ) ;

	if( isnumeric( document.sheet.E3.value ) )
		E3 = formatinput( document.sheet.E3.value ) ;
	else
		document.sheet.E3.value = formatoutput( E3 ) ;

	if( isnumeric( document.sheet.E4.value ) )
		E4 = formatinput( document.sheet.E4.value ) ;
	else
		document.sheet.E4.value = formatoutput( E4 ) ;


	Calculate();
	Update();

	return true;
} /* Validate */


function isnumeric( num )
{
	var result=true;
	var start=0;

	while ((start < num.length) && result )
	{
		if( ( (num.charAt(start)<'0') || (num.charAt(start)>'9') ) && (num.charAt(start)!=',') && (num.charAt(start)!='.') )
			result=false;

		++start;
	}
	return( result );
} /* isnumeric */

function formatinput( num )
{
	var result="";
	var start=0;

	while (start < num.length)
	{
		if( ( (num.charAt(start)>='0') && (num.charAt(start)<='9') ) || (num.charAt(start)=='.') )
		{
			result = result + num.charAt(start);
		}

		++start;
	}
	return( result );
} /* formatinput */


function formatoutput( num )
{
	var result="";
	var work="";
	var i=0;

	work = ' ' + Math.round( num ) + '.00';	// con the bit afterwards into treating this as a string!

		// break up every 3 characters (charat 0 is always ' ')...
	for( i=1 ; i<work.length && work.charAt(i)!='.'; ++i )
	{
		if( work.length - i > 5 && i > 1 )
		{
			if( (work.length - i) % 3== 0 )
			{
				result = result + ',';
			}
		}

		result = result + work.charAt(i);
	}

	return( result );
} /* formatoutput */


function formatoutput2dp( num )
{
	var result="";
	var work="";
	var i=0;

	if( parseInt( num ) == num )	// no fraction
	{
		work = ' ' + num + '.00';
	}
	else if( parseInt( num * 10 ) == num * 10 )
	{
		work = ' ' + num + '0';
	}
	else
	{
		work = ' ' + Math.round( num * 100 ) / 100;	// con the bit afterwards into treating this as a string!
	}

		// break up every 3 characters...
	for( i=1 ; i<work.length; ++i )
	{
		if( work.length - i > 5 && i > 1 )
		{
			if( (work.length - i) % 3 == 0 )
			{
				result = result + ',';
			}
		}

		result = result + work.charAt(i);
	}

	return( result );
} /* formatoutput2dp */

// -->

