// JavaScript Document
// Chris 16 March 2005

// JavaScript Document

function checkBoxes(theForm, field) {
	var t = 0;
	var e = theForm[field];
	var m = e.length;
	for (var idx = 0; idx < m; idx++) {
		if (eval("e[" + idx + "].checked") == true) {
			if ((eval("e[" + idx + "].value") == "OTHER") || (eval("e[" + idx + "].value") == "other")) {
				t = -1000;
			}
			t += 1;
		}
	}
	if (t == 0) {return true;}
	else if (t < 0) {
		return "OTHER selected";
	}	
	else {return false;}
}

function checkCheckBoxValue() {
	var f2;
	var strCheckedValue = "";
	var objCheckBox = document.forms[0].HELPTOPIC;
	
	for (var i = 0; i < objCheckBox.length; i++) {
		if (objCheckBox[i].checked == true) {
			if(objCheckBox[i].value == "other") {
				strCheckedValue = "other";
			}
		}
	}
	if (strCheckedValue == "other") {
		document.getElementById('questionTopicLabel').style.display = "block";
		document.getElementById('questionTopicField').style.display = "block";
	}
}

function checkemail(theForm) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test (theForm.EMAIL.value)) {
		return false;
	}
	return true;
} 

function checkFindOutSelection(theForm) {
	var f2;
	
	f2 = theForm.getAttribute('value');
	if ((f2 =="Publication") || (f2 == "Internet search engine") || (f2 =="Referring website") || (f2 =="Promotional event") || (f2 =="other")) {
		document.getElementById('helpSourceLabel').style.display = "block";
		document.getElementById('helpSourceText').style.display = "block";
	}
	else if ((f2 == "Friend / Colleague") || (f2 == "Select")) {
		document.getElementById('helpSourceLabel').style.display = "none";
		document.getElementById('helpSourceText').style.display = "none";
	}
}

function checkFindOutProfession(objForm) {
	var f2;
	f2 = objForm.getAttribute('value');

	if (f2 =="specialist") {
		document.getElementById('specLabel').style.display = "block";
		document.getElementById('specField').style.display = "block";
		document.getElementById('postgradquallabel').style.display = "block";
		document.getElementById('postgradqualfield').style.display = "block";
		document.getElementById('postgraddeglabel').style.display = "block";
		document.getElementById('postgraddegfield').style.display = "block";
		document.getElementById('postgradmedlabel').style.display = "block";
		document.getElementById('postgradmedfield').style.display = "block";
		document.getElementById('fellowshiplabel').style.display = "block";
		document.getElementById('fellowshipfield').style.display = "block";
	}
	else if ((f2 =="general practitioner") ||  (f2 =="medical researcher/academic")) {
		document.getElementById('specLabel').style.display = "none";
		document.getElementById('specField').style.display = "none";
		document.getElementById('postgradquallabel').style.display = "block";
		document.getElementById('postgradqualfield').style.display = "block";
		document.getElementById('postgraddeglabel').style.display = "block";
		document.getElementById('postgraddegfield').style.display = "block";
		document.getElementById('postgradmedlabel').style.display = "block";
		document.getElementById('postgradmedfield').style.display = "block";
		document.getElementById('fellowshiplabel').style.display = "none";
		document.getElementById('fellowshipfield').style.display = "none";
	}
	else {
		document.getElementById('specLabel').style.display = "none";
		document.getElementById('specField').style.display = "none";
		document.getElementById('postgradquallabel').style.display = "none";
		document.getElementById('postgradqualfield').style.display = "none";
		document.getElementById('postgraddeglabel').style.display = "none";
		document.getElementById('postgraddegfield').style.display = "none";
		document.getElementById('postgradmedlabel').style.display = "none";
		document.getElementById('postgradmedfield').style.display = "none";
		document.getElementById('fellowshiplabel').style.display = "none";
		document.getElementById('fellowshipfield').style.display = "none";
	}
}

