// JavaScript file to validate library application

function PdcRegistrationa()
{

	// Course Name
	if (document.forms[0].CourseTitle1.selectedIndex < 1) {
		alert("Please select a course name.");
		document.forms[0].CourseTitle1.focus();
		return false;
	}
	// Course Date
	if (document.forms[0].CourseDate1.selectedIndex < 1) {
		alert("Please select course date.");
		document.forms[0].CourseDate1.focus();
		return false;
	}
	
	// Salutation
	if (document.forms[0].strSalutation.selectedIndex==0) {
		alert("Please select your salutation.");
		document.forms[0].strSalutation.focus();
		return false;
	}

	// First Name
	if (document.forms[0].strFirstName.value == '') {
		alert("Please enter your first name.");
		document.forms[0].strFirstName.focus();
		return false;
	}

	var valid_Name = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -"
	var ok_Name = "yes";
	var temp;
	for (var i=0; i<document.forms[0].strFirstName.value.length; i++) {
		temp = "" + document.forms[0].strFirstName.value.substring(i, i+1);
		if (valid_Name.indexOf(temp) == "-1") {
			ok_Name = "no";
		}
	}
	/*if (ok_Name == "no") {
		alert("Your first name must not contain any space, number or special characters.");
		document.forms[0].strFirstName.focus();
		document.forms[0].strFirstName.select();
		return false;
	}*/

	// Last Name
	if (document.forms[0].strLastName.value == '') {
		alert("Please enter your last name.");
		document.forms[0].strLastName.focus();
		return false;
	}
	//Designation
	if (document.forms[0].strDesignation.value == '') {
		alert("Please enter your designation.");
		document.forms[0].strDesignation.focus();
		return false;
	}
	
	// NRIC/Passport No
	if (document.forms[0].strICPassport.value == '') {
		alert("Please enter your NRIC/Passport no.");
		document.forms[0].strICPassport.focus();
		return false;
	}
	
	// Nationality
	var selectBox = document.forms[0].dropNationality;
	user_input = selectBox.options[selectBox.selectedIndex].value; // value
	if (selectBox.selectedIndex == 0) {
		alert("Please select your nationality.");
		document.forms[0].dropNationality.focus();
		return false
	}

	// Birth day
	var selectBox = document.forms[0].dropDay;
	user_input = selectBox.options[selectBox.selectedIndex].value; // value
	if (selectBox.selectedIndex == 0) {
		alert("Please select birth day.");
		document.forms[0].dropDay.focus();
		return false
	}

	// Birth month
	var selectBox = document.forms[0].dropMonth;
	user_input = selectBox.options[selectBox.selectedIndex].value; // value
	if (selectBox.selectedIndex == 0) {
		alert("Please select birth month.");
		document.forms[0].dropMonth.focus();
		return false
	}

	// Birth Year
	if (document.forms[0].strYear.value == '') {
		alert("Please enter your birth year");
		document.forms[0].strYear.focus();
		return false;
	}else if(document.forms[0].strYear.value >= (Date().substring(19,24)-18)) {
		alert("Customer should be atleast 18 years old to register online.");
		document.forms[0].strYear.focus();
		return false;
	}
	
	var valid_No = "0123456789"
	var ok_No = "yes";
	var temp;
	for (var i=0; i<document.forms[0].strYear.value.length; i++) {
		temp = "" + document.forms[0].strYear.value.substring(i, i+1);
		if (valid_No.indexOf(temp) == "-1") {
			ok_No = "no";
		}
	}
	if (ok_No == "no") {
		alert("Your birth year must be in digits.");
		document.forms[0].strYear.focus();
		document.forms[0].strYear.select();
		return false;
	}
			
	// Email
	if (document.forms[0].strEmail.value == '') {
		alert("Please enter your e-mail address.");
		document.forms[0].strEmail.focus();
		return false;
	}
	else { // check valid input
	    if (!emailCheck(document.forms[0].strEmail.value)) {
			alert("Please enter your valid e-mail address.");
			document.forms[0].strEmail.focus();
			return false;
		}
	}
	
	//Office TelNo
	if (document.forms[0].strHomeOfficeTelNo.value == '') {
		alert("Please enter your office telephone.");
		document.forms[0].strHomeOfficeTelNo.focus();
		return false;
	}else if (document.forms[0].strHomeOfficeTelNo.value>69999999 || document.forms[0].strHomeOfficeTelNo.value<59999999) {
		alert("Please enter valid office telephone.");
		document.forms[0].strHomeOfficeTelNo.focus();
		return false;
	}
	//MobileNo
	if (document.forms[0].strMobileNo.value == '') {
		alert("Please enter your mobile telephone.");
		document.forms[0].strMobileNo.focus();
		return false;
	}else if (document.forms[0].strMobileNo.value >100000000 || document.forms[0].strMobileNo.value < 79999999) {
		alert("Please enter valid mobile telephone.");
		document.forms[0].strMobileNo.focus();
		return false;
	}
	var valid_Name = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -"
	var ok_Name = "yes";
	var temp;
	for (var i=0; i<document.forms[0].strLastName.value.length; i++) {
		temp = "" + document.forms[0].strLastName.value.substring(i, i+1);
		if (valid_Name.indexOf(temp) == "-1") {
			ok_Name = "no";
		}
	}
	/*if (ok_Name == "no") {
		alert("Your last name must not contain any space, number or special characters.");
		document.forms[0].strLastName.focus();
		document.forms[0].strLastName.select();
		return false;
	}*/

	
	if(document.forms[0].rdoSponsored[0].checked){	
	
		//Company Name
		if (document.forms[0].strCompanyName.value == '') {
			alert("Please enter your company name.");
			document.forms[0].strCompanyName.focus();
			return false;
		}
		//Company Contact Person
		if (document.forms[0].strCompanyContactPerson.value == '') {
			alert("Please enter your company's contact person address.");
			document.forms[0].strCompanyContactPerson.focus();
			return false;
		}//Company Contact Email
		if (document.forms[0].strCompanyContactEmail.value == '') {
			alert("Please enter your company contact e-mail address.");
			document.forms[0].strCompanyContactEmail.focus();
			return false;
		}else { // check valid input
			if (!emailCheck(document.forms[0].strCompanyContactEmail.value)) {
				alert("Please enter your valid company contact e-mail address.");
				document.forms[0].strCompanyContactEmail.focus();
				return false;
			}
		}
		//Company Contact Phone
		if (document.forms[0].strCompanyContactPhone.value == '') {
			alert("Please enter your company contact telephone number.");
			document.forms[0].strCompanyContactPhone.focus();
			return false;
		}else if (document.forms[0].strCompanyContactPhone.value>69999999 || document.forms[0].strCompanyContactPhone.value<59999999) {
			alert("Please enter your valid company contact telephone number.");
			document.forms[0].strCompanyContactPhone.focus();
			return false;
		}
		//Company Fax
		if (document.forms[0].strCompanyFax.value == '') {
			alert("Please enter your company fax.");
			document.forms[0].strCompanyFax.focus();
			return false;
		}else if (document.forms[0].strCompanyFax.value>69999999 || document.forms[0].strCompanyFax.value<59999999) {
			alert("Please enter your valid company fax.");
			document.forms[0].strCompanyFax.focus();
			return false;
		}
		// Address
		if (document.forms[0].strAddress.value == '') {
			alert("Please enter your company address.");
			document.forms[0].strAddress.focus();
			return false;
		}	
	}	
	
		if (document.forms[0].cbxSearchEngine.checked) 
			if(document.forms[0].strSearchEngineName.value==''){
				alert("Please enter search engine name.");
				//document.forms[0].strSearchEngineName.focus();
				return false;
			}
			
		if (document.forms[0].cbxOthers.checked) 
			if(document.forms[0].strOthersValue.value==''){
				alert("Please enter others entry, under what prompted you to enquire about this course.");
				//document.forms[0].strOthersValue.focus();
				return false;
			}
	// Accept Data Protection condition?
	/*if (!document.forms[0].chkDP.checked) {
		if (ConfirmAcceptDP()) {
		    document.forms[0].chkDP.focus();
		    return false;
                }
	}*/

	// Accept online library terms and conditions?
	if (!document.forms[0].chkAccept.checked) {
		alert("You have not accepted the British Council Registration Policy.")
		document.forms[0].chkAccept.focus();
		return false
	}
	//What prompted you to enquire about this course? 
	if (document.forms[0].strSource.value=="Search engine : ") {
		if (document.forms[0].strSearchEngineName.value=="") {
				
			alert("Please enter the search engine name.")
			document.forms[0].strSearchEngineName.focus();
			return false
		}
	}
	if (document.forms[0].strSource.value=="Other : ") {
		if (document.forms[0].strOthersValue.value=="") {
				
			alert("Please enter the other option, that prompted you to enquire about this course.")
			document.forms[0].strOthersValue.focus();
			return false
		}
	}
	
	if(document.forms[0].strSearchEngineName.value!='')
	document.forms[0].strSearchEngineName.value = document.forms[0].strSearchEngineName.value + '</p>';
	if(document.forms[0].strOthersValue.value!='')
	document.forms[0].strOthersValue.value = document.forms[0].strOthersValue.value + '</p>';
	
	return true;
}

