function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* 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. */

alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Ths username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

alert("The username doesn't 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) {
alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}



function isNumber(data) {
    var numStr = "0123456789";
    var thisChar;
    var counter = 0;

    for (var i=0; i<data.length; i++) {
        thisChar = data.substring(i, i+1);
        if (numStr.indexOf(thisChar) != -1) counter ++;
    } 
	//for

    if (counter == data.length) 
          return(true); 
    else 
	  return(false); 
}

function validNric(nric) {
	var cntDigit = 0;
	var chkTotal = 0;
	var floorValue = 0;
	var tmpRemain = 0;
	var tmpCheckDigit;
	var cntLoop = 0;
	var divValue = 0;

	var arrNric = new Array();

	for (cntLoop=0; cntLoop<=8; cntLoop++) {
		arrNric[cntLoop] = nric.charAt(cntLoop);
	}
//	alert("arrNric[0] is " + arrNric[0]);
	
//	Make sure that the nric consist of 7-digits from position 2 to 8
	for (cntLoop=1; cntLoop<=7; cntLoop++) {
		if (arrNric[cntLoop] >= 0 && arrNric[cntLoop] <= 9)
		{	
			//do nothing
			cntDigit ++;			
		}
                else
                {
                        alert("Character at position " + cntLoop + " is not numeric.");
			document.frmElearn.student_nric.focus();
			return false;
                }
	}

//	Calculate the total depending on position of the digit

	chkTotal = 0;
	chkTotal = chkTotal + (parseInt(arrNric[1]) * 2);
	chkTotal = chkTotal + (parseInt(arrNric[2]) * 7);
	chkTotal = chkTotal + (parseInt(arrNric[3]) * 6);
	chkTotal = chkTotal + (parseInt(arrNric[4]) * 5);			
	chkTotal = chkTotal + (parseInt(arrNric[5]) * 4);
	chkTotal = chkTotal + (parseInt(arrNric[6]) * 3);
	chkTotal = chkTotal + (parseInt(arrNric[7]) * 2);

//	Calculate the remainder and add 1 to the remainder
	divValue = chkTotal / 11;
	floorValue = Math.floor(divValue);
	tmpRemain = chkTotal - (floorValue * 11);
	tmpRemain = tmpRemain + 1;

//	Obtain the check digit based on the table
	if (tmpRemain == 1) {
		tmpCheckDigit = 'J';
	}
	else if (tmpRemain == 2) {
		tmpCheckDigit = 'Z';
	}
	else if (tmpRemain == 3) {
		tmpCheckDigit = 'I';
	}
	else if (tmpRemain == 4) {
		tmpCheckDigit = 'H';
	}
	else if (tmpRemain == 5) {
		tmpCheckDigit = 'G';
	}
	else if (tmpRemain == 6) {
		tmpCheckDigit = 'F';
	}
	else if (tmpRemain == 7) {
		tmpCheckDigit = 'E';
	}
	else if (tmpRemain == 8) {
		tmpCheckDigit = 'D';
	}
	else if (tmpRemain == 9) {
		tmpCheckDigit = 'C';
	}
	else if (tmpRemain == 10) {
		tmpCheckDigit = 'B';
	}
	else if (tmpRemain == 11) {
		tmpCheckDigit = 'A';
	}
		

//	compare user entered check digit with validation generated check digit
	if (arrNric[8] == tmpCheckDigit)
		return true;
	else
		return false;
}

