function formValidator(){

	/** Referencing to fields **/
	var firstname = document.getElementById('firstname');
	var lastname = document.getElementById('lastname');

	var email = document.getElementById('email');

	var email_confirmation = document.getElementById('email_confirmation');

	var passwords = document.getElementById('passwords');
	var password_confirmation = document.getElementById('password_confirmation');

	var reminder_question = document.getElementById('reminder_question');
	var reminder_answer = document.getElementById('reminder_answer');

	/** Validate form inputs **/
	if(isAlphabet(firstname, "Please enter only letters for your first name")){
		if(isAlphabet(lastname, "Please enter only letters for your last name")){
			if(emailValidator(email, "Please enter a valid email address")){
				if(retypeConfirmation(email, email_confirmation, "Your email and email confirmation did not match")){
					if(lengthRestriction(passwords, 6, 20)){
						if(retypeConfirmation(passwords, password_confirmation,  "Your password and password confirmation did not match")){
							if(madeSelection(reminder_question, "Please choose a reminder question")){
								if(lengthRestriction(reminder_answer, 4, 100)){
									return true;
								}
							}
						}
					}
				}
			}
		}
	}

	return false;
}


/** Validate change password form **/
function changePassValiation(){
	var email = document.getElementById('email');
	var passwords = document.getElementById('passwords');
	var new_passwords = document.getElementById('new_passwords');
	var new_passwords_confirmation = document.getElementById('new_passwords_confirmation');

	/** Validate form inputs **/
	if(emailValidator(email, "Please enter a valid email address")){
		if(lengthRestriction(passwords, 5, 20)){
			if(lengthRestriction(new_passwords, 5, 20)){
				if(retypeConfirmation(new_passwords, new_passwords_confirmation,  "Your password and password confirmation did not match")){
					return true;
				}
			}
		}
	}
	return false;
}

function validateSubAndPosts(){
	var discussion_subject = document.getElementById('discussion');
	var discussion_body = document.getElementById('discussion_body');
	
	/** Validate form inputs **/
	if(notEmpty(discussion_subject, "Please enter subject")){
		if(notEmpty(discussion_body, "Please enter description")){
			return true;
		}
	}
	return false;
}


function loginValidation(){
	var email = document.getElementById('email');
	if(emailValidator(email, "Please enter a valid email address")){
		return true;
	}
	return false;
}

/** Allow only alphabet characters **/
function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	} else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

/** Allow only alphanumeric characters **/
function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	} else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

/** Validate minumum and maximum values **/
function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	} else {
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

/** Validate first entry against re-type entry **/
function retypeConfirmation(elem1, elem2, helperMsg){
	if(elem1.value == elem2.value){
		return true;
	} else {
		alert(helperMsg);
		elem2.focus();
		return false;
	}
}

/** Validate the dropdown value **/
function madeSelection(elem, helperMsg){
	if(elem.value == 1){
		alert(helperMsg);
		elem.focus();
		return false;
	} else {
		return true;
	}
}

/** Validate email address **/
function emailValidator(elem, helperMsg){
	var emailExp = /^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/;
	if(elem.value.match(emailExp)){
		return true;
	} else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}



function HideContent(d) {
	document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
	document.getElementById(d).style.display = "block";
}
function ReverseDisplay(d) {
	if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
	else { document.getElementById(d).style.display = "none"; }
}

function confirmation(x){
	var result = confirm(x);
	
	if (result){
		return true;
	} else {
		return false;
	}
}

function submitForm(){
	document.admin.submit();
}