function VerifyOnlineLibraryApplication()
{
	// First Name
	if (document.forms[0].strFirstName.value == '') {
		alert("Please enter your first name.");
		document.forms[0].strFirstName.focus();
		return false;
	}

	var valid_Name = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -"
	var ok_Name = "yes";
	var temp;
	for (var i=0; i<document.forms[0].strFirstName.value.length; i++) {
		temp = "" + document.forms[0].strFirstName.value.substring(i, i+1);
		if (valid_Name.indexOf(temp) == "-1") {
			ok_Name = "no";
		}
	}
	if (ok_Name == "no") {
		alert("Your first name must not contain any space, number or special characters.");
		document.forms[0].strFirstName.focus();
		document.forms[0].strFirstName.select();
		return false;
	}

	// Last Name
	if (document.forms[0].strLastName.value == '') {
		alert("Please enter your last name.");
		document.forms[0].strLastName.focus();
		return false;
	}

	var valid_Name = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -"
	var ok_Name = "yes";
	var temp;
	for (var i=0; i<document.forms[0].strLastName.value.length; i++) {
		temp = "" + document.forms[0].strLastName.value.substring(i, i+1);
		if (valid_Name.indexOf(temp) == "-1") {
			ok_Name = "no";
		}
	}
	if (ok_Name == "no") {
		alert("Your last name must not contain any space, number or special characters.");
		document.forms[0].strLastName.focus();
		document.forms[0].strLastName.select();
		return false;
	}

	// Nationality
	var selectBox = document.forms[0].dropNationality;
	user_input = selectBox.options[selectBox.selectedIndex].value; // value
	if (selectBox.selectedIndex == 0) {
		alert("Please select your nationality.");
		document.forms[0].dropNationality.focus();
		return false
	}
	
	// IC/Passport No
	if (document.forms[0].strICPassport.value == '') {
		alert("Please enter your NRIC/Passport number.");
		document.forms[0].strICPassport.focus();
		return false;
	}
	
	// Birth day
	var selectBox = document.forms[0].dropDay;
	user_input = selectBox.options[selectBox.selectedIndex].value; // value
	if (selectBox.selectedIndex == 0) {
		alert("Please select birth day.");
		document.forms[0].dropDay.focus();
		return false
	}

	// Birth month
	var selectBox = document.forms[0].dropMonth;
	user_input = selectBox.options[selectBox.selectedIndex].value; // value
	if (selectBox.selectedIndex == 0) {
		alert("Please select birth month.");
		document.forms[0].dropMonth.focus();
		return false
	}

	// Birth Year
	if (document.forms[0].strYear.value == '') {
		alert("Please enter your birth year");
		document.forms[0].strYear.focus();
		return false;
	}
	var valid_No = "0123456789"
	var ok_No = "yes";
	var temp;
	for (var i=0; i<document.forms[0].strYear.value.length; i++) {
		temp = "" + document.forms[0].strYear.value.substring(i, i+1);
		if (valid_No.indexOf(temp) == "-1") {
			ok_No = "no";
		}
	}
	if (ok_No == "no") {
		alert("Your birth year must be in digits.");
		document.forms[0].strYear.focus();
		document.forms[0].strYear.select();
		return false;
	}

	// Gender
	var selectBox = document.forms[0].dropGender;
	user_input = selectBox.options[selectBox.selectedIndex].value; // value
	if (selectBox.selectedIndex == 0) {
		alert("Please select your gender.");
		document.forms[0].dropGender.focus();
		return false
	}
	
	// Profession
	var selectBox = document.forms[0].dropProfession;
	user_input = selectBox.options[selectBox.selectedIndex].value; // value
	if (selectBox.selectedIndex == 0) {
		alert("Please select your profession.");
		document.forms[0].dropProfession.focus();
		return false
	}
	
	// Email
	if (document.forms[0].strEmail.value == '') {
		alert("Please enter your e-mail address.");
		document.forms[0].strEmail.focus();
		return false;
	}
	else { // check valid input
	    if (!emailCheck(document.forms[0].strEmail.value)) {
			document.forms[0].strEmail.focus();
			return false;
		}
	}
	
	// Address
	if (document.forms[0].strAddress.value == '') {
		alert("Please enter your address.");
		document.forms[0].strAddress.focus();
		return false;
	}

	// Postcode
	if (document.forms[0].strPostcode.value == '') {
		alert("Please enter your postal code.");
		document.forms[0].strPostcode.focus();
		return false;
	}

	// State
	var selectBox = document.forms[0].dropState;
	user_input = selectBox.options[selectBox.selectedIndex].value; // value
	if (selectBox.selectedIndex == 0) {
		alert("Please select your state.");
		document.forms[0].dropState.focus();
		return false
	}
	
	// Accept online library terms and conditions?
	if (!document.forms[0].chkAccept.checked) {
		alert("You have not accepted the British Council Registration Policy.")
		document.forms[0].chkAccept.focus();
		return false
	}

	// Accept Data Protection condition?
	if (!document.forms[0].chkDP.checked) {
		if (!ConfirmAcceptDP()) {
		    document.forms[0].chkDP.focus();
		    return false;
                }
	}

	return true
}


