 function echeck(str) {

  var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   error = "Invalid E-mail\n";
		  return error
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   error = "Invalid E-mail\n";
		   return error
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    error = "Invalid E-mail\n";
		   return error
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    error = "Invalid E-mail\n";
		   return error
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    error = "Invalid E-mail\n";
		    return error
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    error = "Invalid E-mail\n";
		   return error
		 }
		
		 if (str.indexOf(" ")!=-1){
		    error = "Invalid E-mail\n";
		   return error
		 }
		   return "--"
}	
 
function validate ( )
{
   var errors=0;
   var contact=0;
   var reason = "";
   var emailreason = "";
   if ( document.contact.name.value == "" )
   {
       reason += "Please specify your Name.\n";
       document.contact.name.style.backgroundColor = "#ff9999";
       errors ++;
   }
   else
   {
     document.contact.name.style.backgroundColor = "#ffffff";
   }
   
   if ( document.contact.email.value.length > 0 ) //only check the validity of the email address if it has been filled in
   {
   var emailID=document.contact.email
   emailreason = echeck(emailID.value);
   if (emailreason != "--")
	   {
	      emailID.value="";
	      emailID.focus()
	      reason += emailreason;
	      errors++;
	   }
   else
	   {
	   document.contact.email.style.backgroundColor = "#ffffff";
	   contact++;
	   }
   }
   
   if ( document.contact.telephone.value.length > 0 )
   {
     document.contact.telephone.style.backgroundColor = "#ffffff";
     contact++;
   }
       
   if ( document.contact.details.value == "" )
   {
       document.contact.details.style.backgroundColor = "#ff9999";
       reason += "Please specify details of your claim.\n";
       errors ++;
   }
   else
   {
        document.contact.details.style.backgroundColor = "#ffffff";
   }
    
    if(contact==0)
   {
      
      document.contact.telephone.style.backgroundColor = "#ff9999";
      document.contact.email.style.backgroundColor = "#ff9999";
      
      reason += "Please Enter a valid Email address or a Telephone number so we can contact you.\n";
   }
   
   if(!errors && contact!=0)
   {
      document.contact.submit();
   }
   else
   {
      alert("Some fields need correction:\n" + reason);
   }
}



