var phFormat=/^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;

function funcheck(){
	document.frmRegistration.rdHealth[0].checked=false;
	document.frmRegistration.rdHealth[1].checked=false; 
	
	document.frmRegistration.rdAllergies[0].checked=false;
	document.frmRegistration.rdAllergies[1].checked=false; 
	
	document.frmRegistration.rdLilit[0].checked=false;
	document.frmRegistration.rdLilit[1].checked=false; 
	
}

function funRegistration()
{		
	if(document.frmRegistration.drpstudents.options.length=='0'){
		alert("Please enter at least one student's name.");
		document.frmRegistration.txtstname.value='';
		document.frmRegistration.txtstname.focus();
		return false;
	}
	
	var students="";
	for(i=0;i<document.frmRegistration.drpstudents.options.length;++i)
	{
		students+=document.frmRegistration.drpstudents.options[i].value + "@";
	}
	document.frmRegistration.hidStudents.value=students;
		
	if(trim(document.frmRegistration.txtemail.value)==''){
			alert("Please enter a valid email address.");
			document.frmRegistration.txtemail.value='';
			document.frmRegistration.txtemail.focus();
			return false;
		}
	
	if(trim(document.frmRegistration.txtAddress.value)==''){
			alert("Please enter a valid address.");
			document.frmRegistration.txtAddress.value='';
			document.frmRegistration.txtAddress.focus();
			return false;
		}
	
	if(trim(document.frmRegistration.txtCity.value)==''){
			alert("Please enter a valid city.");
			document.frmRegistration.txtCity.value='';
			document.frmRegistration.txtCity.focus();
			return false;
		}
		
	if(trim(document.frmRegistration.txtState.value)==''){
			alert("Please enter a valid state.");
			document.frmRegistration.txtState.value='';
			document.frmRegistration.txtState.focus();
			return false;
		}	
		
	if(trim(document.frmRegistration.txtZip.value)==''){
			alert("Please enter a valid zip code.");
			document.frmRegistration.txtZip.value='';
			document.frmRegistration.txtZip.focus();
			return false;
		}
	if((document.frmRegistration.txtZip.value.length!=5) && checkzip(document.frmRegistration.txtZip.value)==false){
			alert("Please enter a valid zip code.");
			document.frmRegistration.txtZip.value='';
			document.frmRegistration.txtZip.focus();
			return false;
		}
		
	if(trim(document.frmRegistration.txtParentName.value)==''){
			alert("Please enter the name of the parent/guardian.");
			document.frmRegistration.txtParentName.value='';
			document.frmRegistration.txtParentName.focus();
			return false;
		}
		
	if(trim(document.frmRegistration.txtParentContact.value)==''){
			alert("Please enter the phone number of the parent/guardian.");
			document.frmRegistration.txtParentContact.value='';
			document.frmRegistration.txtParentContact.focus();
			return false;
		}	
	if(!phFormat.test(document.frmRegistration.txtParentContact.value) || checkphone(document.frmRegistration.txtParentContact.value)==false){
			alert("Please enter a valid phone number.");
			document.frmRegistration.txtParentContact.value='';
			document.frmRegistration.txtParentContact.focus();
			return false;
		}	
		
	if(trim(document.frmRegistration.txtEmergency.value)==''){
			alert("Please enter an emergency contact.");
			document.frmRegistration.txtEmergency.value='';
			document.frmRegistration.txtEmergency.focus();
			return false;
		}	
		
	if(trim(document.frmRegistration.txtRelation.value)==''){
			alert("Please enter the relationship of the emergency contact to the student(s).");
			document.frmRegistration.txtRelation.value='';
			document.frmRegistration.txtRelation.focus();
			return false;
		}
		
	if(document.frmRegistration.rdHealth[0].checked==false && document.frmRegistration.rdHealth[1].checked==false ){
			alert("Please select an option for health insurance.");
			return false;
		}

	if(document.frmRegistration.rdHealth[0].checked==true){
		if(trim(document.frmRegistration.txtProvider.value)==''){
			alert("Please enter the name of your health insurance provider.");
			document.frmRegistration.txtProvider.value='';
			document.frmRegistration.txtProvider.focus();
			return false;
		}
		if(trim(document.frmRegistration.txtMember.value)==''){
			alert("Please enter your health insurance member number.");
			document.frmRegistration.txtMember.value='';
			document.frmRegistration.txtMember.focus();
			return false;
		}
		if(checkint(document.frmRegistration.txtMember.value)==false){
			alert("Invalid member number.");
			document.frmRegistration.txtMember.value='';
			document.frmRegistration.txtMember.focus();
			return false;
		}
	}
	if(document.frmRegistration.rdAllergies[0].checked==false && document.frmRegistration.rdAllergies[1].checked==false){
		alert("Please select an option for allergies.");
		return false;
	}
	if(document.frmRegistration.rdAllergies[0].checked==true){
		if(trim(document.frmRegistration.txtExplain.value)==''){
			alert("Please explain all allergies.");
			document.frmRegistration.txtExplain.value='';
			document.frmRegistration.txtExplain.focus();
			return false;
		}
	}
	if(document.frmRegistration.rdLilit[0].checked==false && document.frmRegistration.rdLilit[1].checked==false){
		alert("Please select an option for physical limitations.");
		return false;
	}
	if(document.frmRegistration.rdLilit[0].checked==true){
		if(trim(document.frmRegistration.txtExplain2.value)==''){
			alert("Please explain all physical limitations.");
			document.frmRegistration.txtExplain2.value='';
			document.frmRegistration.txtExplain2.focus();
			return false;
		}
	}

	var session=0;
	for(i=0;i<document.frmRegistration.drpSession.options.length;++i) 
	{
		if(document.frmRegistration.drpSession.options[i].selected){
			session++;//=document.frmRegistration.drpSession.options[i].value + ",";
		}
	}
	if(session==0){
		alert("Please select the program you are registering for.");
		return false;
	}else{
			document.frmRegistration.hidSession.value=session;	
	}
	
	

	if(document.frmRegistration.chkAccept.checked==false)
	{
		alert("You must read and accept the Medical Release and Liability Waiver in order to proceed.");
		return false;
	}
	document.frmRegistration.action='registration_submission.php';
	document.frmRegistration.submit();		
}