function ConfirmAcceptDP() {
	var answer = confirm("Please confirm on whether the British Council can contact you on our activities, products, services and events.")
	if (answer){
		return true;
	}
	else{
		return false;
	}
}


function emailCheck(emailStr) 
{
	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


	/* Finally, let's start trying to figure out if the supplied address is valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		//Commented by Vijay alert("Please type in a valid e-mail address as we will be contacting you via e-mail.")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	// See if "user" is valid 
	if (user.match(userPat)==null) {
		// user is not valid
		//Commented by Vijay alert("The e-mail address's username does not seem to be valid.")
		return false
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				//Commented by Vijay alert("Destination IP address is invalid.")
				return false
			}
		}
		return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//Commented by Vijay alert("The domain name in the e-mail address does not seem to be valid.")
		return false
	}

	/* domain name seems valid, but now make sure that it ends in a
	three-letter word (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */

	/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
		// the address must end in a two letter or three letter word.
		//Commented by Vijay alert("The e-mail address must end with a three-letter domain or a two-letter country acronym.")
		return false
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
		var errStr="The e-mail address is missing a hostname."
		//Commented by Vijay alert(errStr)
		return false
	}

	// If we've gotten this far, everything's valid!
	return true;
}


function VerifyContactUs()
{
	// First Name
	if (document.forms[0].strFirstName.value == '') {
		alert("Please enter your first name.");
		document.forms[0].strFirstName.focus();
		return false;
	}

	var valid_Name = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -"
	var ok_Name = "yes";
	var temp;
	for (var i=0; i<document.forms[0].strFirstName.value.length; i++) {
		temp = "" + document.forms[0].strFirstName.value.substring(i, i+1);
		if (valid_Name.indexOf(temp) == "-1") {
			ok_Name = "no";
		}
	}
	if (ok_Name == "no") {
		alert("Your first name must not contain any space, number or special characters.");
		document.forms[0].strFirstName.focus();
		document.forms[0].strFirstName.select();
		return false;
	}

	// Last Name
	if (document.forms[0].strLastName.value == '') {
		alert("Please enter your last name.");
		document.forms[0].strLastName.focus();
		return false;
	}

	var valid_Name = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -"
	var ok_Name = "yes";
	var temp;
	for (var i=0; i<document.forms[0].strLastName.value.length; i++) {
		temp = "" + document.forms[0].strLastName.value.substring(i, i+1);
		if (valid_Name.indexOf(temp) == "-1") {
			ok_Name = "no";
		}
	}
	if (ok_Name == "no") {
		alert("Your last name must not contain any space, number or special characters.");
		document.forms[0].strLastName.focus();
		document.forms[0].strLastName.select();
		return false;
	}

	// Username
	if (document.forms[0].strUsername.value == '') {
		alert("Please enter your username.");
		document.forms[0].strUsername.focus();
		return false;
	}
	
	// Email
	if (document.forms[0].strEmail.value == '') {
		alert("Please enter your e-mail address.");
		document.forms[0].strEmail.focus();
		return false;
	}
	else { // check valid input
	    if (!emailCheck(document.forms[0].strEmail.value)) {
			document.forms[0].strEmail.focus();
			return false;
		}
	}
	return true;
}

