
// ***************INDEX PAGE  - SEARCHING FORMS*******************

//function to validate names search
function validatenamesearch() {

	//ensure a field is entered to search
	if (trim(frmname.firstname.value) == "" && trim(frmname.lastname.value) == "" && frmname.llocation.value == "0") {
		alert("Please enter a first name, last name, or select a location, to search for contacts.")
		frmname.firstname.focus();
		return;
	}

	frmname.submit();

}// validatenamesearch function

//function to validate address search
function validateaddresssearch() {

	//ensure a field is entered to search
	if (trim(frmaddress.building.value) == "" && trim(frmaddress.street.value) == "" && trim(frmaddress.postalcode.value) == "") {
		alert("Please enter a building name, street name, or postal code, to search for contacts.")
		frmaddress.building.focus();
		return;
	}

	frmaddress.submit();
}// validateaddresssearch function

//function to validate number search
function validatenumbersearch() {

	//ensure a field is entered to search
	if (trim(frmnumber.number.value) == "") {
		alert("Please enter a contact number.")
		frmnumber.number.focus();
		return;
	}

	if (trim(frmnumber.number.value) != "") {
		if (!chknumber(frmnumber.number.value)) {
			alert("Please enter a valid contact number.");
			frmnumber.number.focus();
			return;
		}
	}

	frmnumber.submit();
}// validatenumbersearch function

// ***************INDEX PAGE - SEARCHING FORMS*******************

//function to send the index to the next page (TOP )
function go(numberofpages) {
	if (trim(frmgo.pageindex.value)=="") {
		alert("Please enter the page number you wish to view.")
		frmgo.pageindex.focus();
		return;
	}
	
	//if they enter 0 or negative value
	if (frmgo.pageindex.value < 1) {
		alert("Please enter a valid page number you wish to view.")
		frmgo.index.focus();
		return;
	}

	//if they enter a number larger than the number of pages
	numberofpages= parseInt( numberofpages ); 
	page=parseInt(frmgo.pageindex.value);
	if (page > numberofpages) {
		alert("Please enter a valid page number you wish to view.")
		frmgo.pageindex.focus();
		return;
	}

	if (frmgo.pageindex.value != "") {
		if (!chknumber(frmgo.pageindex.value)) {
			alert("Please enter only digits in the page field.");
			frmgo.pageindex.focus();
			return;
		}
	}	
	frmgo.submit();
}//go

//function to send the index to the next page (BOTTOM )
function go1(numberofpages) {
	result=frmgo1.pageindex.value
	if (trim(result)=="") {
		alert("Please enter the page number you wish to view.")
		frmgo1.pageindex.focus();
		return;
	}

	//if they enter 0 or negative value
	if (frmgo1.pageindex.value < 1) {
		alert("Please enter a valid page number you wish to view.")
		frmgo1.pageindex.focus();
		return;
	}

	//if they enter a number larger than the number of pages
	numberofpages= parseInt( numberofpages ); 
	index=parseInt(frmgo1.pageindex.value);
	if (index > numberofpages) {
		alert("Please enter a valid page number you wish to view.")
		frmgo1.pageindex.focus();
		return;
	}

	if (frmgo1.pageindex.value != "") {
		if (!chknumber(frmgo1.pageindex.value)) {
			alert("Please enter only digits in the index field.");
			frmgo1.pageindex.focus();
			return;
		}
	}		
	frmgo1.submit();
}//go1

//function to validate the fields in contactus page
function validatecontact() {
	//Validating the company/school
	if (trim(frmcontact.company.value) == "") {
		alert("Please enter your company/school name.")
		frmcontact.company.focus();
		return false;
	}

	if (trim(frmcontact.tel.value)==""){
		alert("Please enter your contact number.");
		frmcontact.tel.focus();
		return false;
	}

	if (trim(frmcontact.tel.value) != "") {
		if (!chknumber(frmcontact.tel.value)) {
			alert("Please enter a valid contact number.");
			frmcontact.tel.focus();
			return false;
		}
	}

	//Validating the contactperson's email
	if (trim(frmcontact.email.value) == "") {
		alert("Please enter your E-mail address.")
		frmcontact.email.focus();
		return false;
	}

	if (frmcontact.email.value != "") {
		if (!chkmail(frmcontact.email.value)) {
			alert("Please enter a valid E-mail address.");
			frmcontact.email.focus();
			return false;
		}
	}

	//Validating the subjct
	if (trim(frmcontact.subject.value) == "") {
		alert("Please enter your subject.")
		frmcontact.subject.focus();
		return false;
	}

	//Validating the message
	if (trim(frmcontact.enquiry.value) == "") {
		alert("Please enter your enquiry.")
		frmcontact.enquiry.focus();
		return false;
	}

}// validatecontact function

