// Keeps track of answers, builds URLs to retain answers and calculates the results of answers

// To add more questions:
// 1) Increment the question_count variable
// 2) Make sure all answers are in the range 1-9
// 3) Add a section to calculate_results ( possibly adding a new lookup table )

// Amount of questions
var question_count = 15;
// The GET variable the answers are stored with
var answer_get_variable = "answers";
// The GET variable for all questions
var question_get_variable = "question";
// Message to display if the answer is not valid
var message_corrupt = "WARNING: Some information being used is incorrect and will yield incorrect results. Please contact your administrator.";
// Message to display if not all questions on a page have been answered
var message_not_all_answers = "Please make sure you have answered all the questions on this page.";
// Maessage to display if the user is trying to skip too far forward
var message_not_go_forward = "Please make sure you have answered all previous questions.";

// The finish button label
var label_button_finish = "Recalculate results >>";

// The crop quality levels
var message_crop_quality = new Array( "Poor", "Low", "Average", "Good", "Very Good" );

// The variables for the results
var X, Y, Z;

// The answers to all questions
var answer_array = new Array( );
// The encoded answers
var answer_encoded = "";
// A common use counter
var counter = 0;

// Lookup tables for questions
var answer_lookup = new Array
( 
 	[
	 	// Question 1
		[ 5.25, 70.00, 0.00  ],
		[ 5.50, 75.00, 16.87 ],
		[ 7.00, 80.00, 42.18 ]
	],
	[ 
	 	// Question 2
		[ 0.00,  0.00, 0.00 ],
		[ 0.00,  5.00, 4.00 ],
		[ 0.00, 10.00, 4.00 ]
	],
	[
	 	// Question 3
		[  0.00,   0.00,  0.00 ],
		[  0.00,   0.00,  0.00 ],
		[ -0.30,  -5.00,  0.00 ],
		[ -0.65, -10.00,  0.00 ]
	],
	[
	 	// Question 4
		[ -0.29, 0.00,  0.00 ],
		[ -0.17, 0.00, 11.00 ],
		[  0.00, 0.00, 16.00 ]
	],
	[
	 	// Question 5 is used only to get to answer 6 and 7		
	],
	[
	 	// Question 6
		[ -0.30,   0.00, 41.00 ],
		[ -0.10,   0.00, 46.00 ],
		[  0.00,   0.00, 50.00 ],
        [ -0.10,   0.00, 50.00 ],
		[  0.00,   0.00, 55.00 ],
		[  0.10,  -5.00, 59.00 ],
		[  0.00,   0.00, 59.00 ],
		[  0.10,  -5.00, 64.00 ],
		[  0.20, -10.00, 68.00 ]
	],
	[
	 	// Question 7
		[ -0.10, 0.00,  0.00 ],
		[ -0.10, 0.00, 17.00 ],
		[  0.20, 0.00, 42.00 ],
        [  0.00, 0.00,  3.00 ],
		[  0.10, 0.00, 20.00 ],
		[  0.20, 0.00, 55.00 ],
		[  0.20, 0.00,  5.00 ],
		[  0.20, 0.00, 32.00 ],
		[  0.20, 0.00, 72.00 ]
	],
	[
		// Question 8
		[ 0.00, 0.00,  0.00 ],
		[ 0.23, 0.42, 16.00 ],
		[ 0.23, 0.42, 25.00 ]
	],
	[
	 	// Question 9 does not exist
	],
	[
	 	// Question 10
		[  0.00, 1.50, 27.00 ],
		[ -0.75, 0.00, 66.00 ],
		[ -0.75, 0.00,  0.00 ]
	],
	[
	 	// Question 11
		[ -1.00,  20.00, 20.00 ],
		[   0.5,   0.00, 20.00 ],
		[  1.90, -18.00, 20.00 ]
	],
	[
	 	// Question 12
		[ 0.00, -10.00, 0.00 ],
		[ 0.00,   0.00, 2.00 ],
		[ 0.00,   0.00, 4.00 ],
		[ 0.00,  -5.00, 6.00 ]
	],
	[
	 	// Question 13
		[ 0.00, -13.50,  5.00 ],
		[ 0.00,  -6.50,  5.00 ],
		[ 0.00,   0.00, 10.00 ]
	],
	[
		// Question 14
		[ -0.55, -8.00, 0.00 ],
		[  0.00,  0.00, 8.00 ],
		[ -0.30, -5.00, 6.00 ]
	],
	[
		// Question 15
		[ -0.50, -5.00, 230.00 ],
		[ -0.25,  0.00, 246.50 ],
		[ -0.80,  0.00, 100.00 ],
		[ -0.20,  0.00, 102.00 ]
	]
);

