function validateRegForm(theForm) {
var reason = "";
	var num=theForm.numparticipants.value;
	var fieldname="";
	var fieldname1="";
	var fieldname2="";
	theForm.submit.value="Validating Form...";
	//theForm.submit.disabled=true;
	for (i=1;i<=num;i++)
	{
		if(reason != "") {
	    	reason += "\n";
    	}
		fieldname = 'fullname_'+i;
		reason += validateEmpty(theForm[fieldname],64,num,i,"Name is required","Name is too long");
		fieldname = 'gender_'+i;
  		reason += validateGender(theForm[fieldname],num,i);
  		fieldname = 'dateofbirth_day_'+i;
  		fieldname1 = 'dateofbirth_month_'+i;
  		fieldname2 = 'dateofbirth_year_'+i;
  		reason += validateDOB(theForm[fieldname],theForm[fieldname1],theForm[fieldname2],num,i);
  		fieldname = 'address_'+i;
  		reason += validateEmpty(theForm[fieldname],100,num,i,"Address is required","Address is too long");
  		fieldname = 'city_'+i;
  		reason += validateEmpty(theForm[fieldname],64,num,i,"City is required","City is too long");
  		fieldname = 'email_'+i;
  		reason += validateEmail(theForm[fieldname],num,i,"Email is required","Invalid Email");
  		fieldname = 'tel_home_'+i;
  		fieldname1 = 'tel_mobile_'+i;
  		reason += validateTels(theForm[fieldname],theForm[fieldname1],32,num,i,"At least one Telephone # required","Home Tel. # too long","Mobile Tel. # too long");
  		fieldname = 'picture_'+i;
  		reason += validateFile(theForm[fieldname]);
  		fieldname = 'father_name_'+i;
  		reason += validateEmpty(theForm[fieldname],64,num,i,"Father's Name is required","Father's Name is too long");
  		fieldname = 'father_occupation_'+i;
  		reason += validateEmpty(theForm[fieldname],64,num,i,"Father's Occupation is required","Father's Occupation is too long");
  		fieldname = 'father_email_'+i;
  		reason += validateEmail(theForm[fieldname],num,i,"Father's Email is required","Father's Email Invalid");
  		fieldname = 'tel_father_office_'+i;
  		fieldname1 = 'tel_father_mobile_'+i;
  		reason += validateTels(theForm[fieldname],theForm[fieldname1],32,num,i,"Father's Telephone # required","Father's Office Tel. # too long","Father's Mobile Tel. # too long");
  		fieldname = 'mother_name_'+i;
  		reason += validateEmpty(theForm[fieldname],64,num,i,"Mother's Name is required","Mother's Name is too long");
  		fieldname = 'mother_occupation_'+i;
  		reason += validateEmpty(theForm[fieldname],64,num,i,"Mother's Occupation is required","Mother's Occupation is too long");
  		fieldname = 'mother_email_'+i;
  		reason += validateEmail(theForm[fieldname],num,i,"Mother's Email is required","Mother's Email Invalid");
  		fieldname = 'tel_mother_office_'+i;
  		fieldname1 = 'tel_mother_mobile_'+i;
  		reason += validateTels(theForm[fieldname],theForm[fieldname1],32,num,i,"Mother's Telephone # required","Mother's Office Tel. # too long","Mother's Mobile Tel. # too long");
  		fieldname = 'health_'+i;
  		reason += validateEmpty(theForm[fieldname],500,num,i,"Health Information is required","Health Information is too long");
	}
      
  if (reason != "") {
    alert("There were errors in the form:\n\n" + reason);
    theForm.submit.value="Accept Rules and Submit Form";
    //theForm.submit.disabled=false;
    return false;
  }
  theForm.submit.value="Sending Form...";
  return true;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmpty(fld,fldsize,num,i,msg1,msg2) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Coral'; 
        error = msg1;
    } else if (fld.value.length > fldsize){
	    fld.style.background = 'Coral'; 
        error = msg2;
    } else {
        fld.style.background = 'White';
    }
    if(error != "" && num > 1) {
	    error += " for Participant #"+i;
    }
    if(error != "") {
	    error += "\n";
    }
    return error;  
}