function funNameEnable(){
	document.getElementById("name").innerHTML="<table border='0' cellpadding='0' cellspacing='8' align='center' width='100%'><tr><td align='right'  width='277' valign='top'><span class='orangetext'>*</span> Name of Provider:</td><td width='325' align='left' valign='top'><input name='txtProvider' type='text' class='textbox' ></td></tr><tr><td align='right' valign='top'><span class='orangetext'>*</span> Member Number:</td><td align='left' valign='top'><input name='txtMember' type='text' class='textbox' /></td></tr></table";	
}
function funNameDisable(){
	document.getElementById("name").innerHTML="";
}

function funAllergyYes(){
	document.getElementById("allergy").innerHTML="<table border='0' cellpadding='0' cellspacing='8' align='center' width='100%'><tr><td  width='277' align='right' valign='top'><span class='orangetext'>*</span> Please explain:</td><td width='325' align='left' valign='top'><textarea name='txtExplain' cols='45' rows='5' class='textarea' ></textarea></td></tr></table>";
}
function funAllergyNo(){
	document.getElementById("allergy").innerHTML="";
}

function funLimitYes(){
	document.getElementById("ph_limit").innerHTML="<table border='0' cellpadding='0' cellspacing='8' align='center' width='100%'><tr><td align='right'  width='277' valign='top'><span class='orangetext'>*</span> Please explain:</td><td width='325' align='left' valign='top'><textarea name='txtExplain2' cols='45' rows='5' class='textarea' ></textarea></td></tr></table>";
}
function funLimitNo(){
	document.getElementById("ph_limit").innerHTML="";
}