// Initialise all answers to zero
for ( counter = 0; counter < question_count; counter++ )
{
	answer_array[ counter ] = 0;
}

// The first things we need to do is decode the currently stored answers
var variables = document.location.search;

if ( variables != "" )
{
	var begin_answer_string = variables.indexOf( answer_get_variable + "=" );

	if ( begin_answer_string > 0 )
	{
		begin_answer_string = begin_answer_string + answer_get_variable.length + 1;
		
		var end_answer_string = begin_answer_string + question_count;
		var answers = variables.substring( begin_answer_string, end_answer_string );
	
		var counter;

		for ( counter = 0; counter < question_count; counter++ )
		{
			answer_array[ counter ] = parseInt( answers.substring( counter, counter + 1 ) );
		}		
	}

	// Now we update any answer fields that may have been submitted
	var last_save_point = variables.indexOf( question_get_variable );
	
	while ( last_save_point > 0 )
	{
		last_save_point = last_save_point + question_get_variable.length;
		
		var place_holder = last_save_point;
		var question_number;
		var question_value;
		
		last_save_point = variables.indexOf( "=", last_save_point );
		question_number = variables.substring( place_holder, last_save_point );
		
		place_holder = last_save_point + 1;
		last_save_point = variables.indexOf( "&", last_save_point );
		
		if ( last_save_point < 1 )
		{
			question_value = variables.substring( place_holder, variables.length );
		}
		else
		{
			question_value = variables.substring( place_holder, last_save_point );			
			last_save_point = variables.indexOf( question_get_variable, last_save_point );
		}
		
		// Check the value
		if ( ( parseInt( question_value ) > 9 ) || ( parseInt( question_value ) < 1 ) )
		{
			alert( message_corrupt );
		}
		
		// Update the internal value
		if ( parseInt( question_number ) <= question_count )
		{
			answer_array[ parseInt( question_number ) - 1 ] = parseInt( question_value );
		}
	}
}

// Now we re-encode the answers array
for ( counter = 0; counter < question_count; counter++ )
{
	answer_encoded = answer_encoded + answer_array[ counter ].toString( );
}	

// Output a URL with the answer data attached
function encode_url( href, url, question )
{
	if ( ( question > 0 ) && ( question < 65535 ) )
	{
		if ( answer_array[ question - 1 ] == 0 )
		{
			alert( message_not_go_forward );
			return false;
		}
	}
	else if ( question == 65535 )
	{
		for ( counter = 0; counter < question_count; counter++ )
		{
			if ( ( answer_array[ counter ] == 0 ) && ( counter != 8 /* Question 9 does not have an answer */ ) )
			{
				alert( message_not_go_forward );
				return false;
			}
		}
	}
	
	if ( url.indexOf( answer_get_variable + "=" ) > 0 )
	{
		return;
	}
	
	if ( url.indexOf( "?" ) > 0 )
	{
		href.href = url + "&" + answer_get_variable + "=" + answer_encoded;
	}
	else
	{
		href.href = url + "?" + answer_get_variable + "=" + answer_encoded;
	}
	
	return true;
}

// Output a hidden form variable containing the answer data
function encode_form( )
{
	document.write( "<input type=hidden name=" + answer_get_variable + " value=" + answer_encoded + " />" );
}

// Output a submit button to take you to the results page if all questions have been answered
function encode_finish_button( form_name, results_page )
{
	for ( counter = 0; counter < question_count; counter++ )
	{
		if ( ( answer_array[ counter ] == 0 ) && ( counter != 8 /* Question 9 does not have an answer */ ) )
		{
			return;
		}
	}

	document.write( "<input type=submit name=submit value=\"" + label_button_finish + "\" onClick=\"javascript: " + form_name + ".action='" + results_page + "'\" />" );
}

// Reselect the correct answer for a question
function answer_check( radio, question_number )
{
	for ( counter = 0; counter < radio.length; counter++ ) 
	{
		if ( radio[ counter ].value == answer_array[ question_number - 1 ] )
		{
			radio[ counter ].checked = true;
		}													
	}
}