function validateGender(fld,num,i)
{
	var error = "";
	
	for (var x=0; x < fld.length; x++)
	{
		if (fld[x].checked)
      	{
      		var fldVal = fld[x].value;
      	}
    }
	if (fldVal != "Male" && fldVal != "Female") {
        error = "Gender is required";
    }
    if(error != "" && num > 1) {
	    error += " for Participant #"+i;
    }
    if(error != "") {
	    error += "\n";
    }
    return error;
}
	
function validateDOB(day,month,year,num,i)
{
	var error = "";
	
	if ((day.value == "0") || (month.value == "0") || (year.value == "0")) {
        error = "Date of Birth required";
    }
	else if ((day.value < 1 || day.value > 31) || (month.value < 1 || month.value > 12) || (year.value < 1900 || year.value > 2009)) {
        error = "Invalid Date";
    }
    if(error != "" && num > 1) {
	    error += " for Participant #"+i;
    }
    if(error != "") {
	    error += "\n";
    }
    return error;
}

function validateTels(fld1,fld2,fldsize,num,i,msg1,msg2,msg3)
{
	var error = "";
	
	if ((fld1.value == "") && (fld2.value == "")) {
		fld1.style.background = 'Coral'; 
		fld2.style.background = 'Coral'; 
        error = msg1;
    } else if (fld1.value.length > fldsize){
	    fld1.style.background = 'Coral'; 
        error = msg2;
    } else if (fld2.value.length > fldsize){
		fld2.style.background = 'Coral'; 
        error = msg3;
    }
    else {
        fld1.style.background = 'White';
        fld2.style.background = 'White';
    }
    if(error != "" && num > 1) {
	    error += " for Participant #"+i;
    }
    if(error != "") {
	    error += "\n";
    }
    return error;
}
	
function validateEmail(fld,num,i,msg1,msg2) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Coral';
        error = msg1;
    } else if (fld.value.toLowerCase() !="n/a" && fld.value.toLowerCase() != "none" && !emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Coral';
        error = msg2;
    } else if (fld.value.toLowerCase() !="n/a" && fld.value.toLowerCase() != "none" && fld.value.match(illegalChars)) {
        fld.style.background = 'Coral';
        error = msg2;
    } else {
        fld.style.background = 'White';
    }
    if(error != "" && num > 1) {
	    error += " for Participant #"+i;
    }
    if(error != "") {
	    error += "\n";
    }
    return error;
}

function validateFile(fld,i,num) 
{
	var error="";
	if (fld.value.length == 0) {
		fld.style.background = 'Coral';
        error="Image File is required";
    }
    else if(!/(\.bmp|\.gif|\.jpg|\.jpeg|\.png|\.zip|\.rar)$/i.test(fld.value)) {
	    fld.style.background = 'Coral';
		error="Invalid image file type";
		fld.value='';
	}
	else {
		fld.style.background = 'White';
	}
	if(error != "" && num > 1) {
	    error += " for Participant #"+i;
    }
    if(error != "") {
	    error += "\n";
    }
	return error;
}

function copyData(i1,i2)
{
	var fieldarray=["father_name_","father_occupation_","father_email_","tel_father_office_","tel_father_mobile_","mother_name_","mother_occupation_","mother_email_","tel_mother_office_","tel_mother_mobile_"];
	var fieldname1="";
	var fieldname2="";
	
	for (i=0;i<fieldarray.length;i++)
	{
		if (document.form2[fieldarray[i]+i1].value.length != 0)
		{
			document.form2[fieldarray[i]+i2].value=document.form2[fieldarray[i]+i1].value;
		}
	}
}

function clearForm()
{
	if(confirm("Are you sure you want to clear the form?"))
	{
		document.form2.reset();
	}
}
