function clickBox(clickedBox,boxGroup,groupMax){

	var i=1;

	while(i<=groupMax){

		var checkbox=document.getElementById(boxGroup+""+i);

		checkbox.checked=false;

		i++;

	}

	clickedBox.checked=true;

}



function buildChoices(boxGroup,groupMax){

	var i=1;

	var choices="";

	var first=true;

	while(i<=groupMax){

		var checkbox=document.getElementById(boxGroup+""+i);

		if(checkbox.checked==true){

			if(first){

				first=false;

			}else{

				choices += ", ";

			}

			choices+=checkbox.value;

			if(checkbox.value=="Other"){

				var optionField=document.getElementById(boxGroup+"other");

				if(optionField==null || optionField.value==""){

					choices+=" (<none specified>)";

				} else {

					choices+=" ("+optionField.value+")";				

				}			

			}

		}

		i++;

	}

	return choices +=".";

}



function checkChecks(boxGroup,groupMax){

	var i=1;

	while(i<=groupMax){

		var checkbox=document.getElementById(boxGroup+""+i);

		if(checkbox.checked==true){

			return true;

		}

		i++;

	}

	return false;

}



function validateFields() {

	// grab the fields in the form and the form itself

	var title = document.getElementById('title');

	var name = document.getElementById('name');

	var surname = document.getElementById('surname');

	var jobtitle = document.getElementById('jobtitle');

	var organisation = document.getElementById('organisation');

	var address = document.getElementById('address');

	var town = document.getElementById('town');

	var county = document.getElementById('county');

	var postcode = document.getElementById('postcode');

	var telephone = document.getElementById('telephone');

	var email = document.getElementById('email');

	var areaother=document.getElementById("areaother");

	var placeother=document.getElementById("placeother");

	var positionother=document.getElementById("positionother");

	var therapyother=document.getElementById("therapyother");

	var nhsother=document.getElementById("nhsother");

	var areacheck=document.getElementById("area20");

	var placecheck=document.getElementById("place9");

	var positioncheck=document.getElementById("position18");

	var therapycheck=document.getElementById("therapy21");

	var nhscheck=document.getElementById("nhs9");



	var whiteSpace = /^[\s]+$/;

	var okToSend = true;

	

	// Check to see if a required field is blank or null. If so, set an error message.

	if ( title.value == '' || whiteSpace.test(title.value) ) {

		handleMessage("Please enter your title.");

		okToSend = false;

	} else if ( name.value == '' || whiteSpace.test(name.value) ) {

		handleMessage("Please enter your name.");

		okToSend = false;

	} else if ( surname.value == '' || whiteSpace.test(surname.value) ) {

		handleMessage("Please enter your surname.");

		okToSend = false;

	} else if ( jobtitle.value == '' || whiteSpace.test(jobtitle.value) ) {

		handleMessage("Please enter your job title.");

		okToSend = false;

	} else if ( organisation.value == '' || whiteSpace.test(organisation.value) ) {

		handleMessage("Please enter your organisation name.");

		okToSend = false;

	} else if ( address.value == '' || whiteSpace.test(address.value) ) {

		handleMessage("Please enter your address.");

		okToSend = false;

	} else if ( town.value == '' || whiteSpace.test(town.value) ) {

		handleMessage("Please enter your town.");

		okToSend = false;

	} else if ( county.value == '' || whiteSpace.test(county.value) ) {

		handleMessage("Please enter your county.");

		okToSend = false;

	} else if ( postcode.value == '' || whiteSpace.test(postcode.value) ) {

		handleMessage("Please enter your postcode.");

		okToSend = false;

	} else if ( telephone.value == '' || whiteSpace.test(telephone.value) ) {

		handleMessage("Please enter your telephone.");

		okToSend = false;

	} else if(!emailCheck(email.value)){

		okToSend = false;

	} else if(!checkChecks('position',18)){

		handleMessage("Please select your position/job title.");

		okToSend = false;	

	} else if(!checkChecks('area',20)){

		handleMessage("Please select at least one responsibilty area.");

		okToSend = false;	

	} else if(!checkChecks('sha',13)){

		handleMessage("Please select at least one SHA you work in.");

		okToSend = false;	

	} else if(!checkChecks('therapy',21)){

		handleMessage("Please select at least one area of work.");

		okToSend = false;	

	} else if(!checkChecks('place',9)){

		handleMessage("Please select your principal place of work.");

		okToSend = false;	

	} else if(areacheck.checked==true && (areaother.value == '' || areaother.value == null)){

		handleMessage("Please tell us about your other area of responsibility.");

		okToSend = false;	

	} else if(therapycheck.checked==true && (therapyother.value == '' || therapyother.value == null)){

		handleMessage("Please tell us about the other therapy area you work in.");

		okToSend = false;	

	} else if(positioncheck.checked==true && (positionother.value == '' || positionother.value == null)){

		handleMessage("Please tell us about your position.");

		okToSend = false;	

	} else if(placecheck.checked==true && (placeother.value == '' || placeother.value == null)){

		handleMessage("Please tell us about your principle place of work.");

		okToSend = false;	

	} else if(nhscheck.checked==true && (nhsother.value == '' || nhsother.value == null)){

		handleMessage("Please tell us about your area of NHS management.");

		okToSend = false;	

	

	}



	// If any of the checks for required information failed, 	

	if(okToSend == true){

		sendEmail();

	}

}



