function change_data ( id, new_content ) {
	try {
		document.getElementById(id).innerHTML = new_content;
	}
	catch (e) {
		alert('Change data error with id: ' + id);
	}
}

function myQuiz () {
	this.questionNumber = 0;
	this.question = "";
	this.answer = 0;
	this.initTime = 0;
	this.totalTime = 0;
	this.totalWrong = 0;
	this.answered = 0;
	this.attempted = 0;
}

myQuiz.prototype.init = function () {
	this.questionNumber += 1;
	
	if ( this.questionNumber > 20 ) {
		this.sendData();
		return;
	}
	
	var type = this.random(10);
	
	if ( type == 0 || type == 1 ) {
		this.add();
	}
	else if ( type == 2 || type == 3 ) {
		this.subtract();
	}
	else if ( type == 4 || type == 5 ) {
		this.multiply();
	}
	else if ( type == 6 || type == 7 ) {
		this.divide();
	}
	else if ( type == 8 ) {
		this.square();
	}
	else if ( type == 9 ) {
		this.triangle();
	}
	else {
		this.power();
	}
	
	change_data('quesNumber', this.questionNumber );
	change_data('question', this.question );
	
	this.initTime = new Date().getTime();
	
	document.answer.answer.focus();
}

myQuiz.prototype.sendData = function () {
	var myForm = document.createElement("form");
	var myTime = document.createElement("input");
	var myIncr = document.createElement("input");
	
	myForm.action = 'results.php';
	myForm.method = 'post';
	
	myTime.name  = 'time';
	myTime.type  = 'hidden';
	myTime.value = this.totalTime;
	
	myIncr.name  = 'wrong';
	myIncr.type  = 'hidden';
	myIncr.value = this.totalWrong;
	
	myForm.appendChild(myTime);
	myForm.appendChild(myIncr);
	document.getElementById('result').appendChild(myForm);
	myForm.submit();
}

myQuiz.prototype.getAnswer = function () {
	if ( this.answered ) {
		this.init();
		this.answered = 0;
		return false;
	}
	
	var userAns = document.answer.answer.value;
	var answerDate = new Date().getTime();
	var difference = answerDate - this.initTime;
	
	if ( ! userAns ) { return false }
	
	if ( userAns == this.answer ) {
		if ( parseInt( document.getElementById('fastest').innerHTML ) > difference ){
			change_data( 'fastest', difference );
		}
		
		this.answered = 1;
		this.attempted = 0;
		this.totalTime += difference;
		document.getElementById('result').style.color = '#007F00';
		change_data('result', 'Correct! The answer is ' + this.answer + '. You took ' + difference + 'ms to respond.' );
	} else {
		//this.totalTime += 3000;
		this.totalWrong += 1;
		this.attempted++;
		change_data('wrong', this.totalWrong);
		document.getElementById('result').style.color = "#FF0000";
		change_data('result', 'Incorrect! Please try again. (attempt ' + this.attempted + ')' );
	}
	
	change_data('time', this.totalTime);
	document.answer.answer.value = "";
	
	return false;
}

myQuiz.prototype.add = function () {
	var num1 = this.random(100);
	var num2 = this.random(100);
	this.question = "What is " + num1 + " + " + num2 + "?";
	this.answer = num1 + num2;
}

myQuiz.prototype.subtract = function () {
	var num1 = this.random(100);
	var num2 = this.random(100);
	this.question = "What is " + num1 + " - " + num2 + "?";
	this.answer = num1 - num2;
}

myQuiz.prototype.multiply = function () {
	var num1 = this.random(15);
	var num2 = this.random(15);
	this.question = "What is " + num1 + " x " + num2 + "?";
	this.answer = num1 * num2;
}

myQuiz.prototype.divide = function () {
	var num1 = this.random(14) + 1;
	var num2 = this.random(15);
	var product = num1 * num2;
	this.question = "What is " + product + " / " + num1 + "?";
	this.answer = num2;
}

myQuiz.prototype.power = function () {
	var base  = this.random(9) + 1;
	var power = this.random(3);
	this.question = "What is " + base + "<sup>" + power + "</sup>?";
	this.answer = Math.pow(base, power); 
}

myQuiz.prototype.triangle = function () {
	var height = this.random(8) + 1;
	var width = this.random(8) + 1;
	this.question = "What is the area of a <b>triangle</b> with a base of " + width + " and a height of " + height + "?";
	this.answer = ( height * width ) / 2;
}

myQuiz.prototype.square = function () {
	var height = this.random(14) + 1;
	var width = this.random(14) + 1;
	this.question = "What is the area of a <b>rectangle</b> with a width of " + width + " and a height of " + height + "?";
	this.answer = ( width * height );
}

myQuiz.prototype.random = function ( max ) {
	max += 1
	return Math.floor(Math.random() * max);
}
