// $Id: profile_base.js 10667 2011-08-12 11:22:43Z dmf $
// Functions to support validation of join, join_access & profile_base

// Variables to be populated in the HTML: 
// none, but valStrings should come from requiredFields.js

// A fall-back to prevent failure in case the function emailCheck doesn't exist.
if (typeof checkEmail == "undefined") {
	function checkEmail () {
		return true;
	}
}

// Username validation
function checkUsername (formElement) {
	valStrings["username"] = formElement.value;
	if (!formElement.value.match(/^\w{3,20}$/) && formElement.value) {
		alert(valStrings["usernameLength"]); 
		formElement.value = formElement.value.replace(/\W/g, '');
		if (formElement.value.length < 3) {
			formElement.value = '';
		}
		formElement.focus();
		return false;
	} else if (formElement.value.match(/\d/g) && formElement.value.match(/\d/g).length > 4) {
		alert(valStrings["usernameDigits"]); 
		formElement.value = formElement.value.replace(/\d/g, '');
		if (formElement.value.length < 3) {
			formElement.value = '';
		}
		formElement.focus();
		return false;
	} else {
		return true;
	}
}

// Checks that two email address fields match
function checkEmails (checkForm) {
	if (checkForm.email.value == checkForm.email2.value) {
		return true;
	} else {
		alert (valStrings["emailMismatch"]);
		checkForm.email.value = checkForm.email2.value = '';
		checkForm.email.focus();
		return false;
	}
}

// Activation code validation
function checkActivationCode (formElement) {
	if (!formElement.value.match(/^[a-zA-Z]{3}[1-9]{3}$/) && formElement.value) {
		alert(valStrings["badActivationFormat"]); 
		formElement.value = '';
		formElement.focus();
		return false;
	} else {
		return true;
	}
}

// Checks length bounds on a single password
function checkPassword (formElement) {
	if (formElement.value && formElement.value.length < 4) {
		alert(valStrings["passwordLength"]); 
		formElement.value = '';
		formElement.focus();
		return false;
	}
	if (formElement.value && formElement.value.match(/^[0-9]+$/)) {
		alert(valStrings["passwordAllNumeric"]); 
		formElement.focus();
		return false;
	}
	
	if (formElement.form.username.length && formElement.value.toLowerCase() == formElement.form.username.value.toLowerCase()) {
		alert(valStrings["passwordUsername"]); 
		formElement.value = '';
		formElement.focus();
		return false;
	}
	
	// Verify that the password field does NOT contain any part of the username
	if (formElement.value.length && formElement.form.username.value.toLowerCase().match(formElement.value.toLowerCase())) {
		alert(valStrings["passwordUsernameStrMatch"]);
		formElement.value = '';
		formElement.focus();
		return false;
	}
	return true;
}

// Checks that two password fields match
function checkPasswords (checkForm) {
	if (checkForm.password.value == checkForm.password2.value) {
		return true;
	} else {
		alert (valStrings["passwordMismatch"]);
		checkForm.password.value = checkForm.password2.value = '';
		checkForm.password.focus();
		return false;
	}
}

// Checks for default firstName
function checkFirstName (formElement) {
	if (formElement.value == valStrings["defaultFirstName"]) {
		alert ('"' + formElement.value + '" ' + valStrings["err_badFirstName"]);
		formElement.focus();
		return false;
	} else {
		return true;
	}
}

// Verify that the password does NOT contain any part of the first name
function comparePasswordName (formElement, password) {
	if (formElement.value.toLowerCase().match(password.value.toLowerCase()) && formElement.value) {
		alert(valStrings["passwordUsernameStrMatch"]);
		password.value = password.form.password2.value = '';
		password.focus();
		return false;
	}
	
	return true;
}

// Run a password value through our suite of password checks
function doAllPasswordChecks (checkForm) {
	return (checkPassword (checkForm.password)
		&& checkPasswords (checkForm)
		&& comparePasswordName (checkForm.firstName, checkForm.password)
		&& comparePasswordName (checkForm.surname, checkForm.password));
}

// Checks for default surname
function checkSurname (formElement) {
	if (formElement.value == valStrings["defaultSurname"]) {
		alert ('"' + formElement.value + '" ' + valStrings["err_badSurname"]);
		formElement.focus();
		return false;
	} else {
		return true;
	}
}

