// JavaScript Document
/**
 * Validates a contact request form. Returns true if validation passes, false if not.
 * If validation fails error fields in the form will be set to display the errors to the user.
 */
function conRequestValidation(form) {
	errorCounter = 0;
	errorDetails = "";
	
	// Validate forename
	if(!validateText(document.getElementById('conreq_Forename').value)) {
		errorCounter++;
		document.getElementById('forenameError').innerHTML = "You have not specified your forename.";
		document.getElementById('forenameError').className = "mini formError";
	} else {
		document.getElementById('forenameError').innerHTML = "";
		document.getElementById('forenameError').className = "mini formError hidden";
	}
	
	// Validate surname
	if(!validateText(document.getElementById('conreq_Surname').value)) {
		errorCounter++;
		document.getElementById('surnameError').innerHTML = "You have not specified your surname.";
		document.getElementById('surnameError').className = "mini formError";
	} else {
		document.getElementById('surnameError').innerHTML = "";
		document.getElementById('surnameError').className = "mini formError hidden";
	}
	
	// Validate email
	if(!validateText(document.getElementById('conreq_Email').value)) {
		errorCounter++;
		document.getElementById('emailError').innerHTML = "You have not specified your email address.";
		document.getElementById('emailError').className = "mini formError";
	} else {
		document.getElementById('emailError').innerHTML = "";
		document.getElementById('emailError').className = "mini formError hidden";
	}
	
	// Validate request type
	if(!(document.getElementById('conreq_ReqTypeInfo').checked || document.getElementById('conreq_ReqTypeContact').checked || document.getElementById('conreq_ReqTypeOther').checked)) {
		errorCounter++;
		document.getElementById('typeError').innerHTML = "You have not specified your request type.";
		document.getElementById('typeError').className = "mini formError";
	} else {
		document.getElementById('typeError').innerHTML = "";
		document.getElementById('typeError').className = "mini formError hidden";
	}
	
	// Validate address
	if(!validateText(document.getElementById('conreq_Address1').value)) {
		errorCounter++;
		document.getElementById('addressError').innerHTML = "You have not specified your address.";
		document.getElementById('addressError').className = "mini formError";
	} else {
		document.getElementById('addressError').innerHTML = "";
		document.getElementById('addressError').className = "mini formError hidden";
	}
	
	// Validate postcode
	if(!validateText(document.getElementById('conreq_Postcode').value)) {
		errorCounter++;
		document.getElementById('postcodeError').innerHTML = "You have not specified your postcode.";
		document.getElementById('postcodeError').className = "mini formError";
	} else {
		document.getElementById('postcodeError').innerHTML = "";
		document.getElementById('postcodeError').className = "mini formError hidden";
	}
	
	// Validate telephone
	if(!validateText(document.getElementById('conreq_Telephone').value)) {
		errorCounter++;
		document.getElementById('telephoneError').innerHTML = "You have not specified your phone number.";
		document.getElementById('telephoneError').className = "mini formError";
	} else {
		document.getElementById('telephoneError').innerHTML = "";
		document.getElementById('telephoneError').className = "mini formError hidden";
	}
	
	// Validate comment
	if(!validateText(document.getElementById('conreq_Comment').value)) {
		errorCounter++;
		document.getElementById('commentError').innerHTML = "You have not specified any details of your request.";
		document.getElementById('commentError').className = "mini formError long";
	} else {
		document.getElementById('commentError').innerHTML = "";
		document.getElementById('commentError').className = "mini formError long hidden";
	}
	
	if(errorCounter > 0) {
		// Display error notice on the form
		document.getElementById('errorMessage').innerHTML = "<p><span class=\"formError\">" + errorCounter + " errors were found in your form submission. Please correct these and try again.</span></p>\n";
		return false;
	}
	else {
		document.getElementById('errorMessage').innerHTML = "";
		return true;
	}
}



/**
 * Validates a publication request form. Returns true if validation passes, false if not.
 * If validation fails error fields in the form will be set to display the errors to the user.
 */
