//function to validate the fields FOR Login
function validateLogin() {

	if (trim(frmlogin.name.value) == "") {
		alert("Please select your name to log-in.")
		frmlogin.name.focus();
		return false;
	}

	//ensure the password is not empty
	if (trim(frmlogin.password.value) == "") {
		alert("Please enter your password. Email to admin@camnet.com.sg if you have any problems logging.")
		frmlogin.password.focus();
		return false;
	}

	return true;
} // validateLogin

//function to requestPassword
function requestPassword() {

	if (trim(frmlogin.nric.value) == "") {
		alert("Please enter your NRIC.")
		frmlogin.nric.focus();
		return false;
	}

	if (trim(frmlogin.email.value) == "") {
		alert("Please enter your email address.")
		frmlogin.email.focus();
		return false;
	}


	return true;
} // requestPassword

//function to changePassword
function changePassword(password) {
	var password=password

	if (trim(frmpassword.oldpassword.value) == "") {
		alert("Please enter your old password.")
		frmpassword.oldpassword.focus();
		return false;
	}

	if (trim(frmpassword.oldpassword.value) != trim(password)) {
		alert("You have keyed in an incorrect password. Please key in again.")
		frmpassword.oldpassword.focus();
		return false;
	}

	if (trim(frmpassword.newpassword.value) == "") {
		alert("Please enter your new password.")
		frmpassword.newpassword.focus();
		return false;
	}

	if (trim(frmpassword.confirmpassword.value) == "") {
		alert("Please enter your new password again.")
		frmpassword.confirmpassword.focus();
		return false;
	}

	if ((trim(frmpassword.confirmpassword.value)) != trim(frmpassword.newpassword.value)) {
		alert("The new password and confirmation password are not the same. Please re-enter your passwords again.")
		frmpassword.confirmpassword.focus();
		return false;
	}


	return true;
} // changePassword

//function to emailSupport
function emailSupport() {

	if (trim(frmSupport.name.value) == "") {
		alert("Please enter your full name.")
		frmSupport.name.focus();
		return false;
	}

	if (trim(frmSupport.email.value) == "") {
		alert("Please enter your email address.")
		frmSupport.email.focus();
		return false;
	}

	if (trim(frmSupport.comments.value) == "") {
		alert("Please enter your comments.")
		frmSupport.comments.focus();
		return false;
	}


	return true;
} // emailSupport


//function to validate the fields FOR adding a training course
function validateAddTraining() {

	//ensure the name is not empty
	if (trim(frmAddTraining.trainingTitle.value) == "") {
		alert("Please add the training title.")
		frmAddTraining.trainingTitle.focus();
		return false;
	}

	//STARTING OF DATES VALIDATION
	var sleap = 0;
	sday = frmAddTraining.startDay.value
	smonth = frmAddTraining.startMonth.value
	syear = frmAddTraining.startYear.value	

	/* Validation of other months with day 31 */
	 if ((sday > 30) && ((smonth == 4) || (smonth == 6) || (smonth == 9) || (smonth == 11))) {
		 alert("Please enter a valid start date.")
		 frmAddTraining.startDay.focus();
		 return false;	
	}
			
	/* Validation leap-year / february / day */
	if ((syear % 4 == 0) || (syear % 100 == 0) || (syear % 400 == 0)) {
	  sleap = 1;
	}
	if ((smonth == 2) && (sleap == 1) && (sday > 29)) {
	  alert("Please select a valid start date, February's leap year does not have day "+sday+".");
	  frmAddTraining.startDay.focus();
	  return false;
	}
	if ((smonth == 2) && (sleap != 1) && (sday > 28)) {
		alert("Please select a valid start date, February does not have day "+sday+".");
		frmAddTraining.startDay.focus();
		return false;
	}

	var eleap = 0;
	eday = frmAddTraining.endDay.value
	emonth = frmAddTraining.endMonth.value
	eyear = frmAddTraining.endYear.value	

	if (eday=="0"){
		alert("Please select an end day.");
		frmAddTraining.endDay.focus();
		return false;
	}

	if (emonth=="0"){
		alert("Please select an end month.");
		frmAddTraining.endMonth.focus();
		return false;
	}

	if (eyear=="0"){
		alert("Please select an end year.");
		frmAddTraining.endYear.focus();
		return false;
	}

	/* Validation of other months with day 31 */
	if ((eday > 30) && ((emonth == 4) || (emonth == 6) || (emonth == 9) || (emonth == 11))) {
		 alert("Please enter a valid end date.")
		 frmAddTraining.endDay.focus();
		 return false;	
	}
			
	/* Validation leap-year / february / day */
	if ((eyear % 4 == 0) || (eyear % 100 == 0) || (eyear % 400 == 0)) {
	  eleap = 1;
	}
	if ((emonth == 2) && (eleap == 1) && (eday > 29)) {
	  alert("Please select a valid end date, February's leap year does not have day "+eday+".");
	  frmAddTraining.endDay.focus();
	  return false;
	}
	 if ((emonth == 2) && (eleap != 1) && (eday > 28)) {
		alert("Please select a valid end date, February does not have day "+eday+".");
		frmAddTraining.endDay.focus();
		return false;
	}

	if (syear==eyear && parseInt(smonth)> parseInt(emonth)){
		alert("Please check your dates." )
		frmAddTraining.startMonth.focus();
		return false;
	}		

	if (syear > eyear) {
		alert("Please check your dates." )
		frmAddTraining.startYear.focus();
		return false;
	}		
	
	//ENDING OF DATES VALIDATION

	if (trim(frmAddTraining.trainingVenue.value) == "") {
		alert("Please enter the training venue.")
		frmAddTraining.trainingVenue.focus();
		return false;
	}

	if (trim(frmAddTraining.trainingHours.value) == "") {
		alert("Please enter the number of training hours.")
		frmAddTraining.trainingHours.focus();
		return false;
	}

	if (trim(frmAddTraining.trainingFees.value) == "") {
		alert("Please enter the training fees.")
		frmAddTraining.trainingFees.focus();
		return false;
	}
	if (trim(frmAddTraining.trainerName.value) == "") {
		alert("Please enter the trainer's name.")
		frmAddTraining.trainerName.focus();
		return false;
	}
	
	return true;
}//validateAddTraining