function handleMessage(message){

	alert(message);

}



/**

 * Sends the actual data in the form to the server via an AJAX request.

 * Change this method to extract whatever data you need to be taken from

 * the form and uploaded to the server.

**/

function sendEmail () {

	// grab the fields in the form and the form itself

	var ptitle = document.getElementById('title');

	var pname = document.getElementById('name');

	var psurname = document.getElementById('surname');

	var pjobtitle = document.getElementById('jobtitle');

	var porganisation = document.getElementById('organisation');

	var paddress = document.getElementById('address');

	var ptown = document.getElementById('town');

	var pcounty = document.getElementById('county');

	var ppostcode = document.getElementById('postcode');

	var ptelephone = document.getElementById('telephone');

	var pemail = document.getElementById('email');

	var psha = document.getElementById('sha');

	var pnhs = document.getElementById('nhs');



	// the page on the server that sends the email

	var page = "./sitefiles/contactMailer.asp?contact=true&xml=true";

	showContactTimer(); // quickly begin the load bar

	

	// convert (&, +, =) to string equivs. Needed so URL encoded POST won't choke.

	var title = cleanString(ptitle.value);

	var name = cleanString(pname.value);

	var surname = cleanString(psurname.value);

	var jobtitle = cleanString(pjobtitle.value);

	var organisation = cleanString(porganisation.value);

	var address = cleanString(paddress.value);

	var town = cleanString(ptown.value);

	var county = cleanString(pcounty.value);

	var postcode = cleanString(ppostcode.value);

	var telephone = cleanString(ptelephone.value);

	var email = cleanString(pemail.value);

	var area = cleanString(buildChoices('area', 20));

	var position = cleanString(buildChoices('position', 18));

	var place = cleanString(buildChoices('place', 9));

	var therapy = cleanString(buildChoices('therapy', 21));

	var opt = cleanString(buildChoices('opt', 2));

	var sha = cleanString(buildChoices('sha', 13));

	var nhs = cleanString(buildChoices('nhs', 9));





	// create the data that is to be sent

	var data = "title="+title+

				"&name="+name+

				"&surname="+surname+

				"&jobtitle="+jobtitle+

				"&organisation="+organisation+

				"&address="+address+

				"&town="+town+

				"&county="+county+

				"&postcode="+postcode+

				"&telephone="+telephone+

				"&email="+email+

				"&area="+area+

				"&position="+position+

				"&place="+place+

				"&therapy="+therapy+

				"&sha="+sha+

				"&nhs="+nhs+

				"&opt="+opt;

	

	// put the data into the request to be sent to the server

	loadXMLPosDoc(page,data);

	//alert("Form is filled out correctly.\n\nHowever, the site is not set up to allow program to be executed.");

	//alert(data);

}



/**

 * Cleans a string so that it can go in a string

**/

function cleanString(str){

	str = str.replace(/&/g,"**am**");

	str = str.replace(/=/g,"**eq**");

	str = str.replace(/\+/g,"**pl**");

	str = str.replace(/ /g,"_");

	return str;

}



function showContactTimer () {

	var loader = document.getElementById('feedback');

	loader.innerHTML = "Please wait while we send your email.";

	sentTimer = setTimeout("hideContactTimer()",6000);

}



function hideContactTimer () {

	handleMessage(grabPosXML("confirmation"));

	window.location = 'http://www.clinicalpharmacy.org.uk'

}



function ajaxContact() {

}

addEventForForms(window, 'load',ajaxContact, false);