function VerifyNeedHelp()
{
	// First Name
	if (document.forms[0].strFirstName.value == '') {
		alert("Please enter your first name.");
		document.forms[0].strFirstName.focus();
		return false;
	}

	var valid_Name = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -"
	var ok_Name = "yes";
	var temp;
	for (var i=0; i<document.forms[0].strFirstName.value.length; i++) {
		temp = "" + document.forms[0].strFirstName.value.substring(i, i+1);
		if (valid_Name.indexOf(temp) == "-1") {
			ok_Name = "no";
		}
	}
	if (ok_Name == "no") {
		alert("Your first name must not contain any space, number or special characters.");
		document.forms[0].strFirstName.focus();
		document.forms[0].strFirstName.select();
		return false;
	}

	// Last Name
	if (document.forms[0].strLastName.value == '') {
		alert("Please enter your last name.");
		document.forms[0].strLastName.focus();
		return false;
	}

	var valid_Name = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -"
	var ok_Name = "yes";
	var temp;
	for (var i=0; i<document.forms[0].strLastName.value.length; i++) {
		temp = "" + document.forms[0].strLastName.value.substring(i, i+1);
		if (valid_Name.indexOf(temp) == "-1") {
			ok_Name = "no";
		}
	}
	if (ok_Name == "no") {
		alert("Your last name must not contain any space, number or special characters.");
		document.forms[0].strLastName.focus();
		document.forms[0].strLastName.select();
		return false;
	}

	// Email
	if (document.forms[0].strEmail.value == '') {
		alert("Please enter your e-mail address.");
		document.forms[0].strEmail.focus();
		return false;
	}
	else { // check valid input
	    if (!emailCheck(document.forms[0].strEmail.value)) {
			document.forms[0].strEmail.focus();
			return false;
		}
	}
	return true;
}

function VerifyTellYourFriends()
{
	// Email
	if (document.forms[0].strSourceEmail.value == '') {
		alert("Please enter your e-mail address.");
		document.forms[0].strSourceEmail.focus();
		return false;
	}
	else { // check valid input
	    if (!emailCheck(document.forms[0].strSourceEmail.value)) {
			document.forms[0].strSourceEmail.focus();
			return false;
		}
	}
	return true;
}
