// JavaScript Document
function validateContactForm(contactNameObj, contactEmailObj, contactMessageObj) {
	//initialize tracking variables
	var errors = 0;
	var errorFlags = 0;
	
	//validate name field; required field
	if(!isValidName(contactNameObj)) 
	{
		errors++;
		errorFlags = errorFlags | ((contactNameObj.value.length == 0) ? 1 : 2);
	}
	
	//validate email field; required field; must be in a valid email address format
	if(!isValidEmail(contactEmailObj)) 
	{
		errors++;
		errorFlags = errorFlags | ((contactEmailObj.value.length == 0) ? 4 : 8);		
	}
	
	//validate message field; required field
	if(!isValidMessage(contactMessageObj)) 
	{
		errors++;
		errorFlags = errorFlags | 16;		
	}	
	
	//if any errors are found, display them and return 'false'; otherwise, return 'true'
	if(errors > 0) {
		displayContactFormErrors(errors, errorFlags);
		return false;
	}
	else {
		location.href = "thanks.htm";
		//return true;
	}
}

function isValidName(contactNameObj) {
	//name can only contain letters, spaces, hyphens, and apostrophes
	var namePattern = /[\sa-zA-Z]+/;
	
	return ((contactNameObj.value.length > 0) ? true : false);
}

function isValidEmail(contactEmailObj) {
	return (((contactEmailObj.value.length > 0) && (contactEmailObj.value.indexOf("@") > 0) && (contactEmailObj.value.indexOf(".") > 2) && (contactEmailObj.value.lastIndexOf(".") > contactEmailObj.value.indexOf("@"))) ? true : false);
}

function isValidMessage(contactMessageObj) {
	return ((contactMessageObj.value.length > 0) ? true : false);	
}

function displayContactFormErrors(errors, errorFlags) {
	//clear any previously displayed error messages
	resetErrorMessages();
	
	//build and display error summary
	var errorMessage = "<span class=\"errorTextBold\">Uh-oh!!<br />" + errors + ((errors > 1) ? " errors were " : " error was ") + "found in your request!<br />Please correct them and try again.</span>";
	document.getElementById('page_dialog').innerHTML = errorMessage;
	
	//display field errors
	document.getElementById('fieldLabel_name').innerHTML = ((errorFlags & 1) ? "(Required)" : ((errorFlags & 2) ? "(Invalid characters found)" : ""));	
	document.getElementById('fieldLabel_email').innerHTML = ((errorFlags & 4) ? "(Required)" : ((errorFlags & 8) ? "(Invalid email address)" : ""));
	document.getElementById('fieldLabel_message').innerHTML = ((errorFlags & 16) ? "(Required)" : "");	
}

function resetErrorMessages() {
	document.getElementById('page_dialog').innerHTML = "\"I'd love to hear from my visitors-hope you liked my site! If you have any awesome job prospects, let me know!\"";
	document.getElementById('fieldLabel_name').innerHTML = "";
	document.getElementById('fieldLabel_email').innerHTML = "";
	document.getElementById('fieldLabel_message').innerHTML = "";
}

function displayPopQuizAnswer(questionID, questionResult, scorePoint, quizEnd) {
	//display question result
	var openingTag = ((scorePoint == 1) ? "<span style='color:#0000ff;'>" : "<span style='color:#ff0000;'>");
	document.getElementById(questionID).innerHTML = openingTag + questionResult + "</span>";
	
	//add result to running score
	
	//if the last question has been reached, display an on-screen summary message
	if(quizEnd == 1)
	{
		document.getElementById('quiz_result_panel').innerHTML = "<b>Thanks for participating. Out of 5 questions, you answered " + document.pop_quiz.score_tracker.value + " correctly.</b>";
	}
}

function displayComicImage(imageURL) {
	var imageWin = window.open(imageURL, imageWin, "height=500,width=700,resizable=1");
}
