function IsDigit(o,e) {
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (((code < 48)||(code > 57 ))) 
	{
		if (code != 8 && code != 46 && code != 9 && code != 45 && code != 40 && code != 41)
		{
			return false;
		}
	}
}
function Trim(inputString)
{
	var retValue = inputString;
	var ch = retValue.substring(0, 1);

	while (ch == " ")
	{ // Check for space at the start of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}

	ch = retValue.substring(retValue.length-1, retValue.length);

	while (ch == " ")
	{ // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}	
	return retValue;
}

function IsEmpty(v){
	varValue = Trim(v);
	if (varValue.length == 0){ return true; }else{ return false; }
}
function IsValidEmail(str){
	var at = '@';
	var dot = '.';
	var lat = parseInt(str.indexOf(at));
	var ldot = parseInt(str.lastIndexOf(dot));
	var lstr = parseInt(str.length);
	
	//no '@' or '@' is first character or '@' is the last character
	if ((lat <= 0) || (lat == parseInt(lstr-1)))
		return false;

	//no '.' or '.' is first character or '.' is the last character
	if ((ldot <= 0) || (ldot == parseInt(lstr-1)))
		return false;

	//presence of another '@'
	if (str.indexOf(at, parseInt(lat+1)) != -1) 
		return false;

	//presence of '.' before or after '@'
	if ((str.substr(parseInt(lat - 1), 1) == dot) || (str.substr(parseInt(lat + 1), 1) == dot))
		return false;

	//check '.' is at least one character after '@'
	if (str.indexOf(dot, parseInt(lat + 2)) == -1) 
		return false;

	//check for blank
	if (str.indexOf(" ") != -1) 
		return false;

	//check the length after the last '.' is not less than 2 characters
	if (str.substr(parseInt(ldot + 1)).length < 2) 
		return false;

	if (!IsAlphaNumeric(str.substr(ldot + 1)))
		return false;	
	
	return true;
}

function IsAlphaNumeric(str){
	for (i=0; i<str.length; i++){
		if (!((str.charCodeAt(i)>=97) && (str.charCodeAt(i)<=122)) && !((str.charCodeAt(i)>=65) && (str.charCodeAt(i)<=90)) && !((str.charCodeAt(i)>=48) && (str.charCodeAt(i)<=57))){
			return false;
		}
	}
	return true;
}

function Validate(){
	if (IsEmpty(document.getElementById("name").value))
	{
		alert("Please enter your given name.");
		document.getElementById("name").focus();
		return;
	}
	if (IsEmpty(document.getElementById("lastname").value))
	{
		alert("Please enter your last name.");
		document.getElementById("lastname").focus();
		return;
	}
	if (IsEmpty(document.getElementById("address").value))
	{
		alert("Please enter your address.");
		document.getElementById("address").focus();
		return;
	}
	/*if (IsEmpty(document.getElementById("area").value))
	{
		alert("Please enter contest area.");
		document.getElementById("area").focus();
		return;
	}
	*/
	if (IsEmpty(document.getElementById("phone").value))
	{
		alert("Please enter your phone number.");
		document.getElementById("phone").focus();
		return;
	}
	else 
	{
		if (document.getElementById("phone").value.length < 8 || (document.getElementById("phone").value.charAt(0) != "9" && document.getElementById("phone").value.charAt(0) != "8" && document.getElementById("phone").value.charAt(0) != "6"))
		{
			alert("Please enter a valid phone number.");
			document.getElementById("phone").focus();
			return;
		}
	}
	if (IsEmpty(document.getElementById("email").value))
	{
		alert("Please enter your email address.");
		document.getElementById("email").focus();
		return;
	}
	else
	{
		if (!IsValidEmail(document.getElementById("email").value))
		{
			alert("Please enter a valid email address.");
			document.getElementById("email").focus();
			return;
		}
	}
	if (IsEmpty(document.getElementById("nric").value))
	{
		alert("Please enter your IC number.");
		document.getElementById("nric").focus();
		return;
	}
	if (IsEmpty(document.getElementById("msg1").value))
	{
		alert("Please enter Message 1.");
		document.getElementById("msg1").focus();
		return;
	}
	if (IsEmpty(document.getElementById("msg2").value))
	{
		alert("Please enter Message 2.");
		document.getElementById("msg2").focus();
		return;
	}
	document.form1.submit();
	
}