// Validate username and password of login form
function validateLogin() {
	if(!validateLoginUsername(document.loginForm.username.value))
		return false;
	if(!validateLoginPassword(document.loginForm.password.value))
		return false;
	document.loginForm.submit();
	return true;
}

// Validate username of login form
function validateLoginUsername(username) {
	// Ensure username has been entered
	if(username == "" || username == null) {
		alert("You must enter a username.");
		return false;
	}
	// Ensure username is of proper length
	if(username.length < 6 || username.length > 15) {
		alert("Username must be between 6 and 15 characters");
		return false;
	}
	// Ensure username contains only letters, numbers and underscores
	var illegalChars = /\W/;
	if((illegalChars.test(username))) {
		alert("Username can only contain letters, numbers and underscores.");
		return false;
	}
	return true;
}

// Validate password of login form
function validateLoginPassword(password) {
	// Ensure username has been entered
	if(password == "" || password == null) {
		alert("You must enter a password.");
		return false;
	}
	// Ensure password is of proper length
	if(password.length < 6 || password.length > 15) {
		alert("Password must be between 6 and 15 characters");
		return false;
	}
	// Ensure password contains only letters, numbers and underscores
	var illegalChars = /\W/;
	if((illegalChars.test(password))) {
		alert("Password can only contain letters, numbers and underscores.");
		return false;
	}
	return true;
}

// Validate fields of post form
function validatePost() {
	if(!validatePostTitle(document.postForm.title.value))
		return false;
	if(!validatePostLocation(document.postForm.location.value))
		return false;
	if(!validatePostCrossroads(document.postForm.crossroads.value))
		return false;
	// Must handle duration here somehow
	if(!validateContact(document.postForm.phone1.value + document.postForm.phone2.value + document.postForm.phone3.value, document.postForm.email1.value, document.postForm.email2.value))
		return false;
	if(!validatePostPhone(document.postForm.phone1.value + document.postForm.phone2.value + document.postForm.phone3.value))
		return false;
	if(!validatePostEmail(document.postForm.email1.value, document.postForm.email2.value))
		return false;
	if(!validatePostDescription(document.postForm.description.value))
		return false;
	document.postForm.submit();
	return true;
}

// Validate title of post form
function validatePostTitle(title) {
	// Ensure title has been entered
	if(title == "" || title == null) {
		alert("You must enter a Wee Post Title.");
		return false;
	}
	// Ensure title contains only letters, numbers, underscores and whitespaces
	// Remove whitespaces for testing
	var titleToCheck = title.replace(/\s/g, "");
	var illegalChars = /\W/;
	if((illegalChars.test(titleToCheck))) {
		alert("Wee Post Title can only contain letters and numbers.");
		return false;
	}
	return true;
}

// Validate location of post form
function validatePostLocation(location) {
	// Ensure location has been selected
	if(location == "" || location == null) {
		alert("You must select a location.");
		return false;
	}
	return true;
}

// Validate title of post form
function validatePostCrossroads(crossroads) {
	// Ensure crossroads have been entered
	if(crossroads == "" || crossroads == null) {
		alert("You must enter the Nearby Crossroads.");
		return false;
	}
	// Ensure crossroads contain only letters, numbers, underscores and whitespaces
	// Remove whitespaces for testing
	var crossroadsToCheck = crossroads.replace(/\s/g, "");
	var illegalChars = /\W/;
	if((illegalChars.test(crossroadsToCheck))) {
		alert("Nearby Crossroads can only contain letters and numbers.");
		return false;
	}
	return true;
}

// Ensure that a valid contact method has been given
function validateContact(phone, email1, email2) {
	var phoneEntered = true;
	var email1Entered = true;
	var email2Entered = true;
	// Check what contact method was entered by the user
	if(phone == "" || phone == null) {
		phoneEntered = false;
	}
	if(email1 == "" || email1 == null) {
		email1Entered = false;
	}
	if(email2 == "" || email2 == null) {
		email2Entered = false;
	}
	// Validate the proper contact method(s)
	if(!phoneEntered && !email1Entered && !email2Entered) {
		alert("You must enter a Contact Phone or Contact Email");
		return false;
	} else if(!phoneEntered && (email1Entered || email2Entered)) {
		return validatePostEmail(email1, email2);
	} else if(phoneEntered && !(email1Entered || email2Entered)) {
		return validatePostPhone(phone);
	} else {
		return validatePostPhone(phone) && validatePostEmail(email1, email2);
	}
}

// Validate phone of post form
function validatePostPhone(phone) {
	// Ensure phone has been entered
	if(phone == "" || phone == null) {
		alert("You must enter a Contact Phone.");
		return false;
	}
	// Ensure phone has length of 10
	if(phone.length != 10) {
		alert("You must enter a 10-digit Contact Phone");
		return false;
	}
	// Ensure phone is a number
	if(isNaN(phone)) {
		alert("You must enter only numeric digits for Contact Phone.");
		return false;
	}
	return true;
}

// Validate email of post form
function validatePostEmail(email1, email2) {
	// Ensure both emails have been entered
	if(email1 == "" || email1 == null) {
		alert("You must enter a Contact Email.");
		return false;
	}
	if(email2 == "" || email2 == null) {
		alert("You must re-enter your Contact Email.");
		return false;
	}
	// Ensure emails are equal
	if(email1 != email2) {
		alert("Your Contact Emails must be the same.");
		return false;
	}
	// Ensure email is of proper format
	apos = email1.indexOf("@");
	dotpos = email1.lastIndexOf(".");
	if(apos < 1 || dotpos - apos < 2) {
		alert("You must enter a valid Contact Email.");
		return false;
	}
	return true;
}

// Validate description of post form
function validatePostDescription(description) {
	// Ensure description has been entered
	if(description == "" || description == null || description == "Include responsibilites, qualification, compensation, etc.") {
		alert("You must enter a Full Description.");
		return false;
	}
	return true;
}