var total = 0;

$(document).ready(function() {
	dropDownMenus();
	
	setupAutoFill();
	
	quizSubmitHover();
	
	greenChemistryIQHover();
	
	initQuiz();
	startQuiz();
	submitAnswer();
	nextQuestion();
});

// hover for submit button on quiz form
function quizSubmitHover() {
	$('form#quiz-form button.button-submit').hover(
	function(event) {
		$(this).addClass('hover');
	},
	function(event) {
		$(this).removeClass('hover');
	});
}

// hover for "What is your Green Chemistry IQ?" box on home page
function greenChemistryIQHover() {
	$('div#whatisyouriq').hover(
	function(event) {
		$(this).addClass('hover');
	},
	function(event) {
		$(this).removeClass('hover');
	});
}

//dropdown menus
function dropDownMenus() {
	$('ul#nav-top > li').hover(
	function(event) {
		$(this).children('a').addClass('hover');
		$(this).addClass('hover');
	},
	function(event) {
		$(this).children('a').removeClass('hover');
		$(this).removeClass('hover');
	});
}

//this clears the input text fields when clicked on and restores the text if they are left blank
function setupAutoFill() {
	//look for text fields and textareas that have the "wipe" class
	$("input.wipe, textarea.wipe").each(function() {
		//stash the current value
		var v = $(this).val();
		$(this).focus(function(){
			//blank out the input field on focus
			if ($(this).val() == v) {
				$(this).val("");
			}
		}).blur(function(){
			//restore previous value if field was left blank
			if ($(this).val() == "") {
				$(this).val(v);
			}
		});
	});
}

//email form validation from w3schools
function validate_email(field,alerttxt) {
	with (field) {
		apos = value.indexOf("@");
		dotpos = value.lastIndexOf(".");
		if (apos < 1 || dotpos-apos < 2) {
			alert(alerttxt);return false;
		}
		else {
			return true;
		}
	}
}

//registration form validation
function isValidForm(thisForm) {
	if (thisForm.elements['firstname'].value == '' || (/^(\s)+$/.test(thisForm.elements['firstname'].value)) || thisForm.elements['firstname'].value == 'FIRST NAME') {
		alert('Please enter your first name.');
		return false;
	} else if (thisForm.elements['lastname'].value == '' || (/^(\s)+$/.test(thisForm.elements['lastname'].value)) || thisForm.elements['lastname'].value == 'LAST NAME') {
		alert('Please enter your last name.');
		return false;
	} else if (!(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(thisForm.email.value))) {
		alert('Please enter a valid email address.');
		return false;
	} else if (thisForm.elements['org'].value == '' || (/^(\s)+$/.test(thisForm.elements['org'].value)) || thisForm.elements['org'].value == 'ORGANIZATION') {
		alert('Please enter the name of your organization.');
		return false;
	}
	return true;
}


//initialize the quiz
function initQuiz() {
	$('div#quiz-questions').hide();
}

//start the quiz
function startQuiz() {
	$('div#quiz-intro a.button-takethetest').click(function() {
		if(isValidForm(document.getElementById('quiz-form'))) {
			$('div#quiz-intro').hide();
			$('div#quiz-questions').show();
			$('p.correct').hide();
			$('p.incorrect').hide();
			for (i = 1; i <= 12; i++) {
				$('p#text-q' + i).hide();
				$('div#responses-q' + i).hide();
				$('div#answer-q' + i).hide();
			}
			$('p#text-q1').show();
			$('div#responses-q1').show();
		}
	});
}

function submitAnswer() {
	$('div.question a.button-submit').click(function() {
		var id = $(this).parent().attr('id').slice(11);
		$('p#text-q' + id).hide();
		$('div#responses-q' + id).hide();
		$('div#answer-q' + id).show();
		if (correctAnswer(id)) {
			$('p.correct').show();
			total++;
			$('form#quiz-form input#total').val(total);
		} else {
			$('p.incorrect').show();
		}
		updateProgress(Number(100*parseInt(id)/12));
	});
}

function nextQuestion() {
	$('div.question a.button-nextquestion').click(function() {
		var id = $(this).parent().attr('id').slice(8);
		$('p.correct').hide();
		$('p.incorrect').hide();
		$('p#text-q' + (parseInt(id)+1)).show();
		$('div#responses-q' + (parseInt(id)+1)).show();
		$('div#answer-q' + id).hide();
	});
}

function correctAnswer(questionNum) {
	if (questionNum == '1') {
		return ($("input[name='q1']:checked").attr('value').slice(4) == '1');
	} else if (questionNum == '2') {
		return ($("input[name='q2']:checked").attr('value').slice(4) == '4');
	} else if (questionNum == '3') {
		return ($("input[name='q3']:checked").attr('value').slice(4) == '3');
	} else if (questionNum == '4') {
		return ($("input[name='q4']:checked").attr('value').slice(4) == '2');
	} else if (questionNum == '5') {
		return ($("input[name='q5']:checked").attr('value').slice(4) == '3');
	} else if (questionNum == '6') {
		return ($("input[name='q6']:checked").attr('value').slice(4) == '1');
	} else if (questionNum == '7') {
		return ($("input[name='q7']:checked").attr('value').slice(4) == '2');
	} else if (questionNum == '8') {
		return ($("input[name='q8']:checked").attr('value').slice(4) == '2');
	} else if (questionNum == '9') {
		return ($("input[name='q9']:checked").attr('value').slice(4) == '2');
	} else if (questionNum == '10') {
		return ($("input[name='q10']:checked").attr('value').slice(5) == '1');
	} else if (questionNum == '11') {
		return ($("input[name='q11']:checked").attr('value').slice(5) == '1');
	} else if (questionNum == '12') {
		return ($("input[name='q12']:checked").attr('value').slice(5) == '2');
	}
}

function updateProgress(percent) {
	$('div#completed').css({'width' : percent + '%'});
}


function testForEnter() 
{    
	if (event.keyCode == 13) 
	{        
		event.cancelBubble = true;
		event.returnValue = false;
         }
}

document.onkeypress = function(e)
{
e = e || window.event;
if (typeof e != 'undefined')
{
var tgt = e.target || e.srcElement;
if (typeof tgt != 'undefined' && /input/i.test(tgt.nodeName))
return (typeof e.keyCode != 'undefined') ? e.keyCode != 13 : true;
}
}