// Check DOB validity and check for underage users.
function checkBirthDate (formElement) {
	var year = parseFloat(formElement.form.birthDateYear.options[formElement.form.birthDateYear.selectedIndex].text);
	var month = parseFloat(formElement.form.birthDateMonth.options[formElement.form.birthDateMonth.selectedIndex].value);
	var day = parseFloat(formElement.form.birthDateDay.options[formElement.form.birthDateDay.selectedIndex].text);
	var birthDate = new Date (year, (month - 1), day);
	if (day != birthDate.getDate() || (month-1) != birthDate.getMonth() || year != birthDate.getFullYear()) {
		alert(valStrings["invalidBirthDate"]);
		formElement.focus();
		return false;
	}
	var minAge = serverDate.substr(0,4) - parseFloat(formElement.form.birthDateYear.options[formElement.form.birthDateYear.length-1].text);
	var birthMin = new Date (serverDate.substr(0,4) - minAge, serverDate.substr(5,2) - 1, serverDate.substr(8,2));
	if (birthDate.getTime() > birthMin.getTime()) {
		alert(valStrings["tooYoung"]);
		formElement.focus();
		return false;
	}
	return true;
}

// Check that min & max are the right way round and importance is correct
function checkAgeRange (formElement) {
	var mAgeMin = formElement.form.mAgeMin.selectedIndex;
	var mAgeMax = formElement.form.mAgeMax.selectedIndex;
	if (mAgeMax < mAgeMin) {
		formElement.form.mAgeMin.options[mAgeMin].selected = false;
		formElement.form.mAgeMax.options[mAgeMax].selected = false;
		formElement.form.mAgeMin.options[mAgeMax].selected = true;
		formElement.form.mAgeMax.options[mAgeMin].selected = true;
	}
	return true;
}

// Conditions for form submission
function submitJoin (checkForm, relaxed) {
	return (checkRequiredFields(checkForm) 
		&& checkUsername (checkForm.username)
		&& checkEmails (checkForm)
		&& checkEmail (checkForm.email, relaxed)
		&& checkEmail (checkForm.email2, relaxed));
}

function submitAffiliatesJoin (checkForm) {
	return (checkRequiredFields(checkForm) 
		&& checkUsername (checkForm.username)
		&& checkEmail (checkForm.email));
}

function submitJoin_v2 (checkForm, relaxed) {
	var interimResult = checkRequiredFields(checkForm);
	interimResult = interimResult && checkUsername (checkForm.username);
	interimResult = interimResult && checkPassword (checkForm.password);
	interimResult = interimResult && checkPasswords (checkForm);
	
	interimResult = interimResult && comparePasswordName (checkForm.firstName, checkForm.password);
	interimResult = interimResult && comparePasswordName (checkForm.surname, checkForm.password);
	
	if (typeof valStrings["defaultFirstName"] != "undefined") {
		interimResult = interimResult && checkFirstName (checkForm.firstName);
		interimResult = interimResult && checkSurname (checkForm.surname);
	}
	if (checkForm.email2.value == "[suppressed]") {
		interimResult = interimResult && checkEmail (checkForm.email, relaxed);
	} else {
		interimResult = interimResult && checkEmails (checkForm);
		interimResult = interimResult && checkEmail (checkForm.email, relaxed);
		interimResult = interimResult && checkEmail (checkForm.email2, relaxed);
	}
	if (typeof checkForm.birthDateDay != "undefined") {
		interimResult = interimResult && checkBirthDate (checkForm.birthDateDay);
	}
	if (typeof valStrings["invalidPostCodeFormat"] != "undefined" && checkForm.countryID.options[checkForm.countryID.selectedIndex].value == "uk") {
		interimResult = interimResult && validatePostCodeUK(checkForm.postCode, false, true);
	}
	
	return interimResult;
}

function submitJoin_access (checkForm) {
	return (checkRequiredFields(checkForm) 
		&& checkActivationCode (checkForm.activationCode)
		&& doAllPasswordChecks (checkForm));
}

function submitProfile_base (checkForm, relaxed) {
	if (checkForm.password.value || checkForm.password2.value) {
		return (checkRequiredFields(checkForm) 
			&& checkUsername (checkForm.username)
			&& checkEmail (checkForm.email, relaxed)
			&& doAllPasswordChecks (checkForm));
	} else {
		return (checkRequiredFields(checkForm) 
			&& checkUsername (checkForm.username)
			&& checkEmail (checkForm.email, relaxed));
	}
}

function submitProfile_clone (checkForm) {
	return (checkRequiredFields(checkForm)
		&& checkUsername (checkForm.username)
		&& doAllPasswordChecks (checkForm));
}
