﻿Event.observe(window, 'load', init, false);

function validateAmount(control) {
	var isValidAmount = getFloatFromTextbox(control);

	if (! isValidAmount) {
		control.value = "";
	}

	return isValidAmount;
}


function validatePercentageAmount(control) {
	var isValidAmount = getFloatFromTextbox(control);

	if (isValidAmount) {
		var amount = parseFloat(control.value);
		
		if (amount < 0 || amount > 100) {
			isValidAmount = false;	
		}
	}
	
	if (! isValidAmount) {
		control.value = "";
	} 

	return isValidAmount;
}


var QUESTION_NAME_PREFIX = "question";
var NEXT_LINK_NAME_PREFIX = "nextQ";
var EARNEST_POINTS_CONTROL_NAME = "earned_points";
var QUESTIONS_FORM_NAME = "questions";


var currentQuestion = 1;

var questions = [
	{
		responsePoints: null  /* question 1 */
	},

	{
		responsePoints: null,  /* question 2 */
		inputField: "inputQ2",
		inputValidator: validateAmount,
		pointsCalculator: function(value) {
			var points = 0;

			if (value <= 100) {
				points = 0;
			} else if (value >= 101 && value <= 500) {
				points = 400;
			} else if (value >= 501 && value <= 1000) {
				points = 2000;
			} else if (value >= 1001 && value <= 5000) {
				points = 10000;
			} else if (value >= 5001 && value <= 20000) {
				points = 20000;
			} else if (value >= 20001 && value <= 100000) {
				points = 50000;
			} else if (value >= 100001) {
				points = 250000;
			}

			return points;
		}
	},

	{
		responsePoints: null /* question 3 */
	},

	{
		responsePoints: null /* question 4 */
	},

	{
		responsePoints: null,  /* question 5 */
		inputField: "inputQ5",
		inputValidator: validateAmount,
		pointsCalculator: function(value) {
			var points = 0;

			if (value <= 100) {
				points = 0;
			} else if (value >= 101 && value <= 200) {
				points = 2000;
			} else if (value >= 201 && value <= 500) {
				points = 5000;
			} else if (value >= 501 && value <= 1000) {
				points = 10000;
			} else if (value >= 1001 && value <= 3000) {
				points = 30000;
			} else if (value >= 3001 && value <= 10000) {
				points = 100000;
			} else if (value >= 10001 && value <= 20000) {
				points = 200000;
			} else if (value >= 20001 && value <= 50000) {
				points = 500000;
			} else if (value >= 50001 && value <= 100000) {
				points = 1000000;
			} else if (value >= 100001) {
				points = 1500000;
			}

			return points;
		}
	},

	{
		responsePoints: null,  /* question 6 */
		inputField: "inputQ6",
		inputValidator: validateAmount,
		pointsCalculator: function(value) {
			var points = 0;

			if (value >= 201 && value <= 1000) {
				points = 100;
			} else if (value >= 1001 && value <= 5000) {
				points = 250;
			} else if (value >= 5001 && value <= 10000) {
				points = 2000;
			} else if (value >= 10001 && value <= 50000) {
				points = 5000;
			} else if (value >= 50001 && value <= 100000) {
				points = 10000;
			} else if (value >= 100001 && value <= 300000) {
				points = 30000;
			} else if (value >= 300001 && value <= 600000) {
				points = 60000;
			} else if (value >= 600001 && value <= 1000000) {
				points = 100000;
			} else if (value >= 1000001) {
				points = 250000;
			}

			return points;
		}
	},

	{
		responsePoints: null,  /* question 7 */
		inputField: "inputQ7",
		inputValidator: validateAmount,
		pointsCalculator: function(value) {
			var points = 0;

			if (value >= 11 && value <= 100) {
				points = 100;
			} else if (value >= 101 && value <= 500) {
				points = 1000;
			} else if (value >= 501 && value <= 2000) {
				points = 10000;
			} else if (value >= 2001 && value <= 10000) {
				points = 20000;
			} else if (value >= 10001) {
				points = 100000;
			}

			return points;
		}
	},

	{
		responsePoints: null,  /* question 8 */
		inputField: "inputQ8",
		inputValidator: validateAmount,
		pointsCalculator: function(value) {
			var points = 0;
			if (value <=100) {
				points = 0;
			} else if (value >= 101 && value <= 200) {
				points = 500;
			} else if (value >= 201 && value <= 500) {
				points = 1000;
			} else if (value >= 501 && value <= 2000) {
				points = 5000;
			} else if (value >= 2001 && value <= 5000) {
				points = 10000;
			} else if (value >= 5001 && value <= 10000) {
				points = 50000;
			} else if (value >= 10001 && value <= 50000) {
				points = 100000;
			} else if (value >= 50001 && value <= 100000) {
				points = 200000;
			} else if (value >= 100001) {
				points = 300000;
			}

			return points;
		}
	},

	{
		responsePoints: null,  /* question 9 */
		inputField: "inputQ9",
		inputValidator: validateAmount,
		pointsCalculator: function(value) {
			var points = 0;

			if (value >= 2 && value <= 10) {
				points = 50;
			} else if (value >= 11 && value <= 20) {
				points = 200;
			} else if (value >= 21 && value <= 50) {
				points = 500;
			} else if (value >= 51 && value <= 100) {
				points = 2000;
			} else if (value >= 101) {
				points = 10000;
			}

			return points;
		}
	},

	{
		responsePoints: null  /* question 10 */
	},

	{
		responsePoints: null  /* question 11 */
	},

	{
		responsePoints: null  /* question 12 */
	},

	{
		responsePoints: null  /* question 13 */
	},

	{
		responsePoints: null  /* question 14 */
	},

	{
		responsePoints: null,  /* question 15 */
		inputField: "inputQ15",
		inputValidator: validateAmount,
		pointsCalculator: function(value) {
			var points = 0;

			if (value >= 1 && value <= 9999) {
				points = 100000;
			} else if (value >= 10000 && value <= 49999) {
				points = 50000;
			} else if (value >= 50000 && value <= 99999) {
				points = 10000;
			} else if (value >= 100000 && value <= 999999) {
				points = 1000;
			} else if (value >= 1000000 && value <= 5000001) {
				points = 100;
			} else if (value >= 5000002) {
				points = 0;
			}

			return points;
		}
	},

	{
		responsePoints: null  /* question 16 */
	},

	{
		responsePoints: null,  /* question 17 */
		inputField: "inputQ17",
		inputValidator: validateAmount,
		pointsCalculator: function(value) {
			var points = 0;
			
			if (value == 0) {
				points = 0;
			} else if (value >= 1 && value <= 999) {
				points = 1000;
			} else if (value >= 1000 && value <= 4999) {
				points = 4000;
			} else if (value >= 5000 && value <= 10000) {
				points = 5000;
			} else if (value >= 10001 && value <= 50000) {
				points = 50000;
			} else if (value >= 50001 && value <= 100000) {
				points = 500000;
			} else if (value >= 100001 && value <= 500000) {
				points = 1000000;
			} else if (value >= 500001) {
				points = 2000000;
			}

			return points;
		}
	},

	{
		responsePoints: null,  /* question 18 */
		inputField: "inputQ18",
		inputValidator: validatePercentageAmount,
		pointsCalculator: function(value) {
			var points = ((questions[5 - 1].responsePoints)/2) * (value / 100);

			return points;
		}
	},

	{
		responsePoints: null,  /* question 19 */
		inputField: "radioq18",
		pointsCalculator: function(value) {
			var points = 0;
			var totalSoFar;

			this.responsePoints = 0;
			totalSoFar = calculateTotalPoints();

			switch (value) {
				case "1":  // Finance
					points = totalSoFar * .2;
					break;

				case "2": // Gambling
					points = totalSoFar * .25;
					break;

				case "3": // Travel
					points = totalSoFar * .15;
					break;

				case "4": // Everything else

					//method 1
					//points = totalSoFar * .2;

					//method 2
					//points = (questions[5 - 1].responsePoints) * .2;
					
					points = totalSoFar;	
					break;
			}

			return points;
		}
	}
]