//function to validate the feedback
function feedbackvalidate() {

	//ensure the email field is not empty
	if (trim(frm.email.value) == "") {
		alert("Please enter your email.")
		frm.email.focus();
		return false;
	}

	if (trim(frm.email.value) != "") {
		if (!chkmail(frm.email.value)) {
			alert("Please enter a valid email.");
			frm.email.focus();
			return false;
		}
	}

	if (trim(frm.tel.value) != "") {
		if (!chknumber(frm.tel.value)) {
			alert("Please enter a valid contact number.")
			frm.tel.focus();
			return false;
		}
	}

	//ensure the subject field is not empty
	if (trim(frm.subject.value) == "") {
		alert("Please select a subject header.")
		frm.subject.focus();
		return false;
	}

	//ensure the comments is not empty
	result=trim(frm.comments.value);
	if (result == "") {
		alert("Please enter your feedback.")
		frm.comments.focus();
		return false;
	}
}//feedbackvalidate


//function to validate adding names
function chkAddname() {

	//ensure the usedname is not empty
	if (trim(frmaddname.usedname.value) == "") {
		alert("Please enter the frequently used name.")
		frmaddname.usedname.focus();
		return false;
	}

	//ensure the firstname is not empty
	if (trim(frmaddname.firstname.value) == "") {
		alert("Please enter the first name.")
		frmaddname.firstname.focus();
		return false;
	}

	//ensure the lastname is not empty
	if (trim(frmaddname.lastname.value) == "") {
		alert("Please enter the last name.")
		frmaddname.lastname.focus();
		return false;
	}

	//ensure the streetname is not empty
	if (trim(frmaddname.street.value) == "") {
		alert("Please enter the street address.")
		frmaddname.street.focus();
		return false;
	}

	if (trim(frmaddname.postalcode.value) == "") {
		if (trim(frmaddname.postalcode.value) == "") {
			alert("Please enter the postalcode.")
			frmaddname.postalcode.focus();
			return false;
		}
	}

	if (!chknumber(frmaddname.postalcode.value)) {
		alert("Please enter a valid postalcode.")
		frmaddname.postalcode.focus();
		return false;
	}

	//ensure the location field is not empty
	if (trim(frmaddname.llocation.value) == "0") {
		alert("Please select the nearest location.")
		frmaddname.llocation.focus();
		return false;
	}
		
	if (trim(frmaddname.email.value) != "") {
		if (!chkmail(frmaddname.email.value)) {
			alert("Please enter a valid email.");
			frmaddname.email.focus();
			return false;
		}
	}

	if (trim(frmaddname.tel.value) != "") {
		if (!chknumber(frmaddname.tel.value)) {
			alert("Please enter a valid tel number.")
			frmaddname.tel.focus();
			return false;
		}
	}

	if (trim(frmaddname.mobile.value) != "") {
		if (!chknumber(frmaddname.mobile.value)) {
			alert("Please enter a valid contact mobile number.")
			frmaddname.mobile.focus();
			return false;
		}
	}

	//STARTING DATE OF BIRTH VALIDATION
	var leap = 0;

	var dday = frmaddname.birthDay.value
	var mmonth = frmaddname.birthMonth.value
	var yyear = frmaddname.birthYear.value	
	
	if (dday!="0" && mmonth=="0"){
		alert("Please select your Birth Month.");
		frmaddname.birthMonth.focus();
		return false;
	}
	
	if (dday!="0" && yyear=="0"){
		alert("Please select your Birth Year.");
		frmaddname.birthYear.focus();
		return false;
	}

	if (mmonth!="0" && dday=="0"){
		alert("Please select your Birth Day.");
		frmaddname.birthDay.focus();
		return false;
	}
	
	if (mmonth!="0" && yyear=="0"){
		alert("Please select your Birth Year.");
		frmaddname.birthYear.focus();
		return false;
	}

	if (yyear!="0" && dday=="0"){
		alert("Please select your Birth Day.");
		frmaddname.birthDay.focus();
		return false;
	}
	
	if (yyear!="0" && mmonth=="0"){
		alert("Please select your Birth Month.");
		frmaddname.birthMonth.focus();
		return false;
	}

	/* Validation of other months with day 31 */
	 if ((dday > 30) && ((mmonth == 4) || (mmonth == 6) || (mmonth == 9) || (mmonth == 11))) {
		 alert("Please enter a valid Date of Birth.")
		 frmaddname.birthDay.focus();
		 return false;
	}
		
	/* Validation leap-year / february / day */
	if ((yyear % 4 == 0) || (yyear % 100 == 0) || (yyear % 400 == 0)) {
		 leap = 1;
	 }

	if ((mmonth == 2) && (leap == 1) && (dday > 29)) {
		alert("Please select a valid Date of Birth, February's leap year does not have day "+dday+".");
		frmaddname.birthDay.focus();
		return false;
	 }
	
	 if ((mmonth == 2) && (leap != 1) && (dday > 28)) {
		alert("Please select a valid Date of Birth, February does not have day "+dday+".");
		frmaddname.birthDay.focus();
		return false;
	 }

	//END OF DATE OF BIRTH VALIDATION

}//chkAddname