function pubRequestValidation(form) {
	errorCounter = 0;
	errorDetails = "";
	
	// Validate forename
	if(!validateText(document.getElementById('pubreq_Forename').value)) {
		errorCounter++;
		document.getElementById('forenameError').innerHTML = "You have not specified your forename.";
		document.getElementById('forenameError').className = "mini formError";
	} else {
		document.getElementById('forenameError').innerHTML = "";
		document.getElementById('forenameError').className = "mini formError hidden";
	}
	
	// Validate surname
	if(!validateText(document.getElementById('pubreq_Surname').value)) {
		errorCounter++;
		document.getElementById('surnameError').innerHTML = "You have not specified your surname.";
		document.getElementById('surnameError').className = "mini formError";
	} else {
		document.getElementById('surnameError').innerHTML = "";
		document.getElementById('surnameError').className = "mini formError hidden";
	}
	
	// Validate email
	if(!validateText(document.getElementById('pubreq_Email').value)) {
		errorCounter++;
		document.getElementById('emailError').innerHTML = "You have not specified your email address.";
		document.getElementById('emailError').className = "mini formError";
	} else {
		document.getElementById('emailError').innerHTML = "";
		document.getElementById('emailError').className = "mini formError hidden";
	}
	
	// Validate publications
	var publicationOptions = document.getElementById('pubreq_Publications').options;
	var publicationCounter = 0;
	for(i=0; i< publicationOptions.length; i++) {
		if(publicationOptions[i].selected) {
			publicationCounter++;
		}
	}
	if(publicationCounter <= 0) {
		errorCounter++;
		document.getElementById('publicationsError').innerHTML = "You have not selected any publications.";
		document.getElementById('publicationsError').className = "mini formError long";
	} else {
		document.getElementById('publicationsError').innerHTML = "";
		document.getElementById('publicationsError').className = "mini formError long hidden";
	}
	
	// Validate format
	if(!(document.getElementById('pubreq_FormatPDF').checked || document.getElementById('pubreq_FormatHardCopy').checked)) {
		errorCounter++;
		document.getElementById('formatError').innerHTML = "You have not specified a format.";
		document.getElementById('formatError').className = "mini formError";
	} else {
		document.getElementById('formatError').innerHTML = "";
		document.getElementById('formatError').className = "mini formError hidden";
	}
	
	// Validate address
	if(!validateText(document.getElementById('pubreq_Address1').value)) {
		errorCounter++;
		document.getElementById('addressError').innerHTML = "You have not specified your address.";
		document.getElementById('addressError').className = "mini formError";
	} else {
		document.getElementById('addressError').innerHTML = "";
		document.getElementById('addressError').className = "mini formError hidden";
	}
	
	// Validate postcode
	if(!validateText(document.getElementById('pubreq_Postcode').value)) {
		errorCounter++;
		document.getElementById('postcodeError').innerHTML = "You have not specified your postcode.";
		document.getElementById('postcodeError').className = "mini formError";
	} else {
		document.getElementById('postcodeError').innerHTML = "";
		document.getElementById('postcodeError').className = "mini formError hidden";
	}
	
	// Validate telephone
	if(!validateText(document.getElementById('pubreq_Telephone').value)) {
		errorCounter++;
		document.getElementById('telephoneError').innerHTML = "You have not specified your phone number.";
		document.getElementById('telephoneError').className = "mini formError";
	} else {
		document.getElementById('telephoneError').innerHTML = "";
		document.getElementById('telephoneError').className = "mini formError hidden";
	}
	
	if(errorCounter > 0) {
		// Display error notice on the form
		document.getElementById('errorMessage').innerHTML = "<p><span class=\"formError\">" + errorCounter + " errors were found in your form submission. Please correct these and try again.</span></p>\n";
		return false;
	}
	else {
		document.getElementById('errorMessage').innerHTML = "";
		return true;
	}
}


/**
 * Check that the field has been set.
 */
function validateText(field) {
	return (field.length > 0);
}


/**
 * Check that the field is at between 6 and 8 characters long, inclusive.
 */
function validatePostcode(field) {
	return (field.length >= 6 && field.length <= 8);
}


/**
 * Check that the email address contains 3 parts in the format: <local>@<domain>.<tld>
 */
function validateEmail(field) {
	var emailParts = new Array();
	emailParts = field.split("@");
	
	if(emailParts.length == 2) {
		var local = emailParts[0];
		var domain = emailParts[1];
		
		if(local.length <= 0) {
			// local part of the email address is empty
			return false;
		}
		else if(domain.length > 0) {
			// Check that the TLD part of the email address is at least 2 characters long
			var tldLength = domain.length - domain.lastIndexOf(".");
			if(tldLength < 2) {
				return false;
			}
			else {
				return true;
			}
		}
	}
	else {
		return false;
	}
}


/**
 * Check that the phone number is valid, and contains 11 digits.
 */
function validatePhone(field) {
	// Remove the following characters: '(' ')' '\' '/' '-' '+'
	field = field.replace("(", "");
	field = field.replace(")", "");
	field = field.replace("\\", "");
	field = field.replace("/", "");
	field = field.replace("-", "");
	field = field.replace("+", "");
	
	// Check that only numbers remain, and that there are 11 of them.
	return (field.length == 11 && IsNumeric(field));
}


/**
 * IsNumeric validation function, modified from : http://www.codetoad.com/javascript/isnumeric.asp
 */
function IsNumeric(sText) {
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		var Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}