function funPopulateStuName1()
{	
	if(trim(document.frmRegistration.txtstname.value)!=''){		

		if(trim(document.frmRegistration.txtage.value)==''){
			alert("Please enter the participant's date of birth.");
			document.frmRegistration.txtage.value='';
			document.frmRegistration.txtage.focus();
			return false;
		}

		var sellang2len=document.frmRegistration.drpstudents.options.length;
		var sellang2ind=sellang2len;
		var temp=1;
		for(p=0;p<sellang2len;++p)
		{
			if(document.frmRegistration.drpstudents.options[p].value==document.frmRegistration.txtstname.value)
			{
				temp=0;
			}
		}
		if(temp==1)
		{
			var st_val=document.frmRegistration.txtstname.value+","+document.frmRegistration.txtage.value;
			document.frmRegistration.drpstudents.options[sellang2ind]=new Option(st_val,st_val);
			sellang2ind++;
		}
		document.frmRegistration.txtstname.value='';
		document.frmRegistration.txtage.value='';
	}
}

function funRemoveSelect1(){
	var sellang2len=document.frmRegistration.drpstudents.options.length;

	for(i=0;i<document.frmRegistration.drpstudents.options.length;)
	{
		if(document.frmRegistration.drpstudents.options[i].selected)
		{	
			document.frmRegistration.drpstudents.options[i]=null;
			sellang2len--;
		}
		else
		++i;
	}
}

function funZipFormat(){
	if(document.frmRegistration.txtZip.value.length==6)	{
		val=document.frmRegistration.txtZip.value;
		if(val.indexOf('-')!=5){
			num=val.slice(0,5)+"-"+val.slice(5,6);
			document.frmRegistration.txtZip.value=num;	
		}
	}
}

function funAgeFormat(){
	if(document.frmRegistration.txtage.value.length==3){
		val=document.frmRegistration.txtage.value;
		if(val.indexOf('/')!=2){
			num=val.slice(0,2)+"/"+val.slice(2,3);
			document.frmRegistration.txtage.value=num;
		}
	}
	if(document.frmRegistration.txtage.value.length==6)	{
		val=document.frmRegistration.txtage.value;
		pos=val.indexOf('/');
		if(val.indexOf('/',pos+1)!=5){
			num=val.slice(0,2)+"/"+val.slice(3,5)+"/"+val.slice(5,6);
			document.frmRegistration.txtage.value=num;	
		}
	}
}

function funPhoneFormat(){
	if(document.frmRegistration.txtParentContact.value.length==4){
		val=document.frmRegistration.txtParentContact.value;
		if(val.indexOf('-')!=3){
			num=val.slice(0,3)+"-"+val.slice(3,4);
			document.frmRegistration.txtParentContact.value=num;
		}
	}
	if(document.frmRegistration.txtParentContact.value.length==8)	{
		val=document.frmRegistration.txtParentContact.value;
		pos=val.indexOf('-');
		if(val.indexOf('-',pos+1)!=7){
			num=val.slice(0,3)+"-"+val.slice(4,7)+"-"+val.slice(7,8);
			document.frmRegistration.txtParentContact.value=num;	
		}
	}
}

function funEMPhoneFormat(){
	if(document.frmRegistration.txtEmergencyContact.value.length==4){
		val=document.frmRegistration.txtEmergencyContact.value;
		if(val.indexOf('-')!=3){
			num=val.slice(0,3)+"-"+val.slice(3,4);
			document.frmRegistration.txtEmergencyContact.value=num;
		}
	}
	if(document.frmRegistration.txtEmergencyContact.value.length==8)	{
		val=document.frmRegistration.txtEmergencyContact.value;
		pos=val.indexOf('-');
		if(val.indexOf('-',pos+1)!=7){
			num=val.slice(0,3)+"-"+val.slice(4,7)+"-"+val.slice(7,8);
			document.frmRegistration.txtEmergencyContact.value=num;	
		}
	}
}

function checkzip(val){
	var exps=/(^\d{5}-\d{4}$)/;
	if(!exps.test(val))
	{
		return false;
	}
	else
	{
		return true;
	}	
}