//function to validate adding names
function chkAddname1() {

	//ensure the usedname is not empty
	if (trim(frmaddname.usedname.value) == "") {
		alert("Please enter your frequently used name, which your contacts identify you with.")
		frmaddname.usedname.focus();
		return false;
	}

	//ensure the firstname is not empty
	if (trim(frmaddname.firstname.value) == "") {
		alert("Please enter your first name.")
		frmaddname.firstname.focus();
		return false;
	}

	//ensure the lastname is not empty
	if (trim(frmaddname.lastname.value) == "") {
		alert("Please enter your last name.")
		frmaddname.lastname.focus();
		return false;
	}

	//ensure the streetname is not empty
	if (trim(frmaddname.street.value) == "") {
		alert("Please enter your street address, e.g Woodlands St 82.")
		frmaddname.street.focus();
		return false;
	}

	if (trim(frmaddname.postalcode.value) == "") {
		if (trim(frmaddname.postalcode.value) == "") {
			alert("Please enter the postalcode.")
			frmaddname.postalcode.focus();
			return false;
		}
	}

	if (!chknumber(frmaddname.postalcode.value)) {
		alert("Please enter a valid postalcode, only digits.")
		frmaddname.postalcode.focus();
		return false;
	}

	//ensure the location field is not empty
	if (trim(frmaddname.llocation.value) == "0") {
		alert("Please select the location to your address.")
		frmaddname.llocation.focus();
		return false;
	}
		
//	if (trim(frmaddname.email.value) != "") {
		if (!chkmail(frmaddname.email.value)) {
			alert("Please enter a valid email.");
			frmaddname.email.focus();
			return false;
		}
//	}

	if (trim(frmaddname.tel.value) != "") {
		if (!chknumber(frmaddname.tel.value)) {
			alert("Please enter a valid tel number, only digits.")
			frmaddname.tel.focus();
			return false;
		}
	}

	if (trim(frmaddname.mobile.value) != "") {
		if (!chknumber(frmaddname.mobile.value)) {
			alert("Please enter a valid contact mobile number, only digits.")
			frmaddname.mobile.focus();
			return false;
		}
	}

	//STARTING DATE OF BIRTH VALIDATION
	var leap = 0;

	var dday = frmaddname.birthDay.value
	var mmonth = frmaddname.birthMonth.value
	var yyear = frmaddname.birthYear.value	
	
	if (dday!="0" && mmonth=="0"){
		alert("Please select your Birth Month.");
		frmaddname.birthMonth.focus();
		return false;
	}
	
	if (dday!="0" && yyear=="0"){
		alert("Please select your Birth Year.");
		frmaddname.birthYear.focus();
		return false;
	}

	if (mmonth!="0" && dday=="0"){
		alert("Please select your Birth Day.");
		frmaddname.birthDay.focus();
		return false;
	}
	
	if (mmonth!="0" && yyear=="0"){
		alert("Please select your Birth Year.");
		frmaddname.birthYear.focus();
		return false;
	}

	if (yyear!="0" && dday=="0"){
		alert("Please select your Birth Day.");
		frmaddname.birthDay.focus();
		return false;
	}
	
	if (yyear!="0" && mmonth=="0"){
		alert("Please select your Birth Month.");
		frmaddname.birthMonth.focus();
		return false;
	}

	/* Validation of other months with day 31 */
	 if ((dday > 30) && ((mmonth == 4) || (mmonth == 6) || (mmonth == 9) || (mmonth == 11))) {
		 alert("Please enter a valid Date of Birth.")
		 frmaddname.birthDay.focus();
		 return false;
	}
		
	/* Validation leap-year / february / day */
	if ((yyear % 4 == 0) || (yyear % 100 == 0) || (yyear % 400 == 0)) {
		 leap = 1;
	 }

	if ((mmonth == 2) && (leap == 1) && (dday > 29)) {
		alert("Please select a valid Date of Birth, February's leap year does not have day "+dday+".");
		frmaddname.birthDay.focus();
		return false;
	 }
	
	 if ((mmonth == 2) && (leap != 1) && (dday > 28)) {
		alert("Please select a valid Date of Birth, February does not have day "+dday+".");
		frmaddname.birthDay.focus();
		return false;
	 }

	 if (frmaddname.agreement.checked == false) {
		alert("Please read the Assertion of Identity and agree to these conditions by checking the box.");
		return false;
	}

	//END OF DATE OF BIRTH VALIDATION

}//chkAddname1


