

//  set up listener on page load
window.onload=setObjects;

//  Declare Global Variables
var validRealname, validEmail, validEnquiry, validRecipient, validSubject, validOccupation, validTrust;

function setObjects() {
	//  assign onblur event to function
	document.contact_form.realname.onblur=validate_Realname;
	document.contact_form.Email.onblur=validate_Email;
	document.contact_form.enquiry.onblur=validate_Enquiry;
	document.contact_form.SendToAddress.onblur=validate_Recipient;
	document.contact_form.subject.onblur=validate_Subject;
	document.contact_form.occupation.onblur=validate_Occupation;
	document.contact_form.trust.onblur=validate_Trust;
}

function validate_Recipient (nodeID) {
	// create variable and assign recipient selected
	var str = document.contact_form.SendToAddress.selectedIndex;
	var nodeID = "SendToAddress";
	if (str !== 0) {
		validRecipient = true;
		var oldError = document.getElementById(nodeID);
		//  if value entered is valid check whether error message exists
		if (oldError.childNodes[1]) {
			// if yes, remove error
			remText(oldError);
		} 
	} else {
		validRecipient = false;
		var newText = "Please select recipient";
		addText(nodeID,newText);
	}
}

function validate_Subject (nodeID) {
	// create variable and assign recipient selected
	var str = document.contact_form.subject.selectedIndex;
	var nodeID = "subject";
	if (str !== 0 && str !== undefined) {
		validSubject = true;
		var oldError = document.getElementById(nodeID);
		//  if value entered is valid check whether error message exists
		if (oldError.childNodes[1]) {
			// if yes, remove error
			remText(oldError);
		} 
	} else {
		validSubject = false;
		var newText = "Please select subject";
		addText(nodeID,newText);
	}
}
		
function validate_Occupation(nodeID) {
	// create variable and assign real name entered
	var str = document.contact_form.occupation.value;
	var nodeID = "occupation";
	if (str) {
		validOccupation = true
		var oldError = document.getElementById(nodeID);
		//  if value entered is valid check whether error message exists
		if (oldError.childNodes[1]) {
			// if yes, remove error
			remText(oldError);
		} 
	} else {
		var newText = "Please enter occupation";
		addText(nodeID,newText);
	}
}
		
function validate_Trust(nodeID) {
	// create variable and assign real name entered
	var str = document.contact_form.trust.value;
	var nodeID = "trust";
	if (str) {
		validTrust = true
		var oldError = document.getElementById(nodeID);
		//  if value entered is valid check whether error message exists
		if (oldError.childNodes[1]) {
			// if yes, remove error
			remText(oldError);
		} 
	} else {
		var newText = "Please enter NHS Trust";
		addText(nodeID,newText);
	}
}
		
function validate_Realname(nodeID) {
	// create variable and assign real name entered
	var str = document.contact_form.realname.value;
	var nodeID = "realname";
	if (str) {
		validRealname = true
		var oldError = document.getElementById(nodeID);
		//  if value entered is valid check whether error message exists
		if (oldError.childNodes[1]) {
			// if yes, remove error
			remText(oldError);
		} 
	} else {
		var newText = "Please enter name";
		addText(nodeID,newText);
	}
}

function validate_Email(nodeID) {
	//  create regular expression and assign to variable
	var re = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	// create variable and assign Email entered
	var str = document.contact_form.Email.value;
	var nodeID = "Email";
	if (str !== null) {
		validEmail = (re.test(str));
		switch (validEmail) {
		case true:
			var oldError = document.getElementById(nodeID);
			//  if value entered is valid check whether error message exists
			if (oldError.childNodes[1]) {
				// if yes, remove error
				remText(oldError);
			}
				break;
		
		case false:
			var newText = "Please enter a valid email address";
			addText(nodeID,newText);
			break;
		}
	} else {
		validEmail = false;
	}
}

function validate_Enquiry() {
	//  Create regular expression to test validity
	//var re = /^(?:http)$/;
	//  Create str variable and assign it the enquiry
	var str = document.contact_form.enquiry.value;
	var nodeID = "enquiry";
	if (str) {
		validEnquiry = true;
		var oldError = document.getElementById(nodeID);
		//  if value entered is valid check whether error message exists
		if (oldError.childNodes[1]) {
			// if yes, remove error
			remText(oldError);
		}
	} else {
		validEnquiry = false;
		var newText = "Please enter enquiry";
		addText(nodeID,newText);
	}
}

function addText(nodeID, newText) {
		var oldError = document.getElementById(nodeID);
		//  if value entered is valid check whether error message exists
		if (oldError.childNodes[1]) {
			// if yes, remove error
			remText(oldError);
		}
	// create node and add text
	var node=document.createTextNode(newText);
	//  append text to existing element
	document.getElementById(nodeID).appendChild(node);
}

function remText(oldError) {
	oldError.removeChild(oldError.childNodes[1]);
}

function validate_Form() {
	validate_Realname();
	validate_Email();
	validate_Enquiry();
	validate_Recipient();
	if (validRealname && validEmail && validEnquiry && validRecipient) {
		return true;
	} else {
		return false;
	}
}

function validate_ProForm() {
	validate_Realname();
	validate_Email();
	validate_Enquiry();
	validate_Subject();
	validate_Occupation();
	validate_Trust();
	if (validRealname && validEmail && validEnquiry && validSubject && validOccupation && validTrust) {
		return true;
	} else {
		return false;
	}
}

function validate_DCForm() {
	validate_Realname();
	validate_Email();
	validate_Enquiry();
	validate_Subject();
	if (validRealname && validEmail && validEnquiry && validSubject) {
		return true;
	} else {
		return false;
	}
}