function checkPostcode(strPostcode) {
	re = /^\d{4}$/
	
	// Check that the value contains only numbers, one or more times
	if (!re.test(strPostcode)) {
		return false;
	}
	return true;
}
//Cycles through the radio buttons passed to it, and returns the selected value, or false if none have been selected.
function getRadioButtonValue(objRadio) {
	var i = 0;
	var intLength = objRadio.length;

	for(i = 0; i < intLength; i++) {
		 if(objRadio[i].checked) {
		 	return objRadio[i].value;
		 }
	}
	return false;
}
//This function recurses through the parent nodes of the element passed to it, until it finds the parent node matching the tag type
//passed to it.
function getFormName(objElement,strElementType) {
	if (objElement.nodeName == strElementType) {
		return objElement;
	}
	else {
		return getFormName(objElement.parentNode,strElementType);
	}
}
//Takes the radio buttons passed to it, gets the parent forms name and if "other" has been selected, displays or hides the fields
//passed to it.
function displayFieldRadio(objFormField,strDisplayLabel,strDisplayField) {
	var objForm = getFormName(objFormField,"FORM");
	strFormName = objForm.name;
	var objCheckField = document.forms[strFormName].elements[objFormField.name];

	if ((getRadioButtonValue(objCheckField) == "other") || getRadioButtonValue(objCheckField) == "OTHER") {
		document.getElementById(strDisplayLabel).style.display = "block";
		document.forms[strFormName].elements[strDisplayField].style.display = "block";
	}
	else {
		document.getElementById(strDisplayLabel).style.display = "none";
		document.forms[strFormName].elements[strDisplayField].style.display = "none";
	}
}
//Takes the check boxes passed to it, gets the parent forms' name and if "other" has been selected, displays or hides the fields
//passed to it.
function displayFieldCheck(objFormField,strDisplayLabel,strDisplayField) {
	var objForm = getFormName(objFormField,"FORM");

	if (checkBoxes(objForm,objFormField.name) == "OTHER selected") {
		document.getElementById(strDisplayLabel).style.display = "block";
		document.forms[objForm.name].elements[strDisplayField].style.display = "block";
	}
	else {
		document.getElementById(strDisplayLabel).style.display = "none";
		document.forms[objForm.name].elements[strDisplayField].style.display = "none";
	}
}

function unselectNoPostgrad(objFormField,strRadio) {
	var objForm = getFormName(objFormField,"FORM");
	document.forms[objForm.name].elements[strRadio].checked = false;
}

function validateemployhelpform(theForm) {
	var f1 = "HELPTOPIC";
	var f2;
	var selectBox = document.forms[0].FindOut;
	
	if (!getRadioButtonValue(theForm.TITLE)) {
		alert("Please select your title");
		theForm.TITLE[0].focus();
		return false;
	}
	if (theForm.FIRSTNAME.value == "") {
		alert("Please enter your first name");
		theForm.FIRSTNAME.focus();
		return false;
	}
	if (theForm.LASTNAME.value == "") {
		alert("Please enter your last name");
		theForm.LASTNAME.focus();
		return false;
	}
	if (!checkPostcode(theForm.POSTCODE.value)) {
		alert("Please enter a valid postcode.");
		theForm.POSTCODE.focus();
		return false;
	}
	if (checkemail(theForm)) {
		alert("Please enter a valid email address");
		theForm.EMAIL.focus();
		return false;
	}
	if (getRadioButtonValue(theForm.ENQTYPE) == false) {
		alert("Please select your employer type.");
		return false;
	}
	else if (getRadioButtonValue(theForm.ENQTYPE) == "other") {
		if (theForm.ENQOTHER.value == "") {
			alert("Please specify your employer type.");
			theForm.ENQOTHER.focus();
			return false;
		}
	}
	if (checkBoxes(theForm, f1) == "OTHER selected") {
		if (theForm.HELPOTHER.value == "") {
			alert("Please specify what your question is about.");
			theForm.HELPOTHER.focus();
			return false;
		}
	}
	else if (checkBoxes(theForm, f1) == true) {
		alert("Please select one or more checkboxes");
		return false;
	}
	if (theForm.FEEDBACKCOMMENTS.value == "") {
		alert("Please enter your question");
		theForm.FEEDBACKCOMMENTS.focus();
		return false;
	}
	f2 = getRadioButtonValue(theForm.FINDOUT)
	if (!f2) {
		alert("Please select an option describing how you found out about DoctorConnect.")
		return false;
	}
	if ((theForm.SOURCE.value == "") && f2 && (f2 != "Friend / Colleague")) {
		alert("Please select the source from which you found out about DoctorConnect.")
		theForm.SOURCE.focus();
		return false;
	}
}

