var xmlhttp;
var errmsg = "";

function validate_form(){
	var validate = true;
	errmsg = "";
	errmsg+= "Please specify the following fields:\n";
	
	if(validate_courseinfo() == true){
		validate = false;
	}
	
	if(validate_companyinfo() == true){
		validate = false;
	}
	
	
	
	for(i = 1; i<=counter; i++){
		if(document.getElementById('entry'+i) != null){
			if(validate_participantinfo(i) == true){
				validate = false;
			}
		}else{
			document.ENROLMENT_FORM.IGNORE_ENTRY.value += ","+i;
		}
	}
	
	if(validate == false){
		alert(errmsg);
	}
	return validate;
	
}

function validate_courseinfo(){
	var error = false;
	
	if(document.ENROLMENT_FORM.COURSE_NAME.selectedIndex == 0){
		error = true;
		errmsg+= "- the course you want to enrol in\n";
	}
	
	if(document.ENROLMENT_FORM.COURSE_DATE.value == ""){
		error = true;
		errmsg+= "- the date that for the course\n";
	}
	
	return error;
}



function validate_companyinfo(){
	var error = false;
	
	if(document.ENROLMENT_FORM.COMPANY_NAME.value.length == 0){
		error = true;
		errmsg+= "- company name\n";
	}
	
	if(document.ENROLMENT_FORM.COMPANY_ADDRESS.value.length == 0){
		error = true;
		errmsg+= "- company address\n";
	}
	
	if(document.ENROLMENT_FORM.CONTACT_PERSON.value.length == 0){
		error = true;
		errmsg+= "- company contact person\n";
	}
	
	if(document.ENROLMENT_FORM.CONTACT_PERSON_EMAIL.value.length == 0){
		error = true;
		errmsg+= "- company contact person's email\n";
	}else{
		apos=document.ENROLMENT_FORM.CONTACT_PERSON_EMAIL.value.indexOf("@");
  		dotpos=document.ENROLMENT_FORM.CONTACT_PERSON_EMAIL.value.lastIndexOf(".");
  		if (apos<1||dotpos-apos<2){
			error = true;
			errmsg+= "- company contact person's email invalid\n";
		}
	}
	
	if(document.ENROLMENT_FORM.CONTACT_PERSON_TEL.value.length == 0){
		error = true;
		errmsg+= "- company contact person's contact number\n";
	}
	
	return error;
}




function validate_participantinfo(no){
	var error = false;
	if(eval("document.ENROLMENT_FORM.PARTICIPANT_NAME"+no+".value.length") == 0){
		error = true;
		//errmsg+= "- \n";
	}else{	
	
	if(eval("document.ENROLMENT_FORM.PARTICIPANT_EMAIL"+no+".value.length") == 0){
		error = true;
		//errmsg+= "- company address\n";
	}else{
		papos=eval("document.ENROLMENT_FORM.PARTICIPANT_EMAIL"+no+".value").indexOf("@");
  		pdotpos=eval("document.ENROLMENT_FORM.PARTICIPANT_EMAIL"+no+".value").lastIndexOf(".");
  		if (papos<1||pdotpos-papos<2){
			error = true;
		}
	}
	
	if(eval("document.ENROLMENT_FORM.PARTICIPANT_CITIZENSHIP"+no+".selectedIndex") == 0){
		error = true;
		//errmsg+= "- company address\n";
	}
	
	if(eval("document.ENROLMENT_FORM.PARTICIPANT_NRIC"+no+".value.length") == 0){
		error = true;
		//errmsg+= "- company contact person\n";
	}
	
	if(eval("document.ENROLMENT_FORM.PARTICIPANT_AGE"+no+".selectedIndex") == 0){
		error = true;
		//errmsg+= "- company contact person's email\n";
	}
	
	if(eval("document.ENROLMENT_FORM.PARTICIPANT_DESIGNATION"+no+".selectedIndex") == 0){
		error = true;
		//errmsg+= "- company contact person's contact number\n";
	}
	
	if(eval("document.ENROLMENT_FORM.PARTICIPANT_EDU_QUALIFICATION"+no+".selectedIndex") == 0){
		error = true;
		//errmsg+= "- company contact person's contact number\n";
	}
	
	if(eval("document.ENROLMENT_FORM.PARTICIPANT_SALARY"+no+".selectedIndex") == 0){
		error = true;
		//errmsg+= "- company contact person's contact number\n";
	}
	
	if(eval("document.ENROLMENT_FORM.PARTICIPANT_GENDER"+no+".selectedIndex") == 0){
		error = true;
		//errmsg+= "- company contact person's contact number\n";
	}
	
	if(eval("document.ENROLMENT_FORM.PARTICIPANT_SPECIAL_DIET"+no+".value.length") == 0){
		error = true;
		//errmsg+= "- company contact person's contact number\n";
	}
	
	if(eval("document.ENROLMENT_FORM.PARTICIPANT_TRAINING_DURING_WORKING"+no+".selectedIndex") == 0){
		error = true;
		//errmsg+= "- company contact person's contact number\n";
	}
	}

	if(error ==true){
		errmsg+="- participant, "+eval("document.ENROLMENT_FORM.PARTICIPANT_NAME"+no+".value")+"'s information\n";
	}
	
	return error;
}


