function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   return false;
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
	
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }
 		 return true;				
	}

function CheckRequired(){
	var thisform = document.forms["membership"];
	var reqelements = thisform.elements.required.value.split(",");
	for (element in reqelements){
		formelement = thisform.elements[reqelements[element]];
			if ( (formelement.value==null) || (formelement.value=="") ) {
			alert("Required entry '" + formelement.title + "' missing - please correct & resubmit");
			formelement.focus();
			return false;
		}
	}
}

function ValidateEmail(){

	var thisform = document.forms["membership"];
	var emailID=thisform.elements["email"];
	var emailID2=thisform.elements["emailconf"];
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email address");
		emailID.focus();
		return false;
	}
	if ((emailID2.value==null)||(emailID2.value=="")){
		alert("Please confirm your Email address");
		emailID.focus();
		return false;
	}
	if (emailID.value!=emailID2.value){
		alert("Email addresses do not match");
		emailID2.focus();
		return false;
	}
	if (echeck(emailID.value)==false){
		alert("Email address is not valid");
		emailID.focus();
		return false;
	}
	return true
 }

function ValidateParent(){
	if (CheckRequired()==false){
		return false;
	};
	if (ValidateEmail()==false){
		return false;
	};

	// Get references to elements to be checked
	var thisform = document.forms["membership"];
	var forum=thisform.elements.forum[2];
	var phpBBname=thisform.elements.phpBBname;
	var MedicName=thisform.elements.MedicName;
	var MedicYes=thisform.elements.Medication[0];
	var OtherTherapyChk=thisform.elements.OtherTherapy;
	var TherapyDtl=thisform.elements.TherapyDetails;
	var ssHelpChk=thisform.elements.SSHelp[0];
	var ssHelpDtl=thisform.elements.SSHelpDetails;
	var PDAStatusOther=thisform.elements.PDAStatusOther;
	
	// Find the other checkbox for PDA status
	for (var i = 0; i < thisform.elements.PDAStatus.length; i++) {
		var PDAStat=thisform.elements.PDAStatus[i];
		if (PDAStat.value=="Other") 
			break;
	}

	if ((forum.checked==true)&&((phpBBname.value=="")||(phpBBname.value==null))) {
		alert("Please give your username on the PDA forums");
		phpBBname.focus();
		return false;
	}
	if ((PDAStat.checked==true)&&((PDAStatusOther.value=="")||(PDAStatusOther.value==null))) {
		alert("Please describe the current status for your child");
		PDAStatusOther.focus();
		return false;
	}
	if (MedicName.value==MedicName.defaultValue){
		MedicName.value = "";
	}
	if ((MedicYes.checked==true)&&((MedicName.value=="")||(MedicName.value==null))){
		alert("Please give name of medication");
		MedicName.focus();
		return false;
	}
	if ((OtherTherapyChk.checked==true)&&((TherapyDtl.value=="")||(TherapyDtl.value==null))){
		alert("Please give details of other therapy");
		TherapyDtl.focus();
		return false;
	}
	if ((ssHelpChk.checked==true)&&((ssHelpDtl.value=="")||(ssHelpDtl.value==null))){
		alert("Please give brief indication of help received from Social Services");
		ssHelpDtl.focus();
		return false;
	}
	return true;
}

function ValidateProf(){
	if (CheckRequired()==false){
		return false;
	};
	if (ValidateEmail()==false){
		return false;
	};

	// Get references to elements to be checked
	var thisform = document.forms["membership"];
	var forum=thisform.elements.forum[2];
	var phpBBname=thisform.elements.phpBBname;

	if ((forum.checked==true)&&((phpBBname.value=="")||(phpBBname.value==null))) {
		alert("Please give your username on the PDA forums");
		phpBBname.focus();
		return false;
	}
	return true;
}

