/* validation and other functions for html forms */

var redX 		= "<img src=\"./images/content/redx.png\" width=\"25\" height=\"18\" alt=\"Invalid\" title=\"Invalid.\" /> ";
var greenCheck = "<img src=\"./images/content/greencheck.png\" width=\"25\" height=\"18\" alt=\"Valid\" title=\"Valid.\" /> ";

function validateLogin(obj) {
	var validUsername = validateUsername(obj.username);
	var validPassword = validatePassword(obj.comment);

	if (validUsername && validPassword)
		return true;
	
	return false;
}

function validateContact(obj) {
	var validEmail = validateEmail(obj.email);
	var validComment = validateComment(obj.comment);
	var validCaptcha = validateCaptcha(obj.captcha);

	if (validEmail && validComment && validCaptcha)
		return true;
	
	return false;
}

function validateUsername(obj) {
	var value = obj.value;
	var statusBox = document.getElementById(obj.name + "Status");
	var bad = false;
	var msg = "";
	
	//make sure it's not empty and is valid
	if (value == "") {
		msg = "You must enter a username.";
		bad = true;
	} else if (value.length < 6) {
		msg = "Your username must be at least 6 characters.";
		bad = true;
	} else if (value.length > 24) {
		msg = "Your username must be less than 25 characters.";
		bad = true;
	} else {
		msg = "You entered a valid username.";	
	}
	
	//set the message to be displayed in the status box next to the form element
	if (bad) {
		msg = redX + msg;
	} else {
		msg = greenCheck + msg;
	}
	
	//display the status box
	displayMsg(statusBox, msg, bad);
	
	return !bad;
}

function validatePassword(obj) {
	var value = obj.value;
	var statusBox = document.getElementById(obj.name + "Status");
	var bad = false;
	var msg = "";
	
	//make sure it's not empty and is valid
	if (value == "") {
		msg = "You must enter a password.";
		bad = true;
	} else if (value.length < 6) {
		msg = "Your password must be at least 6 characters.";
		bad = true;
	} else if (value.length > 16) {
		msg = "Your password must be less than 17 characters.";
		bad = true;
	} else {
		msg = "You entered a valid password.";
	}
	
	//set the message to be displayed in the status box next to the form element
	if (bad) {
		msg = redX + msg;
	} else {
		msg = greenCheck + msg;
	}
	
	//display the status box
	displayMsg(statusBox, msg, bad);
	
	return !bad;
}

function validateEmail(obj) {
	var value = obj.value;
	var statusBox = document.getElementById(obj.name + "Status");
	var bad = false;
	var msg = "";
	
	//make sure it's not empty and is valid
	if (value == "") {
		msg = "You must enter an email.";
		bad = true;
	} else if (!isEmail(value)) {
		msg = "The email you entered is not valid.";
		bad = true;
	} else {
		msg = "You entered a valid email.";
	}
	
	//set the message to be displayed in the status box next to the form element
	if (bad) {
		msg = redX + msg;
	} else {
		msg = greenCheck + msg;
	}
	
	//display the status box
	displayMsg(statusBox, msg, bad);
	
	return !bad;
}

function validateComment(obj) {
	var value = obj.value;
	var statusBox = document.getElementById(obj.name + "Status");
	var bad = false;
	var msg = "";
	
	//make sure it's not empty and is valid
	if (value == "") {
		msg = "You didn't enter a comment.";
		bad = true;
	} else {
		msg = "You entered a valid comment.";
	}
	
	//set the message to be displayed in the status box next to the form element
	if (bad) {
		msg = redX + msg;
	} else {
		msg = greenCheck + msg;
	}
	
	//display the status box
	displayMsg(statusBox, msg, bad);
	
	return !bad;
}

function validateCaptcha(obj) {
	var value = obj.value;
	var statusBox = document.getElementById(obj.name + "Status");
	var bad = false;
	var msg = "";
	
	//make sure it's not empty and is valid
	if (value == "") {
		msg = "You didn't enter the code.";
		bad = true;
	} else if (value.length < 6) {
		msg = "Your code needs to be 6 characters.";
		bad = true;
	} else {
		msg = "Ok, let's hope you're not a robot.";
	}
	
	//set the message to be displayed in the status box next to the form element
	if (bad) {
		msg = redX + msg;
	} else {
		msg = greenCheck + msg;
	}

	//display the status box
	displayMsg(statusBox, msg, bad);
	
	return !bad;
}

function displayMsg(statusBox, msg, bad) {
	if (bad) {
		statusBox.className = "invalid";
	} else {
		statusBox.className = "valid";
	}
	
	statusBox.innerHTML = msg;
}

function isEmail(e) {
   var emailExp = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
   return emailExp.test(e);
}