//function to validate the fields FOR adding a leave
function validateAddLeave() {

	//STARTING OF DATES VALIDATION
	var sleap = 0;
	sday = frmAddLeave.startDay.value
	smonth = frmAddLeave.startMonth.value
	syear = frmAddLeave.startYear.value	

	if (sday=="0"){
		alert("Please select a leave starting day.");
		frmAddLeave.startDay.focus();
		return false;
	}

	if (smonth=="0"){
		alert("Please select a leave start month.");
		frmAddLeave.startMonth.focus();
		return false;
	}

	if (syear=="0"){
		alert("Please select a leave start year.");
		frmAddLeave.startYear.focus();
		return false;
	}

	/* Validation of other months with day 31 */
	 if ((sday > 30) && ((smonth == 4) || (smonth == 6) || (smonth == 9) || (smonth == 11))) {
		 alert("Please enter a valid leave start date.")
		 frmAddLeave.startDay.focus();
		 return false;	
	}
			
	/* Validation leap-year / february / day */
	if ((syear % 4 == 0) || (syear % 100 == 0) || (syear % 400 == 0)) {
	  sleap = 1;
	}
	if ((smonth == 2) && (sleap == 1) && (sday > 29)) {
	  alert("Please select a valid leave start date, February's leap year does not have day "+sday+".");
	  frmAddLeave.startDay.focus();
	  return false;
	}
	if ((smonth == 2) && (sleap != 1) && (sday > 28)) {
		alert("Please select a valid leave start date, February does not have day "+sday+".");
		frmAddLeave.startDay.focus();
		return false;
	}

	var eleap = 0;
	eday = frmAddLeave.endDay.value
	emonth = frmAddLeave.endMonth.value
	eyear = frmAddLeave.endYear.value	

	if (eday=="0"){
		alert("Please select a leave end day.");
		frmAddLeave.endDay.focus();
		return false;
	}

	if (emonth=="0"){
		alert("Please select a leave end month.");
		frmAddLeave.endMonth.focus();
		return false;
	}

	if (eyear=="0"){
		alert("Please select a leave end year.");
		frmAddLeave.endYear.focus();
		return false;
	}

	/* Validation of other months with day 31 */
	if ((eday > 30) && ((emonth == 4) || (emonth == 6) || (emonth == 9) || (emonth == 11))) {
		 alert("Please enter a valid leave end date.")
		 frmAddLeave.endDay.focus();
		 return false;	
	}
			
	/* Validation leap-year / february / day */
	if ((eyear % 4 == 0) || (eyear % 100 == 0) || (eyear % 400 == 0)) {
	  eleap = 1;
	}
	if ((emonth == 2) && (eleap == 1) && (eday > 29)) {
	  alert("Please select a valid leave end date, February's leap year does not have day "+eday+".");
	  frmAddLeave.endDay.focus();
	  return false;
	}
	 if ((emonth == 2) && (eleap != 1) && (eday > 28)) {
		alert("Please select a valid leave end date, February does not have day "+eday+".");
		frmAddLeave.endDay.focus();
		return false;
	}

	if (syear==eyear && parseInt(smonth)> parseInt(emonth)){
		alert("Please check your leave dates." )
		frmAddLeave.startMonth.focus();
		return false;
	}		

	if (syear > eyear) {
		alert("Please check your leave dates." )
		frmAddLeave.startYear.focus();
		return false;
	}		
	
	//ENDING OF DATES VALIDATION

	if (trim(frmAddLeave.personTakingOver.value) == "") {
		alert("Please enter the person who will be taking over your duties.")
		frmAddLeave.personTakingOver.focus();
		return false;
	}
	
	return true;
}//validateAddLeave