function calculateTotalPoints() {
	var total = 0;

	for (var i = 0;  i < questions.length; ++i) {
		var question;

		question = questions[i];
		total += question.responsePoints;
	}

	return total;
}


function goNext(question_id) {
	$(QUESTION_NAME_PREFIX + question_id).style.display = 'none';

	if ( $(QUESTION_NAME_PREFIX + (question_id + 1) ) ) {
		var question;

	    question_id++;
		question = questions[question_id - 1];

	    $(QUESTION_NAME_PREFIX + question_id).style.display = 'block';

		if (question.inputField != null
			&& eval("$(QUESTIONS_FORM_NAME)." + question.inputField).focus) {
			eval("$(QUESTIONS_FORM_NAME)." + question.inputField).focus();
		}

	} else {

	    $(EARNEST_POINTS_CONTROL_NAME).value = calculateTotalPoints();
	    $(QUESTIONS_FORM_NAME).submit();

	}

	return question_id;
}


function goBack() {
	$(QUESTION_NAME_PREFIX + currentQuestion).style.display='none';
	currentQuestion--;
	$(QUESTION_NAME_PREFIX + currentQuestion).style.display='block';
}


function processReponseAndContinue(responsePoints) {
	var question = questions[currentQuestion - 1];

	if (responsePoints == null) {

		if (question.inputValidator
			&& ! question.inputValidator( eval("$(QUESTIONS_FORM_NAME)." + question.inputField)) ) {
			return;
		}

		if (question.pointsCalculator) {
			var fieldValue = eval("$(QUESTIONS_FORM_NAME)." + question.inputField + ".value");
			
			if (fieldValue == null) {
				
				// Assumes it is a radio button.				
				var radioButton = eval("$(QUESTIONS_FORM_NAME)." + question.inputField);				
				
				for (var i = 0; i < radioButton.length; ++i) {
					
					if (radioButton[i].checked) {
						fieldValue = radioButton[i].value;
						break;
					}
				} 
				
			}
			
			responsePoints = question.pointsCalculator(fieldValue);				
		}
	}

	question.responsePoints = responsePoints;

	currentQuestion = goNext(currentQuestion);
}


function init() {
	currentQuestion = 1;
}


function getFloatFromTextbox( control )
{
	var floatTest = /^\$?(\d{1,3},?(\d{3},?)*\d{3}(\.\d{0,2})?|\d{1,3}(\.\d{0,2})?|\.\d{1,2}?)$/;
	return floatTest.test(control.value);
}


function handleFormSubmit() {
	processReponseAndContinue();
}


var isIE = navigator.appName.indexOf('Microsoft') != -1;