function checkEntry() {

    var alphaStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";   
    var postcode = document.frmElearn.postal_code.value;
    var emailAdd = document.frmElearn.student_email.value;
//to set to uppercase first 19/06/2002    var NricNo   = document.frmElearn.student_nric.value;
    var NricNo   =  document.frmElearn.student_nric.value.toUpperCase();
    var invalid = " "; // Invalid character is a space (18/11/2002)

    if (document.frmElearn.student_nric.value == null ||
        document.frmElearn.student_nric.value.length < 9) 
    {
       alert("Please provide your NRIC no. in the format S1234567A.");
       document.frmElearn.student_nric.focus();
       return false;
    }
    else if ((document.frmElearn.student_nric.value.length == 9) &&
	     (document.frmElearn.student_nric.value.charAt(0).toUpperCase() != "S") &&
	     (document.frmElearn.student_nric.value.charAt(0).toUpperCase() != "T"))
    {
          alert("First character of NRIC no. should be a 'S' or 'T'.");
          document.frmElearn.student_nric.focus();
          return false;
    }   
    else if ((document.frmElearn.student_nric.value.length == 9) &&
	     (alphaStr.indexOf(document.frmElearn.student_nric.value.charAt(8).toUpperCase()) == -1))	
    {	
          alert("Nric check digit should be an alphabetic character.");
          document.frmElearn.student_nric.focus();
          return false;
    }


//**29/04/2002 - validate on the check digit in the nric
    else if (!validNric(NricNo))
    {
          alert("Please enter a valid NRIC no.");
          document.frmElearn.student_nric.focus();
          return false;
    }
    else if (document.frmElearn.student_name.value == null ||
             document.frmElearn.student_name.value == "") 
    {
       alert("Please enter your name.");
       document.frmElearn.student_name.focus();
       return false;
    }
	else if (document.frmElearn.selectRace.value == "0") 
    {
       alert("Please select your Race.");
       document.frmElearn.selectRace.focus();
       return false;
    }
    else if (!document.frmElearn.student_sex[0].checked &&
             !document.frmElearn.student_sex[1].checked) 
    {
       alert("Please select your gender(Male/Female).");
       document.frmElearn.student_sex[0].focus();
       return false;
    }
    else if (document.frmElearn.dob_day.value == null ||
             document.frmElearn.dob_day.value == "0")
    {
       alert("Please complete entry of your birth-date.");
       document.frmElearn.dob_day.focus();
       return false;
    }
    else if (document.frmElearn.dob_month.value == null ||
             document.frmElearn.dob_month.value == "0")
    {
       alert("Please complete entry of your birth-date.");
       document.frmElearn.dob_month.focus();
       return false;
    }
    else if (document.frmElearn.dob_year.value == null ||
  	     document.frmElearn.dob_year.value.length < 4)
    {
       alert("Please enter your year of birth in the format YYYY e.g. 1963.");
       document.frmElearn.dob_year.focus();
       return false;
    }
    else if (!isNumber(document.frmElearn.dob_year.value))
    {
       alert("Year of birth should be numeric.");	
       document.frmElearn.dob_year.focus();
       return false;		
    }
    else if (document.frmElearn.student_email.value == null ||
             document.frmElearn.student_email.value == "") 
    {
       alert("Please enter EMAIL account. If you do not have an email account, you can get it for FREE at http://www.hotmail.com.");
       document.frmElearn.student_email.focus();
       return false;
    }
    else if (!emailCheck(emailAdd))
    {
       //alert("Please enter a valid email address.");	
       document.frmElearn.student_email.focus();
       return false;		
    }  
	//alert("block/house no. = " && document.frmElearn.block_no.value);
    else if (document.frmElearn.block_no.value == null ||
             document.frmElearn.block_no.value == "" ||
			 document.frmElearn.block_no.value.length <= 0) 
    {
       alert("Please enter your block/house no.");
       document.frmElearn.block_no.focus();
       return false;
    }
    else if (document.frmElearn.street_name.value == null ||
             document.frmElearn.street_name.value == "" ||
			 document.frmElearn.street_name.value.length <= 0) 
    {
       alert("Please enter name of street. Eg. Hougang St. 51, Gerald Drive.");
       document.frmElearn.street_name.focus();
       return false;
    }
//**06/04/2002 postal_code mandatory requested by Training Joanne
    else if (document.frmElearn.postal_code.value == null ||
             document.frmElearn.postal_code.value == "" ||
			 document.frmElearn.postal_code.value.length <= 0) 
    {
       alert("Please enter postal code.");
       document.frmElearn.postal_code.focus();
       return false;
    }
    else if ((document.frmElearn.postal_code.value.length >= 4) &&
	     !isNumber(postcode))
    {
       alert("Postal code should contain numbers only.");
       document.frmElearn.postal_code.focus();
       return false;
    }
//**25/04/2002 force use the 6-digit postal code
    else if (document.frmElearn.postal_code.value.length < 6) 
    {
       alert("Please enter 6-digits postal code.");
       document.frmElearn.postal_code.focus();
       return false;
    }
    else if  ((document.frmElearn.house_telephone.value == null ||
              document.frmElearn.house_telephone.value == "") &&
             (document.frmElearn.office_telephone.value == null ||
              document.frmElearn.office_telephone.value == "") &&
             (document.frmElearn.handphone.value == null ||
              document.frmElearn.handphone.value == ""))
    {
       alert("Please provide at least 1 way to contact you. Thank you.");
       document.frmElearn.house_telephone.focus();
       return false;
    }
//**06/04/2002 lta_reference_no, lta_approved_date, preferred_pwd mandatory requested by Training Joanne
    else if (document.frmElearn.lta_reference_no.value == null ||
             document.frmElearn.lta_reference_no.value == "") 
    {
       alert("Please enter the LTA Ref. No.");
       document.frmElearn.lta_reference_no.focus();
       return false;
    }
    else if (document.frmElearn.lta_day.value == null ||
             document.frmElearn.lta_day.value == "0")
    {
       alert("Please complete entry of LTA Approved Date.");
       document.frmElearn.lta_day.focus();
       return false;
    }
    else if (document.frmElearn.lta_month.value == null ||
             document.frmElearn.lta_month.value == "0")
    {
       alert("Please complete entry of LTA Approved Date.");
       document.frmElearn.lta_month.focus();
       return false;
    }
    else if (document.frmElearn.lta_year.value == null ||
	     document.frmElearn.lta_year.value.length < 4)
    {
       alert("Please enter year of LTA Approved Date. Year should be in the format YYYY e.g. 2001.");
       document.frmElearn.lta_year.focus();
       return false;
    }
    else if (!isNumber(document.frmElearn.lta_year.value))
    {
       alert("Year of LTA Approved Date should be numeric.");	
       document.frmElearn.lta_year.focus();
       return false;		
    }
    else if (document.frmElearn.preferred_pwd.value == null ||
             document.frmElearn.preferred_pwd.value == "") 
    {
       alert("Please enter a password of your choice.");
       document.frmElearn.preferred_pwd.focus();
       return false;
    }
//**11/04/2002 soonchwee suggests that password should be at least 6 alphanumeric long
    else if (document.frmElearn.preferred_pwd.value.length < 6)
    {
       alert("Password should be at least 6 chars (numeric/alphabet) long.");
       document.frmElearn.preferred_pwd.focus();
       return false;
    }
//**18/11/2002 Spaces should not be allowed in password
   else if (document.frmElearn.preferred_pwd.value.indexOf(invalid) > -1) 
    {
       alert("Spaces are not allowed in password.");
       document.frmElearn.preferred_pwd.focus();
       return false;
    }
//Orientation date must be selected
   else if (document.frmElearn.date_orientation.value == "0") 
    {
       alert("Please select Orientation Date.");
       document.frmElearn.date_orientation.focus();
       return false;
    }
	//Highest Qualification must be selected
   else if (document.frmElearn.qualification.value == "0") 
    {
       alert("Please select your highest qualification.");
       document.frmElearn.qualification.focus();
       return false;
    }
//**25/04/2002
    else if (!document.frmElearn.payment_mode[0].checked &&
             !document.frmElearn.payment_mode[1].checked &&
             !document.frmElearn.payment_mode[2].checked ) //Changed for including sponsorship on 5/12/02
    {
       alert("Please select payment mode.");
       document.frmElearn.payment_mode[0].focus();
       return false;
    }

    return true;
}

