function validateEmailAddress(email) {
	var username = "";
	var domain = "";
	var count=0;
	var pos;
	
	// Check for invalid characters in email address ******************
	var invalid = false;
	var cv;
	
	for(var i=0;i<email.length;i++) {
		cv = email.charCodeAt(i);
		
		if(cv<48) invalid=true;
		if(cv>57 && cv<65) invalid=true;
		if(cv>90 && cv<97) invalid=true;
		if(cv>122) invalid=true;
		if(cv==64 || cv==95 || cv==45 || cv==46) invalid=false;
	}
	if(invalid!=false) {
		alert("Invalid email address");
		return false;
	}
	// ****************************************************************
	
    // First check that there is only one '@' in the email address
	for(var i=0; i<email.length; i++) {
	    if(email.charAt(i)=='@') count=count+1;
	};
	if(count>1) {
	    alert("Invalid email address");
		return false;
	};
	//Check the location of @ to ensure its valid
	pos=email.indexOf('@',0);
	if(pos<1 || pos>=email.length-1) {
	    alert("Invalid email address");
		return false;
	};
	
	//Split the email address into two sections
	username=email.substring(0,email.indexOf('@',0));
	//alert(username);
	domain=email.substring(email.indexOf('@',0)+1,email.length);
	//alert("domain: "+domain);

	
	if(checkEmailforDots(domain,true)!=true) {
	    return false;
	};
	if(checkEmailforDots(username,false)!=true) {
	    return false;
	};
	   
	//Check end of domain for correct pattern
	if(domain.charAt(domain.length-2)==".") {
	    alert("Invalid email address");
		return false;
	};
	//if(domain.charAt(domain.length-3)=="." && 
	//  domain.charAt(domain.length-6)!=".") {
	//       alert("Invalid email address : Error 5a");
	//	   return false;
	//};
	
	if(domain.charAt(domain.length-3)=="." && 
	   domain.charAt(domain.length-4)==".") {
	       alert("Invalid email address");
		   return false;
	};
	
	//If execution of code has reached here then Email address is valid
	return true;
}
	
function checkEmailforDots(text,needed) {
	var pos = 0;
	
	//Check that there is at least one '.'
	//If there is make sure its not the first character or the last
    if(needed!=false) {
        if(text.indexOf('.',0)<0) {
	        alert("Invalid Email Address");
		    return false;
	    };
    };
	if(text.charAt(0)=='.' || text.charAt(text.length-1)=='.') {
	    alert("Invalid Email Address");
        return false;
	};
		
	//Check for double dots
	if(text.indexOf("..",0)>=0) {
	    alert("Invalid email address");
	    return false;
	};
	return true;
}