/**
 *   DHTML email validation script. 
 *   Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 **/

function ValEmail()
{
	var emailID = document.contact.email;
	
	if (emailID.value == "")	
	{
		return true;		//validate blank fields only during submit
	}
	else
	{
		if (echeck (emailID.value)==false)
		{
			emailID.focus();
			return false;
		}
		return true;
 	}
}

function echeck (str)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length - 1;	//decrement the length by 1 to reflect the last position
	var ldot=str.indexOf(dot);

	var msg = "The E-mail you have entered is invalid. \nKindly check through it and re-enter. \nThank you.";

	//"@" not found or found at beginning or last position
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		alert (msg);
		return false;
	}

	//"." not found or found at beginning or last position
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		alert (msg);
		return false;
	}
	
	//extra "@" found 
	if (str.indexOf(at,(lat+1))!=-1)
	{
		alert (msg);
		return false;
	}

	//"." found just before or after "@"
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		alert (msg);
		return false;
	}

	//spaces (" ") found in email address
	if ( str.indexOf(" ")!=-1 )
	{
		alert (msg);
		return false;
	}
		 
	return true;					
}