//function to validate the date fields
function checkdates() {

	//for duration starting date
	var sleap = 0;
	sday = frm.startDay.value
	smonth = frm.startMonth.value
	syear = frm.startYear.value	

	if (frm.startDay.value==""){
		alert("Please select an start day.");
		frm.startDay.focus();
		return false;
	}

	if (frm.startMonth.value==""){
		alert("Please select an start month.");
		frm.startMonth.focus();
		return false;
	}

	if (frm.startYear.value==""){
		alert("Please select an start year.");
		frm.startYear.focus();
		return false;
	}

	/* Validation of other months with day 31 */
	 if ((sday > 30) && ((smonth == 4) || (smonth == 6) || (smonth == 9) || (smonth == 11))) {
		 alert("Please select a valid date")
		 frm.startDay.focus();
		 return false;	
	}
			
	/* Validation leap-year / february / day */
    if ((syear % 4 == 0) || (syear % 100 == 0) || (syear % 400 == 0)) {
	  sleap = 1;
	}
	 if ((smonth == 2) && (sleap == 1) && (sday > 29)) {
	    alert("Please select a valid date, February's leap year does not have day "+sday+".");
	    frm.startDay.focus();
		return false;
	  }
	 if ((smonth == 2) && (sleap != 1) && (sday > 28)) {
			alert("Please select a valid date, February does not have day "+sday+".");
			frm.startDay.focus();
			return false;
	 }

	if (frm.endDay.value==""){
		alert("Please select an end day.");
		frm.endDay.focus();
		return false;
	}

	if (frm.endMonth.value==""){
		alert("Please select an end month.");
		frm.endMonth.focus();
		return false;
	}

	if (frm.endYear.value==""){
		alert("Please select an end year.");
		frm.endYear.focus();
		return false;
	}

	//for duration ending date
	var eleap = 0;
	eday = frm.endDay.value
	emonth = frm.endMonth.value
	eyear = frm.endYear.value	

	/* Validation of other months with day 31 */
	 if ((eday > 30) && ((emonth == 4) || (emonth == 6) || (emonth == 9) || (emonth == 11))) {
		 alert("Please enter a valid posting ending date")
		 frm.endDay.focus();
		 return false;	
	}
			
	/* Validation leap-year / february / day */
	if ((eyear % 4 == 0) || (eyear % 100 == 0) || (eyear % 400 == 0)) {
	  eleap = 1;
    }

	if ((emonth == 2) && (eleap == 1) && (eday > 29)) {
		 alert("Please select a valid posting ending date, February's leap year does not have day "+eday+".");
		 frm.endDay.focus();
    	  return false;
    }

	if ((emonth== 2) && (eleap != 1) && (eday > 28)) {
		alert("Please select a valid posting ending date, February does not have day "+eday+".");
		frm.endDay.focus();
		return false;
    }

	if (frm.startYear.value == frm.endYear.value && parseInt(frm.startMonth.value) > parseInt(emonth) ) {
		alert("Please check your posting dates." )
		frm.startMonth.focus();
		return false;
	}		

	if (frm.startYear.value > frm.endYear.value) {
		alert("Please check your posting dates." )
		frm.startYear.focus();
		return false;
	}	
	
	return true;
	
}

//*********VALIDATE UPLOAD SHARED DOCUMENT**************
function checkshareupload(){
		
	//document title
	if (trim(frmdocument.fileTitle.value)==""){
		alert("Please enter the title of your document.");
		frmdocument.fileTitle.focus();
		return ;
	}

	if (trim(frmdocument.fileDescription.value)==""){
		alert("Please enter a brief description of this document, so that it will prove informative to fellow Staff.");
		frmdocument.fileDescription.focus();
		return ;
	}

	var oRegEx=/\.doc$|\.pdf$|\.rtf$|\.txt$/gi;
	if(!oRegEx.test(frmdocument.filename.value)){
		alert("Please check that your document is one of the following formats and has the correct file extension:\n\n- Microsoft Word Document (*.doc, *.rtf)\n- Adobe Portable Document Format (*.pdf)\n- Rich Text Format (*.rtf)\n- HTML Document (*.htm, *.html)\n- Text Only (*.txt)\n\n");
		frmdocument.filename.focus();
		return ;
	}

	frmdocument.submit();
	
}//function checkshareupload
//*********VALIDATE UPLOAD SHARED DOCUMENT**************