function validateworkhelpform(theForm) {
	var f2 = "HELPTOPIC";
	var f3;
	var selectBox = document.forms[0].FINDOUT;
	
	if (!getRadioButtonValue(theForm.TITLE)) {
		alert("Please select your title");
		theForm.TITLE[0].focus();
		return false;
	}
	if (theForm.FIRSTNAME.value == "") {
		alert("Please enter your first name");
		theForm.FIRSTNAME.focus();
		return false;
	}
	if (theForm.LASTNAME.value == "") {
		alert("Please enter your last name");
		theForm.LASTNAME.focus();
		return false;
	}
	if (checkemail(theForm)) {
		alert("Please enter a valid email address");
		theForm.EMAIL.focus();
		return false;
	}	
	//Check the optional fields based on the profession that was selected (assuming something was actually selected).
	var strProfession = getRadioButtonValue(theForm.ENQTYPE)
	if (!strProfession) {
		alert("Please select your profession.");
		theForm.ENQTYPE[0].focus();
		return false;
	}//If users select "specialist" they must specify speciality
	else if ((strProfession == "specialist") || (strProfession == "general practioner") || (strProfession == "medical researcher/academic")) {
		if ((strProfession == "specialist") && (document.helpformwork.SPECTYPE.value == "")) {
			alert("Please specify your medical speciality.");
			document.helpformwork.SPECTYPE.focus();
			return false;
		}
		
		var strPostGrad = theForm.NOPOSTGRAD.checked
		if (!strPostGrad && (document.helpformwork.POSTGRAD1.value == "") && (document.helpformwork.POSTGRAD2.value == "")) {
			alert("Please indicate your post-graduate qualifications.");
			return false;
		}
		else if (strPostGrad && ((document.helpformwork.POSTGRAD1.value != "") || (document.helpformwork.POSTGRAD2.value != ""))) {
			alert("Please indicate whether you have no postgraduate qualifications or else provide details of the postgraduate degree and medical school at which you studied.");
			return false;
		}
		else if ((document.helpformwork.POSTGRAD1.value == "") && (document.helpformwork.POSTGRAD2.value != "")) {
			alert("Please enter details of your postgraduate degree.");
			document.helpformwork.POSTGRAD1.focus();
			return false;
		}
		else if ((document.helpformwork.POSTGRAD2.value == "") && (document.helpformwork.POSTGRAD1.value != "")) {
			alert("Please enter details of the medical school at which you studied.");
			document.helpformwork.POSTGRAD2.focus();
			return false;
		}
		
		strPostGrad = getRadioButtonValue(document.helpformwork.FELLOWSHIP);
		if ((strProfession == "specialist") && (strPostGrad == "")) {
			alert("Please indicate whether you have a fellowship.");
			return false;
		}
	}
	
	if (document.helpformwork.WORKHELPMEDQUAL.options[document.helpformwork.WORKHELPMEDQUAL.selectedIndex].value == "default") {
		alert("Please enter the country in which you obtained your primary medical qualification");
		document.helpformwork.WORKHELPMEDQUAL.focus();
		return false;
	}
	if (document.helpformwork.CURRENTCOUNTRYPRACTICE.options[document.helpformwork.CURRENTCOUNTRYPRACTICE.selectedIndex].value == "default") {
		alert("Please enter the country in which you currently practice.");
		document.helpformwork.CURRENTCOUNTRYPRACTICE.focus();
		return false;
	}
	if(checkBoxes(theForm, f2) == true) {
		alert("Please select what your question is about. You can check one or more checkboxes.");
		return false;
	}
	else if (checkBoxes(theForm, f2) == "OTHER selected") {
		if (theForm.HELPOTHER.value == "") {
			alert("Please specify the topic of your question.");
			theForm.HELPOTHER.focus();
			return false;
		}
	}
	if (document.helpformwork.FEEDBACKCOMMENTS.value == "") {
		alert("Please enter your feedback.");
		document.helpformwork.FEEDBACKCOMMENTS.focus();
		return false;
	}
	
	f3 = getRadioButtonValue(theForm.FINDOUT)
	if (!f3) {
		alert("Please select an option describing how you found out about DoctorConnect.")
		return false;
	}
	if ((document.helpformwork.SOURCE.value == "") && f3 && (f3 != "Friend / Colleague")) {
		alert("Please specify the source from which you found out about DoctorConnect.")
		return false;
	}
}

//Validate the general feedback form
function validatefeedback(theForm) {
	if (theForm.Email.value == "") {
		alert("Please enter your email address in the email field.")
		theForm.Email.focus();
		return false;
	}
	else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test (theForm.Email.value))) {
		alert("Please enter a valid email address.")
		theForm.Email.focus();
		return false;
	}
	else if (theForm.feedbackcomments.value == "") {
		alert("Please enter your comments in the comments field.")
		theForm.feedbackcomments.focus();
		return false;
	}
	else {
		return true;
	}
}