// Check if one of the selections have been checked
function answer_checked( radio )
{
	for ( counter = 0; counter < radio.length; counter++ ) 
	{
		if ( radio[ counter ].checked )
		{
			return true;
		}													
	}
	
	return false;
}

// Get the answer on a radio button
function get_answer( radio )
{
	for ( counter = 0; counter < radio.length; counter++ ) 
	{
		if ( radio[ counter ].checked )
		{
			return counter + 1;
		}													
	}
	
	return 0;
}

// Calculate the results using the users answers
function calculate_results( )
{
	// Make sure we start at zeros
	X = 0;
	Y = 0;
	Z = 0;
	
	// Add all the values together
	for ( counter = 0; counter < question_count; counter++ )
	{
		/*
		var ox, oy, oz;
		ox=X;
		oy=Y;
		oz=Z;
		*/
		
		switch ( counter + 1 )
		{
			case 1: // Question 1
			case 2: // Question 2
			case 3: // Question 3
			case 4: // Question 4
			case 8: // Question 8
			case 10: // Question 10
			case 11: // Question 11	
			case 12: // Question 12	
			case 13: // Question 13	
			case 14: // Question 14
			case 15: // Question 15	
			
				if ( answer_array[ counter ] > 0 )
				{
					X = X + answer_lookup[ counter ][ answer_array[ counter ] - 1 ][ 0 ];
					Y = Y + answer_lookup[ counter ][ answer_array[ counter ] - 1 ][ 1 ];
					Z = Z + answer_lookup[ counter ][ answer_array[ counter ] - 1 ][ 2 ];
				}
				break;
				
			case 5: // Question 5
				// Question 5 is used only to get to answer 6 and 7
			case 9: // Question 9
				// There is no question 9
				break;
				
			case 6:	// Question 6
			case 7: // Question 7
			
				if ( answer_array[ 5 - 1 ] >  0 )
				{
					X = X + answer_lookup[ counter ][ ( ( answer_array[ counter ] - 1 ) * 3 ) + ( answer_array[ 5 - 1 ] - 1 ) ][ 0 ];
					Y = Y + answer_lookup[ counter ][ ( ( answer_array[ counter ] - 1 ) * 3 ) + ( answer_array[ 5 - 1 ] - 1 ) ][ 1 ];
					Z = Z + answer_lookup[ counter ][ ( ( answer_array[ counter ] - 1 ) * 3 ) + ( answer_array[ 5 - 1 ] - 1 ) ][ 2 ];
				}
			
				break;				
			
			default: // Something has gone wrong
				alert( message_corrupt );
				break;
		}
		//document.write( "("+(counter+1)+") X: " +(X-ox)+"   Y: "+(Y-oy)+"   Z: "+(Z-oz)+"<br>");
	}
	//document.write( "(final) X: " +(X)+"   Y: "+(Y)+"   Z: "+(Z)+"<br>");
}

function number_round( number, decimal_places )
{
	return ( Math.round( number * Math.pow( 10, decimal_places ) ) /  Math.pow( 10, decimal_places ) ); 
}

function display_answer_tonnes_per_hectare( )
{
	document.write( number_round( X, 1 ) );
}

function display_answer_crop_quality( )
{
	var round_Y = number_round( Y, 0 );
	
	if ( round_Y < 60 )
	{
		document.write( message_crop_quality[ 0 ] );
		return;
	}
	
	if ( round_Y < 70 )
	{
		document.write( message_crop_quality[ 1 ] );
		return;
	}
	
	if ( round_Y < 85 )
	{
		document.write( message_crop_quality[ 2 ] );
		return;
	}
	
	if ( round_Y < 100 )
	{
		document.write( message_crop_quality[ 3 ] );
		return;
	}
		
	document.write( message_crop_quality[ 4 ] );
}

function display_answer_cost_per_hectare( symbol )
{
	if ( Z < 0 )
	{
		document.write( "-" + symbol + Math.abs( number_round( Z, 0 ) ) ); 
	}
	else
	{
		document.write( symbol + number_round( Z, 0 ) );
	}
}

function display_answer_cost_per_tonne_drymatter( symbol )
{
	var cost = 0
	
	if ( X > 0 )
	{
		cost = number_round( Z / X, 0 );
	}
	
	if ( cost < 0 )
	{
		document.write( "-" + symbol + Math.abs( cost ) ); 
	}
	else
	{
		document.write( symbol + cost );
	}
}