// thanhsonkbs@gmail.com
function CheckValid()
{
	if (document.frm.fname.value == '')
	{
		alert('Please enter your first name!');
		document.frm.fname.focus();
		return false;
	}
	if (document.frm.lname.value == '')
	{
		alert('Please enter your last name!');
		document.frm.lname.focus();
		return false;
	}
	if (document.frm.cemail.value == '')
	{
		alert('Please enter your email!');
		document.frm.cemail.focus();
		return false;
	}
	 	
		ok=true
	if(document.frm.cemail.value=="")
	{
		alert("Please enter e-mail address.")
		document.frm.cemail.focus()
		ok=false
	}
	else
	{
		mail=document.frm.cemail.value
		at_pos=mail.indexOf("@")
		dot_pos=mail.indexOf(".")
		if(at_pos< 1 || dot_pos< 1)
		{
			alert("Please check position of '@' and '.' in email address.")
			document.frm.cemail.focus()
			ok=false
		}
		else
		{
			mail=document.frm.cemail.value
			condition="yes"
			var at_count=0
			var dot_count=0
			var temp=0
			for(var i=0;i< mail.length;i++)
			{
				if((mail.charCodeAt(i)>0 && mail.charCodeAt(i)< 48)||(mail.charCodeAt(i)>57 && mail.charCodeAt(i)< 65)||(mail.charCodeAt(i)>91 && mail.charCodeAt(i)< 97)||mail.charCodeAt(i)>122)
				{
					if(mail.charAt(i)=="@"||mail.charAt(i)==".")
					{
							if(mail.charAt(i)=="@"){at_count++}else{dot_count++} // counts the no. of times @ and . appears in email
							if(dot_count>=1)
							{
								dot_pos=i
								if((dot_pos>at_pos) && temp==0)
								{
									pos=dot_pos-at_pos
									temp++
								}								
							}
					}
					else
					{
						condition="no"
						i=mail.length
					}
				}
			}
			if(condition=="no")
			{
				alert("Your email contains a blank space or special character.")
				document.frm.cemail.focus()
				ok=false
			}
			else
			{
				if(at_count>1)
				{
					alert("E-mail contains extra @ ")
					document.frm.cemail.focus()
					ok=false
				}
				else
				{
					if(pos< 2)
				{
					alert("Missing domain name between '@' and '.'")
					document.frm.cemail.focus()
					ok=false
					i=mail.length
					}
					else
				{	
					count=dot_pos+1
					domain=""
					for(count;count< mail.length;count++)
						{
							domain=domain+mail.charAt(count)		
						}
						dom=new Array("au","com","net","org","edu","in","mil","gov","arpa","biz","aero","name","coop","info","pro","museum")
						error="yes"
						for(var k=0;k< dom.length;k++)
						{
							if(domain==dom[k])
							{
								k=dom.length
								error="no"
							}
						}
						if((error=="yes" && (domain.length>2)) || (domain.length< 2))
						{
							alert("Domain name must end with well known domains \n or 2-lettered country name. eg com,edu,in etc.")
							document.frm.cemail.focus()
							ok=false
						}								
					}
				}
			}

		}
	}
	return ok
	
	
	
}
   