//function to validate the feedback action
function feedbackaction() {

	//for duration starting date
	var sleap = 0;
	sday = frmFeedback.startDay.value
	smonth = frmFeedback.startMonth.value
	syear = frmFeedback.startYear.value	

	if (frmFeedback.startDay.value==""){
		alert("Please select an start day.");
		frmFeedback.startDay.focus();
		return false;
	}

	if (frmFeedback.startMonth.value==""){
		alert("Please select an start month.");
		frmFeedback.startMonth.focus();
		return false;
	}

	if (frmFeedback.startYear.value==""){
		alert("Please select an start year.");
		frmFeedback.startYear.focus();
		return false;
	}

	/* Validation of other months with day 31 */
	 if ((sday > 30) && ((smonth == 4) || (smonth == 6) || (smonth == 9) || (smonth == 11))) {
		 alert("Please select a valid date")
		 frmFeedback.startDay.focus();
		 return false;	
	}
			
	/* Validation leap-year / february / day */
    if ((syear % 4 == 0) || (syear % 100 == 0) || (syear % 400 == 0)) {
	  sleap = 1;
	}
	 if ((smonth == 2) && (sleap == 1) && (sday > 29)) {
	    alert("Please select a valid date, February's leap year does not have day "+sday+".");
	    frmFeedback.startDay.focus();
		return false;
	  }
	 if ((smonth == 2) && (sleap != 1) && (sday > 28)) {
			alert("Please select a valid date, February does not have day "+sday+".");
			frmFeedback.startDay.focus();
			return false;
	 }

	if (trim(frmFeedback.actionTaken.value) == "") {
		alert("Please enter the action taken regarding the feedback.")
		frmFeedback.actionTaken.focus();
		return false;
	}

	return true;
} // feedbackaction



//CHECK LEAVE DROP DOWN BOX
function chkLeave() {

	if (frmSetLeave.annualLeave.value==""){
		alert("Please select the number of Annual Leaves allocated for this staff.");
		frmSetLeave.annualLeave.focus();
		return false;
	}

	if (frmSetLeave.medicalLeave.value==""){
		alert("Please select the number of Medical Leaves allocated for this staff.");
		frmSetLeave.medicalLeave.focus();
		return false;
	}

	return true;
} // chkLeave


//CHECK SALARY ADDITIONS
function validateAddSalary() {

	if (frmAddSalary.grossPay.value==""){
		alert("Please enter the Gross Pay for this staff.");
		frmAddSalary.grossPay.focus();
		return false;
	}

	if (frmAddSalary.grossPay.value != "") {
		if (!chknumber(frmAddSalary.grossPay.value)) {
			alert("Please enter a valid Gross Pay for this staff.")
			frmAddSalary.grossPay.focus();
			return false;
		}
	}

	if (frmAddSalary.employerContribution.value==""){
		alert("Please enter the employer contributed amount to CPF, for this staff.");
		frmAddSalary.employerContribution.focus();
		return false;
	}

	if (frmAddSalary.employerContribution.value != "") {
		if (!chknumber(frmAddSalary.employerContribution.value)) {
			alert("Please enter a valid employer contributed amount to CPF, for this staff.")
			frmAddSalary.employerContribution.focus();
			return false;
		}
	}

	if (frmAddSalary.employeeContribution.value==""){
		alert("Please enter the employee contributed amount to CPF, for this staff.");
		frmAddSalary.employeeContribution.focus();
		return false;
	}

	if (frmAddSalary.employeeContribution.value != "") {
		if (!chknumber(frmAddSalary.employeeContribution.value)) {
			alert("Please enter a valid employee contributed amount to CPF, for this staff.")
			frmAddSalary.employeeContribution.focus();
			return false;
		}
	}

	return true;
} // validateAddSalary

//CHECK SENDING MESSAGES
function validateSendMessage() {

	if (trim(frmSendMessage.messageTitle.value)==""){
		alert("Please enter the title of your message.");
		frmSendMessage.messageTitle.focus();
		return false;
	}

	if (trim(frmSendMessage.message.value)==""){
		alert("Please enter your message.");
		frmSendMessage.message.focus();
		return false;
	}

	var checkflag = "false";
	field=frmSendMessage.recipients

	for (i = 0; i < field.length; i++) {
		 if (field[i].checked == true){
			checkflag="true"
		 }
	}

	if (checkflag == "false") {
		alert("Please select at least one recipient.");
		return false;
	}
	

	return true;
} // validateAddSalary

//*********VALIDATE UPLOAD SHARED DOCUMENT**************